C++ Utilities  4.8.0
Common C++ classes and routines used by my applications 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 "../conversion/types.h"
7 
8 #include <ctime>
9 #include <limits>
10 #include <string>
11 
12 namespace ChronoUtilities {
13 
19  DateAndTime,
20  DateOnly,
21  TimeOnly,
24 };
25 
30 enum class DayOfWeek {
31  Monday,
32  Tuesday,
33  Wednesday,
34  Thursday,
35  Friday,
36  Saturday,
37  Sunday
38 };
39 
44 enum class DatePart {
45  Year,
46  Month,
47  DayOfYear,
48  Day
49 };
50 
52 public:
53  explicit constexpr DateTime();
54  explicit constexpr DateTime(uint64 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  static DateTime fromTimeStampGmt(time_t timeStamp);
65 
66  constexpr uint64 totalTicks() const;
67  int year() const;
68  int month() const;
69  int day() const;
70  int dayOfYear() const;
71  constexpr DayOfWeek dayOfWeek() const;
72  constexpr int hour() const;
73  constexpr int minute() const;
74  constexpr int second() const;
75  constexpr int millisecond() const;
76  constexpr bool isNull() const;
77  constexpr TimeSpan timeOfDay() const;
78  bool isLeapYear() const;
79  constexpr bool isEternity() const;
80  constexpr bool isSameDay(const DateTime &other) const;
81  std::string toString(DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime, bool noMilliseconds = false) const;
82  void toString(std::string &result, DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime, bool noMilliseconds = false) const;
83  std::string toIsoString(TimeSpan timeZoneDelta = TimeSpan()) const;
84  static const char *printDayOfWeek(DayOfWeek dayOfWeek, bool abbreviation = false);
85 
86  static constexpr DateTime eternity();
87  static constexpr DateTime unixEpochStart();
88  static DateTime now();
89  static DateTime gmtNow();
90 #if defined(PLATFORM_UNIX)
91  static DateTime exactGmtNow();
92 #endif
93  constexpr static bool isLeapYear(int year);
94  static int daysInMonth(int year, int month);
95 
96  constexpr bool operator==(const DateTime &other) const;
97  constexpr bool operator!=(const DateTime &other) const;
98  constexpr bool operator<(const DateTime &other) const;
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 DateTime operator+(const TimeSpan &timeSpan) const;
103  constexpr DateTime operator-(const TimeSpan &timeSpan) const;
104  constexpr TimeSpan operator+(const DateTime &other) const;
105  constexpr TimeSpan operator-(const DateTime &other) const;
106  DateTime &operator+=(const TimeSpan &timeSpan);
107  DateTime &operator-=(const TimeSpan &timeSpan);
108 
109 private:
110  static uint64 dateToTicks(int year, int month, int day);
111  static uint64 timeToTicks(int hour, int minute, int second, double millisecond);
112  int getDatePart(DatePart part) const;
113 
114  uint64 m_ticks;
115  static const int m_daysPerYear;
116  static const int m_daysPer4Years;
117  static const int m_daysPer100Years;
118  static const int m_daysPer400Years;
119  static const int m_daysTo1601;
120  static const int m_daysTo1899;
121  static const int m_daysTo10000;
122  static const int m_daysToMonth365[13];
123  static const int m_daysToMonth366[13];
124  static const int m_daysInMonth365[12];
125  static const int m_daysInMonth366[12];
126 };
127 
131 constexpr inline DateTime::DateTime()
132  : m_ticks(0)
133 {
134 }
135 
139 constexpr inline DateTime::DateTime(uint64 ticks)
140  : m_ticks(ticks)
141 {
142 }
143 
147 inline DateTime DateTime::fromDate(int year, int month, int day)
148 {
149  return DateTime(dateToTicks(year, month, day));
150 }
151 
156 {
157  return DateTime(timeToTicks(hour, minute, second, millisecond));
158 }
159 
163 inline DateTime DateTime::fromDateAndTime(int year, int month, int day, int hour, int minute, int second, double millisecond)
164 {
165  if (uint64 ticks = dateToTicks(year, month, day)) {
166  return DateTime(ticks + timeToTicks(hour, minute, second, millisecond));
167  }
168  return DateTime();
169 }
170 
174 inline DateTime DateTime::fromString(const std::string &str)
175 {
176  return fromString(str.data());
177 }
178 
184 inline DateTime DateTime::fromIsoStringGmt(const char *str)
185 {
186  const auto tmp = fromIsoString(str);
187  return tmp.first - tmp.second;
188 }
189 
195 inline DateTime DateTime::fromIsoStringLocal(const char *str)
196 {
197  return fromIsoString(str).first;
198 }
199 
203 constexpr inline uint64 DateTime::totalTicks() const
204 {
205  return m_ticks;
206 }
207 
211 inline int DateTime::year() const
212 {
213  return getDatePart(DatePart::Year);
214 }
215 
219 inline int DateTime::month() const
220 {
221  return getDatePart(DatePart::Month);
222 }
223 
227 inline int DateTime::day() const
228 {
229  return getDatePart(DatePart::Day);
230 }
231 
235 inline int DateTime::dayOfYear() const
236 {
237  return getDatePart(DatePart::DayOfYear);
238 }
239 
244 constexpr inline DayOfWeek DateTime::dayOfWeek() const
245 {
246  return static_cast<DayOfWeek>((m_ticks / TimeSpan::ticksPerDay) % 7l);
247 }
248 
252 constexpr inline int DateTime::hour() const
253 {
254  return m_ticks / TimeSpan::ticksPerHour % 24ul;
255 }
256 
260 constexpr inline int DateTime::minute() const
261 {
262  return m_ticks / TimeSpan::ticksPerMinute % 60ul;
263 }
264 
268 constexpr inline int DateTime::second() const
269 {
270  return m_ticks / TimeSpan::ticksPerSecond % 60ul;
271 }
272 
276 constexpr inline int DateTime::millisecond() const
277 {
278  return m_ticks / TimeSpan::ticksPerMillisecond % 1000ul;
279 }
280 
285 constexpr inline bool DateTime::isNull() const
286 {
287  return m_ticks == 0;
288 }
289 
293 constexpr inline TimeSpan DateTime::timeOfDay() const
294 {
295  return TimeSpan(m_ticks % TimeSpan::ticksPerDay);
296 }
297 
301 inline bool DateTime::isLeapYear() const
302 {
303  return isLeapYear(year());
304 }
305 
309 constexpr inline bool DateTime::isEternity() const
310 {
311  return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
312 }
313 
317 constexpr inline bool DateTime::isLeapYear(int year)
318 {
319  return (year % 4 != 0) ? false : ((year % 100 == 0) ? (year % 400 == 0) : true);
320 }
321 
325 inline int DateTime::daysInMonth(int year, int month)
326 {
327  return (month >= 1 && month <= 12) ? (isLeapYear(year) ? m_daysInMonth366[month - 1] : m_daysInMonth365[month - 1]) : (0);
328 }
329 
333 constexpr inline bool DateTime::isSameDay(const DateTime &other) const
334 {
335  return (m_ticks / TimeSpan::ticksPerDay) == (other.m_ticks / TimeSpan::ticksPerDay);
336 }
337 
341 constexpr inline DateTime DateTime::eternity()
342 {
343  return DateTime(std::numeric_limits<decltype(m_ticks)>::max());
344 }
345 
350 {
351  return DateTime(621355968000000000);
352 }
353 
359 {
360  return DateTime::fromTimeStamp(time(nullptr));
361 }
362 
368 {
369  return DateTime::fromTimeStampGmt(time(nullptr));
370 }
371 
375 constexpr inline bool DateTime::operator==(const DateTime &other) const
376 {
377  return m_ticks == other.m_ticks;
378 }
379 
383 constexpr inline bool DateTime::operator!=(const DateTime &other) const
384 {
385  return m_ticks != other.m_ticks;
386 }
387 
391 constexpr inline bool DateTime::operator<(const DateTime &other) const
392 {
393  return m_ticks < other.m_ticks;
394 }
395 
399 constexpr inline bool DateTime::operator>(const DateTime &other) const
400 {
401  return m_ticks > other.m_ticks;
402 }
403 
407 constexpr inline bool DateTime::operator<=(const DateTime &other) const
408 {
409  return m_ticks <= other.m_ticks;
410 }
411 
415 constexpr inline bool DateTime::operator>=(const DateTime &other) const
416 {
417  return m_ticks >= other.m_ticks;
418 }
419 
424 constexpr inline DateTime DateTime::operator+(const TimeSpan &timeSpan) const
425 {
426  return DateTime(m_ticks + timeSpan.m_ticks);
427 }
428 
433 constexpr inline DateTime DateTime::operator-(const TimeSpan &timeSpan) const
434 {
435  return DateTime(m_ticks - timeSpan.m_ticks);
436 }
437 
442 constexpr inline TimeSpan DateTime::operator+(const DateTime &other) const
443 {
444  return TimeSpan(m_ticks + other.m_ticks);
445 }
446 
451 constexpr inline TimeSpan DateTime::operator-(const DateTime &other) const
452 {
453  return TimeSpan(m_ticks - other.m_ticks);
454 }
455 
459 inline DateTime &DateTime::operator+=(const TimeSpan &timeSpan)
460 {
461  m_ticks += timeSpan.m_ticks;
462  return *this;
463 }
464 
468 inline DateTime &DateTime::operator-=(const TimeSpan &timeSpan)
469 {
470  m_ticks -= timeSpan.m_ticks;
471  return *this;
472 }
473 }
474 
475 namespace std {
476 template <> struct hash<ChronoUtilities::DateTime> {
477  inline size_t operator()(const ChronoUtilities::DateTime &dateTime) const
478  {
479  return hash<decltype(dateTime.totalTicks())>()(dateTime.totalTicks());
480  }
481 };
482 }
483 
484 #endif // CHRONO_UTILITIES_DATETIME_H
int year() const
Gets the year component of the date represented by this instance.
Definition: datetime.h:211
constexpr int minute() const
Gets the minute component of the date represented by this instance.
Definition: datetime.h:260
DateTime & operator-=(const TimeSpan &timeSpan)
Substracts a TimeSpan from the current instance.
Definition: datetime.h:468
static DateTime now()
Returns a DateTime object that is set to the current date and time on this computer, expressed as the local time.
Definition: datetime.h:358
constexpr DateTime operator-(const TimeSpan &timeSpan) const
Substracts another instance.
Definition: datetime.h:433
constexpr DateTime operator+(const TimeSpan &timeSpan) const
Adds another instance.
Definition: datetime.h:424
static constexpr uint64 ticksPerDay
Definition: timespan.h:79
static std::pair< DateTime, TimeSpan > fromIsoString(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition: datetime.cpp:116
static int daysInMonth(int year, int month)
Returns the number of days in the specified month and year.
Definition: datetime.h:325
Represents an instant in time, typically expressed as a date and time of day.
Definition: datetime.h:51
Contains classes providing a means for handling date and time information.
Definition: datetime.h:12
constexpr bool isEternity() const
Returns whether the instance has the maximal number of ticks.
Definition: datetime.h:309
static DateTime fromString(const std::string &str)
Parses the given std::string as DateTime.
Definition: datetime.h:174
constexpr uint64 totalTicks() const
Gets the number of ticks which represent the value of the current instance.
Definition: datetime.h:203
static constexpr DateTime eternity()
Constructs a new instance of the DateTime class with the maximal number of ticks. ...
Definition: datetime.h:341
Represents a time interval.
Definition: timespan.h:27
STL namespace.
constexpr bool operator>=(const DateTime &other) const
Indicates whether a specified DateTime is greater or equal than another specified DateTime...
Definition: datetime.h:415
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:163
static constexpr uint64 ticksPerMillisecond
Definition: timespan.h:75
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:147
constexpr int second() const
Gets the second component of the date represented by this instance.
Definition: datetime.h:268
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
constexpr TimeSpan timeOfDay() const
Gets the time of day as TimeSpan for this instance.
Definition: datetime.h:293
int day() const
Gets the day component of the date represented by this instance.
Definition: datetime.h:227
DateTime & operator+=(const TimeSpan &timeSpan)
Adds a TimeSpan to the current instance.
Definition: datetime.h:459
static constexpr uint64 ticksPerHour
Definition: timespan.h:78
constexpr bool isNull() const
Returns ture if the date represented by the current DateTime class is null.
Definition: datetime.h:285
static DateTime fromIsoStringGmt(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition: datetime.h:184
constexpr bool isSameDay(const DateTime &other) const
Returns and indication whether two DateTime instances represent the same day.
Definition: datetime.h:333
static DateTime gmtNow()
Returns a DateTime object that is set to the current date and time on this computer, expressed as the GMT time.
Definition: datetime.h:367
static constexpr DateTime unixEpochStart()
Returns the DateTime object for the "1970-01-01T00:00:00Z".
Definition: datetime.h:349
constexpr int millisecond() const
Gets the millisecond component of the date represented by this instance.
Definition: datetime.h:276
constexpr bool operator==(const DateTime &other) const
Indicates whether two DateTime instances are equal.
Definition: datetime.h:375
constexpr bool operator<(const DateTime &other) const
Indicates whether a specified DateTime is less than another specified DateTime.
Definition: datetime.h:391
DatePart
Specifies the date part.
Definition: datetime.h:44
constexpr DateTime()
Constructs a DateTime.
Definition: datetime.h:131
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:153
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:155
constexpr bool operator>(const DateTime &other) const
Indicates whether a specified DateTime is greater than another specified DateTime.
Definition: datetime.h:399
DateTimeOutputFormat
Specifies the output format.
Definition: datetime.h:18
static DateTime fromTimeStampGmt(time_t timeStamp)
Constructs a new DateTime object with the GMT time from the specified UNIX timeStamp.
Definition: datetime.cpp:70
int month() const
Gets the month component of the date represented by this instance.
Definition: datetime.h:219
bool isLeapYear() const
Returns an indication whether the year of the dae represented by this instance is a leap year...
Definition: datetime.h:301
size_t operator()(const ChronoUtilities::DateTime &dateTime) const
Definition: datetime.h:477
std::string operator+(const Tuple &lhs, const std::string &rhs)
Allows construction of final string from previously constructed string-tuple and trailing string via ...
static DateTime fromTimeStamp(time_t timeStamp)
Constructs a new DateTime object with the local time from the specified UNIX timeStamp.
Definition: datetime.cpp:56
static constexpr uint64 ticksPerSecond
Definition: timespan.h:76
int dayOfYear() const
Gets the day of the year represented by this instance.
Definition: datetime.h:235
constexpr bool operator<=(const DateTime &other) const
Indicates whether a specified DateTime is less or equal than another specified DateTime.
Definition: datetime.h:407
static DateTime fromIsoStringLocal(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition: datetime.h:195
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
DayOfWeek
Specifies the day of the week.
Definition: datetime.h:30
constexpr int hour() const
Gets the hour component of the date represented by this instance.
Definition: datetime.h:252
static constexpr uint64 ticksPerMinute
Definition: timespan.h:77
constexpr bool operator!=(const DateTime &other) const
Indicates whether two DateTime instances are not equal.
Definition: datetime.h:383
constexpr DayOfWeek dayOfWeek() const
Gets the day of the week represented by this instance.
Definition: datetime.h:244