C++ Utilities  4.17.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  uint64 &ticks();
68  constexpr uint64 totalTicks() const;
69  int year() const;
70  int month() const;
71  int day() const;
72  int dayOfYear() const;
73  constexpr DayOfWeek dayOfWeek() const;
74  constexpr int hour() const;
75  constexpr int minute() const;
76  constexpr int second() const;
77  constexpr int millisecond() const;
78  constexpr int microsecond() const;
79  constexpr int nanosecond() const;
80  constexpr bool isNull() const;
81  constexpr TimeSpan timeOfDay() const;
82  bool isLeapYear() const;
83  constexpr bool isEternity() const;
84  constexpr bool isSameDay(const DateTime &other) const;
85  std::string toString(DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime, bool noMilliseconds = false) const;
86  void toString(std::string &result, DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime, bool noMilliseconds = false) const;
87  std::string toIsoString(TimeSpan timeZoneDelta = TimeSpan()) const;
88  static const char *printDayOfWeek(DayOfWeek dayOfWeek, bool abbreviation = false);
89 
90  static constexpr DateTime eternity();
91  static constexpr DateTime unixEpochStart();
92  static DateTime now();
93  static DateTime gmtNow();
94 #if defined(PLATFORM_UNIX) && !defined(PLATFORM_MAC)
95  static DateTime exactGmtNow();
96 #endif
97  constexpr static bool isLeapYear(int year);
98  static int daysInMonth(int year, int month);
99 
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 bool operator>=(const DateTime &other) const;
106  constexpr DateTime operator+(const TimeSpan &timeSpan) const;
107  constexpr DateTime operator-(const TimeSpan &timeSpan) const;
108  constexpr TimeSpan operator+(const DateTime &other) const;
109  constexpr TimeSpan operator-(const DateTime &other) const;
110  DateTime &operator+=(const TimeSpan &timeSpan);
111  DateTime &operator-=(const TimeSpan &timeSpan);
112 
113 private:
114  static uint64 dateToTicks(int year, int month, int day);
115  static uint64 timeToTicks(int hour, int minute, int second, double millisecond);
116  int getDatePart(DatePart part) const;
117 
118  uint64 m_ticks;
119  static const int m_daysPerYear;
120  static const int m_daysPer4Years;
121  static const int m_daysPer100Years;
122  static const int m_daysPer400Years;
123  static const int m_daysTo1601;
124  static const int m_daysTo1899;
125  static const int m_daysTo10000;
126  static const int m_daysToMonth365[13];
127  static const int m_daysToMonth366[13];
128  static const int m_daysInMonth365[12];
129  static const int m_daysInMonth366[12];
130 };
131 
135 constexpr inline DateTime::DateTime()
136  : m_ticks(0)
137 {
138 }
139 
143 constexpr inline DateTime::DateTime(uint64 ticks)
144  : m_ticks(ticks)
145 {
146 }
147 
152 inline DateTime DateTime::fromDate(int year, int month, int day)
153 {
154  return DateTime(dateToTicks(year, month, day));
155 }
156 
161 inline DateTime DateTime::fromTime(int hour, int minute, int second, double millisecond)
162 {
163  return DateTime(timeToTicks(hour, minute, second, millisecond));
164 }
165 
171 inline DateTime DateTime::fromDateAndTime(int year, int month, int day, int hour, int minute, int second, double millisecond)
172 {
173  if (uint64 ticks = dateToTicks(year, month, day)) {
174  return DateTime(ticks + timeToTicks(hour, minute, second, millisecond));
175  }
176  return DateTime();
177 }
178 
188 inline DateTime DateTime::fromString(const std::string &str)
189 {
190  return fromString(str.data());
191 }
192 
199 inline DateTime DateTime::fromIsoStringGmt(const char *str)
200 {
201  const auto tmp = fromIsoString(str);
202  return tmp.first - tmp.second;
203 }
204 
211 inline DateTime DateTime::fromIsoStringLocal(const char *str)
212 {
213  return fromIsoString(str).first;
214 }
215 
220 {
221  return m_ticks;
222 }
223 
227 constexpr inline uint64 DateTime::totalTicks() const
228 {
229  return m_ticks;
230 }
231 
235 inline int DateTime::year() const
236 {
237  return getDatePart(DatePart::Year);
238 }
239 
243 inline int DateTime::month() const
244 {
245  return getDatePart(DatePart::Month);
246 }
247 
251 inline int DateTime::day() const
252 {
253  return getDatePart(DatePart::Day);
254 }
255 
259 inline int DateTime::dayOfYear() const
260 {
261  return getDatePart(DatePart::DayOfYear);
262 }
263 
268 constexpr inline DayOfWeek DateTime::dayOfWeek() const
269 {
270  return static_cast<DayOfWeek>((m_ticks / TimeSpan::m_ticksPerDay) % 7l);
271 }
272 
276 constexpr inline int DateTime::hour() const
277 {
278  return m_ticks / TimeSpan::m_ticksPerHour % 24ul;
279 }
280 
284 constexpr inline int DateTime::minute() const
285 {
286  return m_ticks / TimeSpan::m_ticksPerMinute % 60ul;
287 }
288 
292 constexpr inline int DateTime::second() const
293 {
294  return m_ticks / TimeSpan::m_ticksPerSecond % 60ul;
295 }
296 
300 constexpr inline int DateTime::millisecond() const
301 {
302  return m_ticks / TimeSpan::m_ticksPerMillisecond % 1000ul;
303 }
304 
308 constexpr int DateTime::microsecond() const
309 {
310  return m_ticks / TimeSpan::ticksPerMicrosecond % 1000ul;
311 }
312 
318 constexpr int DateTime::nanosecond() const
319 {
320  return m_ticks % 10ul * TimeSpan::nanosecondsPerTick;
321 }
322 
327 constexpr inline bool DateTime::isNull() const
328 {
329  return m_ticks == 0;
330 }
331 
335 constexpr inline TimeSpan DateTime::timeOfDay() const
336 {
337  return TimeSpan(m_ticks % TimeSpan::m_ticksPerDay);
338 }
339 
343 inline bool DateTime::isLeapYear() const
344 {
345  return isLeapYear(year());
346 }
347 
351 constexpr inline bool DateTime::isEternity() const
352 {
353  return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
354 }
355 
359 constexpr inline bool DateTime::isLeapYear(int year)
360 {
361  return (year % 4 != 0) ? false : ((year % 100 == 0) ? (year % 400 == 0) : true);
362 }
363 
367 inline int DateTime::daysInMonth(int year, int month)
368 {
369  return (month >= 1 && month <= 12) ? (isLeapYear(year) ? m_daysInMonth366[month - 1] : m_daysInMonth365[month - 1]) : (0);
370 }
371 
375 constexpr inline bool DateTime::isSameDay(const DateTime &other) const
376 {
377  return (m_ticks / TimeSpan::m_ticksPerDay) == (other.m_ticks / TimeSpan::m_ticksPerDay);
378 }
379 
383 constexpr inline DateTime DateTime::eternity()
384 {
385  return DateTime(std::numeric_limits<decltype(m_ticks)>::max());
386 }
387 
392 {
393  return DateTime(621355968000000000);
394 }
395 
401 {
402  return DateTime::fromTimeStamp(std::time(nullptr));
403 }
404 
410 {
411  return DateTime::fromTimeStampGmt(std::time(nullptr));
412 }
413 
417 constexpr inline bool DateTime::operator==(const DateTime &other) const
418 {
419  return m_ticks == other.m_ticks;
420 }
421 
425 constexpr inline bool DateTime::operator!=(const DateTime &other) const
426 {
427  return m_ticks != other.m_ticks;
428 }
429 
433 constexpr inline bool DateTime::operator<(const DateTime &other) const
434 {
435  return m_ticks < other.m_ticks;
436 }
437 
441 constexpr inline bool DateTime::operator>(const DateTime &other) const
442 {
443  return m_ticks > other.m_ticks;
444 }
445 
449 constexpr inline bool DateTime::operator<=(const DateTime &other) const
450 {
451  return m_ticks <= other.m_ticks;
452 }
453 
457 constexpr inline bool DateTime::operator>=(const DateTime &other) const
458 {
459  return m_ticks >= other.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 DateTime DateTime::operator-(const TimeSpan &timeSpan) const
476 {
477  return DateTime(m_ticks - timeSpan.m_ticks);
478 }
479 
484 constexpr inline TimeSpan DateTime::operator+(const DateTime &other) const
485 {
486  return TimeSpan(m_ticks + other.m_ticks);
487 }
488 
495 constexpr inline TimeSpan DateTime::operator-(const DateTime &other) const
496 {
497  return TimeSpan(m_ticks - other.m_ticks);
498 }
499 
503 inline DateTime &DateTime::operator+=(const TimeSpan &timeSpan)
504 {
505  m_ticks += timeSpan.m_ticks;
506  return *this;
507 }
508 
512 inline DateTime &DateTime::operator-=(const TimeSpan &timeSpan)
513 {
514  m_ticks -= timeSpan.m_ticks;
515  return *this;
516 }
517 } // namespace ChronoUtilities
518 
519 namespace std {
520 template <> struct hash<ChronoUtilities::DateTime> {
521  inline size_t operator()(const ChronoUtilities::DateTime &dateTime) const
522  {
523  return hash<decltype(dateTime.totalTicks())>()(dateTime.totalTicks());
524  }
525 };
526 } // namespace std
527 
528 #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:235
constexpr int minute() const
Returns the minute component of the date represented by this instance.
Definition: datetime.h:284
DateTime & operator-=(const TimeSpan &timeSpan)
Substracts a TimeSpan from the current instance.
Definition: datetime.h:512
static DateTime now()
Returns a DateTime object that is set to the current date and time on this computer,...
Definition: datetime.h:400
constexpr DateTime operator-(const TimeSpan &timeSpan) const
Substracts another instance.
Definition: datetime.h:475
constexpr DateTime operator+(const TimeSpan &timeSpan) const
Adds another instance.
Definition: datetime.h:466
constexpr int nanosecond() const
Returns the nanosecond component of the date represented by this instance.
Definition: datetime.h:318
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:367
#define CPP_UTILITIES_EXPORT
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:351
static DateTime fromString(const std::string &str)
Parses the given std::string as DateTime.
Definition: datetime.h:188
constexpr uint64 totalTicks() const
Returns the number of ticks which represent the value of the current instance.
Definition: datetime.h:227
static constexpr DateTime eternity()
Constructs a new instance of the DateTime class with the maximal number of ticks.
Definition: datetime.h:383
Represents a time interval.
Definition: timespan.h:28
constexpr bool operator>=(const DateTime &other) const
Indicates whether a specified DateTime is greater or equal than another specified DateTime.
Definition: datetime.h:457
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:171
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:152
constexpr int second() const
Returns the second component of the date represented by this instance.
Definition: datetime.h:292
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
static constexpr int64 ticksPerMicrosecond
Definition: timespan.h:82
constexpr TimeSpan timeOfDay() const
Returns the time of day as TimeSpan for this instance.
Definition: datetime.h:335
int day() const
Returns the day component of the date represented by this instance.
Definition: datetime.h:251
DateTime & operator+=(const TimeSpan &timeSpan)
Adds a TimeSpan to the current instance.
Definition: datetime.h:503
constexpr bool isNull() const
Returns ture if the date represented by the current DateTime class is null.
Definition: datetime.h:327
static DateTime fromIsoStringGmt(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition: datetime.h:199
constexpr T max(T first, T second)
Returns the greatest of the given items.
Definition: math.h:29
constexpr bool isSameDay(const DateTime &other) const
Returns and indication whether two DateTime instances represent the same day.
Definition: datetime.h:375
static DateTime gmtNow()
Returns a DateTime object that is set to the current date and time on this computer,...
Definition: datetime.h:409
static constexpr int64 nanosecondsPerTick
Definition: timespan.h:81
static constexpr DateTime unixEpochStart()
Returns the DateTime object for the "1970-01-01T00:00:00Z".
Definition: datetime.h:391
uint64 & ticks()
Returns a mutable reference to the total ticks.
Definition: datetime.h:219
constexpr int millisecond() const
Returns the millisecond component of the date represented by this instance.
Definition: datetime.h:300
constexpr bool operator==(const DateTime &other) const
Indicates whether two DateTime instances are equal.
Definition: datetime.h:417
constexpr bool operator<(const DateTime &other) const
Indicates whether a specified DateTime is less than another specified DateTime.
Definition: datetime.h:433
DatePart
Specifies the date part.
Definition: datetime.h:45
constexpr DateTime()
Constructs a DateTime.
Definition: datetime.h:135
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:172
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:161
constexpr bool operator>(const DateTime &other) const
Indicates whether a specified DateTime is greater than another specified DateTime.
Definition: datetime.h:441
constexpr int microsecond() const
Returns the microsecond component of the date represented by this instance.
Definition: datetime.h:308
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:243
bool isLeapYear() const
Returns an indication whether the year represented by this instance is a leap year.
Definition: datetime.h:343
size_t operator()(const ChronoUtilities::DateTime &dateTime) const
Definition: datetime.h:521
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:259
constexpr bool operator<=(const DateTime &other) const
Indicates whether a specified DateTime is less or equal than another specified DateTime.
Definition: datetime.h:449
static DateTime fromIsoStringLocal(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition: datetime.h:211
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:276
constexpr bool operator!=(const DateTime &other) const
Indicates whether two DateTime instances are not equal.
Definition: datetime.h:425
constexpr DayOfWeek dayOfWeek() const
Returns the day of the week represented by this instance.
Definition: datetime.h:268