C++ Utilities  4.13.0
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 "../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 
45 enum class DatePart {
46  Year,
47  Month,
48  DayOfYear,
49  Day
50 };
51 
53 public:
54  explicit constexpr DateTime();
55  explicit constexpr DateTime(uint64 ticks);
56  static DateTime fromDate(int year = 1, int month = 1, int day = 1);
57  static DateTime fromTime(int hour = 0, int minute = 0, int second = 0, double millisecond = 0.0);
58  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);
59  static DateTime fromString(const std::string &str);
60  static DateTime fromString(const char *str);
61  static std::pair<DateTime, TimeSpan> fromIsoString(const char *str);
62  static DateTime fromIsoStringGmt(const char *str);
63  static DateTime fromIsoStringLocal(const char *str);
64  static DateTime fromTimeStamp(time_t timeStamp);
65  static DateTime fromTimeStampGmt(time_t timeStamp);
66 
67  constexpr uint64 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 uint64 dateToTicks(int year, int month, int day);
114  static uint64 timeToTicks(int hour, int minute, int second, double millisecond);
115  int getDatePart(DatePart part) const;
116 
117  uint64 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(uint64 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 (uint64 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 uint64 DateTime::totalTicks() const
219 {
220  return m_ticks;
221 }
222 
226 inline int DateTime::year() const
227 {
228  return getDatePart(DatePart::Year);
229 }
230 
234 inline int DateTime::month() const
235 {
236  return getDatePart(DatePart::Month);
237 }
238 
242 inline int DateTime::day() const
243 {
244  return getDatePart(DatePart::Day);
245 }
246 
250 inline int DateTime::dayOfYear() const
251 {
252  return getDatePart(DatePart::DayOfYear);
253 }
254 
259 constexpr inline DayOfWeek DateTime::dayOfWeek() const
260 {
261  return static_cast<DayOfWeek>((m_ticks / TimeSpan::m_ticksPerDay) % 7l);
262 }
263 
267 constexpr inline int DateTime::hour() const
268 {
269  return m_ticks / TimeSpan::m_ticksPerHour % 24ul;
270 }
271 
275 constexpr inline int DateTime::minute() const
276 {
277  return m_ticks / TimeSpan::m_ticksPerMinute % 60ul;
278 }
279 
283 constexpr inline int DateTime::second() const
284 {
285  return m_ticks / TimeSpan::m_ticksPerSecond % 60ul;
286 }
287 
291 constexpr inline int DateTime::millisecond() const
292 {
293  return m_ticks / TimeSpan::m_ticksPerMillisecond % 1000ul;
294 }
295 
299 constexpr int DateTime::microsecond() const
300 {
301  return m_ticks / TimeSpan::ticksPerMicrosecond % 1000ul;
302 }
303 
309 constexpr int DateTime::nanosecond() const
310 {
311  return m_ticks % 10ul * TimeSpan::nanosecondsPerTick;
312 }
313 
318 constexpr inline bool DateTime::isNull() const
319 {
320  return m_ticks == 0;
321 }
322 
326 constexpr inline TimeSpan DateTime::timeOfDay() const
327 {
328  return TimeSpan(m_ticks % TimeSpan::m_ticksPerDay);
329 }
330 
334 inline bool DateTime::isLeapYear() const
335 {
336  return isLeapYear(year());
337 }
338 
342 constexpr inline bool DateTime::isEternity() const
343 {
344  return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
345 }
346 
350 constexpr inline bool DateTime::isLeapYear(int year)
351 {
352  return (year % 4 != 0) ? false : ((year % 100 == 0) ? (year % 400 == 0) : true);
353 }
354 
358 inline int DateTime::daysInMonth(int year, int month)
359 {
360  return (month >= 1 && month <= 12) ? (isLeapYear(year) ? m_daysInMonth366[month - 1] : m_daysInMonth365[month - 1]) : (0);
361 }
362 
366 constexpr inline bool DateTime::isSameDay(const DateTime &other) const
367 {
368  return (m_ticks / TimeSpan::m_ticksPerDay) == (other.m_ticks / TimeSpan::m_ticksPerDay);
369 }
370 
374 constexpr inline DateTime DateTime::eternity()
375 {
376  return DateTime(std::numeric_limits<decltype(m_ticks)>::max());
377 }
378 
383 {
384  return DateTime(621355968000000000);
385 }
386 
392 {
393  return DateTime::fromTimeStamp(std::time(nullptr));
394 }
395 
401 {
402  return DateTime::fromTimeStampGmt(std::time(nullptr));
403 }
404 
408 constexpr inline bool DateTime::operator==(const DateTime &other) const
409 {
410  return m_ticks == other.m_ticks;
411 }
412 
416 constexpr inline bool DateTime::operator!=(const DateTime &other) const
417 {
418  return m_ticks != other.m_ticks;
419 }
420 
424 constexpr inline bool DateTime::operator<(const DateTime &other) const
425 {
426  return m_ticks < other.m_ticks;
427 }
428 
432 constexpr inline bool DateTime::operator>(const DateTime &other) const
433 {
434  return m_ticks > other.m_ticks;
435 }
436 
440 constexpr inline bool DateTime::operator<=(const DateTime &other) const
441 {
442  return m_ticks <= other.m_ticks;
443 }
444 
448 constexpr inline bool DateTime::operator>=(const DateTime &other) const
449 {
450  return m_ticks >= other.m_ticks;
451 }
452 
457 constexpr inline DateTime DateTime::operator+(const TimeSpan &timeSpan) const
458 {
459  return DateTime(m_ticks + timeSpan.m_ticks);
460 }
461 
466 constexpr inline DateTime DateTime::operator-(const TimeSpan &timeSpan) const
467 {
468  return DateTime(m_ticks - timeSpan.m_ticks);
469 }
470 
475 constexpr inline TimeSpan DateTime::operator+(const DateTime &other) const
476 {
477  return TimeSpan(m_ticks + other.m_ticks);
478 }
479 
486 constexpr inline TimeSpan DateTime::operator-(const DateTime &other) const
487 {
488  return TimeSpan(m_ticks - other.m_ticks);
489 }
490 
494 inline DateTime &DateTime::operator+=(const TimeSpan &timeSpan)
495 {
496  m_ticks += timeSpan.m_ticks;
497  return *this;
498 }
499 
503 inline DateTime &DateTime::operator-=(const TimeSpan &timeSpan)
504 {
505  m_ticks -= timeSpan.m_ticks;
506  return *this;
507 }
508 } // namespace ChronoUtilities
509 
510 namespace std {
511 template <> struct hash<ChronoUtilities::DateTime> {
512  inline size_t operator()(const ChronoUtilities::DateTime &dateTime) const
513  {
514  return hash<decltype(dateTime.totalTicks())>()(dateTime.totalTicks());
515  }
516 };
517 } // namespace std
518 
519 #endif // CHRONO_UTILITIES_DATETIME_H
DateTime CPP_UTILITIES_EXPORT operator+(DateTime begin, Period period)
Adds the specified period to the specified date.
Definition: period.cpp:62
int year() const
Returns the year component of the date represented by this instance.
Definition: datetime.h:226
constexpr int minute() const
Returns the minute component of the date represented by this instance.
Definition: datetime.h:275
DateTime & operator-=(const TimeSpan &timeSpan)
Substracts a TimeSpan from the current instance.
Definition: datetime.h:503
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:391
constexpr DateTime operator-(const TimeSpan &timeSpan) const
Substracts another instance.
Definition: datetime.h:466
constexpr DateTime operator+(const TimeSpan &timeSpan) const
Adds another instance.
Definition: datetime.h:457
constexpr int nanosecond() const
Returns the nanosecond component of the date represented by this instance.
Definition: datetime.h:309
static std::pair< DateTime, TimeSpan > fromIsoString(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition: datetime.cpp:127
static int daysInMonth(int year, int month)
Returns the number of days in the specified month and year.
Definition: datetime.h:358
Represents an instant in time, typically expressed as a date and time of day.
Definition: datetime.h:52
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:342
static DateTime fromString(const std::string &str)
Parses the given std::string as DateTime.
Definition: datetime.h:187
constexpr uint64 totalTicks() const
Returns the number of ticks which represent the value of the current instance.
Definition: datetime.h:218
static constexpr DateTime eternity()
Constructs a new instance of the DateTime class with the maximal number of ticks. ...
Definition: datetime.h:374
Represents a time interval.
Definition: timespan.h:28
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:448
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
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
constexpr int second() const
Returns the second component of the date represented by this instance.
Definition: datetime.h:283
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
static constexpr int64 ticksPerMicrosecond
Definition: timespan.h:81
constexpr TimeSpan timeOfDay() const
Returns the time of day as TimeSpan for this instance.
Definition: datetime.h:326
int day() const
Returns the day component of the date represented by this instance.
Definition: datetime.h:242
DateTime & operator+=(const TimeSpan &timeSpan)
Adds a TimeSpan to the current instance.
Definition: datetime.h:494
constexpr bool isNull() const
Returns ture if the date represented by the current DateTime class is null.
Definition: datetime.h:318
static DateTime fromIsoStringGmt(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition: datetime.h:198
constexpr bool isSameDay(const DateTime &other) const
Returns and indication whether two DateTime instances represent the same day.
Definition: datetime.h:366
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:400
static constexpr int64 nanosecondsPerTick
Definition: timespan.h:80
static constexpr DateTime unixEpochStart()
Returns the DateTime object for the "1970-01-01T00:00:00Z".
Definition: datetime.h:382
constexpr int millisecond() const
Returns the millisecond component of the date represented by this instance.
Definition: datetime.h:291
constexpr bool operator==(const DateTime &other) const
Indicates whether two DateTime instances are equal.
Definition: datetime.h:408
constexpr bool operator<(const DateTime &other) const
Indicates whether a specified DateTime is less than another specified DateTime.
Definition: datetime.h:424
DatePart
Specifies the date part.
Definition: datetime.h:45
constexpr DateTime()
Constructs a DateTime.
Definition: datetime.h:134
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:162
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
constexpr bool operator>(const DateTime &other) const
Indicates whether a specified DateTime is greater than another specified DateTime.
Definition: datetime.h:432
constexpr int microsecond() const
Returns the microsecond component of the date represented by this instance.
Definition: datetime.h:299
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:73
int month() const
Returns the month component of the date represented by this instance.
Definition: datetime.h:234
bool isLeapYear() const
Returns an indication whether the year represented by this instance is a leap year.
Definition: datetime.h:334
size_t operator()(const ChronoUtilities::DateTime &dateTime) const
Definition: datetime.h:512
static DateTime fromTimeStamp(time_t timeStamp)
Constructs a new DateTime object with the local time from the specified UNIX timeStamp.
Definition: datetime.cpp:59
int dayOfYear() const
Returns the day of the year represented by this instance.
Definition: datetime.h:250
constexpr bool operator<=(const DateTime &other) const
Indicates whether a specified DateTime is less or equal than another specified DateTime.
Definition: datetime.h:440
static DateTime fromIsoStringLocal(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition: datetime.h:210
#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
Returns the hour component of the date represented by this instance.
Definition: datetime.h:267
constexpr bool operator!=(const DateTime &other) const
Indicates whether two DateTime instances are not equal.
Definition: datetime.h:416
constexpr DayOfWeek dayOfWeek() const
Returns the day of the week represented by this instance.
Definition: datetime.h:259