2016-06-11 19:09:14 +02:00
# ifndef CHRONO_UTILITIES_DATETIME_H
# define CHRONO_UTILITIES_DATETIME_H
2015-04-22 18:36:40 +02:00
2015-09-06 20:19:09 +02:00
# include "./timespan.h"
2015-04-22 18:36:40 +02:00
2019-03-13 19:00:37 +01:00
# include <cstdint>
2016-05-08 20:36:26 +02:00
# include <ctime>
2017-05-01 03:13:11 +02:00
# include <limits>
# include <string>
2015-04-22 18:36:40 +02:00
2019-06-10 21:56:46 +02:00
namespace CppUtilities {
2015-04-22 18:36:40 +02:00
/*!
* \ brief Specifies the output format .
* \ sa DateTime : : toString ( )
*/
2017-05-01 03:13:11 +02:00
enum class DateTimeOutputFormat {
2015-04-22 18:36:40 +02:00
DateAndTime , /**< date and time */
DateOnly , /**< date only */
TimeOnly , /**< time only */
DateTimeAndWeekday , /**< date with weekday and time */
2020-04-26 21:38:02 +02:00
DateTimeAndShortWeekday , /**< date with abbreviated weekday and time */
Iso , /**< ISO format like DateTime::toIsoString() */
IsoOmittingDefaultComponents , /**< ISO format like DateTime::toIsoString() omitting default components, e.g. just "2017" instead of "2017-01-01T00:00:00" */
2015-04-22 18:36:40 +02:00
} ;
/*!
* \ brief Specifies the day of the week .
* \ sa DateTime : : dayOfWeek ( )
*/
2017-05-01 03:13:11 +02:00
enum class DayOfWeek {
2015-04-22 18:36:40 +02:00
Monday , /**< Monday */
Tuesday , /**< Tuesday */
Wednesday , /**< Wednesday */
Thursday , /**< Thursday */
Friday , /**< Friday */
Saturday , /**< Saturday */
Sunday /**< Sunday */
} ;
/*!
* \ brief Specifies the date part .
2017-11-29 19:11:01 +01:00
* \ remarks Intended for internal use only .
2015-04-22 18:36:40 +02:00
* \ sa DateTime : : getDatePart ( )
*/
2017-05-01 03:13:11 +02:00
enum class DatePart {
2015-04-22 18:36:40 +02:00
Year , /**< year */
Month , /**< month */
DayOfYear , /**< day of year */
Day /**< day */
} ;
2017-05-01 03:13:11 +02:00
class CPP_UTILITIES_EXPORT DateTime {
2015-04-22 18:36:40 +02:00
public :
2015-09-11 22:01:26 +02:00
explicit constexpr DateTime ( ) ;
2019-03-13 19:00:37 +01:00
explicit constexpr DateTime ( std : : uint64_t ticks ) ;
2015-04-22 18:36:40 +02:00
static DateTime fromDate ( int year = 1 , int month = 1 , int day = 1 ) ;
static DateTime fromTime ( int hour = 0 , int minute = 0 , int second = 0 , double millisecond = 0.0 ) ;
static DateTime fromDateAndTime ( int year = 1 , int month = 1 , int day = 1 , int hour = 0 , int minute = 0 , int second = 0 , double millisecond = 0.0 ) ;
static DateTime fromString ( const std : : string & str ) ;
2016-08-30 19:59:04 +02:00
static DateTime fromString ( const char * str ) ;
static std : : pair < DateTime , TimeSpan > fromIsoString ( const char * str ) ;
2016-09-01 15:42:25 +02:00
static DateTime fromIsoStringGmt ( const char * str ) ;
static DateTime fromIsoStringLocal ( const char * str ) ;
2019-11-17 22:30:51 +01:00
static DateTime fromTimeStamp ( std : : time_t timeStamp ) ;
constexpr static DateTime fromTimeStampGmt ( std : : time_t timeStamp ) ;
template < typename TimePoint > static DateTime fromChronoTimePoint ( TimePoint timePoint ) ;
template < typename TimePoint > constexpr static DateTime fromChronoTimePointGmt ( TimePoint timePoint ) ;
2015-04-22 18:36:40 +02:00
2019-08-19 19:00:50 +02:00
constexpr std : : uint64_t & ticks ( ) ;
2019-03-13 19:00:37 +01:00
constexpr std : : uint64_t totalTicks ( ) const ;
2015-04-22 18:36:40 +02:00
int year ( ) const ;
int month ( ) const ;
int day ( ) const ;
int dayOfYear ( ) const ;
constexpr DayOfWeek dayOfWeek ( ) const ;
constexpr int hour ( ) const ;
constexpr int minute ( ) const ;
constexpr int second ( ) const ;
constexpr int millisecond ( ) const ;
2017-08-23 23:04:22 +02:00
constexpr int microsecond ( ) const ;
constexpr int nanosecond ( ) const ;
2015-04-22 18:36:40 +02:00
constexpr bool isNull ( ) const ;
constexpr TimeSpan timeOfDay ( ) const ;
bool isLeapYear ( ) const ;
2016-01-18 23:41:30 +01:00
constexpr bool isEternity ( ) const ;
2015-04-22 18:36:40 +02:00
constexpr bool isSameDay ( const DateTime & other ) const ;
std : : string toString ( DateTimeOutputFormat format = DateTimeOutputFormat : : DateAndTime , bool noMilliseconds = false ) const ;
void toString ( std : : string & result , DateTimeOutputFormat format = DateTimeOutputFormat : : DateAndTime , bool noMilliseconds = false ) const ;
2020-04-13 01:10:33 +02:00
std : : string toIsoStringWithCustomDelimiters (
TimeSpan timeZoneDelta = TimeSpan ( ) , char dateDelimiter = ' - ' , char timeDelimiter = ' : ' , char timeZoneDelimiter = ' : ' ) const ;
2016-09-01 15:42:25 +02:00
std : : string toIsoString ( TimeSpan timeZoneDelta = TimeSpan ( ) ) const ;
2019-11-17 22:30:51 +01:00
constexpr std : : time_t toTimeStamp ( ) const ;
2015-04-22 18:36:40 +02:00
static const char * printDayOfWeek ( DayOfWeek dayOfWeek , bool abbreviation = false ) ;
2016-01-18 23:41:30 +01:00
static constexpr DateTime eternity ( ) ;
2017-04-19 21:48:23 +02:00
static constexpr DateTime unixEpochStart ( ) ;
2015-04-22 18:36:40 +02:00
static DateTime now ( ) ;
2016-02-15 20:29:52 +01:00
static DateTime gmtNow ( ) ;
2017-07-29 00:32:19 +02:00
# if defined(PLATFORM_UNIX) && !defined(PLATFORM_MAC)
2017-04-19 21:48:23 +02:00
static DateTime exactGmtNow ( ) ;
# endif
2015-04-22 18:36:40 +02:00
constexpr static bool isLeapYear ( int year ) ;
static int daysInMonth ( int year , int month ) ;
2017-05-01 03:13:11 +02:00
constexpr bool operator = = ( const DateTime & other ) const ;
constexpr bool operator ! = ( const DateTime & other ) const ;
constexpr bool operator < ( const DateTime & other ) const ;
constexpr bool operator > ( const DateTime & other ) const ;
constexpr bool operator < = ( const DateTime & other ) const ;
constexpr bool operator > = ( const DateTime & other ) const ;
constexpr DateTime operator + ( const TimeSpan & timeSpan ) const ;
constexpr DateTime operator - ( const TimeSpan & timeSpan ) const ;
constexpr TimeSpan operator + ( const DateTime & other ) const ;
constexpr TimeSpan operator - ( const DateTime & other ) const ;
DateTime & operator + = ( const TimeSpan & timeSpan ) ;
DateTime & operator - = ( const TimeSpan & timeSpan ) ;
2015-04-22 18:36:40 +02:00
private :
2019-03-13 19:00:37 +01:00
static std : : uint64_t dateToTicks ( int year , int month , int day ) ;
static std : : uint64_t timeToTicks ( int hour , int minute , int second , double millisecond ) ;
2015-04-22 18:36:40 +02:00
int getDatePart ( DatePart part ) const ;
2019-03-13 19:00:37 +01:00
std : : uint64_t m_ticks ;
2015-04-22 18:36:40 +02:00
static const int m_daysPerYear ;
static const int m_daysPer4Years ;
static const int m_daysPer100Years ;
static const int m_daysPer400Years ;
static const int m_daysTo1601 ;
static const int m_daysTo1899 ;
static const int m_daysTo10000 ;
static const int m_daysToMonth365 [ 13 ] ;
static const int m_daysToMonth366 [ 13 ] ;
static const int m_daysInMonth365 [ 12 ] ;
static const int m_daysInMonth366 [ 12 ] ;
} ;
/*!
2016-01-18 23:41:30 +01:00
* \ brief Constructs a DateTime .
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline DateTime : : DateTime ( )
: m_ticks ( 0 )
{
}
2015-04-22 18:36:40 +02:00
/*!
2016-01-18 23:41:30 +01:00
* \ brief Constructs a DateTime with the specified number of \ a ticks .
2015-04-22 18:36:40 +02:00
*/
2019-03-13 19:00:37 +01:00
constexpr inline DateTime : : DateTime ( std : : uint64_t ticks )
2017-05-01 03:13:11 +02:00
: m_ticks ( ticks )
{
}
2015-04-22 18:36:40 +02:00
/*!
2016-01-18 23:41:30 +01:00
* \ brief Constructs a DateTime to the specified \ a year , \ a month , and \ a day .
2017-12-03 01:45:11 +01:00
* \ throws Throws a ConversionException if the specified \ a year , \ a month or \ a day is out - of - range .
2015-04-22 18:36:40 +02:00
*/
inline DateTime DateTime : : fromDate ( int year , int month , int day )
{
return DateTime ( dateToTicks ( year , month , day ) ) ;
}
/*!
2016-01-18 23:41:30 +01:00
* \ brief Constructs a DateTime to the specified \ a hour , \ a minute , \ a second and \ a millisecond .
2017-12-03 01:45:11 +01:00
* \ throws Throws a ConversionException if the specified \ a hour , \ a minute , \ a second or \ a millisecond is out - of - range .
2015-04-22 18:36:40 +02:00
*/
inline DateTime DateTime : : fromTime ( int hour , int minute , int second , double millisecond )
{
return DateTime ( timeToTicks ( hour , minute , second , millisecond ) ) ;
}
/*!
2016-01-18 23:41:30 +01:00
* \ brief Constructs a DateTime to the specified \ a year , \ a month , \ a day , \ a hour , \ a minute , \ a second and \ a millisecond .
2017-12-03 01:45:11 +01:00
* \ throws Throws a ConversionException if the specified \ a year , \ a month , \ a day , \ a hour , \ a minute , \ a second or \ a millisecond
* is out - of - range .
2015-04-22 18:36:40 +02:00
*/
inline DateTime DateTime : : fromDateAndTime ( int year , int month , int day , int hour , int minute , int second , double millisecond )
{
2020-04-24 21:55:45 +02:00
return DateTime ( dateToTicks ( year , month , day ) + timeToTicks ( hour , minute , second , millisecond ) ) ;
2015-04-22 18:36:40 +02:00
}
2016-08-30 19:59:04 +02:00
/*!
* \ brief Parses the given std : : string as DateTime .
2017-12-03 01:45:11 +01:00
* \ throws Throws a ConversionException if the specified \ a str does not match the expected time format .
*
* The expected format is something like " 2012-02-29 15:34:20.033 " or " 2012/02/29 15:34:20.033 " . The
* delimiters ' - ' , ' : ' and ' / ' are exchangeable .
*
* \ sa DateTime : : fromIsoString ( )
2016-08-30 19:59:04 +02:00
*/
inline DateTime DateTime : : fromString ( const std : : string & str )
{
return fromString ( str . data ( ) ) ;
}
2016-09-01 15:42:25 +02:00
/*!
* \ brief Parses the specified ISO date time denotation provided as C - style string .
* \ returns Returns the parsed UTC time . That means a possibly denoted time zone delta is subtracted from the time stamp .
2017-12-03 01:45:11 +01:00
* \ throws Throws a ConversionException if the specified \ a str does not match the expected time format .
2016-09-01 15:42:25 +02:00
* \ sa fromIsoString ( )
*/
inline DateTime DateTime : : fromIsoStringGmt ( const char * str )
{
const auto tmp = fromIsoString ( str ) ;
return tmp . first - tmp . second ;
}
/*!
* \ brief Parses the specified ISO date time denotation provided as C - style string .
* \ returns Returns the parsed local time . That means a possibly denoted time zone delta is discarded .
2017-12-03 01:45:11 +01:00
* \ throws Throws a ConversionException if the specified \ a str does not match the expected time format .
2016-09-01 15:42:25 +02:00
* \ sa fromIsoString ( )
*/
inline DateTime DateTime : : fromIsoStringLocal ( const char * str )
{
return fromIsoString ( str ) . first ;
}
2019-08-19 19:00:50 +02:00
/*!
* \ brief Constructs a new DateTime object with the GMT time from the specified UNIX \ a timeStamp .
*/
constexpr inline DateTime DateTime : : fromTimeStampGmt ( std : : time_t timeStamp )
{
return DateTime ( DateTime : : unixEpochStart ( ) . totalTicks ( ) + static_cast < std : : uint64_t > ( timeStamp ) * TimeSpan : : ticksPerSecond ) ;
}
2019-11-17 22:30:51 +01:00
/*!
* \ brief Constructs a new DateTime object with the local time from the specified std : : chrono : : time_point .
* \ remarks Works only with time points of std : : chrono : : system_clock so far . C + + 20 will fix this . Until then this function
* should be considered experimental .
*/
template < typename TimePoint > inline DateTime DateTime : : fromChronoTimePoint ( TimePoint timePoint )
{
return DateTime : : fromTimeStamp ( decltype ( timePoint ) : : clock : : to_time_t ( timePoint ) ) ;
}
/*!
* \ brief Constructs a new DateTime object with the GMT time from the specified std : : chrono : : time_point .
* \ remarks Works only with time points of std : : chrono : : system_clock so far . C + + 20 will fix this . Until then this function
* should be considered experimental .
*/
template < typename TimePoint > constexpr DateTime DateTime : : fromChronoTimePointGmt ( TimePoint timePoint )
{
return DateTime : : fromTimeStampGmt ( decltype ( timePoint ) : : clock : : to_time_t ( timePoint ) ) ;
}
2018-06-23 14:32:46 +02:00
/*!
* \ brief Returns a mutable reference to the total ticks .
*/
2019-08-19 19:00:50 +02:00
constexpr inline std : : uint64_t & DateTime : : ticks ( )
2018-06-23 14:32:46 +02:00
{
return m_ticks ;
}
2015-04-22 18:36:40 +02:00
/*!
2017-12-03 01:45:11 +01:00
* \ brief Returns the number of ticks which represent the value of the current instance .
2015-04-22 18:36:40 +02:00
*/
2019-03-13 19:00:37 +01:00
constexpr inline std : : uint64_t DateTime : : totalTicks ( ) const
2015-04-22 18:36:40 +02:00
{
return m_ticks ;
}
/*!
2017-12-03 01:45:11 +01:00
* \ brief Returns the year component of the date represented by this instance .
2015-04-22 18:36:40 +02:00
*/
inline int DateTime : : year ( ) const
{
return getDatePart ( DatePart : : Year ) ;
}
/*!
2017-12-03 01:45:11 +01:00
* \ brief Returns the month component of the date represented by this instance .
2015-04-22 18:36:40 +02:00
*/
inline int DateTime : : month ( ) const
{
return getDatePart ( DatePart : : Month ) ;
}
/*!
2017-12-03 01:45:11 +01:00
* \ brief Returns the day component of the date represented by this instance .
2015-04-22 18:36:40 +02:00
*/
inline int DateTime : : day ( ) const
{
return getDatePart ( DatePart : : Day ) ;
}
/*!
2017-12-03 01:45:11 +01:00
* \ brief Returns the day of the year represented by this instance .
2015-04-22 18:36:40 +02:00
*/
inline int DateTime : : dayOfYear ( ) const
{
return getDatePart ( DatePart : : DayOfYear ) ;
}
/*!
2017-12-03 01:45:11 +01:00
* \ brief Returns the day of the week represented by this instance .
2015-04-22 18:36:40 +02:00
* \ sa DayOfWeek
*/
constexpr inline DayOfWeek DateTime : : dayOfWeek ( ) const
{
2017-08-23 23:14:18 +02:00
return static_cast < DayOfWeek > ( ( m_ticks / TimeSpan : : ticksPerDay ) % 7l ) ;
2015-04-22 18:36:40 +02:00
}
/*!
2017-12-03 01:45:11 +01:00
* \ brief Returns the hour component of the date represented by this instance .
2015-04-22 18:36:40 +02:00
*/
constexpr inline int DateTime : : hour ( ) const
{
2017-08-23 23:14:18 +02:00
return m_ticks / TimeSpan : : ticksPerHour % 24ul ;
2015-04-22 18:36:40 +02:00
}
/*!
2017-12-03 01:45:11 +01:00
* \ brief Returns the minute component of the date represented by this instance .
2015-04-22 18:36:40 +02:00
*/
constexpr inline int DateTime : : minute ( ) const
{
2017-08-23 23:14:18 +02:00
return m_ticks / TimeSpan : : ticksPerMinute % 60ul ;
2015-04-22 18:36:40 +02:00
}
/*!
2017-12-03 01:45:11 +01:00
* \ brief Returns the second component of the date represented by this instance .
2015-04-22 18:36:40 +02:00
*/
constexpr inline int DateTime : : second ( ) const
{
2017-08-23 23:14:18 +02:00
return m_ticks / TimeSpan : : ticksPerSecond % 60ul ;
2015-04-22 18:36:40 +02:00
}
/*!
2017-12-03 01:45:11 +01:00
* \ brief Returns the millisecond component of the date represented by this instance .
2015-04-22 18:36:40 +02:00
*/
constexpr inline int DateTime : : millisecond ( ) const
{
2017-08-23 23:14:18 +02:00
return m_ticks / TimeSpan : : ticksPerMillisecond % 1000ul ;
2015-04-22 18:36:40 +02:00
}
2017-08-23 23:04:22 +02:00
/*!
2017-12-03 01:45:11 +01:00
* \ brief Returns the microsecond component of the date represented by this instance .
2017-08-23 23:04:22 +02:00
*/
2019-08-19 19:00:50 +02:00
constexpr inline int DateTime : : microsecond ( ) const
2017-08-23 23:04:22 +02:00
{
return m_ticks / TimeSpan : : ticksPerMicrosecond % 1000ul ;
}
/*!
2017-12-03 01:45:11 +01:00
* \ brief Returns the nanosecond component of the date represented by this instance .
2017-08-23 23:04:22 +02:00
* \ remarks The accuracy of the DateTime class is 100 - nanoseconds . Hence the returned value
* will always have two zeros at the end ( in decimal representation ) .
*/
2019-08-19 19:00:50 +02:00
constexpr inline int DateTime : : nanosecond ( ) const
2017-08-23 23:04:22 +02:00
{
return m_ticks % 10ul * TimeSpan : : nanosecondsPerTick ;
}
2015-04-22 18:36:40 +02:00
/*!
2016-01-18 23:41:30 +01:00
* \ brief Returns ture if the date represented by the current DateTime class is null .
2015-04-22 18:36:40 +02:00
* \ sa DateTime
*/
constexpr inline bool DateTime : : isNull ( ) const
{
return m_ticks = = 0 ;
}
/*!
2017-12-03 01:45:11 +01:00
* \ brief Returns the time of day as TimeSpan for this instance .
2015-04-22 18:36:40 +02:00
*/
constexpr inline TimeSpan DateTime : : timeOfDay ( ) const
{
2017-08-23 23:14:18 +02:00
return TimeSpan ( m_ticks % TimeSpan : : ticksPerDay ) ;
2015-04-22 18:36:40 +02:00
}
/*!
2017-11-29 19:11:01 +01:00
* \ brief Returns an indication whether the year represented by this instance is a leap year .
2015-04-22 18:36:40 +02:00
*/
inline bool DateTime : : isLeapYear ( ) const
{
return isLeapYear ( year ( ) ) ;
}
/*!
2016-01-18 23:41:30 +01:00
* \ brief Returns whether the instance has the maximal number of ticks .
*/
constexpr inline bool DateTime : : isEternity ( ) const
{
return m_ticks = = std : : numeric_limits < decltype ( m_ticks ) > : : max ( ) ;
}
/*!
* \ brief Returns an indication whether the specified \ a year is a leap year .
2015-04-22 18:36:40 +02:00
*/
constexpr inline bool DateTime : : isLeapYear ( int year )
{
2017-05-01 03:13:11 +02:00
return ( year % 4 ! = 0 ) ? false : ( ( year % 100 = = 0 ) ? ( year % 400 = = 0 ) : true ) ;
2015-04-22 18:36:40 +02:00
}
/*!
2016-01-18 23:41:30 +01:00
* \ brief Returns the number of days in the specified \ a month and \ a year .
2015-04-22 18:36:40 +02:00
*/
inline int DateTime : : daysInMonth ( int year , int month )
{
2017-05-01 03:13:11 +02:00
return ( month > = 1 & & month < = 12 ) ? ( isLeapYear ( year ) ? m_daysInMonth366 [ month - 1 ] : m_daysInMonth365 [ month - 1 ] ) : ( 0 ) ;
2015-04-22 18:36:40 +02:00
}
/*!
2016-01-18 23:41:30 +01:00
* \ brief Returns and indication whether two DateTime instances represent the same day .
2015-04-22 18:36:40 +02:00
*/
constexpr inline bool DateTime : : isSameDay ( const DateTime & other ) const
{
2017-08-23 23:14:18 +02:00
return ( m_ticks / TimeSpan : : ticksPerDay ) = = ( other . m_ticks / TimeSpan : : ticksPerDay ) ;
2015-04-22 18:36:40 +02:00
}
2019-08-19 19:00:50 +02:00
/*!
* \ brief Returns the string representation of the current instance using the specified \ a format .
* \ remarks If \ a noMilliseconds is true the date will be rounded to full seconds .
* \ sa toIsoString ( ) for ISO format
*/
inline std : : string DateTime : : toString ( DateTimeOutputFormat format , bool noMilliseconds ) const
{
std : : string result ;
toString ( result , format , noMilliseconds ) ;
return result ;
}
2019-11-17 22:30:51 +01:00
/*!
* \ brief Returns the UNIX timestamp for the current instance .
*/
constexpr std : : time_t DateTime : : toTimeStamp ( ) const
{
return ( totalTicks ( ) - DateTime : : unixEpochStart ( ) . totalTicks ( ) ) / TimeSpan : : ticksPerSecond ;
}
2015-04-22 18:36:40 +02:00
/*!
2016-01-18 23:41:30 +01:00
* \ brief Constructs a new instance of the DateTime class with the maximal number of ticks .
*/
constexpr inline DateTime DateTime : : eternity ( )
{
return DateTime ( std : : numeric_limits < decltype ( m_ticks ) > : : max ( ) ) ;
}
2017-04-19 21:48:23 +02:00
/*!
* \ brief Returns the DateTime object for the " 1970-01-01T00:00:00Z " .
*/
constexpr inline DateTime DateTime : : unixEpochStart ( )
{
return DateTime ( 621355968000000000 ) ;
}
2016-05-08 20:36:26 +02:00
/*!
* \ brief Returns a DateTime object that is set to the current date and time on this computer , expressed as the local time .
2017-04-19 21:48:23 +02:00
* \ remarks The time might be rounded to full seconds . Use exactGmtNow ( ) for better precision .
2016-05-08 20:36:26 +02:00
*/
inline DateTime DateTime : : now ( )
{
2018-01-28 18:03:58 +01:00
return DateTime : : fromTimeStamp ( std : : time ( nullptr ) ) ;
2016-05-08 20:36:26 +02:00
}
/*!
* \ brief Returns a DateTime object that is set to the current date and time on this computer , expressed as the GMT time .
2017-04-19 21:48:23 +02:00
* \ remarks The time might be rounded to full seconds . Use exactGmtNow ( ) for better precision .
2016-05-08 20:36:26 +02:00
*/
inline DateTime DateTime : : gmtNow ( )
{
2018-01-28 18:03:58 +01:00
return DateTime : : fromTimeStampGmt ( std : : time ( nullptr ) ) ;
2016-05-08 20:36:26 +02:00
}
2016-01-18 23:41:30 +01:00
/*!
* \ brief Indicates whether two DateTime instances are equal .
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline bool DateTime : : operator = = ( const DateTime & 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 DateTime instances are not equal .
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline bool DateTime : : operator ! = ( const DateTime & 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 DateTime is less than another specified DateTime .
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline bool DateTime : : operator < ( const DateTime & 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 DateTime is greater than another specified DateTime .
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline bool DateTime : : operator > ( const DateTime & 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 DateTime is less or equal than another specified DateTime .
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline bool DateTime : : operator < = ( const DateTime & 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 DateTime is greater or equal than another specified DateTime .
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline bool DateTime : : operator > = ( const DateTime & 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 another instance .
2015-04-22 18:36:40 +02:00
* \ returns The result is another DateTime .
*/
2017-05-01 03:13:11 +02:00
constexpr inline DateTime DateTime : : operator + ( const TimeSpan & timeSpan ) const
2015-04-22 18:36:40 +02:00
{
return DateTime ( m_ticks + timeSpan . m_ticks ) ;
}
/*!
2016-01-18 23:41:30 +01:00
* \ brief Substracts another instance .
2015-04-22 18:36:40 +02:00
* \ returns The result is another DateTime .
*/
2017-05-01 03:13:11 +02:00
constexpr inline DateTime DateTime : : operator - ( const TimeSpan & timeSpan ) const
2015-04-22 18:36:40 +02:00
{
return DateTime ( m_ticks - timeSpan . m_ticks ) ;
}
/*!
2016-01-18 23:41:30 +01:00
* \ brief Adds two instances .
2015-04-22 18:36:40 +02:00
* \ returns The result is a TimeSpan .
*/
2017-05-01 03:13:11 +02:00
constexpr inline TimeSpan DateTime : : operator + ( const DateTime & 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 DateTime instances .
2015-04-22 18:36:40 +02:00
* \ returns The result is a TimeSpan .
2017-12-03 01:45:11 +01:00
* \ remarks For expressing the delta between two concrete DateTime instances in terms of
* years , month and days , use Period : : Period instead .
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
constexpr inline TimeSpan DateTime : : operator - ( const DateTime & 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 a TimeSpan to the current instance .
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
inline DateTime & DateTime : : operator + = ( const TimeSpan & timeSpan )
2015-04-22 18:36:40 +02:00
{
m_ticks + = timeSpan . m_ticks ;
return * this ;
}
/*!
2016-01-18 23:41:30 +01:00
* \ brief Substracts a TimeSpan from the current instance .
2015-04-22 18:36:40 +02:00
*/
2017-05-01 03:13:11 +02:00
inline DateTime & DateTime : : operator - = ( const TimeSpan & timeSpan )
2015-04-22 18:36:40 +02:00
{
m_ticks - = timeSpan . m_ticks ;
return * this ;
}
2019-06-10 21:56:46 +02:00
} // namespace CppUtilities
2015-04-22 18:36:40 +02:00
2016-11-26 13:07:05 +01:00
namespace std {
2019-09-04 18:54:48 +02:00
/// \brief Computes the hash for the CppUtilities::DateTime instance.
2019-06-10 21:56:46 +02:00
template < > struct hash < CppUtilities : : DateTime > {
inline size_t operator ( ) ( const CppUtilities : : DateTime & dateTime ) const
2016-11-26 13:07:05 +01:00
{
2017-05-01 03:13:11 +02:00
return hash < decltype ( dateTime . totalTicks ( ) ) > ( ) ( dateTime . totalTicks ( ) ) ;
2016-11-26 13:07:05 +01:00
}
} ;
2017-09-17 21:45:23 +02:00
} // namespace std
2016-11-26 13:07:05 +01:00
2016-06-11 19:09:14 +02:00
# endif // CHRONO_UTILITIES_DATETIME_H