C++ Utilities  4.10.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 int microsecond() const;
77  constexpr int nanosecond() const;
78  constexpr bool isNull() const;
79  constexpr TimeSpan timeOfDay() const;
80  bool isLeapYear() const;
81  constexpr bool isEternity() const;
82  constexpr bool isSameDay(const DateTime &other) const;
83  std::string toString(DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime, bool noMilliseconds = false) const;
84  void toString(std::string &result, DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime, bool noMilliseconds = false) const;
85  std::string toIsoString(TimeSpan timeZoneDelta = TimeSpan()) const;
86  static const char *printDayOfWeek(DayOfWeek dayOfWeek, bool abbreviation = false);
87 
88  static constexpr DateTime eternity();
89  static constexpr DateTime unixEpochStart();
90  static DateTime now();
91  static DateTime gmtNow();
92 #if defined(PLATFORM_UNIX) && !defined(PLATFORM_MAC)
93  static DateTime exactGmtNow();
94 #endif
95  constexpr static bool isLeapYear(int year);
96  static int daysInMonth(int year, int month);
97 
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 bool operator<=(const DateTime &other) const;
103  constexpr bool operator>=(const DateTime &other) const;
104  constexpr DateTime operator+(const TimeSpan &timeSpan) const;
105  constexpr DateTime operator-(const TimeSpan &timeSpan) const;
106  constexpr TimeSpan operator+(const DateTime &other) const;
107  constexpr TimeSpan operator-(const DateTime &other) const;
108  DateTime &operator+=(const TimeSpan &timeSpan);
109  DateTime &operator-=(const TimeSpan &timeSpan);
110 
111 private:
112  static uint64 dateToTicks(int year, int month, int day);
113  static uint64 timeToTicks(int hour, int minute, int second, double millisecond);
114  int getDatePart(DatePart part) const;
115 
116  uint64 m_ticks;
117  static const int m_daysPerYear;
118  static const int m_daysPer4Years;
119  static const int m_daysPer100Years;
120  static const int m_daysPer400Years;
121  static const int m_daysTo1601;
122  static const int m_daysTo1899;
123  static const int m_daysTo10000;
124  static const int m_daysToMonth365[13];
125  static const int m_daysToMonth366[13];
126  static const int m_daysInMonth365[12];
127  static const int m_daysInMonth366[12];
128 };
129 
133 constexpr inline DateTime::DateTime()
134  : m_ticks(0)
135 {
136 }
137 
141 constexpr inline DateTime::DateTime(uint64 ticks)
142  : m_ticks(ticks)
143 {
144 }
145 
149 inline DateTime DateTime::fromDate(int year, int month, int day)
150 {
151  return DateTime(dateToTicks(year, month, day));
152 }
153 
158 {
159  return DateTime(timeToTicks(hour, minute, second, millisecond));
160 }
161 
165 inline DateTime DateTime::fromDateAndTime(int year, int month, int day, int hour, int minute, int second, double millisecond)
166 {
167  if (uint64 ticks = dateToTicks(year, month, day)) {
168  return DateTime(ticks + timeToTicks(hour, minute, second, millisecond));
169  }
170  return DateTime();
171 }
172 
176 inline DateTime DateTime::fromString(const std::string &str)
177 {
178  return fromString(str.data());
179 }
180 
186 inline DateTime DateTime::fromIsoStringGmt(const char *str)
187 {
188  const auto tmp = fromIsoString(str);
189  return tmp.first - tmp.second;
190 }
191 
197 inline DateTime DateTime::fromIsoStringLocal(const char *str)
198 {
199  return fromIsoString(str).first;
200 }
201 
205 constexpr inline uint64 DateTime::totalTicks() const
206 {
207  return m_ticks;
208 }
209 
213 inline int DateTime::year() const
214 {
215  return getDatePart(DatePart::Year);
216 }
217 
221 inline int DateTime::month() const
222 {
223  return getDatePart(DatePart::Month);
224 }
225 
229 inline int DateTime::day() const
230 {
231  return getDatePart(DatePart::Day);
232 }
233 
237 inline int DateTime::dayOfYear() const
238 {
239  return getDatePart(DatePart::DayOfYear);
240 }
241 
246 constexpr inline DayOfWeek DateTime::dayOfWeek() const
247 {
248  return static_cast<DayOfWeek>((m_ticks / TimeSpan::m_ticksPerDay) % 7l);
249 }
250 
254 constexpr inline int DateTime::hour() const
255 {
256  return m_ticks / TimeSpan::m_ticksPerHour % 24ul;
257 }
258 
262 constexpr inline int DateTime::minute() const
263 {
264  return m_ticks / TimeSpan::m_ticksPerMinute % 60ul;
265 }
266 
270 constexpr inline int DateTime::second() const
271 {
272  return m_ticks / TimeSpan::m_ticksPerSecond % 60ul;
273 }
274 
278 constexpr inline int DateTime::millisecond() const
279 {
280  return m_ticks / TimeSpan::m_ticksPerMillisecond % 1000ul;
281 }
282 
286 constexpr int DateTime::microsecond() const
287 {
288  return m_ticks / TimeSpan::ticksPerMicrosecond % 1000ul;
289 }
290 
296 constexpr int DateTime::nanosecond() const
297 {
298  return m_ticks % 10ul * TimeSpan::nanosecondsPerTick;
299 }
300 
305 constexpr inline bool DateTime::isNull() const
306 {
307  return m_ticks == 0;
308 }
309 
313 constexpr inline TimeSpan DateTime::timeOfDay() const
314 {
315  return TimeSpan(m_ticks % TimeSpan::m_ticksPerDay);
316 }
317 
321 inline bool DateTime::isLeapYear() const
322 {
323  return isLeapYear(year());
324 }
325 
329 constexpr inline bool DateTime::isEternity() const
330 {
331  return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
332 }
333 
337 constexpr inline bool DateTime::isLeapYear(int year)
338 {
339  return (year % 4 != 0) ? false : ((year % 100 == 0) ? (year % 400 == 0) : true);
340 }
341 
345 inline int DateTime::daysInMonth(int year, int month)
346 {
347  return (month >= 1 && month <= 12) ? (isLeapYear(year) ? m_daysInMonth366[month - 1] : m_daysInMonth365[month - 1]) : (0);
348 }
349 
353 constexpr inline bool DateTime::isSameDay(const DateTime &other) const
354 {
355  return (m_ticks / TimeSpan::m_ticksPerDay) == (other.m_ticks / TimeSpan::m_ticksPerDay);
356 }
357 
361 constexpr inline DateTime DateTime::eternity()
362 {
363  return DateTime(std::numeric_limits<decltype(m_ticks)>::max());
364 }
365 
370 {
371  return DateTime(621355968000000000);
372 }
373 
379 {
380  return DateTime::fromTimeStamp(time(nullptr));
381 }
382 
388 {
389  return DateTime::fromTimeStampGmt(time(nullptr));
390 }
391 
395 constexpr inline bool DateTime::operator==(const DateTime &other) const
396 {
397  return m_ticks == other.m_ticks;
398 }
399 
403 constexpr inline bool DateTime::operator!=(const DateTime &other) const
404 {
405  return m_ticks != other.m_ticks;
406 }
407 
411 constexpr inline bool DateTime::operator<(const DateTime &other) const
412 {
413  return m_ticks < other.m_ticks;
414 }
415 
419 constexpr inline bool DateTime::operator>(const DateTime &other) const
420 {
421  return m_ticks > other.m_ticks;
422 }
423 
427 constexpr inline bool DateTime::operator<=(const DateTime &other) const
428 {
429  return m_ticks <= other.m_ticks;
430 }
431 
435 constexpr inline bool DateTime::operator>=(const DateTime &other) const
436 {
437  return m_ticks >= other.m_ticks;
438 }
439 
444 constexpr inline DateTime DateTime::operator+(const TimeSpan &timeSpan) const
445 {
446  return DateTime(m_ticks + timeSpan.m_ticks);
447 }
448 
453 constexpr inline DateTime DateTime::operator-(const TimeSpan &timeSpan) const
454 {
455  return DateTime(m_ticks - timeSpan.m_ticks);
456 }
457 
462 constexpr inline TimeSpan DateTime::operator+(const DateTime &other) const
463 {
464  return TimeSpan(m_ticks + other.m_ticks);
465 }
466 
471 constexpr inline TimeSpan DateTime::operator-(const DateTime &other) const
472 {
473  return TimeSpan(m_ticks - other.m_ticks);
474 }
475 
479 inline DateTime &DateTime::operator+=(const TimeSpan &timeSpan)
480 {
481  m_ticks += timeSpan.m_ticks;
482  return *this;
483 }
484 
488 inline DateTime &DateTime::operator-=(const TimeSpan &timeSpan)
489 {
490  m_ticks -= timeSpan.m_ticks;
491  return *this;
492 }
493 } // namespace ChronoUtilities
494 
495 namespace std {
496 template <> struct hash<ChronoUtilities::DateTime> {
497  inline size_t operator()(const ChronoUtilities::DateTime &dateTime) const
498  {
499  return hash<decltype(dateTime.totalTicks())>()(dateTime.totalTicks());
500  }
501 };
502 } // namespace std
503 
504 #endif // CHRONO_UTILITIES_DATETIME_H
int year() const
Gets the year component of the date represented by this instance.
Definition: datetime.h:213
constexpr int minute() const
Gets the minute component of the date represented by this instance.
Definition: datetime.h:262
DateTime & operator-=(const TimeSpan &timeSpan)
Substracts a TimeSpan from the current instance.
Definition: datetime.h:488
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:378
constexpr DateTime operator-(const TimeSpan &timeSpan) const
Substracts another instance.
Definition: datetime.h:453
constexpr DateTime operator+(const TimeSpan &timeSpan) const
Adds another instance.
Definition: datetime.h:444
constexpr int nanosecond() const
Gets the nanosecond component of the date represented by this instance.
Definition: datetime.h:296
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:345
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:329
static DateTime fromString(const std::string &str)
Parses the given std::string as DateTime.
Definition: datetime.h:176
constexpr uint64 totalTicks() const
Gets the number of ticks which represent the value of the current instance.
Definition: datetime.h:205
static constexpr DateTime eternity()
Constructs a new instance of the DateTime class with the maximal number of ticks. ...
Definition: datetime.h:361
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:435
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:165
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:149
constexpr int second() const
Gets the second component of the date represented by this instance.
Definition: datetime.h:270
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
static constexpr int64 ticksPerMicrosecond
Definition: timespan.h:80
constexpr TimeSpan timeOfDay() const
Gets the time of day as TimeSpan for this instance.
Definition: datetime.h:313
int day() const
Gets the day component of the date represented by this instance.
Definition: datetime.h:229
DateTime & operator+=(const TimeSpan &timeSpan)
Adds a TimeSpan to the current instance.
Definition: datetime.h:479
constexpr bool isNull() const
Returns ture if the date represented by the current DateTime class is null.
Definition: datetime.h:305
static DateTime fromIsoStringGmt(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition: datetime.h:186
constexpr bool isSameDay(const DateTime &other) const
Returns and indication whether two DateTime instances represent the same day.
Definition: datetime.h:353
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:387
static constexpr int64 nanosecondsPerTick
Definition: timespan.h:79
static constexpr DateTime unixEpochStart()
Returns the DateTime object for the "1970-01-01T00:00:00Z".
Definition: datetime.h:369
constexpr int millisecond() const
Gets the millisecond component of the date represented by this instance.
Definition: datetime.h:278
constexpr bool operator==(const DateTime &other) const
Indicates whether two DateTime instances are equal.
Definition: datetime.h:395
constexpr bool operator<(const DateTime &other) const
Indicates whether a specified DateTime is less than another specified DateTime.
Definition: datetime.h:411
DatePart
Specifies the date part.
Definition: datetime.h:44
constexpr DateTime()
Constructs a DateTime.
Definition: datetime.h:133
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:149
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:157
constexpr bool operator>(const DateTime &other) const
Indicates whether a specified DateTime is greater than another specified DateTime.
Definition: datetime.h:419
constexpr int microsecond() const
Gets the microsecond component of the date represented by this instance.
Definition: datetime.h:286
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:221
bool isLeapYear() const
Returns an indication whether the year of the dae represented by this instance is a leap year...
Definition: datetime.h:321
size_t operator()(const ChronoUtilities::DateTime &dateTime) const
Definition: datetime.h:497
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
int dayOfYear() const
Gets the day of the year represented by this instance.
Definition: datetime.h:237
constexpr bool operator<=(const DateTime &other) const
Indicates whether a specified DateTime is less or equal than another specified DateTime.
Definition: datetime.h:427
static DateTime fromIsoStringLocal(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition: datetime.h:197
#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:254
constexpr bool operator!=(const DateTime &other) const
Indicates whether two DateTime instances are not equal.
Definition: datetime.h:403
constexpr DayOfWeek dayOfWeek() const
Gets the day of the week represented by this instance.
Definition: datetime.h:246