cpp-utilities/chrono/timespan.h

377 lines
11 KiB
C
Raw Normal View History

#ifndef CHRONO_UTILITIES_TIMESPAN_H
#define CHRONO_UTILITIES_TIMESPAN_H
2015-04-22 18:36:40 +02:00
#include "../global.h"
2015-09-06 20:19:09 +02:00
#include "../conversion/types.h"
2015-04-22 18:36:40 +02:00
#include <string>
2016-01-18 23:41:30 +01:00
#include <limits>
2015-04-22 18:36:40 +02:00
/*!
* \brief Contains classes providing a means for handling date and time information.
*/
namespace ChronoUtilities
{
class DateTime;
/*!
* \brief Specifies the output format.
* \sa TimeSpan::toString()
*/
enum class TimeSpanOutputFormat
{
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 */
};
class CPP_UTILITIES_EXPORT TimeSpan
2015-04-22 18:36:40 +02:00
{
friend class DateTime;
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 totalMilliseconds() const;
constexpr double totalSeconds() const;
constexpr double totalMinutes() const;
constexpr double totalHours() const;
constexpr double totalDays() const;
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 noMilliseconds = false) const;
void toString(std::string &result, TimeSpanOutputFormat format = TimeSpanOutputFormat::Normal, bool noMilliseconds = false) const;
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
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;
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
*/
constexpr inline TimeSpan::TimeSpan() : m_ticks(0)
{}
/*!
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
*/
constexpr inline TimeSpan::TimeSpan(int64 ticks) : m_ticks(ticks)
{}
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)
{
2016-09-01 15:42:25 +02:00
return TimeSpan(static_cast<int64>(milliseconds * static_cast<double>(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)
{
2016-09-01 15:42:25 +02:00
return TimeSpan(static_cast<int64>(seconds * static_cast<double>(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)
{
2016-09-01 15:42:25 +02:00
return TimeSpan(static_cast<int64>(minutes * static_cast<double>(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)
{
2016-09-01 15:42:25 +02:00
return TimeSpan(static_cast<int64>(hours * static_cast<double>(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)
{
2016-09-01 15:42:25 +02:00
return TimeSpan(static_cast<int64>(days * static_cast<double>(ticksPerDay)));
2015-04-22 18:36:40 +02:00
}
/*!
* \brief Parses the given std::string as TimeSpan.
*/
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 Gets 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;
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Gets 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
{
2016-09-01 15:42:25 +02:00
return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMillisecond);
2015-04-22 18:36:40 +02:00
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Gets 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
{
2016-09-01 15:42:25 +02:00
return static_cast<double>(m_ticks) / static_cast<double>(ticksPerSecond);
2015-04-22 18:36:40 +02:00
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Gets 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
{
2016-09-01 15:42:25 +02:00
return static_cast<double>(m_ticks) / static_cast<double>(ticksPerMinute);
2015-04-22 18:36:40 +02:00
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Gets 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
{
2016-09-01 15:42:25 +02:00
return static_cast<double>(m_ticks) / static_cast<double>(ticksPerHour);
2015-04-22 18:36:40 +02:00
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Gets 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
{
2016-09-01 15:42:25 +02:00
return static_cast<double>(m_ticks) / static_cast<double>(ticksPerDay);
2015-04-22 18:36:40 +02:00
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Gets 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
{
2016-09-01 15:42:25 +02:00
return (m_ticks / ticksPerMillisecond) % 1000l;
2015-04-22 18:36:40 +02:00
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Gets 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
{
2016-09-01 15:42:25 +02:00
return (m_ticks / ticksPerSecond) % 60l;
2015-04-22 18:36:40 +02:00
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Gets 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
{
2016-09-01 15:42:25 +02:00
return (m_ticks / ticksPerMinute) % 60l;
2015-04-22 18:36:40 +02:00
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Gets 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
{
2016-09-01 15:42:25 +02:00
return (m_ticks / ticksPerHour) % 24l;
2015-04-22 18:36:40 +02:00
}
/*!
2016-01-18 23:41:30 +01:00
* \brief Gets 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
{
2016-09-01 15:42:25 +02:00
return (m_ticks / 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
*/
constexpr inline bool TimeSpan::operator ==(const TimeSpan &other) const
{
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
*/
constexpr inline bool TimeSpan::operator !=(const TimeSpan &other) const
{
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
*/
constexpr inline bool TimeSpan::operator <(const TimeSpan &other) const
{
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
*/
constexpr inline bool TimeSpan::operator >(const TimeSpan &other) const
{
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
*/
constexpr inline bool TimeSpan::operator <=(const TimeSpan &other) const
{
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
*/
constexpr inline bool TimeSpan::operator >=(const TimeSpan &other) const
{
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
*/
constexpr inline TimeSpan TimeSpan::operator +(const TimeSpan &other) const
{
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
*/
constexpr inline TimeSpan TimeSpan::operator -(const TimeSpan &other) const
{
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
*/
inline TimeSpan &TimeSpan::operator +=(const TimeSpan &other)
{
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
*/
inline TimeSpan &TimeSpan::operator -=(const TimeSpan &other)
{
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();
}
2015-04-22 18:36:40 +02:00
}
2016-11-26 13:07:05 +01:00
namespace std {
template<> struct hash<ChronoUtilities::TimeSpan>
{
inline size_t operator()(const ChronoUtilities::TimeSpan &timeSpan) const
{
return hash<decltype (timeSpan.totalTicks())>()(timeSpan.totalTicks());
}
};
}
#endif // CHRONO_UTILITIES_TIMESPAN_H