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
int microsecond()
const;
77 constexpr
int nanosecond()
const;
78 constexpr
bool isNull()
const;
79 constexpr
TimeSpan timeOfDay()
const;
80 bool isLeapYear()
const;
81 constexpr
bool isEternity()
const;
82 constexpr
bool isSameDay(
const DateTime &other)
const;
86 static const char *printDayOfWeek(
DayOfWeek dayOfWeek,
bool abbreviation =
false);
88 static constexpr DateTime eternity();
89 static constexpr DateTime unixEpochStart();
90 static DateTime now();
91 static DateTime gmtNow();
92 #if defined(PLATFORM_UNIX) && !defined(PLATFORM_MAC) 93 static DateTime exactGmtNow();
95 constexpr
static bool isLeapYear(
int year);
96 static int daysInMonth(
int year,
int month);
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;
102 constexpr
bool operator<=(
const DateTime &other)
const;
103 constexpr
bool operator>=(
const DateTime &other)
const;
105 constexpr DateTime operator-(
const TimeSpan &timeSpan)
const;
107 constexpr
TimeSpan operator-(
const DateTime &other)
const;
108 DateTime &operator+=(
const TimeSpan &timeSpan);
109 DateTime &operator-=(
const TimeSpan &timeSpan);
112 static uint64 dateToTicks(
int year,
int month,
int day);
113 static uint64 timeToTicks(
int hour,
int minute,
int second,
double millisecond);
114 int getDatePart(
DatePart part)
const;
117 static const int m_daysPerYear;
118 static const int m_daysPer4Years;
119 static const int m_daysPer100Years;
120 static const int m_daysPer400Years;
121 static const int m_daysTo1601;
122 static const int m_daysTo1899;
123 static const int m_daysTo10000;
124 static const int m_daysToMonth365[13];
125 static const int m_daysToMonth366[13];
126 static const int m_daysInMonth365[12];
127 static const int m_daysInMonth366[12];
151 return DateTime(dateToTicks(year, month, day));
159 return DateTime(timeToTicks(hour, minute, second, millisecond));
167 if (
uint64 ticks = dateToTicks(year, month, day)) {
168 return DateTime(ticks + timeToTicks(hour, minute, second, millisecond));
189 return tmp.first - tmp.second;
248 return static_cast<DayOfWeek>((m_ticks / TimeSpan::m_ticksPerDay) % 7l);
256 return m_ticks / TimeSpan::m_ticksPerHour % 24ul;
264 return m_ticks / TimeSpan::m_ticksPerMinute % 60ul;
272 return m_ticks / TimeSpan::m_ticksPerSecond % 60ul;
280 return m_ticks / TimeSpan::m_ticksPerMillisecond % 1000ul;
315 return TimeSpan(m_ticks % TimeSpan::m_ticksPerDay);
331 return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
339 return (year % 4 != 0) ? false : ((year % 100 == 0) ? (year % 400 == 0) :
true);
347 return (month >= 1 && month <= 12) ? (
isLeapYear(year) ? m_daysInMonth366[month - 1] : m_daysInMonth365[month - 1]) : (0);
355 return (m_ticks / TimeSpan::m_ticksPerDay) == (other.m_ticks / TimeSpan::m_ticksPerDay);
363 return DateTime(std::numeric_limits<decltype(m_ticks)>::max());
371 return DateTime(621355968000000000);
397 return m_ticks == other.m_ticks;
405 return m_ticks != other.m_ticks;
413 return m_ticks < other.m_ticks;
421 return m_ticks > other.m_ticks;
429 return m_ticks <= other.m_ticks;
437 return m_ticks >= other.m_ticks;
446 return DateTime(m_ticks + timeSpan.m_ticks);
455 return DateTime(m_ticks - timeSpan.m_ticks);
464 return TimeSpan(m_ticks + other.m_ticks);
473 return TimeSpan(m_ticks - other.m_ticks);
481 m_ticks += timeSpan.m_ticks;
490 m_ticks -= timeSpan.m_ticks;
504 #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.
constexpr int nanosecond() const
Gets the nanosecond component of the date represented by this 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
static constexpr int64 ticksPerMicrosecond
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 int64 nanosecondsPerTick
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.
constexpr int microsecond() const
Gets the microsecond component of the date represented by this instance.
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.