1 #ifndef CHRONO_UTILITIES_DATETIME_H
2 #define CHRONO_UTILITIES_DATETIME_H
56 explicit constexpr
DateTime(std::uint64_t ticks);
57 static DateTime fromDate(
int year = 1,
int month = 1,
int day = 1);
58 static DateTime fromTime(
int hour = 0,
int minute = 0,
int second = 0,
double millisecond = 0.0);
59 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);
60 static DateTime fromString(
const std::string &str);
61 static DateTime fromString(
const char *str);
62 static std::pair<DateTime, TimeSpan> fromIsoString(
const char *str);
63 static DateTime fromIsoStringGmt(
const char *str);
64 static DateTime fromIsoStringLocal(
const char *str);
65 static DateTime fromTimeStamp(std::time_t timeStamp);
66 constexpr
static DateTime fromTimeStampGmt(std::time_t timeStamp);
67 template <
typename TimePo
int>
static DateTime fromChronoTimePoint(TimePoint timePoint);
70 constexpr std::uint64_t &ticks();
71 constexpr std::uint64_t totalTicks()
const;
75 int dayOfYear()
const;
77 constexpr
int hour()
const;
78 constexpr
int minute()
const;
79 constexpr
int second()
const;
80 constexpr
int millisecond()
const;
81 constexpr
int microsecond()
const;
82 constexpr
int nanosecond()
const;
83 constexpr
bool isNull()
const;
84 constexpr
TimeSpan timeOfDay()
const;
85 bool isLeapYear()
const;
86 constexpr
bool isEternity()
const;
87 constexpr
bool isSameDay(
const DateTime &other)
const;
90 std::string toIsoStringWithCustomDelimiters(
91 TimeSpan timeZoneDelta =
TimeSpan(),
char dateDelimiter =
'-',
char timeDelimiter =
':',
char timeZoneDelimiter =
':')
const;
93 constexpr std::time_t toTimeStamp()
const;
94 static const char *printDayOfWeek(
DayOfWeek dayOfWeek,
bool abbreviation =
false);
96 static constexpr
DateTime eternity();
97 static constexpr
DateTime unixEpochStart();
100 #if defined(PLATFORM_UNIX) && !defined(PLATFORM_MAC)
103 constexpr
static bool isLeapYear(
int year);
104 static int daysInMonth(
int year,
int month);
107 constexpr
bool operator!=(
const DateTime &other)
const;
108 constexpr
bool operator<(
const DateTime &other)
const;
109 constexpr
bool operator>(
const DateTime &other)
const;
110 constexpr
bool operator<=(
const DateTime &other)
const;
111 constexpr
bool operator>=(
const DateTime &other)
const;
120 static std::uint64_t dateToTicks(
int year,
int month,
int day);
121 static std::uint64_t timeToTicks(
int hour,
int minute,
int second,
double millisecond);
122 int getDatePart(
DatePart part)
const;
124 std::uint64_t m_ticks;
125 static const int m_daysPerYear;
126 static const int m_daysPer4Years;
127 static const int m_daysPer100Years;
128 static const int m_daysPer400Years;
129 static const int m_daysTo1601;
130 static const int m_daysTo1899;
131 static const int m_daysTo10000;
132 static const int m_daysToMonth365[13];
133 static const int m_daysToMonth366[13];
134 static const int m_daysInMonth365[12];
135 static const int m_daysInMonth366[12];
205 return tmp.first - tmp.second;
384 return m_ticks == std::numeric_limits<decltype(m_ticks)>
::max();
392 return (
year % 4 != 0) ? false : ((
year % 100 == 0) ? (
year % 400 == 0) :
true);
419 toString(result, format, noMilliseconds);
436 return DateTime(std::numeric_limits<decltype(m_ticks)>::
max());
444 return DateTime(621355968000000000);
470 return m_ticks == other.m_ticks;
478 return m_ticks != other.m_ticks;
486 return m_ticks < other.m_ticks;
494 return m_ticks > other.m_ticks;
502 return m_ticks <= other.m_ticks;
510 return m_ticks >= other.m_ticks;
519 return DateTime(m_ticks +
static_cast<std::uint64_t
>(timeSpan.m_ticks));
528 return DateTime(m_ticks -
static_cast<std::uint64_t
>(timeSpan.m_ticks));
537 return TimeSpan(
static_cast<std::int64_t
>(m_ticks + other.m_ticks));
548 return TimeSpan(
static_cast<std::int64_t
>(m_ticks - other.m_ticks));
556 m_ticks +=
static_cast<std::uint64_t
>(timeSpan.m_ticks);
565 m_ticks +=
static_cast<std::uint64_t
>(timeSpan.m_ticks);
Represents an instant in time, typically expressed as a date and time of day.
std::string toString(DateTimeOutputFormat format=DateTimeOutputFormat::DateAndTime, bool noMilliseconds=false) const
Returns the string representation of the current instance using the specified format.
int day() const
Returns the day component of the date represented by this instance.
constexpr DayOfWeek dayOfWeek() const
Returns the day of the week represented by this instance.
bool isLeapYear() const
Returns an indication whether the year represented by this instance is a leap year.
constexpr static DateTime fromTimeStampGmt(std::time_t timeStamp)
Constructs a new DateTime object with the GMT time from the specified UNIX timeStamp.
static DateTime now()
Returns a DateTime object that is set to the current date and time on this computer,...
constexpr bool operator>(const DateTime &other) const
Indicates whether a specified DateTime is greater than another specified DateTime.
static DateTime fromIsoStringLocal(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
constexpr bool isNull() const
Returns true if the date represented by the current DateTime class is null.
int month() const
Returns the month component of the date represented by this instance.
constexpr std::uint64_t totalTicks() const
Returns the number of ticks which represent the value of the current instance.
constexpr DateTime()
Constructs a DateTime.
DateTime & operator-=(const TimeSpan &timeSpan)
Subtracts a TimeSpan from the current instance.
DateTime & operator+=(const TimeSpan &timeSpan)
Adds a TimeSpan to the current instance.
static constexpr DateTime unixEpochStart()
Returns the DateTime object for the "1970-01-01T00:00:00Z".
constexpr int microsecond() const
Returns the microsecond component of the date represented by this instance.
constexpr bool operator>=(const DateTime &other) const
Indicates whether a specified DateTime is greater or equal than another specified DateTime.
constexpr bool operator!=(const DateTime &other) const
Indicates whether two DateTime instances are not equal.
constexpr bool operator==(const DateTime &other) const
Indicates whether two DateTime instances are equal.
static std::pair< DateTime, TimeSpan > fromIsoString(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
constexpr DateTime operator+(const TimeSpan &timeSpan) const
Adds another instance.
int dayOfYear() const
Returns 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.
constexpr bool isSameDay(const DateTime &other) const
Returns and indication whether two DateTime instances represent the same day.
constexpr int hour() const
Returns the hour component of the date represented by this instance.
static DateTime fromString(const std::string &str)
Parses the given std::string as DateTime.
constexpr int second() const
Returns the second component of the date represented by this instance.
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 TimeSpan timeOfDay() const
Returns the time of day as TimeSpan for this instance.
static DateTime fromChronoTimePoint(TimePoint timePoint)
Constructs a new DateTime object with the local time from the specified std::chrono::time_point.
constexpr static DateTime fromChronoTimePointGmt(TimePoint timePoint)
static DateTime gmtNow()
Returns a DateTime object that is set to the current date and time on this computer,...
static DateTime fromTimeStamp(std::time_t timeStamp)
Constructs a new DateTime object with the local time from the specified UNIX timeStamp.
constexpr DateTime operator-(const TimeSpan &timeSpan) const
Subtracts another instance.
constexpr bool isEternity() const
Returns whether the instance has the maximal number of ticks.
constexpr int millisecond() const
Returns the millisecond component of the date represented by this instance.
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 int nanosecond() const
Returns the nanosecond component of the date represented by this instance.
static DateTime fromIsoStringGmt(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
constexpr bool operator<(const DateTime &other) const
Indicates whether a specified DateTime is less than another specified DateTime.
static int daysInMonth(int year, int month)
Returns the number of days in the specified month and year.
constexpr std::uint64_t & ticks()
Returns a mutable reference to the total ticks.
constexpr std::time_t toTimeStamp() const
Returns the UNIX timestamp for the current instance.
static constexpr DateTime eternity()
Constructs a new instance of the DateTime class with the maximal number of ticks.
constexpr int minute() const
Returns the minute component of the date represented by this instance.
int year() const
Returns the year component of the date represented by this instance.
Represents a time interval.
static constexpr std::int64_t ticksPerMinute
static constexpr std::int64_t ticksPerDay
static constexpr std::int64_t ticksPerSecond
static constexpr std::int64_t nanosecondsPerTick
static constexpr std::int64_t ticksPerHour
static constexpr std::int64_t ticksPerMicrosecond
static constexpr std::int64_t ticksPerMillisecond
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
constexpr FlagEnumClass & operator-=(FlagEnumClass &lhs, FlagEnumClass rhs)
constexpr FlagEnumClass & operator+=(FlagEnumClass &lhs, FlagEnumClass rhs)
Contains all utilities provides by the c++utilities library.
CPP_UTILITIES_EXPORT DateTime operator+(DateTime begin, Period period)
Adds the specified period to the specified date.
DatePart
Specifies the date part.
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
constexpr T max(T first, T second)
Returns the greatest of the given items.
DateTimeOutputFormat
Specifies the output format.
@ DateTimeAndShortWeekday
@ IsoOmittingDefaultComponents
DayOfWeek
Specifies the day of the week.
size_t operator()(const CppUtilities::DateTime &dateTime) const