1#ifndef CHRONO_UTILITIES_DATETIME_H
2#define CHRONO_UTILITIES_DATETIME_H
61 static DateTime fromDate(
int year = 1,
int month = 1,
int day = 1);
62 static DateTime fromTime(
int hour = 0,
int minute = 0,
int second = 0,
double millisecond = 0.0);
63 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);
64 static DateTime fromString(
const std::string &str);
65 static DateTime fromString(
const char *str);
66 static std::pair<DateTime, TimeSpan> fromIsoString(
const char *str);
67 static DateTime fromIsoStringGmt(
const char *str);
68 static DateTime fromIsoStringLocal(
const char *str);
69 static DateTime fromTimeStamp(std::time_t timeStamp);
70 constexpr static DateTime fromTimeStampGmt(std::time_t timeStamp);
71 template <
typename TimePo
int>
static DateTime fromChronoTimePoint(TimePoint timePoint);
72 template <
typename TimePo
int>
constexpr static DateTime fromChronoTimePointGmt(TimePoint timePoint);
75 constexpr TickType totalTicks()
const;
79 int dayOfYear()
const;
81 constexpr int hour()
const;
82 constexpr int minute()
const;
83 constexpr int second()
const;
84 constexpr int millisecond()
const;
85 constexpr int microsecond()
const;
86 constexpr int nanosecond()
const;
87 constexpr bool isNull()
const;
88 constexpr TimeSpan timeOfDay()
const;
89 bool isLeapYear()
const;
90 constexpr bool isEternity()
const;
91 constexpr bool isSameDay(
const DateTime &other)
const;
92 std::string toString(
DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime,
bool noMilliseconds =
false)
const;
93 void toString(std::string &result,
DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime,
bool noMilliseconds =
false)
const;
94 std::string toIsoStringWithCustomDelimiters(
95 TimeSpan timeZoneDelta =
TimeSpan(),
char dateDelimiter =
'-',
char timeDelimiter =
':',
char timeZoneDelimiter =
':')
const;
97 constexpr std::time_t toTimeStamp()
const;
98 static const char *printDayOfWeek(
DayOfWeek dayOfWeek,
bool abbreviation =
false);
100 static constexpr DateTime eternity();
101 static constexpr DateTime unixEpochStart();
104#if defined(PLATFORM_UNIX) && !defined(PLATFORM_MAC)
107 constexpr static bool isLeapYear(
int year);
108 static int daysInMonth(
int year,
int month);
111 constexpr bool operator!=(
const DateTime &other)
const;
112 constexpr bool operator<(
const DateTime &other)
const;
113 constexpr bool operator>(
const DateTime &other)
const;
114 constexpr bool operator<=(
const DateTime &other)
const;
115 constexpr bool operator>=(
const DateTime &other)
const;
124 static TickType dateToTicks(
int year,
int month,
int day);
125 static TickType timeToTicks(
int hour,
int minute,
int second,
double millisecond);
126 int getDatePart(
DatePart part)
const;
129 static const int m_daysPerYear;
130 static const int m_daysPer4Years;
131 static const int m_daysPer100Years;
132 static const int m_daysPer400Years;
133 static const int m_daysTo1601;
134 static const int m_daysTo1899;
135 static const int m_daysTo10000;
136 static const int m_daysToMonth365[13];
137 static const int m_daysToMonth366[13];
138 static const int m_daysInMonth365[12];
139 static const int m_daysInMonth366[12];
170 std::string toIsoString(
char dateDelimiter =
'-',
char timeDelimiter =
':',
char timeZoneDelimiter =
':')
const;
436 return m_ticks == std::numeric_limits<TickType>::max();
444 return (
year % 4 != 0) ? false : ((
year % 100 == 0) ? (
year % 400 == 0) :
true);
471 toString(result, format, noMilliseconds);
488 return DateTime(std::numeric_limits<TickType>::max());
496 return DateTime(621355968000000000);
522 return m_ticks == other.m_ticks;
530 return m_ticks != other.m_ticks;
538 return m_ticks < other.m_ticks;
546 return m_ticks > other.m_ticks;
554 return m_ticks <= other.m_ticks;
562 return m_ticks >= other.m_ticks;
608 m_ticks +=
static_cast<TickType>(timeSpan.m_ticks);
617 m_ticks +=
static_cast<TickType>(timeSpan.m_ticks);
627 return hash<CppUtilities::DateTime::TickType>()(dateTime.
totalTicks());
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.
static constexpr 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 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".
static constexpr DateTime fromChronoTimePointGmt(TimePoint timePoint)
Constructs a new DateTime object with the GMT time from the specified std::chrono::time_point.
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.
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 TickType & ticks()
Returns a mutable reference to the total ticks.
constexpr TickType totalTicks() const
Returns the number of ticks which represent the value of the current instance.
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::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 TickType nanosecondsPerTick
static constexpr TickType ticksPerMillisecond
static constexpr TickType ticksPerMinute
static constexpr TickType ticksPerMicrosecond
static constexpr TickType ticksPerSecond
static constexpr TickType ticksPerDay
static constexpr TickType ticksPerHour
#define CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(Namespace, EnumClassType)
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
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.
DateTimeParts
The DateTimeParts enum specifies which parts of a timestamp are present.
DateTimeOutputFormat
Specifies the output format.
@ DateTimeAndShortWeekday
@ IsoOmittingDefaultComponents
DayOfWeek
Specifies the day of the week.
The DateTimeExpression struct holds information about a time expression (e.g.
constexpr DateTime gmt() const
Returns the value in UTC time.
constexpr bool operator==(const DateTimeExpression &other) const
Returns whether the expressions are equivalent.
static DateTimeExpression fromIsoString(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
size_t operator()(const CppUtilities::DateTime &dateTime) const