C++ Utilities  5.0.1
Useful C++ classes and routines such as argument parser, IO and conversion utilities
datetime.h
Go to the documentation of this file.
1 #ifndef CHRONO_UTILITIES_DATETIME_H
2 #define CHRONO_UTILITIES_DATETIME_H
3 
4 #include "./timespan.h"
5 
6 #include <cstdint>
7 #include <ctime>
8 #include <limits>
9 #include <string>
10 
11 namespace CppUtilities {
12 
18  DateAndTime,
19  DateOnly,
20  TimeOnly,
23 };
24 
29 enum class DayOfWeek {
30  Monday,
31  Tuesday,
32  Wednesday,
33  Thursday,
34  Friday,
35  Saturday,
36  Sunday
37 };
38 
44 enum class DatePart {
45  Year,
46  Month,
47  DayOfYear,
48  Day
49 };
50 
52 public:
53  explicit constexpr DateTime();
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);
65 
66  constexpr std::uint64_t &ticks();
67  constexpr std::uint64_t totalTicks() const;
68  int year() const;
69  int month() const;
70  int day() const;
71  int dayOfYear() const;
72  constexpr DayOfWeek dayOfWeek() 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;
84  std::string toString(DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime, bool noMilliseconds = false) const;
85  void toString(std::string &result, DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime, bool noMilliseconds = false) const;
86  std::string toIsoString(TimeSpan timeZoneDelta = TimeSpan()) const;
87  static const char *printDayOfWeek(DayOfWeek dayOfWeek, bool abbreviation = false);
88 
89  static constexpr DateTime eternity();
90  static constexpr DateTime unixEpochStart();
91  static DateTime now();
92  static DateTime gmtNow();
93 #if defined(PLATFORM_UNIX) && !defined(PLATFORM_MAC)
94  static DateTime exactGmtNow();
95 #endif
96  constexpr static bool isLeapYear(int year);
97  static int daysInMonth(int year, int month);
98 
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;
103  constexpr bool operator<=(const DateTime &other) const;
104  constexpr bool operator>=(const DateTime &other) const;
105  constexpr DateTime operator+(const TimeSpan &timeSpan) const;
106  constexpr DateTime operator-(const TimeSpan &timeSpan) const;
107  constexpr TimeSpan operator+(const DateTime &other) const;
108  constexpr TimeSpan operator-(const DateTime &other) const;
109  DateTime &operator+=(const TimeSpan &timeSpan);
110  DateTime &operator-=(const TimeSpan &timeSpan);
111 
112 private:
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;
116 
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];
129 };
130 
134 constexpr inline DateTime::DateTime()
135  : m_ticks(0)
136 {
137 }
138 
142 constexpr inline DateTime::DateTime(std::uint64_t ticks)
143  : m_ticks(ticks)
144 {
145 }
146 
151 inline DateTime DateTime::fromDate(int year, int month, int day)
152 {
153  return DateTime(dateToTicks(year, month, day));
154 }
155 
160 inline DateTime DateTime::fromTime(int hour, int minute, int second, double millisecond)
161 {
162  return DateTime(timeToTicks(hour, minute, second, millisecond));
163 }
164 
170 inline DateTime DateTime::fromDateAndTime(int year, int month, int day, int hour, int minute, int second, double millisecond)
171 {
172  if (std::uint64_t ticks = dateToTicks(year, month, day)) {
173  return DateTime(ticks + timeToTicks(hour, minute, second, millisecond));
174  }
175  return DateTime();
176 }
177 
187 inline DateTime DateTime::fromString(const std::string &str)
188 {
189  return fromString(str.data());
190 }
191 
198 inline DateTime DateTime::fromIsoStringGmt(const char *str)
199 {
200  const auto tmp = fromIsoString(str);
201  return tmp.first - tmp.second;
202 }
203 
210 inline DateTime DateTime::fromIsoStringLocal(const char *str)
211 {
212  return fromIsoString(str).first;
213 }
214 
218 constexpr inline DateTime DateTime::fromTimeStampGmt(std::time_t timeStamp)
219 {
220  return DateTime(DateTime::unixEpochStart().totalTicks() + static_cast<std::uint64_t>(timeStamp) * TimeSpan::ticksPerSecond);
221 }
222 
226 constexpr inline std::uint64_t &DateTime::ticks()
227 {
228  return m_ticks;
229 }
230 
234 constexpr inline std::uint64_t DateTime::totalTicks() const
235 {
236  return m_ticks;
237 }
238 
242 inline int DateTime::year() const
243 {
244  return getDatePart(DatePart::Year);
245 }
246 
250 inline int DateTime::month() const
251 {
252  return getDatePart(DatePart::Month);
253 }
254 
258 inline int DateTime::day() const
259 {
260  return getDatePart(DatePart::Day);
261 }
262 
266 inline int DateTime::dayOfYear() const
267 {
268  return getDatePart(DatePart::DayOfYear);
269 }
270 
275 constexpr inline DayOfWeek DateTime::dayOfWeek() const
276 {
277  return static_cast<DayOfWeek>((m_ticks / TimeSpan::ticksPerDay) % 7l);
278 }
279 
283 constexpr inline int DateTime::hour() const
284 {
285  return m_ticks / TimeSpan::ticksPerHour % 24ul;
286 }
287 
291 constexpr inline int DateTime::minute() const
292 {
293  return m_ticks / TimeSpan::ticksPerMinute % 60ul;
294 }
295 
299 constexpr inline int DateTime::second() const
300 {
301  return m_ticks / TimeSpan::ticksPerSecond % 60ul;
302 }
303 
307 constexpr inline int DateTime::millisecond() const
308 {
309  return m_ticks / TimeSpan::ticksPerMillisecond % 1000ul;
310 }
311 
315 constexpr inline int DateTime::microsecond() const
316 {
317  return m_ticks / TimeSpan::ticksPerMicrosecond % 1000ul;
318 }
319 
325 constexpr inline int DateTime::nanosecond() const
326 {
327  return m_ticks % 10ul * TimeSpan::nanosecondsPerTick;
328 }
329 
334 constexpr inline bool DateTime::isNull() const
335 {
336  return m_ticks == 0;
337 }
338 
342 constexpr inline TimeSpan DateTime::timeOfDay() const
343 {
344  return TimeSpan(m_ticks % TimeSpan::ticksPerDay);
345 }
346 
350 inline bool DateTime::isLeapYear() const
351 {
352  return isLeapYear(year());
353 }
354 
358 constexpr inline bool DateTime::isEternity() const
359 {
360  return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
361 }
362 
366 constexpr inline bool DateTime::isLeapYear(int year)
367 {
368  return (year % 4 != 0) ? false : ((year % 100 == 0) ? (year % 400 == 0) : true);
369 }
370 
374 inline int DateTime::daysInMonth(int year, int month)
375 {
376  return (month >= 1 && month <= 12) ? (isLeapYear(year) ? m_daysInMonth366[month - 1] : m_daysInMonth365[month - 1]) : (0);
377 }
378 
382 constexpr inline bool DateTime::isSameDay(const DateTime &other) const
383 {
384  return (m_ticks / TimeSpan::ticksPerDay) == (other.m_ticks / TimeSpan::ticksPerDay);
385 }
386 
392 inline std::string DateTime::toString(DateTimeOutputFormat format, bool noMilliseconds) const
393 {
394  std::string result;
395  toString(result, format, noMilliseconds);
396  return result;
397 }
398 
402 constexpr inline DateTime DateTime::eternity()
403 {
404  return DateTime(std::numeric_limits<decltype(m_ticks)>::max());
405 }
406 
411 {
412  return DateTime(621355968000000000);
413 }
414 
420 {
421  return DateTime::fromTimeStamp(std::time(nullptr));
422 }
423 
429 {
430  return DateTime::fromTimeStampGmt(std::time(nullptr));
431 }
432 
436 constexpr inline bool DateTime::operator==(const DateTime &other) const
437 {
438  return m_ticks == other.m_ticks;
439 }
440 
444 constexpr inline bool DateTime::operator!=(const DateTime &other) const
445 {
446  return m_ticks != other.m_ticks;
447 }
448 
452 constexpr inline bool DateTime::operator<(const DateTime &other) const
453 {
454  return m_ticks < other.m_ticks;
455 }
456 
460 constexpr inline bool DateTime::operator>(const DateTime &other) const
461 {
462  return m_ticks > other.m_ticks;
463 }
464 
468 constexpr inline bool DateTime::operator<=(const DateTime &other) const
469 {
470  return m_ticks <= other.m_ticks;
471 }
472 
476 constexpr inline bool DateTime::operator>=(const DateTime &other) const
477 {
478  return m_ticks >= other.m_ticks;
479 }
480 
485 constexpr inline DateTime DateTime::operator+(const TimeSpan &timeSpan) const
486 {
487  return DateTime(m_ticks + timeSpan.m_ticks);
488 }
489 
494 constexpr inline DateTime DateTime::operator-(const TimeSpan &timeSpan) const
495 {
496  return DateTime(m_ticks - timeSpan.m_ticks);
497 }
498 
503 constexpr inline TimeSpan DateTime::operator+(const DateTime &other) const
504 {
505  return TimeSpan(m_ticks + other.m_ticks);
506 }
507 
514 constexpr inline TimeSpan DateTime::operator-(const DateTime &other) const
515 {
516  return TimeSpan(m_ticks - other.m_ticks);
517 }
518 
522 inline DateTime &DateTime::operator+=(const TimeSpan &timeSpan)
523 {
524  m_ticks += timeSpan.m_ticks;
525  return *this;
526 }
527 
531 inline DateTime &DateTime::operator-=(const TimeSpan &timeSpan)
532 {
533  m_ticks -= timeSpan.m_ticks;
534  return *this;
535 }
536 } // namespace CppUtilities
537 
538 namespace std {
540 template <> struct hash<CppUtilities::DateTime> {
541  inline size_t operator()(const CppUtilities::DateTime &dateTime) const
542  {
543  return hash<decltype(dateTime.totalTicks())>()(dateTime.totalTicks());
544  }
545 };
546 } // namespace std
547 
548 #endif // CHRONO_UTILITIES_DATETIME_H
CppUtilities::DateTimeOutputFormat
DateTimeOutputFormat
Specifies the output format.
Definition: datetime.h:17
CppUtilities::DateTime::toString
std::string toString(DateTimeOutputFormat format=DateTimeOutputFormat::DateAndTime, bool noMilliseconds=false) const
Returns the string representation of the current instance using the specified format.
Definition: datetime.h:392
CppUtilities::DateTime::fromIsoStringLocal
static DateTime fromIsoStringLocal(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition: datetime.h:210
CppUtilities::DateTime::DateTime
constexpr DateTime()
Constructs a DateTime.
Definition: datetime.h:134
CppUtilities::TimeSpan::ticksPerDay
static constexpr std::int64_t ticksPerDay
Definition: timespan.h:83
CppUtilities::DateTime::isEternity
constexpr bool isEternity() const
Returns whether the instance has the maximal number of ticks.
Definition: datetime.h:358
CppUtilities::DateTime::operator-
constexpr DateTime operator-(const TimeSpan &timeSpan) const
Substracts another instance.
Definition: datetime.h:494
std::hash< CppUtilities::DateTime >::operator()
size_t operator()(const CppUtilities::DateTime &dateTime) const
Definition: datetime.h:541
CppUtilities::TimeSpan::ticksPerMillisecond
static constexpr std::int64_t ticksPerMillisecond
Definition: timespan.h:79
CppUtilities::DateTime::fromTimeStampGmt
constexpr static DateTime fromTimeStampGmt(time_t timeStamp)
Constructs a new DateTime object with the GMT time from the specified UNIX timeStamp.
Definition: datetime.h:218
CppUtilities::DateTime::isLeapYear
bool isLeapYear() const
Returns an indication whether the year represented by this instance is a leap year.
Definition: datetime.h:350
CppUtilities::DateTime::fromDate
static DateTime fromDate(int year=1, int month=1, int day=1)
Constructs a DateTime to the specified year, month, and day.
Definition: datetime.h:151
CppUtilities::DateTime::year
int year() const
Returns the year component of the date represented by this instance.
Definition: datetime.h:242
CppUtilities::DayOfWeek::Thursday
CppUtilities::operator+
DateTime CPP_UTILITIES_EXPORT operator+(DateTime begin, Period period)
Adds the specified period to the specified date.
Definition: period.cpp:60
CppUtilities::DateTime::operator+
constexpr DateTime operator+(const TimeSpan &timeSpan) const
Adds another instance.
Definition: datetime.h:485
CppUtilities::DateTime::eternity
static constexpr DateTime eternity()
Constructs a new instance of the DateTime class with the maximal number of ticks.
Definition: datetime.h:402
CppUtilities::DatePart::Month
CppUtilities::DateTime::fromIsoString
static std::pair< DateTime, TimeSpan > fromIsoString(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition: datetime.cpp:118
CppUtilities::TimeSpan::ticksPerMicrosecond
static constexpr std::int64_t ticksPerMicrosecond
Definition: timespan.h:78
CppUtilities::DateTime::isNull
constexpr bool isNull() const
Returns ture if the date represented by the current DateTime class is null.
Definition: datetime.h:334
CppUtilities::DateTime::operator<=
constexpr bool operator<=(const DateTime &other) const
Indicates whether a specified DateTime is less or equal than another specified DateTime.
Definition: datetime.h:468
CppUtilities::DateTime::millisecond
constexpr int millisecond() const
Returns the millisecond component of the date represented by this instance.
Definition: datetime.h:307
CppUtilities::DateTime::minute
constexpr int minute() const
Returns the minute component of the date represented by this instance.
Definition: datetime.h:291
CppUtilities::DatePart::Year
CppUtilities::DateTime::month
int month() const
Returns the month component of the date represented by this instance.
Definition: datetime.h:250
CppUtilities::max
constexpr T max(T first, T second)
Returns the greatest of the given items.
Definition: math.h:100
CppUtilities::DateTime::second
constexpr int second() const
Returns the second component of the date represented by this instance.
Definition: datetime.h:299
CppUtilities::DateTime::operator!=
constexpr bool operator!=(const DateTime &other) const
Indicates whether two DateTime instances are not equal.
Definition: datetime.h:444
CppUtilities::DateTime::isSameDay
constexpr bool isSameDay(const DateTime &other) const
Returns and indication whether two DateTime instances represent the same day.
Definition: datetime.h:382
CppUtilities::DateTime::operator>=
constexpr bool operator>=(const DateTime &other) const
Indicates whether a specified DateTime is greater or equal than another specified DateTime.
Definition: datetime.h:476
CppUtilities::DayOfWeek::Saturday
CppUtilities::DateTime::operator>
constexpr bool operator>(const DateTime &other) const
Indicates whether a specified DateTime is greater than another specified DateTime.
Definition: datetime.h:460
CppUtilities::DayOfWeek::Monday
CppUtilities::DatePart
DatePart
Specifies the date part.
Definition: datetime.h:44
CppUtilities::DateTimeOutputFormat::DateAndTime
CppUtilities
Contains all utilities provides by the c++utilities library.
Definition: argumentparser.h:17
CppUtilities::DateTime::operator+=
DateTime & operator+=(const TimeSpan &timeSpan)
Adds a TimeSpan to the current instance.
Definition: datetime.h:522
CppUtilities::DateTime::fromTimeStamp
static DateTime fromTimeStamp(time_t timeStamp)
Constructs a new DateTime object with the local time from the specified UNIX timeStamp.
Definition: datetime.cpp:58
CppUtilities::TimeSpan::ticksPerHour
static constexpr std::int64_t ticksPerHour
Definition: timespan.h:82
CppUtilities::DateTime::gmtNow
static DateTime gmtNow()
Returns a DateTime object that is set to the current date and time on this computer,...
Definition: datetime.h:428
CppUtilities::DayOfWeek::Wednesday
CppUtilities::DateTime::operator<
constexpr bool operator<(const DateTime &other) const
Indicates whether a specified DateTime is less than another specified DateTime.
Definition: datetime.h:452
CppUtilities::DateTimeOutputFormat::DateTimeAndWeekday
CppUtilities::DateTime::fromTime
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.
Definition: datetime.h:160
CppUtilities::DateTime::fromDateAndTime
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.
Definition: datetime.h:170
CppUtilities::FlagEnumClassOperations::operator+=
constexpr FlagEnumClass & operator+=(FlagEnumClass &lhs, FlagEnumClass rhs)
Definition: flagenumclass.h:64
CppUtilities::DateTime::dayOfWeek
constexpr DayOfWeek dayOfWeek() const
Returns the day of the week represented by this instance.
Definition: datetime.h:275
CppUtilities::DateTime::nanosecond
constexpr int nanosecond() const
Returns the nanosecond component of the date represented by this instance.
Definition: datetime.h:325
CppUtilities::FlagEnumClassOperations::operator-=
constexpr FlagEnumClass & operator-=(FlagEnumClass &lhs, FlagEnumClass rhs)
Definition: flagenumclass.h:71
CppUtilities::DayOfWeek::Tuesday
CppUtilities::DateTime::now
static DateTime now()
Returns a DateTime object that is set to the current date and time on this computer,...
Definition: datetime.h:419
CppUtilities::DayOfWeek::Sunday
CppUtilities::DayOfWeek
DayOfWeek
Specifies the day of the week.
Definition: datetime.h:29
CppUtilities::DateTime::dayOfYear
int dayOfYear() const
Returns the day of the year represented by this instance.
Definition: datetime.h:266
CppUtilities::DateTime::ticks
constexpr std::uint64_t & ticks()
Returns a mutable reference to the total ticks.
Definition: datetime.h:226
CppUtilities::DateTimeOutputFormat::TimeOnly
CppUtilities::DatePart::DayOfYear
CppUtilities::TimeSpan
Represents a time interval.
Definition: timespan.h:25
CppUtilities::TimeSpan::nanosecondsPerTick
static constexpr std::int64_t nanosecondsPerTick
Definition: timespan.h:77
CppUtilities::DateTimeOutputFormat::DateTimeAndShortWeekday
CppUtilities::DateTime::unixEpochStart
static constexpr DateTime unixEpochStart()
Returns the DateTime object for the "1970-01-01T00:00:00Z".
Definition: datetime.h:410
CppUtilities::DateTime::fromIsoStringGmt
static DateTime fromIsoStringGmt(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition: datetime.h:198
CppUtilities::DateTime::microsecond
constexpr int microsecond() const
Returns the microsecond component of the date represented by this instance.
Definition: datetime.h:315
CppUtilities::DateTime::operator==
constexpr bool operator==(const DateTime &other) const
Indicates whether two DateTime instances are equal.
Definition: datetime.h:436
CppUtilities::DateTime
Represents an instant in time, typically expressed as a date and time of day.
Definition: datetime.h:51
CppUtilities::TimeSpan::ticksPerSecond
static constexpr std::int64_t ticksPerSecond
Definition: timespan.h:80
CppUtilities::DatePart::Day
CppUtilities::DayOfWeek::Friday
CPP_UTILITIES_EXPORT
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
CppUtilities::DateTime::daysInMonth
static int daysInMonth(int year, int month)
Returns the number of days in the specified month and year.
Definition: datetime.h:374
CppUtilities::operator==
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:203
CppUtilities::DateTime::operator-=
DateTime & operator-=(const TimeSpan &timeSpan)
Substracts a TimeSpan from the current instance.
Definition: datetime.h:531
CppUtilities::TimeSpan::ticksPerMinute
static constexpr std::int64_t ticksPerMinute
Definition: timespan.h:81
CppUtilities::DateTime::day
int day() const
Returns the day component of the date represented by this instance.
Definition: datetime.h:258
CppUtilities::DateTime::fromString
static DateTime fromString(const std::string &str)
Parses the given std::string as DateTime.
Definition: datetime.h:187
timespan.h
CppUtilities::DateTime::timeOfDay
constexpr TimeSpan timeOfDay() const
Returns the time of day as TimeSpan for this instance.
Definition: datetime.h:342
CppUtilities::DateTime::totalTicks
constexpr std::uint64_t totalTicks() const
Returns the number of ticks which represent the value of the current instance.
Definition: datetime.h:234
CppUtilities::DateTimeOutputFormat::DateOnly
CppUtilities::DateTime::hour
constexpr int hour() const
Returns the hour component of the date represented by this instance.
Definition: datetime.h:283