1 #ifndef CHRONO_UTILITIES_DATETIME_H 2 #define CHRONO_UTILITIES_DATETIME_H 6 #include "../conversion/types.h" 59 explicit constexpr DateTime(
uint64 ticks);
60 static DateTime fromDate(
int year = 1,
int month = 1,
int day = 1);
61 static DateTime fromTime(
int hour = 0,
int minute = 0,
int second = 0,
double millisecond = 0.0);
62 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);
63 static DateTime fromString(
const std::string &str);
64 static DateTime fromString(
const char *str);
65 static std::pair<DateTime, TimeSpan> fromIsoString(
const char *str);
66 static DateTime fromIsoStringGmt(
const char *str);
67 static DateTime fromIsoStringLocal(
const char *str);
68 static DateTime fromTimeStamp(time_t timeStamp);
69 static DateTime fromTimeStampGmt(time_t timeStamp);
71 constexpr
uint64 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
bool isNull()
const;
82 constexpr
TimeSpan timeOfDay()
const;
83 bool isLeapYear()
const;
84 constexpr
bool isEternity()
const;
85 constexpr
bool isSameDay(
const DateTime &other)
const;
89 static const char *printDayOfWeek(
DayOfWeek dayOfWeek,
bool abbreviation =
false);
91 static constexpr DateTime eternity();
92 static DateTime now();
93 static DateTime gmtNow();
94 constexpr
static bool isLeapYear(
int year);
95 static int daysInMonth(
int year,
int month);
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;
102 constexpr
bool operator >=(
const DateTime &other)
const;
104 constexpr DateTime operator -(
const TimeSpan &timeSpan)
const;
106 constexpr
TimeSpan operator -(
const DateTime &other)
const;
107 DateTime &operator +=(
const TimeSpan &timeSpan);
108 DateTime &operator -=(
const TimeSpan &timeSpan);
111 static uint64 dateToTicks(
int year,
int month,
int day);
112 static uint64 timeToTicks(
int hour,
int minute,
int second,
double millisecond);
113 int getDatePart(
DatePart part)
const;
116 static const int m_daysPerYear;
117 static const int m_daysPer4Years;
118 static const int m_daysPer100Years;
119 static const int m_daysPer400Years;
120 static const int m_daysTo1601;
121 static const int m_daysTo1899;
122 static const int m_daysTo10000;
123 static const int m_daysToMonth365[13];
124 static const int m_daysToMonth366[13];
125 static const int m_daysInMonth365[12];
126 static const int m_daysInMonth366[12];
150 return DateTime(dateToTicks(year, month, day));
158 return DateTime(timeToTicks(hour, minute, second, millisecond));
166 if(
uint64 ticks = dateToTicks(year, month, day)) {
167 return DateTime(ticks + timeToTicks(hour, minute, second, millisecond));
188 return tmp.first - tmp.second;
312 return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
320 return (year % 4 != 0)
332 return (month >= 1 && month <= 12)
334 ? m_daysInMonth366[month - 1]
335 : m_daysInMonth365[month - 1])
352 return DateTime(std::numeric_limits<decltype(m_ticks)>::max());
376 return m_ticks == other.m_ticks;
384 return m_ticks != other.m_ticks;
392 return m_ticks < other.m_ticks;
400 return m_ticks > other.m_ticks;
408 return m_ticks <= other.m_ticks;
416 return m_ticks >= other.m_ticks;
425 return DateTime(m_ticks + timeSpan.m_ticks);
434 return DateTime(m_ticks - timeSpan.m_ticks);
443 return TimeSpan(m_ticks + other.m_ticks);
452 return TimeSpan(m_ticks - other.m_ticks);
460 m_ticks += timeSpan.m_ticks;
469 m_ticks -= timeSpan.m_ticks;
475 #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 constexpr uint64 ticksPerDay
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 constexpr uint64 ticksPerMillisecond
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.
static constexpr uint64 ticksPerHour
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.
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 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...
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 timeStamp.
static constexpr uint64 ticksPerSecond
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.
static constexpr uint64 ticksPerMinute
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.