C++ Utilities  4.6.1
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 <string>
9 #include <limits>
10 #include <ctime>
11 
12 namespace ChronoUtilities
13 {
14 
20 {
21  DateAndTime,
22  DateOnly,
23  TimeOnly,
26 };
27 
32 enum class DayOfWeek
33 {
34  Monday,
35  Tuesday,
36  Wednesday,
37  Thursday,
38  Friday,
39  Saturday,
40  Sunday
41 };
42 
47 enum class DatePart
48 {
49  Year,
50  Month,
51  DayOfYear,
52  Day
53 };
54 
56 {
57 public:
58  explicit constexpr DateTime();
59  explicit constexpr DateTime(uint64 ticks);
60  static DateTime fromDate(int year = 1, int month = 1, int day = 1);
61  static DateTime fromTime(int hour = 0, int minute = 0, int second = 0, double millisecond = 0.0);
62  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);
63  static DateTime fromString(const std::string &str);
64  static DateTime fromString(const char *str);
65  static std::pair<DateTime, TimeSpan> fromIsoString(const char *str);
66  static DateTime fromIsoStringGmt(const char *str);
67  static DateTime fromIsoStringLocal(const char *str);
68  static DateTime fromTimeStamp(time_t timeStamp);
69  static DateTime fromTimeStampGmt(time_t timeStamp);
70 
71  constexpr uint64 totalTicks() const;
72  int year() const;
73  int month() const;
74  int day() const;
75  int dayOfYear() const;
76  constexpr DayOfWeek dayOfWeek() const;
77  constexpr int hour() const;
78  constexpr int minute() const;
79  constexpr int second() const;
80  constexpr int millisecond() const;
81  constexpr bool isNull() const;
82  constexpr TimeSpan timeOfDay() const;
83  bool isLeapYear() const;
84  constexpr bool isEternity() const;
85  constexpr bool isSameDay(const DateTime &other) const;
86  std::string toString(DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime, bool noMilliseconds = false) const;
87  void toString(std::string &result, DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime, bool noMilliseconds = false) const;
88  std::string toIsoString(TimeSpan timeZoneDelta = TimeSpan()) const;
89  static const char *printDayOfWeek(DayOfWeek dayOfWeek, bool abbreviation = false);
90 
91  static constexpr DateTime eternity();
92  static DateTime now();
93  static DateTime gmtNow();
94  constexpr static bool isLeapYear(int year);
95  static int daysInMonth(int year, int month);
96 
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 bool operator >=(const DateTime &other) const;
103  constexpr DateTime operator +(const TimeSpan &timeSpan) const;
104  constexpr DateTime operator -(const TimeSpan &timeSpan) const;
105  constexpr TimeSpan operator +(const DateTime &other) const;
106  constexpr TimeSpan operator -(const DateTime &other) const;
107  DateTime &operator +=(const TimeSpan &timeSpan);
108  DateTime &operator -=(const TimeSpan &timeSpan);
109 
110 private:
111  static uint64 dateToTicks(int year, int month, int day);
112  static uint64 timeToTicks(int hour, int minute, int second, double millisecond);
113  int getDatePart(DatePart part) const;
114 
115  uint64 m_ticks;
116  static const int m_daysPerYear;
117  static const int m_daysPer4Years;
118  static const int m_daysPer100Years;
119  static const int m_daysPer400Years;
120  static const int m_daysTo1601;
121  static const int m_daysTo1899;
122  static const int m_daysTo10000;
123  static const int m_daysToMonth365[13];
124  static const int m_daysToMonth366[13];
125  static const int m_daysInMonth365[12];
126  static const int m_daysInMonth366[12];
127 };
128 
129 
130 
134 constexpr inline DateTime::DateTime() :
135  m_ticks(0)
136 {}
137 
141 constexpr inline DateTime::DateTime(uint64 ticks) :
142  m_ticks(ticks)
143 {}
144 
148 inline DateTime DateTime::fromDate(int year, int month, int day)
149 {
150  return DateTime(dateToTicks(year, month, day));
151 }
152 
157 {
158  return DateTime(timeToTicks(hour, minute, second, millisecond));
159 }
160 
164 inline DateTime DateTime::fromDateAndTime(int year, int month, int day, int hour, int minute, int second, double millisecond)
165 {
166  if(uint64 ticks = dateToTicks(year, month, day)) {
167  return DateTime(ticks + timeToTicks(hour, minute, second, millisecond));
168  }
169  return DateTime();
170 }
171 
175 inline DateTime DateTime::fromString(const std::string &str)
176 {
177  return fromString(str.data());
178 }
179 
185 inline DateTime DateTime::fromIsoStringGmt(const char *str)
186 {
187  const auto tmp = fromIsoString(str);
188  return tmp.first - tmp.second;
189 }
190 
196 inline DateTime DateTime::fromIsoStringLocal(const char *str)
197 {
198  return fromIsoString(str).first;
199 }
200 
204 constexpr inline uint64 DateTime::totalTicks() const
205 {
206  return m_ticks;
207 }
208 
212 inline int DateTime::year() const
213 {
214  return getDatePart(DatePart::Year);
215 }
216 
220 inline int DateTime::month() const
221 {
222  return getDatePart(DatePart::Month);
223 }
224 
228 inline int DateTime::day() const
229 {
230  return getDatePart(DatePart::Day);
231 }
232 
236 inline int DateTime::dayOfYear() const
237 {
238  return getDatePart(DatePart::DayOfYear);
239 }
240 
245 constexpr inline DayOfWeek DateTime::dayOfWeek() const
246 {
247  return static_cast<DayOfWeek>((m_ticks / TimeSpan::ticksPerDay) % 7l);
248 }
249 
253 constexpr inline int DateTime::hour() const
254 {
255  return m_ticks / TimeSpan::ticksPerHour % 24ul;
256 }
257 
261 constexpr inline int DateTime::minute() const
262 {
263  return m_ticks / TimeSpan::ticksPerMinute % 60ul;
264 }
265 
269 constexpr inline int DateTime::second() const
270 {
271  return m_ticks / TimeSpan::ticksPerSecond % 60ul;
272 }
273 
277 constexpr inline int DateTime::millisecond() const
278 {
279  return m_ticks / TimeSpan::ticksPerMillisecond % 1000ul;
280 }
281 
286 constexpr inline bool DateTime::isNull() const
287 {
288  return m_ticks == 0;
289 }
290 
294 constexpr inline TimeSpan DateTime::timeOfDay() const
295 {
296  return TimeSpan(m_ticks % TimeSpan::ticksPerDay);
297 }
298 
302 inline bool DateTime::isLeapYear() const
303 {
304  return isLeapYear(year());
305 }
306 
310 constexpr inline bool DateTime::isEternity() const
311 {
312  return m_ticks == std::numeric_limits<decltype(m_ticks)>::max();
313 }
314 
318 constexpr inline bool DateTime::isLeapYear(int year)
319 {
320  return (year % 4 != 0)
321  ? false
322  : ((year % 100 == 0)
323  ? (year % 400 == 0)
324  : true);
325 }
326 
330 inline int DateTime::daysInMonth(int year, int month)
331 {
332  return (month >= 1 && month <= 12)
333  ? (isLeapYear(year)
334  ? m_daysInMonth366[month - 1]
335  : m_daysInMonth365[month - 1])
336  : (0);
337 }
338 
342 constexpr inline bool DateTime::isSameDay(const DateTime &other) const
343 {
344  return (m_ticks / TimeSpan::ticksPerDay) == (other.m_ticks / TimeSpan::ticksPerDay);
345 }
346 
350 constexpr inline DateTime DateTime::eternity()
351 {
352  return DateTime(std::numeric_limits<decltype(m_ticks)>::max());
353 }
354 
359 {
360  return DateTime::fromTimeStamp(time(nullptr));
361 }
362 
367 {
368  return DateTime::fromTimeStampGmt(time(nullptr));
369 }
370 
374 constexpr inline bool DateTime::operator ==(const DateTime &other) const
375 {
376  return m_ticks == other.m_ticks;
377 }
378 
382 constexpr inline bool DateTime::operator !=(const DateTime &other) const
383 {
384  return m_ticks != other.m_ticks;
385 }
386 
390 constexpr inline bool DateTime::operator <(const DateTime &other) const
391 {
392  return m_ticks < other.m_ticks;
393 }
394 
398 constexpr inline bool DateTime::operator >(const DateTime &other) const
399 {
400  return m_ticks > other.m_ticks;
401 }
402 
406 constexpr inline bool DateTime::operator <=(const DateTime &other) const
407 {
408  return m_ticks <= other.m_ticks;
409 }
410 
414 constexpr inline bool DateTime::operator >=(const DateTime &other) const
415 {
416  return m_ticks >= other.m_ticks;
417 }
418 
423 constexpr inline DateTime DateTime::operator +(const TimeSpan &timeSpan) const
424 {
425  return DateTime(m_ticks + timeSpan.m_ticks);
426 }
427 
432 constexpr inline DateTime DateTime::operator -(const TimeSpan &timeSpan) const
433 {
434  return DateTime(m_ticks - timeSpan.m_ticks);
435 }
436 
441 constexpr inline TimeSpan DateTime::operator +(const DateTime &other) const
442 {
443  return TimeSpan(m_ticks + other.m_ticks);
444 }
445 
450 constexpr inline TimeSpan DateTime::operator -(const DateTime &other) const
451 {
452  return TimeSpan(m_ticks - other.m_ticks);
453 }
454 
458 inline DateTime &DateTime::operator +=(const TimeSpan &timeSpan)
459 {
460  m_ticks += timeSpan.m_ticks;
461  return *this;
462 }
463 
467 inline DateTime &DateTime::operator -=(const TimeSpan &timeSpan)
468 {
469  m_ticks -= timeSpan.m_ticks;
470  return *this;
471 }
472 
473 }
474 
475 #endif // CHRONO_UTILITIES_DATETIME_H
int year() const
Gets the year component of the date represented by this instance.
Definition: datetime.h:212
constexpr int minute() const
Gets the minute component of the date represented by this instance.
Definition: datetime.h:261
DateTime & operator-=(const TimeSpan &timeSpan)
Substracts a TimeSpan from the current instance.
Definition: datetime.h:467
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:432
constexpr DateTime operator+(const TimeSpan &timeSpan) const
Adds another instance.
Definition: datetime.h:423
static constexpr uint64 ticksPerDay
Definition: timespan.h:80
static std::pair< DateTime, TimeSpan > fromIsoString(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition: datetime.cpp:120
static int daysInMonth(int year, int month)
Returns the number of days in the specified month and year.
Definition: datetime.h:330
Represents an instant in time, typically expressed as a date and time of day.
Definition: datetime.h:55
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:310
static DateTime fromString(const std::string &str)
Parses the given std::string as DateTime.
Definition: datetime.h:175
constexpr uint64 totalTicks() const
Gets the number of ticks which represent the value of the current instance.
Definition: datetime.h:204
static constexpr DateTime eternity()
Constructs a new instance of the DateTime class with the maximal number of ticks. ...
Definition: datetime.h:350
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:414
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:164
static constexpr uint64 ticksPerMillisecond
Definition: timespan.h:76
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:148
constexpr int second() const
Gets the second component of the date represented by this instance.
Definition: datetime.h:269
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:294
int day() const
Gets the day component of the date represented by this instance.
Definition: datetime.h:228
DateTime & operator+=(const TimeSpan &timeSpan)
Adds a TimeSpan to the current instance.
Definition: datetime.h:458
static constexpr uint64 ticksPerHour
Definition: timespan.h:79
constexpr bool isNull() const
Returns ture if the date represented by the current DateTime class is null.
Definition: datetime.h:286
static DateTime fromIsoStringGmt(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition: datetime.h:185
constexpr bool isSameDay(const DateTime &other) const
Returns and indication whether two DateTime instances represent the same day.
Definition: datetime.h:342
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:366
constexpr int millisecond() const
Gets the millisecond component of the date represented by this instance.
Definition: datetime.h:277
constexpr bool operator==(const DateTime &other) const
Indicates whether two DateTime instances are equal.
Definition: datetime.h:374
constexpr bool operator<(const DateTime &other) const
Indicates whether a specified DateTime is less than another specified DateTime.
Definition: datetime.h:390
DatePart
Specifies the date part.
Definition: datetime.h:47
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:144
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:156
constexpr bool operator>(const DateTime &other) const
Indicates whether a specified DateTime is greater than another specified DateTime.
Definition: datetime.h:398
DateTimeOutputFormat
Specifies the output format.
Definition: datetime.h:19
static DateTime fromTimeStampGmt(time_t timeStamp)
Constructs a new DateTime object with the GMT time from the specified timeStamp.
Definition: datetime.cpp:68
int month() const
Gets the month component of the date represented by this instance.
Definition: datetime.h:220
bool isLeapYear() const
Returns an indication whether the year of the dae represented by this instance is a leap year...
Definition: datetime.h:302
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 timeStamp.
Definition: datetime.cpp:54
static constexpr uint64 ticksPerSecond
Definition: timespan.h:77
int dayOfYear() const
Gets the day of the year represented by this instance.
Definition: datetime.h:236
constexpr bool operator<=(const DateTime &other) const
Indicates whether a specified DateTime is less or equal than another specified DateTime.
Definition: datetime.h:406
static DateTime fromIsoStringLocal(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition: datetime.h:196
#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:32
constexpr int hour() const
Gets the hour component of the date represented by this instance.
Definition: datetime.h:253
static constexpr uint64 ticksPerMinute
Definition: timespan.h:78
constexpr bool operator!=(const DateTime &other) const
Indicates whether two DateTime instances are not equal.
Definition: datetime.h:382
constexpr DayOfWeek dayOfWeek() const
Gets the day of the week represented by this instance.
Definition: datetime.h:245