1 #ifndef CHRONO_UTILITIES_DATETIME_H 2 #define CHRONO_UTILITIES_DATETIME_H 6 #include "../conversion/types.h" 54 explicit constexpr DateTime(
uint64 ticks);
55 static DateTime fromDate(
int year = 1,
int month = 1,
int day = 1);
56 static DateTime fromTime(
int hour = 0,
int minute = 0,
int second = 0,
double millisecond = 0.0);
57 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);
58 static DateTime fromString(
const std::string &str);
59 static DateTime fromString(
const char *str);
60 static std::pair<DateTime, TimeSpan> fromIsoString(
const char *str);
61 static DateTime fromIsoStringGmt(
const char *str);
62 static DateTime fromIsoStringLocal(
const char *str);
63 static DateTime fromTimeStamp(time_t timeStamp);
64 static DateTime fromTimeStampGmt(time_t timeStamp);
66 constexpr
uint64 totalTicks()
const;
70 int dayOfYear()
const;
72 constexpr
int hour()
const;
73 constexpr
int minute()
const;
74 constexpr
int second()
const;
75 constexpr
int millisecond()
const;
76 constexpr
bool isNull()
const;
77 constexpr
TimeSpan timeOfDay()
const;
78 bool isLeapYear()
const;
79 constexpr
bool isEternity()
const;
80 constexpr
bool isSameDay(
const DateTime &other)
const;
84 static const char *printDayOfWeek(
DayOfWeek dayOfWeek,
bool abbreviation =
false);
86 static constexpr DateTime eternity();
87 static constexpr DateTime unixEpochStart();
88 static DateTime now();
89 static DateTime gmtNow();
90 #if defined(PLATFORM_UNIX) 91 static DateTime exactGmtNow();
93 constexpr
static bool isLeapYear(
int year);
94 static int daysInMonth(
int year,
int month);
96 constexpr
bool operator==(
const DateTime &other)
const;
97 constexpr
bool operator!=(
const DateTime &other)
const;
98 constexpr
bool operator<(
const DateTime &other)
const;
99 constexpr
bool operator>(
const DateTime &other)
const;
100 constexpr
bool operator<=(
const DateTime &other)
const;
101 constexpr
bool operator>=(
const DateTime &other)
const;
103 constexpr DateTime operator-(
const TimeSpan &timeSpan)
const;
105 constexpr
TimeSpan operator-(
const DateTime &other)
const;
106 DateTime &operator+=(
const TimeSpan &timeSpan);
107 DateTime &operator-=(
const TimeSpan &timeSpan);
110 static uint64 dateToTicks(
int year,
int month,
int day);
111 static uint64 timeToTicks(
int hour,
int minute,
int second,
double millisecond);
112 int getDatePart(
DatePart part)
const;
115 static const int m_daysPerYear;
116 static const int m_daysPer4Years;
117 static const int m_daysPer100Years;
118 static const int m_daysPer400Years;
119 static const int m_daysTo1601;
120 static const int m_daysTo1899;
121 static const int m_daysTo10000;
122 static const int m_daysToMonth365[13];
123 static const int m_daysToMonth366[13];
124 static const int m_daysInMonth365[12];
125 static const int m_daysInMonth366[12];
149 return DateTime(dateToTicks(year, month, day));
157 return DateTime(timeToTicks(hour, minute, second, millisecond));
165 if (
uint64 ticks = dateToTicks(year, month, day)) {
166 return DateTime(ticks + timeToTicks(hour, minute, second, millisecond));
187 return tmp.first - tmp.second;
246 return static_cast<DayOfWeek>((m_ticks / TimeSpan::m_ticksPerDay) % 7l);
254 return m_ticks / TimeSpan::m_ticksPerHour % 24ul;
262 return m_ticks / TimeSpan::m_ticksPerMinute % 60ul;
270 return m_ticks / TimeSpan::m_ticksPerSecond % 60ul;
278 return m_ticks / TimeSpan::m_ticksPerMillisecond % 1000ul;
295 return TimeSpan(m_ticks % TimeSpan::m_ticksPerDay);
311 return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
319 return (year % 4 != 0) ? false : ((year % 100 == 0) ? (year % 400 == 0) :
true);
327 return (month >= 1 && month <= 12) ? (
isLeapYear(year) ? m_daysInMonth366[month - 1] : m_daysInMonth365[month - 1]) : (0);
335 return (m_ticks / TimeSpan::m_ticksPerDay) == (other.m_ticks / TimeSpan::m_ticksPerDay);
343 return DateTime(std::numeric_limits<decltype(m_ticks)>::max());
351 return DateTime(621355968000000000);
377 return m_ticks == other.m_ticks;
385 return m_ticks != other.m_ticks;
393 return m_ticks < other.m_ticks;
401 return m_ticks > other.m_ticks;
409 return m_ticks <= other.m_ticks;
417 return m_ticks >= other.m_ticks;
426 return DateTime(m_ticks + timeSpan.m_ticks);
435 return DateTime(m_ticks - timeSpan.m_ticks);
444 return TimeSpan(m_ticks + other.m_ticks);
453 return TimeSpan(m_ticks - other.m_ticks);
461 m_ticks += timeSpan.m_ticks;
470 m_ticks -= timeSpan.m_ticks;
484 #endif // CHRONO_UTILITIES_DATETIME_H
int year() const
Gets the year component of the date represented by this instance.
constexpr int minute() const
Gets the minute component of the date represented by this instance.
DateTime & operator-=(const TimeSpan &timeSpan)
Substracts a TimeSpan from the current instance.
static DateTime now()
Returns a DateTime object that is set to the current date and time on this computer, expressed as the local time.
constexpr DateTime operator-(const TimeSpan &timeSpan) const
Substracts another instance.
constexpr DateTime operator+(const TimeSpan &timeSpan) const
Adds another instance.
static std::pair< DateTime, TimeSpan > fromIsoString(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
static int daysInMonth(int year, int month)
Returns the number of days in the specified month and year.
Represents an instant in time, typically expressed as a date and time of day.
Contains classes providing a means for handling date and time information.
constexpr bool isEternity() const
Returns whether the instance has the maximal number of ticks.
static DateTime fromString(const std::string &str)
Parses the given std::string as DateTime.
constexpr uint64 totalTicks() const
Gets the number of ticks which represent the value of the current instance.
static constexpr DateTime eternity()
Constructs a new instance of the DateTime class with the maximal number of ticks. ...
Represents a time interval.
constexpr bool operator>=(const DateTime &other) const
Indicates whether a specified DateTime is greater or equal than another specified DateTime...
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)
Constructs a DateTime to the specified year, month, day, hour, minute, second and millisecond...
static DateTime fromDate(int year=1, int month=1, int day=1)
Constructs a DateTime to the specified year, month, and day.
constexpr int second() const
Gets the second component of the date represented by this instance.
std::uint64_t uint64
unsigned 64-bit integer
constexpr TimeSpan timeOfDay() const
Gets the time of day as TimeSpan for this instance.
int day() const
Gets the day component of the date represented by this instance.
DateTime & operator+=(const TimeSpan &timeSpan)
Adds a TimeSpan to the current instance.
constexpr bool isNull() const
Returns ture if the date represented by the current DateTime class is null.
static DateTime fromIsoStringGmt(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
constexpr bool isSameDay(const DateTime &other) const
Returns and indication whether two DateTime instances represent the same day.
static DateTime gmtNow()
Returns a DateTime object that is set to the current date and time on this computer, expressed as the GMT time.
static constexpr DateTime unixEpochStart()
Returns the DateTime object for the "1970-01-01T00:00:00Z".
constexpr int millisecond() const
Gets the millisecond component of the date represented by this instance.
constexpr bool operator==(const DateTime &other) const
Indicates whether two DateTime instances are equal.
constexpr bool operator<(const DateTime &other) const
Indicates whether a specified DateTime is less than another specified DateTime.
DatePart
Specifies the date part.
constexpr DateTime()
Constructs a DateTime.
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
static DateTime fromTime(int hour=0, int minute=0, int second=0, double millisecond=0.0)
Constructs a DateTime to the specified hour, minute, second and millisecond.
constexpr bool operator>(const DateTime &other) const
Indicates whether a specified DateTime is greater than another specified DateTime.
DateTimeOutputFormat
Specifies the output format.
static DateTime fromTimeStampGmt(time_t timeStamp)
Constructs a new DateTime object with the GMT time from the specified UNIX timeStamp.
int month() const
Gets the month component of the date represented by this instance.
bool isLeapYear() const
Returns an indication whether the year of the dae represented by this instance is a leap year...
size_t operator()(const ChronoUtilities::DateTime &dateTime) const
std::string operator+(const Tuple &lhs, const std::string &rhs)
Allows construction of final string from previously constructed string-tuple and trailing string via ...
static DateTime fromTimeStamp(time_t timeStamp)
Constructs a new DateTime object with the local time from the specified UNIX timeStamp.
int dayOfYear() const
Gets the day of the year represented by this instance.
constexpr bool operator<=(const DateTime &other) const
Indicates whether a specified DateTime is less or equal than another specified DateTime.
static DateTime fromIsoStringLocal(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
DayOfWeek
Specifies the day of the week.
constexpr int hour() const
Gets the hour component of the date represented by this instance.
constexpr bool operator!=(const DateTime &other) const
Indicates whether two DateTime instances are not equal.
constexpr DayOfWeek dayOfWeek() const
Gets the day of the week represented by this instance.