C++ Utilities 5.26.1
Useful C++ classes and routines such as argument parser, IO and conversion utilities
|
Represents an instant in time, typically expressed as a date and time of day. More...
#include <datetime.h>
Public Types | |
using | TickType = std::uint64_t |
Public Member Functions | |
constexpr | DateTime () |
Constructs a DateTime. | |
constexpr | DateTime (TickType ticks) |
Constructs a DateTime with the specified number of ticks. | |
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. | |
int | year () const |
Returns the year component of the date represented by this instance. | |
int | month () const |
Returns the month component of the date represented by this instance. | |
int | day () const |
Returns the day component of the date represented by this instance. | |
int | dayOfYear () const |
Returns the day of the year represented by this instance. | |
constexpr DayOfWeek | dayOfWeek () const |
Returns the day of the week represented by this instance. | |
constexpr int | hour () const |
Returns the hour component of the date represented by this instance. | |
constexpr int | minute () const |
Returns the minute component of the date represented by this instance. | |
constexpr int | second () const |
Returns the second component of the date represented by this instance. | |
constexpr int | millisecond () const |
Returns the millisecond component of the date represented by this instance. | |
constexpr int | microsecond () const |
Returns the microsecond component of the date represented by this instance. | |
constexpr int | nanosecond () const |
Returns the nanosecond component of the date represented by this instance. | |
constexpr bool | isNull () const |
Returns true if the date represented by the current DateTime class is null. | |
constexpr TimeSpan | timeOfDay () const |
Returns the time of day as TimeSpan for this instance. | |
bool | isLeapYear () const |
Returns an indication whether the year represented by this instance is a leap year. | |
constexpr bool | isEternity () const |
Returns whether the instance has the maximal number of ticks. | |
constexpr bool | isSameDay (const DateTime &other) const |
Returns and indication whether two DateTime instances represent the same day. | |
std::string | toString (DateTimeOutputFormat format=DateTimeOutputFormat::DateAndTime, bool noMilliseconds=false) const |
Returns the string representation of the current instance using the specified format. | |
void | toString (std::string &result, DateTimeOutputFormat format=DateTimeOutputFormat::DateAndTime, bool noMilliseconds=false) const |
Returns the string representation of the current instance using the specified format. | |
std::string | toIsoStringWithCustomDelimiters (TimeSpan timeZoneDelta=TimeSpan(), char dateDelimiter='-', char timeDelimiter=':', char timeZoneDelimiter=':') const |
Returns the string representation of the current instance in the ISO format with custom delimiters, eg. | |
std::string | toIsoString (TimeSpan timeZoneDelta=TimeSpan()) const |
Returns the string representation of the current instance in the ISO format, eg. | |
constexpr std::time_t | toTimeStamp () const |
Returns the UNIX timestamp for the current instance. | |
constexpr bool | operator== (const DateTime &other) const |
Indicates whether two DateTime instances are equal. | |
constexpr bool | operator!= (const DateTime &other) const |
Indicates whether two DateTime instances are not equal. | |
constexpr bool | operator< (const DateTime &other) const |
Indicates whether a specified DateTime is less than another specified DateTime. | |
constexpr bool | operator> (const DateTime &other) const |
Indicates whether a specified DateTime is greater than another specified DateTime. | |
constexpr bool | operator<= (const DateTime &other) const |
Indicates whether a specified DateTime is less or equal than another specified DateTime. | |
constexpr bool | operator>= (const DateTime &other) const |
Indicates whether a specified DateTime is greater or equal than another specified DateTime. | |
constexpr DateTime | operator+ (const TimeSpan &timeSpan) const |
Adds another instance. | |
constexpr DateTime | operator- (const TimeSpan &timeSpan) const |
Subtracts another instance. | |
constexpr TimeSpan | operator+ (const DateTime &other) const |
Adds two instances. | |
constexpr TimeSpan | operator- (const DateTime &other) const |
Subtracts two DateTime instances. | |
DateTime & | operator+= (const TimeSpan &timeSpan) |
Adds a TimeSpan to the current instance. | |
DateTime & | operator-= (const TimeSpan &timeSpan) |
Subtracts a TimeSpan from the current instance. | |
Static Public Member Functions | |
static DateTime | fromDate (int year=1, int month=1, int day=1) |
Constructs a DateTime to the specified year, month, and day. | |
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. | |
static DateTime | fromString (const std::string &str) |
Parses the given std::string as DateTime. | |
static DateTime | fromString (const char *str) |
Parses the given C-style string as DateTime. | |
static std::pair< DateTime, TimeSpan > | fromIsoString (const char *str) |
Parses the specified ISO date time denotation provided as C-style string. | |
static DateTime | fromIsoStringGmt (const char *str) |
Parses the specified ISO date time denotation provided as C-style string. | |
static DateTime | fromIsoStringLocal (const char *str) |
Parses the specified ISO date time denotation provided as C-style string. | |
static DateTime | fromTimeStamp (std::time_t timeStamp) |
Constructs a new DateTime object with the local time from the specified UNIX timeStamp. | |
static constexpr DateTime | fromTimeStampGmt (std::time_t timeStamp) |
Constructs a new DateTime object with the GMT time from the specified UNIX timeStamp. | |
template<typename TimePoint > | |
static DateTime | fromChronoTimePoint (TimePoint timePoint) |
Constructs a new DateTime object with the local time from the specified std::chrono::time_point. | |
template<typename TimePoint > | |
static constexpr DateTime | fromChronoTimePointGmt (TimePoint timePoint) |
Constructs a new DateTime object with the GMT time from the specified std::chrono::time_point. | |
static const char * | printDayOfWeek (DayOfWeek dayOfWeek, bool abbreviation=false) |
Returns the string representation as C-style string for the given day of week. | |
static constexpr DateTime | eternity () |
Constructs a new instance of the DateTime class with the maximal number of ticks. | |
static constexpr DateTime | unixEpochStart () |
Returns the DateTime object for the "1970-01-01T00:00:00Z". | |
static DateTime | now () |
Returns a DateTime object that is set to the current date and time on this computer, expressed as the local time. | |
static DateTime | gmtNow () |
Returns a DateTime object that is set to the current date and time on this computer, expressed as the GMT time. | |
static constexpr bool | isLeapYear (int year) |
Returns an indication whether the specified year is a leap year. | |
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.
Definition at line 55 of file datetime.h.
using CppUtilities::DateTime::TickType = std::uint64_t |
Definition at line 57 of file datetime.h.
|
inlineexplicitconstexpr |
Constructs a DateTime.
Definition at line 194 of file datetime.h.
|
inlineexplicitconstexpr |
Constructs a DateTime with the specified number of ticks.
Definition at line 202 of file datetime.h.
|
inline |
Returns the day component of the date represented by this instance.
Definition at line 334 of file datetime.h.
|
inlineconstexpr |
Returns the day of the week represented by this instance.
Definition at line 351 of file datetime.h.
|
inline |
Returns the day of the year represented by this instance.
Definition at line 342 of file datetime.h.
|
inlinestatic |
Returns the number of days in the specified month and year.
Definition at line 450 of file datetime.h.
|
inlinestaticconstexpr |
Constructs a new instance of the DateTime class with the maximal number of ticks.
Definition at line 486 of file datetime.h.
|
inlinestatic |
Constructs a new DateTime object with the local time from the specified std::chrono::time_point.
Definition at line 284 of file datetime.h.
|
staticconstexpr |
Constructs a new DateTime object with the GMT time from the specified std::chrono::time_point.
Definition at line 294 of file datetime.h.
|
inlinestatic |
Constructs a DateTime to the specified year, month, and day.
Throws | a ConversionException if the specified year, month or day is out-of-range. |
Definition at line 211 of file datetime.h.
|
inlinestatic |
Constructs a DateTime to the specified year, month, day, hour, minute, second and millisecond.
Throws | a ConversionException if the specified year, month, day, hour, minute, second or millisecond is out-of-range. |
Definition at line 230 of file datetime.h.
Parses the specified ISO date time denotation provided as C-style string.
Throws | a ConversionException if the specified str does not match the expected time format. |
Definition at line 105 of file datetime.cpp.
|
inlinestatic |
Parses the specified ISO date time denotation provided as C-style string.
Throws | a ConversionException if the specified str does not match the expected time format. |
Definition at line 255 of file datetime.h.
|
inlinestatic |
Parses the specified ISO date time denotation provided as C-style string.
Throws | a ConversionException if the specified str does not match the expected time format. |
Definition at line 266 of file datetime.h.
|
static |
Parses the given C-style string as DateTime.
Throws | a ConversionException if the specified str does not match the expected time format. |
The expected format is something like "2012-02-29 15:34:20.033" or "2012/02/29 15:34:20.033". The delimiters '-', ':' and '/' are exchangeable.
Definition at line 89 of file datetime.cpp.
|
inlinestatic |
Parses the given std::string as DateTime.
Throws | a ConversionException if the specified str does not match the expected time format. |
The expected format is something like "2012-02-29 15:34:20.033" or "2012/02/29 15:34:20.033". The delimiters '-', ':' and '/' are exchangeable.
Definition at line 244 of file datetime.h.
|
inlinestatic |
Constructs a DateTime to the specified hour, minute, second and millisecond.
Throws | a ConversionException if the specified hour, minute, second or millisecond is out-of-range. |
Definition at line 220 of file datetime.h.
|
static |
Constructs a new DateTime object with the local time from the specified UNIX timeStamp.
Definition at line 69 of file datetime.cpp.
|
inlinestaticconstexpr |
Constructs a new DateTime object with the GMT time from the specified UNIX timeStamp.
Definition at line 274 of file datetime.h.
|
inlinestatic |
Returns a DateTime object that is set to the current date and time on this computer, expressed as the GMT time.
Definition at line 512 of file datetime.h.
|
inlineconstexpr |
Returns the hour component of the date represented by this instance.
Definition at line 359 of file datetime.h.
|
inlineconstexpr |
Returns whether the instance has the maximal number of ticks.
Definition at line 434 of file datetime.h.
|
inline |
Returns an indication whether the year represented by this instance is a leap year.
Definition at line 426 of file datetime.h.
|
inlinestaticconstexpr |
Returns an indication whether the specified year is a leap year.
Definition at line 442 of file datetime.h.
|
inlineconstexpr |
Returns true if the date represented by the current DateTime class is null.
Definition at line 410 of file datetime.h.
|
inlineconstexpr |
Returns and indication whether two DateTime instances represent the same day.
Definition at line 458 of file datetime.h.
|
inlineconstexpr |
Returns the microsecond component of the date represented by this instance.
Definition at line 391 of file datetime.h.
|
inlineconstexpr |
Returns the millisecond component of the date represented by this instance.
Definition at line 383 of file datetime.h.
|
inlineconstexpr |
Returns the minute component of the date represented by this instance.
Definition at line 367 of file datetime.h.
|
inline |
Returns the month component of the date represented by this instance.
Definition at line 326 of file datetime.h.
|
inlineconstexpr |
Returns the nanosecond component of the date represented by this instance.
Definition at line 401 of file datetime.h.
|
inlinestatic |
Returns a DateTime object that is set to the current date and time on this computer, expressed as the local time.
Definition at line 503 of file datetime.h.
|
inlineconstexpr |
Indicates whether two DateTime instances are not equal.
Definition at line 528 of file datetime.h.
Adds another instance.
Definition at line 569 of file datetime.h.
Adds a TimeSpan to the current instance.
Definition at line 606 of file datetime.h.
Subtracts two DateTime instances.
Definition at line 598 of file datetime.h.
Subtracts another instance.
Definition at line 578 of file datetime.h.
Subtracts a TimeSpan from the current instance.
Definition at line 615 of file datetime.h.
|
inlineconstexpr |
Indicates whether a specified DateTime is less than another specified DateTime.
Definition at line 536 of file datetime.h.
|
inlineconstexpr |
Indicates whether a specified DateTime is less or equal than another specified DateTime.
Definition at line 552 of file datetime.h.
|
inlineconstexpr |
Indicates whether two DateTime instances are equal.
Definition at line 520 of file datetime.h.
|
inlineconstexpr |
Indicates whether a specified DateTime is greater than another specified DateTime.
Definition at line 544 of file datetime.h.
|
inlineconstexpr |
Indicates whether a specified DateTime is greater or equal than another specified DateTime.
Definition at line 560 of file datetime.h.
|
static |
Returns the string representation as C-style string for the given day of week.
If abbreviation is true, only the first three letters of the string will be returned.
Definition at line 234 of file datetime.cpp.
|
inlineconstexpr |
Returns the second component of the date represented by this instance.
Definition at line 375 of file datetime.h.
|
inlineconstexpr |
Returns a mutable reference to the total ticks.
Definition at line 302 of file datetime.h.
|
inlineconstexpr |
Returns the time of day as TimeSpan for this instance.
Definition at line 418 of file datetime.h.
Returns the string representation of the current instance in the ISO format, eg.
2016-08-29T21:32:31.588539814+02:00.
Definition at line 222 of file datetime.cpp.
string CppUtilities::DateTime::toIsoStringWithCustomDelimiters | ( | TimeSpan | timeZoneDelta = TimeSpan(), |
char | dateDelimiter = '-', | ||
char | timeDelimiter = ':', | ||
char | timeZoneDelimiter = ':' ) const |
Returns the string representation of the current instance in the ISO format with custom delimiters, eg.
2016/08/29T21-32-31.588539814+02:00 with '/' as dateDelimiter and '-' as timeDelimiter.
Definition at line 188 of file datetime.cpp.
|
inline |
Returns the string representation of the current instance using the specified format.
Definition at line 468 of file datetime.h.
void CppUtilities::DateTime::toString | ( | std::string & | result, |
DateTimeOutputFormat | format = DateTimeOutputFormat::DateAndTime, | ||
bool | noMilliseconds = false ) const |
Returns the string representation of the current instance using the specified format.
Definition at line 116 of file datetime.cpp.
|
inlineconstexpr |
Returns the number of ticks which represent the value of the current instance.
Definition at line 310 of file datetime.h.
|
constexpr |
Returns the UNIX timestamp for the current instance.
Definition at line 478 of file datetime.h.
|
inlinestaticconstexpr |
Returns the DateTime object for the "1970-01-01T00:00:00Z".
Definition at line 494 of file datetime.h.
|
inline |
Returns the year component of the date represented by this instance.
Definition at line 318 of file datetime.h.