C++ Utilities: chrono/datetime.cpp Source File

C++ Utilities 5.34.2 Useful C++ classes and routines such as argument parser, IO and conversion utilities

Loading...
Searching...
No Matches
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
9#if !defined(CPP_UTILITIES_CHRONO_BASED_EXACT_TIME) && (defined(PLATFORM_MAC) || !defined(PLATFORM_UNIX))
10#define CPP_UTILITIES_CHRONO_BASED_EXACT_TIME
11#endif
12#ifdef CPP_UTILITIES_CHRONO_BASED_EXACT_TIME
13#include <chrono>
14#endif
15
16using namespace std;
17
18namespace CppUtilities {
19
20const int DateTime::m_daysPerYear = 365;
21const int DateTime::m_daysPer4Years = 1461;
22const int DateTime::m_daysPer100Years = 36524;
23const int DateTime::m_daysPer400Years = 146097;
24const int DateTime::m_daysTo1601 = 584388;
25const int DateTime::m_daysTo1899 = 693593;
26const int DateTime::m_daysTo10000 = 3652059;
27const int DateTime::m_daysToMonth365[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
28const int DateTime::m_daysToMonth366[13] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
29const int DateTime::m_daysInMonth365[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
30const int DateTime::m_daysInMonth366[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
31
32 template <typename num1, typename num2, typename num3> constexpr bool inRangeInclMax(num1 val, num2 min, num3 max)
33{
34 return (val) >= (min) && (val) <= (max);
35}
36
37 template <typename num1, typename num2, typename num3> constexpr bool inRangeExclMax(num1 val, num2 min, num3 max)
38{
39 return (val) >= (min) && (val) < (max);
40}
41
60
72
76 DateTime DateTime::fromTimeStamp(time_t timeStamp)
77{
78 if (timeStamp) {
79 struct tm *const timeinfo = localtime(&timeStamp);
80 return DateTime::fromDateAndTime(timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min,
81 timeinfo->tm_sec < 60 ? timeinfo->tm_sec : 59, 0);
82 } else {
83 return DateTime();
84 }
85}
86
96 DateTime DateTime::fromString(const char *str)
97{
98 return DateTimeExpression::fromString(str).value;
99}
100
112std::pair<DateTime, TimeSpan> DateTime::fromIsoString(const char *str)
113{
114 const auto expr = DateTimeExpression::fromIsoString(str);
115 return std::make_pair(expr.value, expr.delta);
116}
117
123 void DateTime::toString(string &result, DateTimeOutputFormat format, bool noMilliseconds) const
124{
125 if (format == DateTimeOutputFormat::Iso) {
126 result = toIsoString();
127 return;
128 }
129
130 stringstream s(stringstream::in | stringstream::out);
131 s << setfill('0');
132
133 if (format == DateTimeOutputFormat::IsoOmittingDefaultComponents) {
134 constexpr auto dateDelimiter = '-', timeDelimiter = ':';
135 const int components[] = { year(), month(), day(), hour(), minute(), second(), millisecond(), microsecond(), nanosecond() };
136 const int *const firstTimeComponent = components + 3;
137 const int *const firstFractionalComponent = components + 6;
138 const int *const lastComponent = components + 8;
139 const int *componentsEnd = noMilliseconds ? firstFractionalComponent : lastComponent + 1;
140 for (const int *i = componentsEnd - 1; i > components; --i) {
141 if (i >= firstTimeComponent && *i == 0) {
142 componentsEnd = i;
143 } else if (i < firstTimeComponent && *i == 1) {
144 componentsEnd = i;
145 }
146 }
147 for (const int *i = components; i != componentsEnd; ++i) {
148 if (i == firstTimeComponent) {
149 s << 'T';
150 } else if (i == firstFractionalComponent) {
151 s << '.';
152 }
153 if (i == components) {
154 s << setw(4) << *i;
155 } else if (i < firstFractionalComponent) {
156 if (i < firstTimeComponent) {
157 s << dateDelimiter;
158 } else if (i > firstTimeComponent) {
159 s << timeDelimiter;
160 }
161 s << setw(2) << *i;
162 } else if (i < lastComponent) {
163 s << setw(3) << *i;
164 } else {
165 s << *i / TimeSpan::nanosecondsPerTick;
166 }
167 }
168 result = s.str();
169 return;
170 }
171
172 if (format == DateTimeOutputFormat::DateTimeAndWeekday || format == DateTimeOutputFormat::DateTimeAndShortWeekday)
173 s << printDayOfWeek(dayOfWeek(), format == DateTimeOutputFormat::DateTimeAndShortWeekday) << ' ';
174 if (format == DateTimeOutputFormat::DateOnly || format == DateTimeOutputFormat::DateAndTime || format == DateTimeOutputFormat::DateTimeAndWeekday
175 || format == DateTimeOutputFormat::DateTimeAndShortWeekday)
176 s << setw(4) << year() << '-' << setw(2) << month() << '-' << setw(2) << day();
177 if (format == DateTimeOutputFormat::DateAndTime || format == DateTimeOutputFormat::DateTimeAndWeekday
178 || format == DateTimeOutputFormat::DateTimeAndShortWeekday)
179 s << " ";
180 if (format == DateTimeOutputFormat::TimeOnly || format == DateTimeOutputFormat::DateAndTime || format == DateTimeOutputFormat::DateTimeAndWeekday
181 || format == DateTimeOutputFormat::DateTimeAndShortWeekday) {
182 s << setw(2) << hour() << ':' << setw(2) << minute() << ':' << setw(2) << second();
183 int ms = millisecond();
184 if (!noMilliseconds && ms > 0) {
185 s << '.' << setw(3) << ms;
186 }
187 }
188 result = s.str();
189}
190
195 string DateTime::toIsoStringWithCustomDelimiters(TimeSpan timeZoneDelta, char dateDelimiter, char timeDelimiter, char timeZoneDelimiter) const
196{
197 stringstream s(stringstream::in | stringstream::out);
198 s << setfill('0');
199 s << setw(4) << year() << dateDelimiter << setw(2) << month() << dateDelimiter << setw(2) << day() << 'T' << setw(2) << hour() << timeDelimiter
200 << setw(2) << minute() << timeDelimiter << setw(2) << second();
201 const int milli(millisecond());
202 const int micro(microsecond());
203 const int nano(nanosecond());
204 if (milli || micro || nano) {
205 s << '.' << setw(3) << milli;
206 if (micro || nano) {
207 s << setw(3) << micro;
208 if (nano) {
209 s << nano / TimeSpan::nanosecondsPerTick;
210 }
211 }
212 }
213 if (!timeZoneDelta.isNull()) {
214 if (timeZoneDelta.isNegative()) {
215 s << '-';
216 timeZoneDelta = TimeSpan(-timeZoneDelta.totalTicks());
217 } else {
218 s << '+';
219 }
220 s << setw(2) << timeZoneDelta.hours() << timeZoneDelimiter << setw(2) << timeZoneDelta.minutes();
221 }
222 return s.str();
223}
224
229 string DateTime::toIsoString(TimeSpan timeZoneDelta) const
230{
231 return toIsoStringWithCustomDelimiters(timeZoneDelta);
232}
233
241 const char *DateTime::printDayOfWeek(DayOfWeek dayOfWeek, bool abbreviation)
242{
243 if (abbreviation) {
244 switch (dayOfWeek) {
245 case DayOfWeek::Monday:
246 return "Mon";
247 case DayOfWeek::Tuesday:
248 return "Tue";
249 case DayOfWeek::Wednesday:
250 return "Wed";
251 case DayOfWeek::Thursday:
252 return "Thu";
253 case DayOfWeek::Friday:
254 return "Fri";
255 case DayOfWeek::Saturday:
256 return "Sat";
257 case DayOfWeek::Sunday:
258 return "Sun";
259 }
260 } else {
261 switch (dayOfWeek) {
262 case DayOfWeek::Monday:
263 return "Monday";
264 case DayOfWeek::Tuesday:
265 return "Tuesday";
266 case DayOfWeek::Wednesday:
267 return "Wednesday";
268 case DayOfWeek::Thursday:
269 return "Thursday";
270 case DayOfWeek::Friday:
271 return "Friday";
272 case DayOfWeek::Saturday:
273 return "Saturday";
274 case DayOfWeek::Sunday:
275 return "Sunday";
276 }
277 }
278 return "";
279}
280
284 DateTime DateTime::exactGmtNow()
285{
286#ifdef CPP_UTILITIES_CHRONO_BASED_EXACT_TIME
287 return DateTime(DateTime::unixEpochStart().totalTicks()
288 + static_cast<std::uint64_t>(
289 std::chrono::time_point_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now()).time_since_epoch().count())
290 / 100);
291#else
292 struct timespec t;
293 clock_gettime(CLOCK_REALTIME, &t);
294 return DateTime(DateTime::unixEpochStart().totalTicks() + static_cast<std::uint64_t>(t.tv_sec) * TimeSpan::ticksPerSecond
295 + static_cast<std::uint64_t>(t.tv_nsec) / 100);
296#endif
297}
298
302 DateTime::TickType DateTime::dateToTicks(int year, int month, int day)
303{
304 if (!inRangeInclMax(year, 1, 9999)) {
305 throw ConversionException("year is out of range");
306 }
307 if (!inRangeInclMax(month, 1, 12)) {
308 throw ConversionException("month is out of range");
309 }
310 const auto *const daysToMonth = reinterpret_cast<const int *>(isLeapYear(year) ? m_daysToMonth366 : m_daysToMonth365);
311 const int passedMonth = month - 1;
312 if (!inRangeInclMax(day, 1, daysToMonth[month] - daysToMonth[passedMonth])) {
313 throw ConversionException("day is out of range");
314 }
315 const auto passedYears = static_cast<unsigned int>(year - 1);
316 const auto passedDays = static_cast<unsigned int>(day - 1);
317 return (passedYears * m_daysPerYear + passedYears / 4 - passedYears / 100 + passedYears / 400
318 + static_cast<unsigned int>(daysToMonth[passedMonth]) + passedDays)
319 * TimeSpan::ticksPerDay;
320}
321
325 DateTime::TickType DateTime::timeToTicks(int hour, int minute, int second, double millisecond)
326{
327 if (!inRangeExclMax(hour, 0, 24)) {
328 throw ConversionException("hour is out of range");
329 }
330 if (!inRangeExclMax(minute, 0, 60)) {
331 throw ConversionException("minute is out of range");
332 }
333 if (!inRangeExclMax(second, 0, 60)) {
334 throw ConversionException("second is out of range");
335 }
336 if (!inRangeExclMax(millisecond, 0.0, 1000.0)) {
337 throw ConversionException("millisecond is out of range");
338 }
339 return static_cast<std::uint64_t>(hour * TimeSpan::ticksPerHour) + static_cast<std::uint64_t>(minute * TimeSpan::ticksPerMinute)
340 + static_cast<std::uint64_t>(second * TimeSpan::ticksPerSecond) + static_cast<std::uint64_t>(millisecond * TimeSpan::ticksPerMillisecond);
341}
342
347int DateTime::getDatePart(DatePart part) const
348{
349 const auto fullDays = static_cast<int>(m_ticks / TimeSpan::ticksPerDay);
350 const auto full400YearBlocks = fullDays / m_daysPer400Years;
351 const auto daysMinusFull400YearBlocks = fullDays - full400YearBlocks * m_daysPer400Years;
352 auto full100YearBlocks = daysMinusFull400YearBlocks / m_daysPer100Years;
353 if (full100YearBlocks == 4) {
354 full100YearBlocks = 3;
355 }
356 const auto daysMinusFull100YearBlocks = daysMinusFull400YearBlocks - full100YearBlocks * m_daysPer100Years;
357 const auto full4YearBlocks = daysMinusFull100YearBlocks / m_daysPer4Years;
358 const auto daysMinusFull4YearBlocks = daysMinusFull100YearBlocks - full4YearBlocks * m_daysPer4Years;
359 auto full1YearBlocks = daysMinusFull4YearBlocks / m_daysPerYear;
360 if (full1YearBlocks == 4) {
361 full1YearBlocks = 3;
362 }
363 if (part == DatePart::Year) {
364 return full400YearBlocks * 400 + full100YearBlocks * 100 + full4YearBlocks * 4 + full1YearBlocks + 1;
365 }
366 const auto restDays = daysMinusFull4YearBlocks - full1YearBlocks * m_daysPerYear;
367 if (part == DatePart::DayOfYear) { // day
368 return restDays + 1;
369 }
370 const auto *const daysToMonth = (full1YearBlocks == 3 && (full4YearBlocks != 24 || full100YearBlocks == 3)) ? m_daysToMonth366 : m_daysToMonth365;
371 auto month = 1;
372 while (restDays >= daysToMonth[month]) {
373 ++month;
374 }
375 if (part == DatePart::Month) {
376 return month;
377 } else if (part == DatePart::Day) {
378 return restDays - daysToMonth[month - 1] + 1;
379 }
380 return 0;
381}
382
384static DateTimeParts dateTimePartsFromParsingDistance(const int *valueIndex, const int *values)
385{
386 return static_cast<DateTimeParts>((1 << (valueIndex - values + 1)) - 1);
387}
389
399 DateTimeExpression DateTimeExpression::fromIsoString(const char *str)
400{
401 auto res = DateTimeExpression();
402 int values[9] = { 0 };
403 int *const yearIndex = values + 0;
404 int *const monthIndex = values + 1;
405 int *const dayIndex = values + 2;
406 int *const hourIndex = values + 3;
407 int *const secondsIndex = values + 5;
408 int *const miliSecondsIndex = values + 6;
409 int *const deltaHourIndex = values + 7;
410 int *const valuesEnd = values + 9;
411 int *valueIndex = values;
412 unsigned int remainingDigits = 4;
413 bool deltaNegative = false;
414 double millisecondsFact = 100.0, milliseconds = 0.0;
415 for (const char *strIndex = str;; ++strIndex) {
416 const char c = *strIndex;
417 if (c <= '9' && c >= '0') {
418 if (valueIndex == miliSecondsIndex) {
419 milliseconds += (c - '0') * millisecondsFact;
420 millisecondsFact /= 10;
421 } else {
422 if (!remainingDigits) {
423 if (++valueIndex == miliSecondsIndex || valueIndex >= valuesEnd) {
424 throw ConversionException("Max. number of digits exceeded");
425 }
426 remainingDigits = 2;
427 }
428 *valueIndex *= 10;
429 *valueIndex += c - '0';
430 remainingDigits -= 1;
431 }
432 } else if (c == 'T') {
433 if (++valueIndex != hourIndex) {
434 throw ConversionException("\"T\" expected before hour");
435 }
436 remainingDigits = 2;
437 } else if (c == '-') {
438 if (valueIndex < dayIndex) {
439 ++valueIndex;
440 } else if (++valueIndex >= secondsIndex) {
441 valueIndex = deltaHourIndex;
442 deltaNegative = true;
443 } else {
444 throw ConversionException("Unexpected \"-\" after day");
445 }
446 remainingDigits = 2;
447 } else if (c == '.') {
448 if (valueIndex != secondsIndex) {
449 throw ConversionException("Unexpected \".\"");
450 } else {
451 ++valueIndex;
452 }
453 } else if (c == ':') {
454 if (valueIndex < hourIndex) {
455 throw ConversionException("Unexpected \":\" before hour");
456 } else if (valueIndex == secondsIndex) {
457 throw ConversionException("Unexpected \":\" after second");
458 } else {
459 ++valueIndex;
460 }
461 remainingDigits = 2;
462 } else if ((c == '+') && (++valueIndex >= secondsIndex)) {
463 valueIndex = deltaHourIndex;
464 deltaNegative = false;
465 remainingDigits = 2;
466 } else if ((c == 'Z') && (++valueIndex >= secondsIndex)) {
467 valueIndex = deltaHourIndex + 2;
468 remainingDigits = 2;
469 } else if (c == '\0') {
470 break;
471 } else {
472 throw ConversionException(argsToString("Unexpected \"", c, '\"'));
473 }
474 }
475 res.delta = TimeSpan::fromMinutes(*deltaHourIndex * 60 + values[8]);
476 if (deltaNegative) {
477 res.delta = TimeSpan(-res.delta.totalTicks());
478 }
479 if (valueIndex < monthIndex && !*monthIndex) {
480 *monthIndex = 1;
481 }
482 if (valueIndex < dayIndex && !*dayIndex) {
483 *dayIndex = 1;
484 }
485 res.value = DateTime::fromDateAndTime(*yearIndex, *monthIndex, *dayIndex, *hourIndex, values[4], *secondsIndex, milliseconds);
486 res.parts = dateTimePartsFromParsingDistance(valueIndex, values);
487 return res;
488}
489
499 DateTimeExpression DateTimeExpression::fromString(const char *str)
500{
501 auto res = DateTimeExpression();
502 int values[7] = { 0 };
503 int *const monthIndex = values + 1;
504 int *const dayIndex = values + 2;
505 int *const secondsIndex = values + 5;
506 int *valueIndex = values;
507 int *const valuesEnd = values + 7;
508 double millisecondsFact = 100.0, milliseconds = 0.0;
509 for (const char *strIndex = str;; ++strIndex) {
510 const char c = *strIndex;
511 if (c <= '9' && c >= '0') {
512 if (valueIndex > secondsIndex) {
513 milliseconds += (c - '0') * millisecondsFact;
514 millisecondsFact /= 10;
515 } else {
516 Detail::raiseAndAdd(*valueIndex, 10, c);
517 }
518 } else if ((c == '-' || c == ':' || c == '/') || (c == '.' && (valueIndex == secondsIndex))
519 || ((c == ' ' || c == 'T') && (valueIndex == dayIndex))) {
520 if (++valueIndex == valuesEnd) {
521 break; // just ignore further values for now
522 }
523 } else if (c == '\0') {
524 break;
525 } else {
526 throw ConversionException(argsToString("Unexpected character \"", c, '\"'));
527 }
528 }
529 if (valueIndex < monthIndex && !*monthIndex) {
530 *monthIndex = 1;
531 }
532 if (valueIndex < dayIndex && !*dayIndex) {
533 *dayIndex = 1;
534 }
535 res.value = DateTime::fromDateAndTime(values[0], values[1], *dayIndex, values[3], values[4], *secondsIndex, milliseconds);
536 res.parts = dateTimePartsFromParsingDistance(valueIndex, values);
537 return res;
538}
539
544std::string DateTimeExpression::toIsoString(char dateDelimiter, char timeDelimiter, char timeZoneDelimiter) const
545{
546 auto s = std::stringstream(std::stringstream::in | std::stringstream::out);
547 s << setfill('0');
548 if (parts && DateTimeParts::Year) {
549 s << setw(4) << value.year();
550 }
551 if (parts && DateTimeParts::Month) {
552 if (s.tellp()) {
553 s << dateDelimiter;
554 }
555 s << setw(2) << value.month();
556 }
557 if (parts && DateTimeParts::Day) {
558 if (s.tellp()) {
559 s << dateDelimiter;
560 }
561 s << setw(2) << value.day();
562 }
563 if (parts && DateTimeParts::Hour) {
564 if (s.tellp()) {
565 s << 'T';
566 }
567 s << setw(2) << value.hour();
568 }
569 if (parts && DateTimeParts::Minute) {
570 if (s.tellp()) {
571 s << timeDelimiter;
572 }
573 s << setw(2) << value.minute();
574 }
575 if (parts && DateTimeParts::Second) {
576 if (s.tellp()) {
577 s << timeDelimiter;
578 }
579 s << setw(2) << value.second();
580 }
581 if (parts && DateTimeParts::SubSecond) {
582 const auto milli = value.millisecond();
583 const auto micro = value.microsecond();
584 const auto nano = value.nanosecond();
585 s << '.' << setw(3) << milli;
586 if (micro || nano) {
587 s << setw(3) << micro;
588 if (nano) {
589 s << nano / TimeSpan::nanosecondsPerTick;
590 }
591 }
592 }
593 if (parts && DateTimeParts::TimeZoneDelta) {
594 auto d = delta;
595 if (d.isNegative()) {
596 s << '-';
597 d = TimeSpan(-d.totalTicks());
598 } else {
599 s << '+';
600 }
601 if (parts && DateTimeParts::DeltaHour) {
602 s << setw(2) << d.hours();
603 }
604 if (parts && DateTimeParts::DeltaMinute) {
605 if (parts && DateTimeParts::DeltaHour) {
606 s << timeZoneDelimiter;
607 }
608 s << setw(2) << d.minutes();
609 }
610 }
611 return s.str();
612}
613
614} // namespace CppUtilities
CppUtilities::ConversionException
The ConversionException class is thrown by the various conversion functions of this library when a co...
Definition conversionexception.h:11
CppUtilities::DateTime::toString
std::string toString(DateTimeOutputFormat format=DateTimeOutputFormat::DateAndTime, bool noMilliseconds=false) const
Returns the string representation of the current instance using the specified format.
Definition datetime.h:466
CppUtilities::DateTime::day
int day() const
Returns the day component of the date represented by this instance.
Definition datetime.h:332
CppUtilities::DateTime::dayOfWeek
constexpr DayOfWeek dayOfWeek() const
Returns the day of the week represented by this instance.
Definition datetime.h:349
CppUtilities::DateTime::toIsoStringWithCustomDelimiters
std::string toIsoStringWithCustomDelimiters(TimeSpan timeZoneDelta=TimeSpan(), char dateDelimiter='-', char timeDelimiter=':', char timeZoneDelimiter=':') const
Returns the string representation of the current instance in the ISO format with custom delimiters,...
Definition datetime.cpp:195
CppUtilities::DateTime::isLeapYear
bool isLeapYear() const
Returns an indication whether the year represented by this instance is a leap year.
Definition datetime.h:424
CppUtilities::DateTime::month
int month() const
Returns the month component of the date represented by this instance.
Definition datetime.h:324
CppUtilities::DateTime::DateTime
constexpr DateTime()
Constructs a DateTime.
Definition datetime.h:192
CppUtilities::DateTime::toIsoString
std::string toIsoString(TimeSpan timeZoneDelta=TimeSpan()) const
Returns the string representation of the current instance in the ISO format, eg.
Definition datetime.cpp:229
CppUtilities::DateTime::unixEpochStart
static constexpr DateTime unixEpochStart()
Returns the DateTime object for the "1970-01-01T00:00:00Z".
Definition datetime.h:492
CppUtilities::DateTime::TickType
std::uint64_t TickType
Definition datetime.h:57
CppUtilities::DateTime::microsecond
constexpr int microsecond() const
Returns the microsecond component of the date represented by this instance.
Definition datetime.h:389
CppUtilities::DateTime::fromIsoString
static std::pair< DateTime, TimeSpan > fromIsoString(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition datetime.cpp:112
CppUtilities::DateTime::exactGmtNow
static DateTime exactGmtNow()
Returns a DateTime object that is set to the current date and time on this computer,...
Definition datetime.cpp:284
CppUtilities::DateTime::hour
constexpr int hour() const
Returns the hour component of the date represented by this instance.
Definition datetime.h:357
CppUtilities::DateTime::fromString
static DateTime fromString(const std::string &str)
Parses the given std::string as DateTime.
Definition datetime.h:242
CppUtilities::DateTime::second
constexpr int second() const
Returns the second component of the date represented by this instance.
Definition datetime.h:373
CppUtilities::DateTime::fromDateAndTime
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:228
CppUtilities::DateTime::totalTicks
constexpr TickType totalTicks() const
Returns the number of ticks which represent the value of the current instance.
Definition datetime.h:308
CppUtilities::DateTime::fromTimeStamp
static DateTime fromTimeStamp(std::time_t timeStamp)
Constructs a new DateTime object with the local time from the specified UNIX timeStamp.
Definition datetime.cpp:76
CppUtilities::DateTime::millisecond
constexpr int millisecond() const
Returns the millisecond component of the date represented by this instance.
Definition datetime.h:381
CppUtilities::DateTime::printDayOfWeek
static const char * printDayOfWeek(DayOfWeek dayOfWeek, bool abbreviation=false)
Returns the string representation as C-style string for the given day of week.
Definition datetime.cpp:241
CppUtilities::DateTime::nanosecond
constexpr int nanosecond() const
Returns the nanosecond component of the date represented by this instance.
Definition datetime.h:399
CppUtilities::DateTime::minute
constexpr int minute() const
Returns the minute component of the date represented by this instance.
Definition datetime.h:365
CppUtilities::DateTime::year
int year() const
Returns the year component of the date represented by this instance.
Definition datetime.h:316
CppUtilities::TimeSpan
Represents a time interval.
Definition timespan.h:25
CppUtilities::TimeSpan::isNull
constexpr bool isNull() const
Returns true if the time interval represented by the current TimeSpan class is null.
Definition timespan.h:538
CppUtilities::TimeSpan::nanosecondsPerTick
static constexpr TickType nanosecondsPerTick
Definition timespan.h:99
CppUtilities::TimeSpan::ticksPerMillisecond
static constexpr TickType ticksPerMillisecond
Definition timespan.h:101
CppUtilities::TimeSpan::minutes
constexpr int minutes() const
Returns the minutes component of the time interval represented by the current TimeSpan class.
Definition timespan.h:339
CppUtilities::TimeSpan::totalTicks
constexpr TickType totalTicks() const
Returns the number of ticks that represent the value of the current TimeSpan class.
Definition timespan.h:249
CppUtilities::TimeSpan::ticksPerMinute
static constexpr TickType ticksPerMinute
Definition timespan.h:103
CppUtilities::TimeSpan::fromMinutes
static constexpr TimeSpan fromMinutes(double minutes)
Constructs a new instance of the TimeSpan class with the specified number of minutes.
Definition timespan.h:146
CppUtilities::TimeSpan::ticksPerSecond
static constexpr TickType ticksPerSecond
Definition timespan.h:102
CppUtilities::TimeSpan::ticksPerDay
static constexpr TickType ticksPerDay
Definition timespan.h:105
CppUtilities::TimeSpan::ticksPerHour
static constexpr TickType ticksPerHour
Definition timespan.h:104
CppUtilities::TimeSpan::isNegative
constexpr bool isNegative() const
Returns true if the time interval represented by the current TimeSpan class is negative.
Definition timespan.h:546
CppUtilities::TimeSpan::hours
constexpr int hours() const
Returns the hours component of the time interval represented by the current TimeSpan class.
Definition timespan.h:347
datetime.h
CppUtilities
Contains all utilities provided by the c++utilities library.
Definition argumentparser.h:18
CppUtilities::DatePart
DatePart
Specifies the date part.
Definition datetime.h:48
CppUtilities::DatePart::Day
@ Day
Definition datetime.h:52
CppUtilities::DatePart::DayOfYear
@ DayOfYear
Definition datetime.h:51
CppUtilities::DatePart::Year
@ Year
Definition datetime.h:49
CppUtilities::DatePart::Month
@ Month
Definition datetime.h:50
CppUtilities::inRangeExclMax
constexpr bool inRangeExclMax(num1 val, num2 min, num3 max)
Definition datetime.cpp:37
CppUtilities::inRangeInclMax
constexpr bool inRangeInclMax(num1 val, num2 min, num3 max)
Definition datetime.cpp:32
CppUtilities::argsToString
StringType argsToString(Args &&...args)
Definition stringbuilder.h:260
CppUtilities::max
constexpr T max(T first, T second)
Returns the greatest of the given items.
Definition math.h:96
CppUtilities::DateTimeParts
DateTimeParts
The DateTimeParts enum specifies which parts of a timestamp are present.
Definition datetime.h:143
CppUtilities::DateTimeParts::Day
@ Day
Definition datetime.h:147
CppUtilities::DateTimeParts::SubSecond
@ SubSecond
Definition datetime.h:151
CppUtilities::DateTimeParts::TimeZoneDelta
@ TimeZoneDelta
Definition datetime.h:157
CppUtilities::DateTimeParts::Year
@ Year
Definition datetime.h:145
CppUtilities::DateTimeParts::Minute
@ Minute
Definition datetime.h:149
CppUtilities::DateTimeParts::Month
@ Month
Definition datetime.h:146
CppUtilities::DateTimeParts::DeltaMinute
@ DeltaMinute
Definition datetime.h:153
CppUtilities::DateTimeParts::Hour
@ Hour
Definition datetime.h:148
CppUtilities::DateTimeParts::Second
@ Second
Definition datetime.h:150
CppUtilities::DateTimeParts::DeltaHour
@ DeltaHour
Definition datetime.h:152
CppUtilities::DateTimeOutputFormat
DateTimeOutputFormat
Specifies the output format.
Definition datetime.h:19
CppUtilities::DateTimeOutputFormat::DateTimeAndShortWeekday
@ DateTimeAndShortWeekday
Definition datetime.h:24
CppUtilities::DateTimeOutputFormat::TimeOnly
@ TimeOnly
Definition datetime.h:22
CppUtilities::DateTimeOutputFormat::Iso
@ Iso
Definition datetime.h:25
CppUtilities::DateTimeOutputFormat::DateAndTime
@ DateAndTime
Definition datetime.h:20
CppUtilities::DateTimeOutputFormat::IsoOmittingDefaultComponents
@ IsoOmittingDefaultComponents
Definition datetime.h:26
CppUtilities::DateTimeOutputFormat::DateTimeAndWeekday
@ DateTimeAndWeekday
Definition datetime.h:23
CppUtilities::DateTimeOutputFormat::DateOnly
@ DateOnly
Definition datetime.h:21
CppUtilities::min
constexpr T min(T first, T second)
Returns the smallest of the given items.
Definition math.h:84
CppUtilities::DayOfWeek
DayOfWeek
Specifies the day of the week.
Definition datetime.h:33
CppUtilities::DayOfWeek::Tuesday
@ Tuesday
Definition datetime.h:35
CppUtilities::DayOfWeek::Monday
@ Monday
Definition datetime.h:34
CppUtilities::DayOfWeek::Thursday
@ Thursday
Definition datetime.h:37
CppUtilities::DayOfWeek::Wednesday
@ Wednesday
Definition datetime.h:36
CppUtilities::DayOfWeek::Saturday
@ Saturday
Definition datetime.h:39
CppUtilities::DayOfWeek::Sunday
@ Sunday
Definition datetime.h:40
CppUtilities::DayOfWeek::Friday
@ Friday
Definition datetime.h:38
std
STL namespace.
stringbuilder.h
stringconversion.h
CppUtilities::DateTimeExpression
The DateTimeExpression struct holds information about a time expression (e.g.
Definition datetime.h:161
CppUtilities::DateTimeExpression::delta
TimeSpan delta
Definition datetime.h:163
CppUtilities::DateTimeExpression::value
DateTime value
Definition datetime.h:162
CppUtilities::DateTimeExpression::fromString
static DateTimeExpression fromString(const char *str)
Parses the given C-style string.
Definition datetime.cpp:499
CppUtilities::DateTimeExpression::parts
DateTimeParts parts
Definition datetime.h:164
CppUtilities::DateTimeExpression::toIsoString
std::string toIsoString(char dateDelimiter='-', char timeDelimiter=':', char timeZoneDelimiter=':') const
Returns the string representation of the current instance in the ISO format.
Definition datetime.cpp:544
CppUtilities::DateTimeExpression::fromIsoString
static DateTimeExpression fromIsoString(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
Definition datetime.cpp:399
i
constexpr int i
Definition traitstests.cpp:106