C++ Utilities
5.0.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
54 explicit constexpr
DateTime(std::uint64_t ticks);
55 static DateTime fromDate(
int year = 1,
int month = 1,
int day = 1);
56 static DateTime fromTime(
int hour = 0,
int minute = 0,
int second = 0,
double millisecond = 0.0);
57 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);
58 static DateTime fromString(
const std::string &str);
59 static DateTime fromString(
const char *str);
60 static std::pair<DateTime, TimeSpan> fromIsoString(
const char *str);
61 static DateTime fromIsoStringGmt(
const char *str);
62 static DateTime fromIsoStringLocal(
const char *str);
63 static DateTime fromTimeStamp(time_t timeStamp);
64 constexpr
static DateTime fromTimeStampGmt(time_t timeStamp);
66 constexpr std::uint64_t &ticks();
67 constexpr std::uint64_t totalTicks()
const;
71 int dayOfYear()
const;
73 constexpr
int hour()
const;
74 constexpr
int minute()
const;
75 constexpr
int second()
const;
76 constexpr
int millisecond()
const;
77 constexpr
int microsecond()
const;
78 constexpr
int nanosecond()
const;
79 constexpr
bool isNull()
const;
80 constexpr
TimeSpan timeOfDay()
const;
81 bool isLeapYear()
const;
82 constexpr
bool isEternity()
const;
83 constexpr
bool isSameDay(
const DateTime &other)
const;
87 static const char *printDayOfWeek(
DayOfWeek dayOfWeek,
bool abbreviation =
false);
89 static constexpr
DateTime eternity();
90 static constexpr
DateTime unixEpochStart();
93 #if defined(PLATFORM_UNIX) && !defined(PLATFORM_MAC)
96 constexpr
static bool isLeapYear(
int year);
97 static int daysInMonth(
int year,
int month);
100 constexpr
bool operator!=(
const DateTime &other)
const;
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;
113 static std::uint64_t dateToTicks(
int year,
int month,
int day);
114 static std::uint64_t timeToTicks(
int hour,
int minute,
int second,
double millisecond);
115 int getDatePart(
DatePart part)
const;
117 std::uint64_t m_ticks;
118 static const int m_daysPerYear;
119 static const int m_daysPer4Years;
120 static const int m_daysPer100Years;
121 static const int m_daysPer400Years;
122 static const int m_daysTo1601;
123 static const int m_daysTo1899;
124 static const int m_daysTo10000;
125 static const int m_daysToMonth365[13];
126 static const int m_daysToMonth366[13];
127 static const int m_daysInMonth365[12];
128 static const int m_daysInMonth366[12];
201 return tmp.first - tmp.second;
360 return m_ticks == std::numeric_limits<decltype(m_ticks)>
::max();
368 return (
year % 4 != 0) ? false : ((
year % 100 == 0) ? (
year % 400 == 0) :
true);
395 toString(result, format, noMilliseconds);
404 return DateTime(std::numeric_limits<decltype(m_ticks)>::
max());
412 return DateTime(621355968000000000);
438 return m_ticks == other.m_ticks;
446 return m_ticks != other.m_ticks;
454 return m_ticks < other.m_ticks;
462 return m_ticks > other.m_ticks;
470 return m_ticks <= other.m_ticks;
478 return m_ticks >= other.m_ticks;
487 return DateTime(m_ticks + timeSpan.m_ticks);
496 return DateTime(m_ticks - timeSpan.m_ticks);
505 return TimeSpan(m_ticks + other.m_ticks);
516 return TimeSpan(m_ticks - other.m_ticks);
524 m_ticks += timeSpan.m_ticks;
533 m_ticks -= timeSpan.m_ticks;
547 #endif // CHRONO_UTILITIES_DATETIME_H
DateTimeOutputFormat
Specifies the output format.
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
constexpr static DateTime fromTimeStampGmt(time_t timeStamp)
Constructs a new DateTime object with the GMT time from the specified UNIX timeStamp.
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.
DateTime CPP_UTILITIES_EXPORT operator+(DateTime begin, Period period)
Adds the specified period to the specified date.
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 DateTime fromTimeStamp(time_t timeStamp)
Constructs a new DateTime object with the local time from the specified UNIX timeStamp.
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)
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 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.