C++ Utilities  4.14.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
datetime.cpp
Go to the documentation of this file.
1 #include "./datetime.h"
2 
3 #include "../conversion/stringbuilder.h"
4 #include "../conversion/stringconversion.h"
5 
6 #include <iomanip>
7 #include <sstream>
8 #include <stdexcept>
9 
10 using namespace std;
11 using namespace ChronoUtilities;
12 using namespace ConversionUtilities;
13 
14 const int DateTime::m_daysPerYear = 365;
15 const int DateTime::m_daysPer4Years = 1461;
16 const int DateTime::m_daysPer100Years = 36524;
17 const int DateTime::m_daysPer400Years = 146097;
18 const int DateTime::m_daysTo1601 = 584388;
19 const int DateTime::m_daysTo1899 = 693593;
20 const int DateTime::m_daysTo10000 = 3652059;
21 const int DateTime::m_daysToMonth365[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
22 const int DateTime::m_daysToMonth366[13] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
23 const int DateTime::m_daysInMonth365[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
24 const int DateTime::m_daysInMonth366[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
25 
26 template <typename num1, typename num2, typename num3> constexpr bool inRangeInclMax(num1 val, num2 min, num3 max)
27 {
28  return (val) >= (min) && (val) <= (max);
29 }
30 
31 template <typename num1, typename num2, typename num3> constexpr bool inRangeExclMax(num1 val, num2 min, num3 max)
32 {
33  return (val) >= (min) && (val) < (max);
34 }
35 
59 DateTime DateTime::fromTimeStamp(time_t timeStamp)
60 {
61  if (timeStamp) {
62  struct tm *timeinfo = localtime(&timeStamp);
63  return DateTime::fromDateAndTime(timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min,
64  timeinfo->tm_sec < 60 ? timeinfo->tm_sec : 59, 0);
65  } else {
66  return DateTime();
67  }
68 }
69 
73 DateTime DateTime::fromTimeStampGmt(time_t timeStamp)
74 {
75  return DateTime(DateTime::unixEpochStart().totalTicks() + static_cast<uint64>(timeStamp) * TimeSpan::m_ticksPerSecond);
76 }
77 
87 DateTime DateTime::fromString(const char *str)
88 {
89  int values[6] = { 0 };
90  int *const dayIndex = values + 2;
91  int *const secondsIndex = values + 5;
92  int *valueIndex = values;
93  int *const valuesEnd = values + 7;
94  double miliSecondsFact = 100.0, miliSeconds = 0.0;
95  for (const char *strIndex = str;; ++strIndex) {
96  const char c = *strIndex;
97  if (c <= '9' && c >= '0') {
98  if (valueIndex > secondsIndex) {
99  miliSeconds += (c - '0') * miliSecondsFact;
100  miliSecondsFact /= 10;
101  } else {
102  *valueIndex *= 10;
103  *valueIndex += c - '0';
104  }
105  } else if ((c == '-' || c == ':' || c == '/') || (c == '.' && (valueIndex == secondsIndex)) || (c == ' ' && (valueIndex == dayIndex))) {
106  if (++valueIndex == valuesEnd) {
107  break; // just ignore further values for now
108  }
109  } else if (c == '\0') {
110  break;
111  } else {
112  throw ConversionException(argsToString("unexpected ", c));
113  }
114  }
115  return DateTime::fromDateAndTime(values[0], values[1], *dayIndex, values[3], values[4], *secondsIndex, miliSeconds);
116 }
117 
127 std::pair<DateTime, TimeSpan> DateTime::fromIsoString(const char *str)
128 {
129  int values[9] = { 0 };
130  int *const dayIndex = values + 2;
131  int *const hourIndex = values + 3;
132  int *const secondsIndex = values + 5;
133  int *const miliSecondsIndex = values + 6;
134  int *const deltaHourIndex = values + 7;
135  int *valueIndex = values;
136  bool deltaNegative = false;
137  double miliSecondsFact = 100.0, miliSeconds = 0.0;
138  for (const char *strIndex = str;; ++strIndex) {
139  const char c = *strIndex;
140  if (c <= '9' && c >= '0') {
141  if (valueIndex == miliSecondsIndex) {
142  miliSeconds += (c - '0') * miliSecondsFact;
143  miliSecondsFact /= 10;
144  } else {
145  *valueIndex *= 10;
146  *valueIndex += c - '0';
147  }
148  } else if (c == 'T') {
149  if (++valueIndex != hourIndex) {
150  throw ConversionException("\"T\" expected before hour");
151  }
152  } else if (c == '-') {
153  if (valueIndex < dayIndex) {
154  ++valueIndex;
155  } else if (++valueIndex == deltaHourIndex) {
156  deltaNegative = true;
157  } else {
158  throw ConversionException("unexpected \"-\" after day");
159  }
160  } else if (c == '.') {
161  if (valueIndex != secondsIndex) {
162  throw ConversionException("unexpected \".\"");
163  } else {
164  ++valueIndex;
165  }
166  } else if (c == ':') {
167  if (valueIndex < hourIndex) {
168  throw ConversionException("unexpected \":\" before hour");
169  } else if (valueIndex == secondsIndex) {
170  throw ConversionException("unexpected \":\" after second");
171  } else {
172  ++valueIndex;
173  }
174  } else if ((c == '+') && (++valueIndex == deltaHourIndex)) {
175  deltaNegative = false;
176  } else if ((c == 'Z') && (++valueIndex == deltaHourIndex)) {
177  valueIndex += 2;
178  } else if (c == '\0') {
179  break;
180  } else {
181  throw ConversionException(argsToString("unexpected \"", c, '\"'));
182  }
183  }
184  auto delta(TimeSpan::fromMinutes(*deltaHourIndex * 60 + values[8]));
185  if (deltaNegative) {
186  delta = TimeSpan(-delta.totalTicks());
187  }
188  return make_pair(DateTime::fromDateAndTime(values[0], values[1], *dayIndex, *hourIndex, values[4], *secondsIndex, miliSeconds), delta);
189 }
190 
196 string DateTime::toString(DateTimeOutputFormat format, bool noMilliseconds) const
197 {
198  string result;
199  toString(result, format, noMilliseconds);
200  return result;
201 }
202 
208 void DateTime::toString(string &result, DateTimeOutputFormat format, bool noMilliseconds) const
209 {
210  stringstream s(stringstream::in | stringstream::out);
211  s << setfill('0');
212  if (format == DateTimeOutputFormat::DateTimeAndWeekday || format == DateTimeOutputFormat::DateTimeAndShortWeekday)
213  s << printDayOfWeek(dayOfWeek(), format == DateTimeOutputFormat::DateTimeAndShortWeekday) << ' ';
214  if (format == DateTimeOutputFormat::DateOnly || format == DateTimeOutputFormat::DateAndTime || format == DateTimeOutputFormat::DateTimeAndWeekday
215  || format == DateTimeOutputFormat::DateTimeAndShortWeekday)
216  s << setw(4) << year() << '-' << setw(2) << month() << '-' << setw(2) << day();
217  if (format == DateTimeOutputFormat::DateAndTime || format == DateTimeOutputFormat::DateTimeAndWeekday
218  || format == DateTimeOutputFormat::DateTimeAndShortWeekday)
219  s << " ";
220  if (format == DateTimeOutputFormat::TimeOnly || format == DateTimeOutputFormat::DateAndTime || format == DateTimeOutputFormat::DateTimeAndWeekday
221  || format == DateTimeOutputFormat::DateTimeAndShortWeekday) {
222  s << setw(2) << hour() << ':' << setw(2) << minute() << ':' << setw(2) << second();
223  int ms = millisecond();
224  if (!noMilliseconds && ms > 0) {
225  s << '.' << setw(3) << ms;
226  }
227  }
228  result = s.str();
229 }
230 
235 string DateTime::toIsoString(TimeSpan timeZoneDelta) const
236 {
237  stringstream s(stringstream::in | stringstream::out);
238  s << setfill('0');
239  s << setw(4) << year() << '-' << setw(2) << month() << '-' << setw(2) << day() << 'T' << setw(2) << hour() << ':' << setw(2) << minute() << ':'
240  << setw(2) << second();
241  const int milli(millisecond());
242  const int micro(microsecond());
243  const int nano(nanosecond());
244  if (milli || micro || nano) {
245  s << '.' << setw(3) << milli;
246  if (micro || nano) {
247  s << setw(3) << micro;
248  if (nano) {
249  s << nano / TimeSpan::nanosecondsPerTick;
250  }
251  }
252  }
253  if (!timeZoneDelta.isNull()) {
254  if (timeZoneDelta.isNegative()) {
255  s << '-';
256  timeZoneDelta = TimeSpan(-timeZoneDelta.totalTicks());
257  } else {
258  s << '+';
259  }
260  s << setw(2) << timeZoneDelta.hours() << ':' << setw(2) << timeZoneDelta.minutes();
261  }
262  return s.str();
263 }
264 
272 const char *DateTime::printDayOfWeek(DayOfWeek dayOfWeek, bool abbreviation)
273 {
274  if (abbreviation) {
275  switch (dayOfWeek) {
276  case DayOfWeek::Monday:
277  return "Mon";
278  case DayOfWeek::Tuesday:
279  return "Tue";
280  case DayOfWeek::Wednesday:
281  return "Wed";
282  case DayOfWeek::Thursday:
283  return "Thu";
284  case DayOfWeek::Friday:
285  return "Fri";
286  case DayOfWeek::Saturday:
287  return "Sat";
288  case DayOfWeek::Sunday:
289  return "Sun";
290  }
291  } else {
292  switch (dayOfWeek) {
293  case DayOfWeek::Monday:
294  return "Monday";
295  case DayOfWeek::Tuesday:
296  return "Tuesday";
297  case DayOfWeek::Wednesday:
298  return "Wednesday";
299  case DayOfWeek::Thursday:
300  return "Thursday";
301  case DayOfWeek::Friday:
302  return "Friday";
303  case DayOfWeek::Saturday:
304  return "Saturday";
305  case DayOfWeek::Sunday:
306  return "Sunday";
307  }
308  }
309  return "";
310 }
311 
312 #if defined(PLATFORM_UNIX) && !defined(PLATFORM_MAC)
313 
317 DateTime DateTime::exactGmtNow()
318 {
319  struct timespec t;
320  clock_gettime(CLOCK_REALTIME, &t);
321  return DateTime(
322  DateTime::unixEpochStart().totalTicks() + static_cast<uint64>(t.tv_sec) * TimeSpan::m_ticksPerSecond + static_cast<uint64>(t.tv_nsec) / 100);
323 }
324 #endif
325 
329 uint64 DateTime::dateToTicks(int year, int month, int day)
330 {
331  if (!inRangeInclMax(year, 1, 9999)) {
332  throw ConversionException("year is out of range");
333  }
334  if (!inRangeInclMax(month, 1, 12)) {
335  throw ConversionException("month is out of range");
336  }
337  const auto *const daysToMonth = reinterpret_cast<const int *>(isLeapYear(year) ? m_daysToMonth366 : m_daysToMonth365);
338  const int passedMonth = month - 1;
339  if (!inRangeInclMax(day, 1, daysToMonth[month] - daysToMonth[passedMonth])) {
340  throw ConversionException("day is out of range");
341  }
342  const auto passedYears = static_cast<unsigned int>(year - 1);
343  const auto passedDays = static_cast<unsigned int>(day - 1);
344  return (passedYears * m_daysPerYear + passedYears / 4 - passedYears / 100 + passedYears / 400
345  + static_cast<unsigned int>(daysToMonth[passedMonth]) + passedDays)
346  * TimeSpan::m_ticksPerDay;
347 }
348 
352 uint64 DateTime::timeToTicks(int hour, int minute, int second, double millisecond)
353 {
354  if (!inRangeExclMax(hour, 0, 24)) {
355  throw ConversionException("hour is out of range");
356  }
357  if (!inRangeExclMax(minute, 0, 60)) {
358  throw ConversionException("minute is out of range");
359  }
360  if (!inRangeExclMax(second, 0, 60)) {
361  throw ConversionException("second is out of range");
362  }
363  if (!inRangeExclMax(millisecond, 0.0, 1000.0)) {
364  throw ConversionException("millisecond is out of range");
365  }
366  return static_cast<uint64>(hour * TimeSpan::m_ticksPerHour) + static_cast<uint64>(minute * TimeSpan::m_ticksPerMinute)
367  + static_cast<uint64>(second * TimeSpan::m_ticksPerSecond) + static_cast<uint64>(millisecond * TimeSpan::m_ticksPerMillisecond);
368 }
369 
374 int DateTime::getDatePart(DatePart part) const
375 {
376  const int fullDays = m_ticks / TimeSpan::m_ticksPerDay;
377  const int full400YearBlocks = fullDays / m_daysPer400Years;
378  const int daysMinusFull400YearBlocks = fullDays - full400YearBlocks * m_daysPer400Years;
379  int full100YearBlocks = daysMinusFull400YearBlocks / m_daysPer100Years;
380  if (full100YearBlocks == 4) {
381  full100YearBlocks = 3;
382  }
383  const int daysMinusFull100YearBlocks = daysMinusFull400YearBlocks - full100YearBlocks * m_daysPer100Years;
384  const int full4YearBlocks = daysMinusFull100YearBlocks / m_daysPer4Years;
385  const int daysMinusFull4YearBlocks = daysMinusFull100YearBlocks - full4YearBlocks * m_daysPer4Years;
386  int full1YearBlocks = daysMinusFull4YearBlocks / m_daysPerYear;
387  if (full1YearBlocks == 4) {
388  full1YearBlocks = 3;
389  }
390  if (part == DatePart::Year) {
391  return full400YearBlocks * 400 + full100YearBlocks * 100 + full4YearBlocks * 4 + full1YearBlocks + 1;
392  }
393  const int restDays = daysMinusFull4YearBlocks - full1YearBlocks * m_daysPerYear;
394  if (part == DatePart::DayOfYear) { // day
395  return restDays + 1;
396  }
397  const int *const daysToMonth = (full1YearBlocks == 3 && (full4YearBlocks != 24 || full100YearBlocks == 3)) ? m_daysToMonth366 : m_daysToMonth365;
398  int month = 1;
399  while (restDays >= daysToMonth[month]) {
400  ++month;
401  }
402  if (part == DatePart::Month) {
403  return month;
404  } else if (part == DatePart::Day) {
405  return restDays - daysToMonth[month - 1] + 1;
406  }
407  return 0;
408 }
constexpr bool isNegative() const
Returns ture if the time interval represented by the current TimeSpan class is negative.
Definition: timespan.h:391
constexpr bool inRangeExclMax(num1 val, num2 min, num3 max)
Definition: datetime.cpp:31
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 int64 totalTicks() const
Returns the number of ticks that represent the value of the current TimeSpan class.
Definition: timespan.h:187
The ConversionException class is thrown by the various conversion functions of this library when a co...
Represents a time interval.
Definition: timespan.h:28
constexpr int hours() const
Returns the hours component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:285
constexpr StringType argsToString(Args &&... args)
STL namespace.
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
constexpr bool isNull() const
Returns ture if the time interval represented by the current TimeSpan class is null.
Definition: timespan.h:383
constexpr T max(T first, T second)
Returns the greatest of the given items.
Definition: math.h:29
constexpr int minutes() const
Returns the minutes component of the time interval represented by the current TimeSpan class...
Definition: timespan.h:277
DatePart
Specifies the date part.
Definition: datetime.h:45
Contains several functions providing conversions between different data types.
DateTimeOutputFormat
Specifies the output format.
Definition: datetime.h:18
constexpr bool inRangeInclMax(num1 val, num2 min, num3 max)
Definition: datetime.cpp:26
constexpr T min(T first, T second)
Returns the smallest of the given items.
Definition: math.h:17
DayOfWeek
Specifies the day of the week.
Definition: datetime.h:30