1 #ifndef CHRONO_UTILITIES_DATETIME_H 2 #define CHRONO_UTILITIES_DATETIME_H 6 #include "../conversion/types.h" 56 static DateTime fromDate(
int year = 1,
int month = 1,
int day = 1);
57 static DateTime fromTime(
int hour = 0,
int minute = 0,
int second = 0,
double millisecond = 0.0);
58 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);
59 static DateTime fromString(
const std::string &str);
60 static DateTime fromString(
const char *str);
61 static std::pair<DateTime, TimeSpan> fromIsoString(
const char *str);
62 static DateTime fromIsoStringGmt(
const char *str);
63 static DateTime fromIsoStringLocal(
const char *str);
64 static DateTime fromTimeStamp(time_t timeStamp);
65 static DateTime fromTimeStampGmt(time_t timeStamp);
68 constexpr
uint64 totalTicks()
const;
72 int dayOfYear()
const;
74 constexpr
int hour()
const;
75 constexpr
int minute()
const;
76 constexpr
int second()
const;
77 constexpr
int millisecond()
const;
78 constexpr
int microsecond()
const;
79 constexpr
int nanosecond()
const;
80 constexpr
bool isNull()
const;
81 constexpr
TimeSpan timeOfDay()
const;
82 bool isLeapYear()
const;
83 constexpr
bool isEternity()
const;
84 constexpr
bool isSameDay(
const DateTime &other)
const;
88 static const char *printDayOfWeek(
DayOfWeek dayOfWeek,
bool abbreviation =
false);
90 static constexpr
DateTime eternity();
91 static constexpr
DateTime unixEpochStart();
94 #if defined(PLATFORM_UNIX) && !defined(PLATFORM_MAC) 97 constexpr
static bool isLeapYear(
int year);
98 static int daysInMonth(
int year,
int month);
101 constexpr
bool operator!=(
const DateTime &other)
const;
102 constexpr
bool operator<(
const DateTime &other)
const;
103 constexpr
bool operator>(
const DateTime &other)
const;
104 constexpr
bool operator<=(
const DateTime &other)
const;
105 constexpr
bool operator>=(
const DateTime &other)
const;
114 static uint64 dateToTicks(
int year,
int month,
int day);
115 static uint64 timeToTicks(
int hour,
int minute,
int second,
double millisecond);
116 int getDatePart(
DatePart part)
const;
119 static const int m_daysPerYear;
120 static const int m_daysPer4Years;
121 static const int m_daysPer100Years;
122 static const int m_daysPer400Years;
123 static const int m_daysTo1601;
124 static const int m_daysTo1899;
125 static const int m_daysTo10000;
126 static const int m_daysToMonth365[13];
127 static const int m_daysToMonth366[13];
128 static const int m_daysInMonth365[12];
129 static const int m_daysInMonth366[12];
202 return tmp.first - tmp.second;
270 return static_cast<DayOfWeek>((m_ticks / TimeSpan::m_ticksPerDay) % 7l);
278 return m_ticks / TimeSpan::m_ticksPerHour % 24ul;
286 return m_ticks / TimeSpan::m_ticksPerMinute % 60ul;
294 return m_ticks / TimeSpan::m_ticksPerSecond % 60ul;
302 return m_ticks / TimeSpan::m_ticksPerMillisecond % 1000ul;
337 return TimeSpan(m_ticks % TimeSpan::m_ticksPerDay);
353 return m_ticks == std::numeric_limits<decltype(m_ticks)>
::max();
361 return (
year % 4 != 0) ? false : ((
year % 100 == 0) ? (
year % 400 == 0) :
true);
377 return (m_ticks / TimeSpan::m_ticksPerDay) == (other.m_ticks / TimeSpan::m_ticksPerDay);
385 return DateTime(std::numeric_limits<decltype(m_ticks)>::
max());
393 return DateTime(621355968000000000);
419 return m_ticks == other.m_ticks;
427 return m_ticks != other.m_ticks;
435 return m_ticks < other.m_ticks;
443 return m_ticks > other.m_ticks;
451 return m_ticks <= other.m_ticks;
459 return m_ticks >= other.m_ticks;
468 return DateTime(m_ticks + timeSpan.m_ticks);
477 return DateTime(m_ticks - timeSpan.m_ticks);
486 return TimeSpan(m_ticks + other.m_ticks);
497 return TimeSpan(m_ticks - other.m_ticks);
505 m_ticks += timeSpan.m_ticks;
514 m_ticks -= timeSpan.m_ticks;
528 #endif // CHRONO_UTILITIES_DATETIME_H DateTime CPP_UTILITIES_EXPORT operator+(DateTime begin, Period period)
Adds the specified period to the specified date.
int year() const
Returns the year component of the date represented by this instance.
constexpr int minute() const
Returns 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,...
constexpr DateTime operator-(const TimeSpan &timeSpan) const
Substracts another instance.
constexpr DateTime operator+(const TimeSpan &timeSpan) const
Adds another instance.
constexpr int nanosecond() const
Returns 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.
#define CPP_UTILITIES_EXPORT
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
Returns 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
Returns 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
Returns the time of day as TimeSpan for this instance.
int day() const
Returns 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 T max(T first, T second)
Returns the greatest of the given items.
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,...
static constexpr int64 nanosecondsPerTick
static constexpr DateTime unixEpochStart()
Returns the DateTime object for the "1970-01-01T00:00:00Z".
uint64 & ticks()
Returns a mutable reference to the total ticks.
constexpr int millisecond() const
Returns 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
Returns 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
Returns the month component of the date represented by this instance.
bool isLeapYear() const
Returns an indication whether the year represented by this instance is a leap year.
size_t operator()(const ChronoUtilities::DateTime &dateTime) const
static DateTime fromTimeStamp(time_t timeStamp)
Constructs a new DateTime object with the local time from the specified UNIX timeStamp.
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.
static DateTime fromIsoStringLocal(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
DayOfWeek
Specifies the day of the week.
constexpr int hour() const
Returns 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
Returns the day of the week represented by this instance.