📄 timespan.h
字号:
//
// Timespan.h
//
// $Id: //poco/Main/Foundation/include/Foundation/Timespan.h#5 $
//
// Definition of the Timespan class.
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#ifndef Foundation_Timespan_INCLUDED
#define Foundation_Timespan_INCLUDED
#ifndef Foundation_Foundation_INCLUDED
#include "Foundation/Foundation.h"
#endif
#ifndef Foundation_Timestamp_INCLUDED
#include "Foundation/Timestamp.h"
#endif
Foundation_BEGIN
class Foundation_API Timespan
/// A class that represents time spans up to microsecond resolution.
{
public:
typedef Timestamp::TimeDiff TimeDiff;
Timespan();
/// Creates a zero Timespan.
Timespan(TimeDiff microseconds);
/// Creates a Timespan.
Timespan(int days, int hours, int minutes, int seconds, int microseconds);
/// Creates a Timespan.
Timespan(const Timespan& timespan);
/// Creates a Timespan from another one.
~Timespan();
/// Destroys the Timespan.
Timespan& operator = (const Timespan& timespan);
/// Assignment operator.
Timespan& operator = (TimeDiff microseconds);
/// Assignment operator.
Timespan& assign(int days, int hours, int minutes, int seconds, int microseconds);
/// Assigns a new span.
bool operator == (const Timespan& ts) const;
bool operator != (const Timespan& ts) const;
bool operator > (const Timespan& ts) const;
bool operator >= (const Timespan& ts) const;
bool operator < (const Timespan& ts) const;
bool operator <= (const Timespan& ts) const;
bool operator == (TimeDiff microseconds) const;
bool operator != (TimeDiff microseconds) const;
bool operator > (TimeDiff microseconds) const;
bool operator >= (TimeDiff microseconds) const;
bool operator < (TimeDiff microseconds) const;
bool operator <= (TimeDiff microseconds) const;
Timespan operator + (const Timespan& d) const;
Timespan operator - (const Timespan& d) const;
Timespan& operator += (const Timespan& d);
Timespan& operator -= (const Timespan& d);
Timespan operator + (TimeDiff microseconds) const;
Timespan operator - (TimeDiff microseconds) const;
Timespan& operator += (TimeDiff microseconds);
Timespan& operator -= (TimeDiff microseconds);
int days() const;
/// Returns the number of days.
int hours() const;
/// Returns the number of hours (0 to 23).
int totalHours() const;
/// Returns the total number of hours.
int minutes() const;
/// Returns the number of minutes (0 to 59).
int totalMinutes() const;
/// Returns the total number of minutes.
int seconds() const;
/// Returns the number of seconds (0 to 59).
int totalSeconds() const;
/// Returns the total number of seconds.
int milliseconds() const;
/// Returns the number of milliseconds (0 to 999).
TimeDiff totalMilliseconds() const;
/// Returns the total number of milliseconds.
int microseconds() const;
/// Returns the number of microseconds (0 to 999).
TimeDiff totalMicroseconds() const;
/// Returns the total number of microseconds.
static const TimeDiff MILLISECONDS; /// The number of microseconds in a millisecond.
static const TimeDiff SECONDS; /// The number of microseconds in a second.
static const TimeDiff MINUTES; /// The number of microseconds in a minute.
static const TimeDiff HOURS; /// The number of microseconds in a hour.
static const TimeDiff DAYS; /// The number of microseconds in a day.
private:
TimeDiff _span;
};
//
// inlines
//
inline int Timespan::days() const
{
return int(_span/DAYS);
}
inline int Timespan::hours() const
{
return int((_span/HOURS) % 24);
}
inline int Timespan::totalHours() const
{
return int(_span/HOURS);
}
inline int Timespan::minutes() const
{
return int((_span/MINUTES) % 60);
}
inline int Timespan::totalMinutes() const
{
return int(_span/MINUTES);
}
inline int Timespan::seconds() const
{
return int((_span/SECONDS) % 60);
}
inline int Timespan::totalSeconds() const
{
return int(_span/SECONDS);
}
inline int Timespan::milliseconds() const
{
return int((_span/MILLISECONDS) % 1000);
}
inline Timespan::TimeDiff Timespan::totalMilliseconds() const
{
return _span/MILLISECONDS;
}
inline int Timespan::microseconds() const
{
return int(_span % 1000);
}
inline Timespan::TimeDiff Timespan::totalMicroseconds() const
{
return _span;
}
inline bool Timespan::operator == (const Timespan& ts) const
{
return _span == ts._span;
}
inline bool Timespan::operator != (const Timespan& ts) const
{
return _span != ts._span;
}
inline bool Timespan::operator > (const Timespan& ts) const
{
return _span > ts._span;
}
inline bool Timespan::operator >= (const Timespan& ts) const
{
return _span >= ts._span;
}
inline bool Timespan::operator < (const Timespan& ts) const
{
return _span < ts._span;
}
inline bool Timespan::operator <= (const Timespan& ts) const
{
return _span <= ts._span;
}
inline bool Timespan::operator == (TimeDiff microseconds) const
{
return _span == microseconds;
}
inline bool Timespan::operator != (TimeDiff microseconds) const
{
return _span != microseconds;
}
inline bool Timespan::operator > (TimeDiff microseconds) const
{
return _span > microseconds;
}
inline bool Timespan::operator >= (TimeDiff microseconds) const
{
return _span >= microseconds;
}
inline bool Timespan::operator < (TimeDiff microseconds) const
{
return _span < microseconds;
}
inline bool Timespan::operator <= (TimeDiff microseconds) const
{
return _span <= microseconds;
}
Foundation_END
#endif // Foundation_Timespan_INCLUDED
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -