C++ Utilities
5.7.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
|
Go to the documentation of this file. 1 #ifndef CHRONO_UTILITIES_DATETIME_H
2 #define CHRONO_UTILITIES_DATETIME_H
22 DateTimeAndShortWeekday,
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;
88 std::string toString(
DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime,
bool noMilliseconds =
false)
const;
89 void toString(std::string &result,
DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime,
bool noMilliseconds =
false)
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;
268 return getDatePart(DatePart::Year);
276 return getDatePart(DatePart::Month);
292 return getDatePart(DatePart::DayOfYear);
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 + timeSpan.m_ticks);
528 return DateTime(m_ticks - timeSpan.m_ticks);
537 return TimeSpan(m_ticks + other.m_ticks);
548 return TimeSpan(m_ticks - other.m_ticks);
556 m_ticks += timeSpan.m_ticks;
565 m_ticks -= timeSpan.m_ticks;
580 #endif // CHRONO_UTILITIES_DATETIME_H
DateTimeOutputFormat
Specifies the output format.
constexpr std::time_t toTimeStamp() const
Returns the UNIX timestamp for the current instance.
std::string toString(DateTimeOutputFormat format=DateTimeOutputFormat::DateAndTime, bool noMilliseconds=false) const
Returns the string representation of the current instance using the specified format.
static DateTime fromIsoStringLocal(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
constexpr DateTime()
Constructs a DateTime.
static constexpr std::int64_t ticksPerDay
constexpr bool isEternity() const
Returns whether the instance has the maximal number of ticks.
constexpr DateTime operator-(const TimeSpan &timeSpan) const
Substracts another instance.
size_t operator()(const CppUtilities::DateTime &dateTime) const
static constexpr std::int64_t ticksPerMillisecond
bool isLeapYear() const
Returns an indication whether the year represented by this instance is a leap year.
static DateTime fromDate(int year=1, int month=1, int day=1)
Constructs a DateTime to the specified year, month, and day.
int year() const
Returns the year component of the date represented by this instance.
constexpr static DateTime fromTimeStampGmt(std::time_t timeStamp)
Constructs a new DateTime object with the GMT time from the specified UNIX timeStamp.
CPP_UTILITIES_EXPORT DateTime operator+(DateTime begin, Period period)
Adds the specified period to the specified date.
constexpr static DateTime fromChronoTimePointGmt(TimePoint timePoint)
constexpr DateTime operator+(const TimeSpan &timeSpan) const
Adds another instance.
static constexpr DateTime eternity()
Constructs a new instance of the DateTime class with the maximal number of ticks.
static std::pair< DateTime, TimeSpan > fromIsoString(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
static constexpr std::int64_t ticksPerMicrosecond
constexpr bool isNull() const
Returns ture if the date represented by the current DateTime class is null.
constexpr bool operator<=(const DateTime &other) const
Indicates whether a specified DateTime is less or equal than another specified DateTime.
constexpr int millisecond() const
Returns the millisecond component of the date represented by this instance.
constexpr int minute() const
Returns the minute component of the date represented by this instance.
int month() const
Returns the month component of the date represented by this instance.
constexpr T max(T first, T second)
Returns the greatest of the given items.
constexpr int second() const
Returns the second component of the date represented by this instance.
constexpr bool operator!=(const DateTime &other) const
Indicates whether two DateTime instances are not equal.
constexpr bool isSameDay(const DateTime &other) const
Returns and indication whether two DateTime instances represent the same day.
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 a specified DateTime is greater than another specified DateTime.
DatePart
Specifies the date part.
Contains all utilities provides by the c++utilities library.
DateTime & operator+=(const TimeSpan &timeSpan)
Adds a TimeSpan to the current instance.
static constexpr std::int64_t ticksPerHour
static DateTime gmtNow()
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 less than another specified DateTime.
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.
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.
constexpr FlagEnumClass & operator+=(FlagEnumClass &lhs, FlagEnumClass rhs)
static DateTime fromChronoTimePoint(TimePoint timePoint)
Constructs a new DateTime object with the local time from the specified std::chrono::time_point.
constexpr DayOfWeek dayOfWeek() const
Returns the day of the week represented by this instance.
constexpr int nanosecond() const
Returns the nanosecond component of the date represented by this instance.
constexpr FlagEnumClass & operator-=(FlagEnumClass &lhs, FlagEnumClass rhs)
static DateTime now()
Returns a DateTime object that is set to the current date and time on this computer,...
DayOfWeek
Specifies the day of the week.
int dayOfYear() const
Returns the day of the year represented by this instance.
constexpr std::uint64_t & ticks()
Returns a mutable reference to the total ticks.
Represents a time interval.
static DateTime fromTimeStamp(std::time_t timeStamp)
Constructs a new DateTime object with the local time from the specified UNIX timeStamp.
static constexpr std::int64_t nanosecondsPerTick
static constexpr DateTime unixEpochStart()
Returns the DateTime object for the "1970-01-01T00:00:00Z".
static DateTime fromIsoStringGmt(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
constexpr int microsecond() const
Returns the microsecond component of the date represented by this instance.
constexpr bool operator==(const DateTime &other) const
Indicates whether two DateTime instances are equal.
Represents an instant in time, typically expressed as a date and time of day.
static constexpr std::int64_t ticksPerSecond
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
static int daysInMonth(int year, int month)
Returns the number of days in the specified month and year.
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
DateTime & operator-=(const TimeSpan &timeSpan)
Substracts a TimeSpan from the current instance.
static constexpr std::int64_t ticksPerMinute
int day() const
Returns the day component of the date represented by this instance.
static DateTime fromString(const std::string &str)
Parses the given std::string as DateTime.
constexpr TimeSpan timeOfDay() const
Returns the time of day as TimeSpan for this instance.
constexpr std::uint64_t totalTicks() const
Returns the number of ticks which represent the value of the current instance.
constexpr int hour() const
Returns the hour component of the date represented by this instance.