cpp-utilities/chrono/timespan.h

423 lines
13 KiB
C
Raw Normal View History

#ifndef CHRONO_UTILITIES_TIMESPAN_H
#define CHRONO_UTILITIES_TIMESPAN_H
2015-04-22 18:36:40 +02:00
2015-09-06 20:19:09 +02:00
#include "../conversion/types.h"
2017-05-01 03:13:11 +02:00
#include "../global.h"
2015-04-22 18:36:40 +02:00
#include <functional>
2016-01-18 23:41:30 +01:00
#include <limits>
2017-05-01 03:13:11 +02:00
#include <string>
2015-04-22 18:36:40 +02:00
/*!
* \brief Contains classes providing a means for handling date and time information.
*/
2017-05-01 03:13:11 +02:00
namespace ChronoUtilities {
2015-04-22 18:36:40 +02:00
class DateTime;
/*!
* \brief Specifies the output format.
* \sa TimeSpan::toString()
*/
2017-05-01 03:13:11 +02:00
enum class TimeSpanOutputFormat {
2015-04-22 18:36:40 +02:00
Normal, /**< the normal form of specifing a time interval: hh:mm:ss */
WithMeasures, /**< measures are used, eg.: 34 d 5 h 10 min 7 s 31 ms */
TotalSeconds, /**< total seconds (as returned by totalSeconds()), eg. 2304.342 */
2015-04-22 18:36:40 +02:00
};
2017-05-01 03:13:11 +02:00
class CPP_UTILITIES_EXPORT TimeSpan {
2015-04-22 18:36:40 +02:00
friend class DateTime;
2017-05-01 03:13:11 +02:00
2015-04-22 18:36:40 +02:00
public:
2015-09-11 22:01:26 +02:00
explicit constexpr TimeSpan();
explicit constexpr TimeSpan(int64 ticks);
2015-04-22 18:36:40 +02:00
static constexpr TimeSpan fromMilliseconds(double milliseconds);
static constexpr TimeSpan fromSeconds(double seconds);
static constexpr TimeSpan fromMinutes(double minutes);
static constexpr TimeSpan fromHours(double hours);
static constexpr TimeSpan fromDays(double days);
static TimeSpan fromString(const std::string &str, char separator = ':');
static TimeSpan fromString(const char *str, char separator);
2016-01-18 23:41:30 +01:00
static constexpr TimeSpan negativeInfinity();
static constexpr TimeSpan infinity();
2015-04-22 18:36:40 +02:00
constexpr int64 totalTicks() const;
constexpr double totalMicroseconds() const;
2015-04-22 18:36:40 +02:00
constexpr double totalMilliseconds() const;
constexpr double totalSeconds() const;
constexpr double totalMinutes() const;
constexpr double totalHours() const;
constexpr double totalDays() const;
constexpr int nanoseconds() const;
constexpr int microseconds() const;
2015-04-22 18:36:40 +02:00
constexpr int milliseconds() const;
constexpr int seconds() const;
constexpr int minutes() const;
constexpr int hours() const;
constexpr int days() const;
constexpr bool operator==(const TimeSpan &other) const;
constexpr bool operator!=(const TimeSpan &other) const;
constexpr bool operator<(const TimeSpan &other) const;
constexpr bool operator>(const TimeSpan &other) const;
constexpr bool operator<=(const TimeSpan &other) const;
constexpr bool operator>=(const TimeSpan &other) const;
constexpr TimeSpan operator+(const TimeSpan &other) const;
constexpr TimeSpan operator-(const TimeSpan &other) const;
TimeSpan &operator+=(const TimeSpan &other);
TimeSpan &operator-=(const TimeSpan &other);
std::string toString(TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
void toString(std::string &result, TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool fullSeconds = false) const;
2015-04-22 18:36:40 +02:00
constexpr bool isNull() const;
constexpr bool isNegative() const;
2016-01-18 23:41:30 +01:00
constexpr bool isNegativeInfinity() const;
constexpr bool isInfinity() const;
2015-04-22 18:36:40 +02:00
2017-06-25 15:09:16 +02:00
// TODO: make those public constants signed in next major release and remove private ones then
static constexpr int64 nanosecondsPerTick = 100uL;
static constexpr int64 ticksPerMicrosecond = 10uL;
2016-09-01 15:42:25 +02:00
static constexpr uint64 ticksPerMillisecond = 10000uL;
static constexpr uint64 ticksPerSecond = 10000000uL;
static constexpr uint64 ticksPerMinute = 600000000uL;
static constexpr uint64 ticksPerHour = 36000000000uL;
static constexpr uint64 ticksPerDay = 864000000000uL;
2017-06-25 15:09:16 +02:00
private:
static constexpr int64 m_ticksPerMillisecond = 10000L;
static constexpr int64 m_ticksPerSecond = 10000000L;
static constexpr int64 m_ticksPerMinute = 600000000L;
static constexpr int64 m_ticksPerHour = 36000000000L;
static constexpr int64 m_ticksPerDay = 864000000000L;
2015-04-22 18:36:40 +02:00
private:
int64 m_ticks;
};
/*!
2016-01-18 23:41:30 +01:00
* \brief Constructs a new instance of the TimeSpan class with zero ticks.
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline TimeSpan::TimeSpan()
: m_ticks(0)
{
}
2015-04-22 18:36:40 +02:00
/*!
2016-01-18 23:41:30 +01:00
* \brief Constructs a new instance of the TimeSpan class with the specified number of ticks.
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline TimeSpan::TimeSpan(int64 ticks)
: m_ticks(ticks)
{
}
2015-04-22 18:36:40 +02:00
2016-01-18 23:41:30 +01:00
/*!
* \brief Constructs a new instance of the TimeSpan class with the specified number of miliseconds.
*/
2015-04-22 18:36:40 +02:00
constexpr inline TimeSpan TimeSpan::fromMilliseconds(double milliseconds)
{
2017-06-25 15:09:16 +02:00
return TimeSpan(static_cast<int64>(milliseconds * static_cast<double>(m_ticksPerMillisecond)));
2015-04-22 18:36:40 +02:00
}
2016-01-18 23:41:30 +01:00
/*!
* \brief Constructs a new instance of the TimeSpan class with the specified number of seconds.
*/
2015-04-22 18:36:40 +02:00
constexpr inline TimeSpan TimeSpan::fromSeconds(double seconds)
{
2017-06-25 15:09:16 +02:00
return TimeSpan(static_cast<int64>(seconds * static_cast<double>(m_ticksPerSecond)));
2015-04-22 18:36:40 +02:00
}
2016-01-18 23:41:30 +01:00
/*!
* \brief Constructs a new instance of the TimeSpan class with the specified number of minutes.
*/
2015-04-22 18:36:40 +02:00
constexpr inline TimeSpan TimeSpan::fromMinutes(double minutes)
{
2017-06-25 15:09:16 +02:00
return TimeSpan(static_cast<int64>(minutes * static_cast<double>(m_ticksPerMinute)));
2015-04-22 18:36:40 +02:00
}
2016-01-18 23:41:30 +01:00
/*!
* \brief Constructs a new instance of the TimeSpan class with the specified number of hours.
*/
2015-04-22 18:36:40 +02:00
constexpr inline TimeSpan TimeSpan::fromHours(double hours)
{
2017-06-25 15:09:16 +02:00
return TimeSpan(static_cast<int64>(hours * static_cast<double>(m_ticksPerHour)));
2015-04-22 18:36:40 +02:00
}
2016-01-18 23:41:30 +01:00
/*!
* \brief Constructs a new instance of the TimeSpan class with the specified number of days.
*/
2015-04-22 18:36:40 +02:00
constexpr inline TimeSpan TimeSpan::fromDays(double days)
{
2017-06-25 15:09:16 +02:00
return TimeSpan(static_cast<int64>(days * static_cast<double>(m_ticksPerDay)));
2015-04-22 18:36:40 +02:00
}
/*!
* \brief Parses the given std::string as TimeSpan.
* \throws Throws a ConversionException if the specified \a str does not match the expected format.
*
* The expected format is "days:hours:minutes:seconds", eg. "5:31:4.521" for 5 hours, 31 minutes
* and 4.521 seconds. So parts at the front can be omitted and the parts can be fractions. The
* colon can be changed by specifying another \a separator.
*/
inline TimeSpan TimeSpan::fromString(const std::string &str, char separator)
{
return TimeSpan::fromString(str.data(), separator);
}
2015-04-22 18:36:40 +02:00
/*!
2016-01-18 23:41:30 +01:00
* \brief Constructs a new instace of the TimeSpan class with the minimal number of ticks.
*/
constexpr inline TimeSpan TimeSpan::negativeInfinity()
{
return TimeSpan(std::numeric_limits<decltype(m_ticks)>::min());
}
/*!
* \brief Constructs a new instace of the TimeSpan class with the maximal number of ticks.
*/
constexpr inline TimeSpan TimeSpan::infinity()
{
return TimeSpan(std::numeric_limits<decltype(m_ticks)>::max());
}
/*!
* \brief Returns the number of ticks that represent the value of the current TimeSpan class.
2015-04-22 18:36:40 +02:00
*/
constexpr inline int64 TimeSpan::totalTicks() const
{
return m_ticks;
}
/*!
* \brief Returns the value of the current TimeSpan class expressed in whole and fractional microseconds.
*/
constexpr double TimeSpan::totalMicroseconds() const
{
return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMicrosecond);
}
2015-04-22 18:36:40 +02:00
/*!
* \brief Returns the value of the current TimeSpan class expressed in whole and fractional milliseconds.
2015-04-22 18:36:40 +02:00
*/
constexpr inline double TimeSpan::totalMilliseconds() const
{
2017-06-25 15:09:16 +02:00
return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerMillisecond);
2015-04-22 18:36:40 +02:00
}
/*!
* \brief Returns the value of the current TimeSpan class expressed in whole and fractional seconds.
2015-04-22 18:36:40 +02:00
*/
constexpr inline double TimeSpan::totalSeconds() const
{
2017-06-25 15:09:16 +02:00
return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerSecond);
2015-04-22 18:36:40 +02:00
}
/*!
* \brief Returns the value of the current TimeSpan class expressed in whole and fractional minutes.
2015-04-22 18:36:40 +02:00
*/
constexpr inline double TimeSpan::totalMinutes() const
{
2017-06-25 15:09:16 +02:00
return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerMinute);
2015-04-22 18:36:40 +02:00
}
/*!
* \brief Returns the value of the current TimeSpan class expressed in whole and fractional hours.
2015-04-22 18:36:40 +02:00
*/
constexpr inline double TimeSpan::totalHours() const
{
2017-06-25 15:09:16 +02:00
return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerHour);
2015-04-22 18:36:40 +02:00
}
/*!
* \brief Returns the value of the current TimeSpan class expressed in whole and fractional days.
2015-04-22 18:36:40 +02:00
*/
constexpr inline double TimeSpan::totalDays() const
{
2017-06-25 15:09:16 +02:00
return static_cast<double>(m_ticks) / static_cast<double>(m_ticksPerDay);
2015-04-22 18:36:40 +02:00
}
/*!
* \brief Returns the nanoseconds component of the time interval represented by the current TimeSpan class.
* \remarks The accuracy of the TimeSpan class is 100-nanoseconds. Hence the returned value
* will always have two zeros at the end (in decimal representation).
*/
constexpr int TimeSpan::nanoseconds() const
{
return m_ticks % 10l * TimeSpan::nanosecondsPerTick;
}
/*!
* \brief Returns the microseconds component of the time interval represented by the current TimeSpan class.
*/
constexpr int TimeSpan::microseconds() const
{
return (m_ticks / ticksPerMicrosecond) % 1000l;
}
2015-04-22 18:36:40 +02:00
/*!
* \brief Returns the miliseconds component of the time interval represented by the current TimeSpan class.
2015-04-22 18:36:40 +02:00
*/
constexpr inline int TimeSpan::milliseconds() const
{
2017-06-25 15:09:16 +02:00
return (m_ticks / m_ticksPerMillisecond) % 1000l;
2015-04-22 18:36:40 +02:00
}
/*!
* \brief Returns the seconds component of the time interval represented by the current TimeSpan class.
2015-04-22 18:36:40 +02:00
*/
constexpr inline int TimeSpan::seconds() const
{
2017-06-25 15:09:16 +02:00
return (m_ticks / m_ticksPerSecond) % 60l;
2015-04-22 18:36:40 +02:00
}
/*!
* \brief Returns the minutes component of the time interval represented by the current TimeSpan class.
2015-04-22 18:36:40 +02:00
*/
constexpr inline int TimeSpan::minutes() const
{
2017-06-25 15:09:16 +02:00
return (m_ticks / m_ticksPerMinute) % 60l;
2015-04-22 18:36:40 +02:00
}
/*!
* \brief Returns the hours component of the time interval represented by the current TimeSpan class.
2015-04-22 18:36:40 +02:00
*/
constexpr inline int TimeSpan::hours() const
{
2017-06-25 15:09:16 +02:00
return (m_ticks / m_ticksPerHour) % 24l;
2015-04-22 18:36:40 +02:00
}
/*!
* \brief Returns the days component of the time interval represented by the current TimeSpan class.
2015-04-22 18:36:40 +02:00
*/
constexpr inline int TimeSpan::days() const
{
2017-06-25 15:09:16 +02:00
return (m_ticks / m_ticksPerDay);
2015-04-22 18:36:40 +02:00
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Indicates whether two TimeSpan instances are equal.
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline bool TimeSpan::operator==(const TimeSpan &other) const
2015-04-22 18:36:40 +02:00
{
return m_ticks == other.m_ticks;
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Indicates whether two TimeSpan instances are not equal.
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline bool TimeSpan::operator!=(const TimeSpan &other) const
2015-04-22 18:36:40 +02:00
{
return m_ticks != other.m_ticks;
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Indicates whether a specified TimeSpan is less than another specified TimeSpan.
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline bool TimeSpan::operator<(const TimeSpan &other) const
2015-04-22 18:36:40 +02:00
{
return m_ticks < other.m_ticks;
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Indicates whether a specified TimeSpan is greater than another specified TimeSpan.
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline bool TimeSpan::operator>(const TimeSpan &other) const
2015-04-22 18:36:40 +02:00
{
return m_ticks > other.m_ticks;
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Indicates whether a specified TimeSpan is less or equal than another specified TimeSpan.
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline bool TimeSpan::operator<=(const TimeSpan &other) const
2015-04-22 18:36:40 +02:00
{
return m_ticks <= other.m_ticks;
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Indicates whether a specified TimeSpan is greater or equal than another specified TimeSpan.
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline bool TimeSpan::operator>=(const TimeSpan &other) const
2015-04-22 18:36:40 +02:00
{
return m_ticks >= other.m_ticks;
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Adds two TimeSpan instances.
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline TimeSpan TimeSpan::operator+(const TimeSpan &other) const
2015-04-22 18:36:40 +02:00
{
return TimeSpan(m_ticks + other.m_ticks);
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Substracts two TimeSpan instances.
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline TimeSpan TimeSpan::operator-(const TimeSpan &other) const
2015-04-22 18:36:40 +02:00
{
return TimeSpan(m_ticks - other.m_ticks);
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Adds another TimeSpan to the current instance.
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
inline TimeSpan &TimeSpan::operator+=(const TimeSpan &other)
2015-04-22 18:36:40 +02:00
{
m_ticks += other.m_ticks;
return *this;
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Substracts another TimeSpan from the current instance.
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
inline TimeSpan &TimeSpan::operator-=(const TimeSpan &other)
2015-04-22 18:36:40 +02:00
{
m_ticks -= other.m_ticks;
return *this;
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Returns ture if the time interval represented by the current TimeSpan class is null.
2015-04-22 18:36:40 +02:00
*/
constexpr inline bool TimeSpan::isNull() const
{
return m_ticks == 0;
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Returns ture if the time interval represented by the current TimeSpan class is negative.
2015-04-22 18:36:40 +02:00
*/
constexpr inline bool TimeSpan::isNegative() const
{
return m_ticks < 0;
}
2016-01-18 23:41:30 +01:00
/*!
* \brief Returns whether the time inverval represented by the current instance is the smallest representable TimeSpan.
*/
constexpr inline bool TimeSpan::isNegativeInfinity() const
{
return m_ticks == std::numeric_limits<decltype(m_ticks)>::min();
}
/*!
* \brief Returns whether the time inverval represented by the current instance is the longest representable TimeSpan.
*/
constexpr inline bool TimeSpan::isInfinity() const
{
return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
}
} // namespace ChronoUtilities
2015-04-22 18:36:40 +02:00
2016-11-26 13:07:05 +01:00
namespace std {
2017-05-01 03:13:11 +02:00
template <> struct hash<ChronoUtilities::TimeSpan> {
2016-11-26 13:07:05 +01:00
inline size_t operator()(const ChronoUtilities::TimeSpan &timeSpan) const
{
2017-05-01 03:13:11 +02:00
return hash<decltype(timeSpan.totalTicks())>()(timeSpan.totalTicks());
2016-11-26 13:07:05 +01:00
}
};
} // namespace std
2016-11-26 13:07:05 +01:00
#endif // CHRONO_UTILITIES_TIMESPAN_H