3#include "../conversion/stringbuilder.h"
4#include "../conversion/stringconversion.h"
14const int DateTime::m_daysPerYear = 365;
15const int DateTime::m_daysPer4Years = 1461;
16const int DateTime::m_daysPer100Years = 36524;
17const int DateTime::m_daysPer400Years = 146097;
18const int DateTime::m_daysTo1601 = 584388;
19const int DateTime::m_daysTo1899 = 693593;
20const int DateTime::m_daysTo10000 = 3652059;
21const int DateTime::m_daysToMonth365[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
22const int DateTime::m_daysToMonth366[13] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };
23const int DateTime::m_daysInMonth365[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
24const int DateTime::m_daysInMonth366[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
26template <
typename num1,
typename num2,
typename num3>
constexpr bool inRangeInclMax(num1 val, num2
min, num3
max)
28 return (val) >= (
min) && (val) <= (
max);
31template <
typename num1,
typename num2,
typename num3>
constexpr bool inRangeExclMax(num1 val, num2
min, num3
max)
33 return (val) >= (
min) && (val) < (
max);
74 struct tm *
const timeinfo = localtime(&timeStamp);
75 return DateTime::fromDateAndTime(timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min,
76 timeinfo->tm_sec < 60 ? timeinfo->tm_sec : 59, 0);
110 return std::make_pair(expr.value, expr.delta);
125 stringstream s(stringstream::in | stringstream::out);
129 constexpr auto dateDelimiter =
'-', timeDelimiter =
':';
131 const int *
const firstTimeComponent = components + 3;
132 const int *
const firstFractionalComponent = components + 6;
133 const int *
const lastComponent = components + 8;
134 const int *componentsEnd = noMilliseconds ? firstFractionalComponent : lastComponent + 1;
135 for (
const int *
i = componentsEnd - 1;
i > components; --
i) {
136 if (
i >= firstTimeComponent && *
i == 0) {
138 }
else if (
i < firstTimeComponent && *
i == 1) {
142 for (
const int *
i = components;
i != componentsEnd; ++
i) {
143 if (
i == firstTimeComponent) {
145 }
else if (
i == firstFractionalComponent) {
148 if (
i == components) {
150 }
else if (
i < firstFractionalComponent) {
151 if (
i < firstTimeComponent) {
153 }
else if (
i > firstTimeComponent) {
157 }
else if (
i < lastComponent) {
171 s << setw(4) <<
year() <<
'-' << setw(2) <<
month() <<
'-' << setw(2) <<
day();
177 s << setw(2) <<
hour() <<
':' << setw(2) <<
minute() <<
':' << setw(2) <<
second();
179 if (!noMilliseconds && ms > 0) {
180 s <<
'.' << setw(3) << ms;
192 stringstream s(stringstream::in | stringstream::out);
194 s << setw(4) <<
year() << dateDelimiter << setw(2) <<
month() << dateDelimiter << setw(2) <<
day() <<
'T' << setw(2) <<
hour() << timeDelimiter
195 << setw(2) <<
minute() << timeDelimiter << setw(2) <<
second();
199 if (milli || micro || nano) {
200 s <<
'.' << setw(3) << milli;
202 s << setw(3) << micro;
208 if (!timeZoneDelta.
isNull()) {
215 s << setw(2) << timeZoneDelta.
hours() << timeZoneDelimiter << setw(2) << timeZoneDelta.
minutes();
276#if defined(PLATFORM_UNIX) && !defined(PLATFORM_MAC)
284 clock_gettime(CLOCK_REALTIME, &t);
286 +
static_cast<std::uint64_t
>(t.tv_nsec) / 100);
296 throw ConversionException(
"year is out of range");
299 throw ConversionException(
"month is out of range");
301 const auto *
const daysToMonth =
reinterpret_cast<const int *
>(
isLeapYear(
year) ? m_daysToMonth366 : m_daysToMonth365);
302 const int passedMonth =
month - 1;
304 throw ConversionException(
"day is out of range");
306 const auto passedYears =
static_cast<unsigned int>(
year - 1);
307 const auto passedDays =
static_cast<unsigned int>(
day - 1);
308 return (passedYears * m_daysPerYear + passedYears / 4 - passedYears / 100 + passedYears / 400
309 +
static_cast<unsigned int>(daysToMonth[passedMonth]) + passedDays)
316DateTime::TickType DateTime::timeToTicks(
int hour,
int minute,
int second,
double millisecond)
319 throw ConversionException(
"hour is out of range");
322 throw ConversionException(
"minute is out of range");
325 throw ConversionException(
"second is out of range");
328 throw ConversionException(
"millisecond is out of range");
338int DateTime::getDatePart(
DatePart part)
const
341 const auto full400YearBlocks = fullDays / m_daysPer400Years;
342 const auto daysMinusFull400YearBlocks = fullDays - full400YearBlocks * m_daysPer400Years;
343 auto full100YearBlocks = daysMinusFull400YearBlocks / m_daysPer100Years;
344 if (full100YearBlocks == 4) {
345 full100YearBlocks = 3;
347 const auto daysMinusFull100YearBlocks = daysMinusFull400YearBlocks - full100YearBlocks * m_daysPer100Years;
348 const auto full4YearBlocks = daysMinusFull100YearBlocks / m_daysPer4Years;
349 const auto daysMinusFull4YearBlocks = daysMinusFull100YearBlocks - full4YearBlocks * m_daysPer4Years;
350 auto full1YearBlocks = daysMinusFull4YearBlocks / m_daysPerYear;
351 if (full1YearBlocks == 4) {
355 return full400YearBlocks * 400 + full100YearBlocks * 100 + full4YearBlocks * 4 + full1YearBlocks + 1;
357 const auto restDays = daysMinusFull4YearBlocks - full1YearBlocks * m_daysPerYear;
361 const auto *
const daysToMonth = (full1YearBlocks == 3 && (full4YearBlocks != 24 || full100YearBlocks == 3)) ? m_daysToMonth366 : m_daysToMonth365;
363 while (restDays >= daysToMonth[
month]) {
369 return restDays - daysToMonth[
month - 1] + 1;
375static DateTimeParts dateTimePartsFromParsingDistance(
const int *valueIndex,
const int *values)
377 return static_cast<DateTimeParts>((1 << (valueIndex - values + 1)) - 1);
393 int values[9] = { 0 };
394 int *
const yearIndex = values + 0;
395 int *
const monthIndex = values + 1;
396 int *
const dayIndex = values + 2;
397 int *
const hourIndex = values + 3;
398 int *
const secondsIndex = values + 5;
399 int *
const miliSecondsIndex = values + 6;
400 int *
const deltaHourIndex = values + 7;
401 int *
const valuesEnd = values + 9;
402 int *valueIndex = values;
403 unsigned int remainingDigits = 4;
404 bool deltaNegative =
false;
405 double millisecondsFact = 100.0, milliseconds = 0.0;
406 for (
const char *strIndex = str;; ++strIndex) {
407 const char c = *strIndex;
408 if (c <=
'9' && c >=
'0') {
409 if (valueIndex == miliSecondsIndex) {
410 milliseconds += (c -
'0') * millisecondsFact;
411 millisecondsFact /= 10;
413 if (!remainingDigits) {
414 if (++valueIndex == miliSecondsIndex || valueIndex >= valuesEnd) {
420 *valueIndex += c -
'0';
421 remainingDigits -= 1;
423 }
else if (c ==
'T') {
424 if (++valueIndex != hourIndex) {
428 }
else if (c ==
'-') {
429 if (valueIndex < dayIndex) {
431 }
else if (++valueIndex >= secondsIndex) {
432 valueIndex = deltaHourIndex;
433 deltaNegative =
true;
438 }
else if (c ==
'.') {
439 if (valueIndex != secondsIndex) {
444 }
else if (c ==
':') {
445 if (valueIndex < hourIndex) {
447 }
else if (valueIndex == secondsIndex) {
453 }
else if ((c ==
'+') && (++valueIndex >= secondsIndex)) {
454 valueIndex = deltaHourIndex;
455 deltaNegative =
false;
457 }
else if ((c ==
'Z') && (++valueIndex >= secondsIndex)) {
458 valueIndex = deltaHourIndex + 2;
460 }
else if (c ==
'\0') {
468 res.delta =
TimeSpan(-res.delta.totalTicks());
470 if (valueIndex < monthIndex && !*monthIndex) {
473 if (valueIndex < dayIndex && !*dayIndex) {
476 res.value =
DateTime::fromDateAndTime(*yearIndex, *monthIndex, *dayIndex, *hourIndex, values[4], *secondsIndex, milliseconds);
477 res.parts = dateTimePartsFromParsingDistance(valueIndex, values);
493 int values[7] = { 0 };
494 int *
const monthIndex = values + 1;
495 int *
const dayIndex = values + 2;
496 int *
const secondsIndex = values + 5;
497 int *valueIndex = values;
498 int *
const valuesEnd = values + 7;
499 double millisecondsFact = 100.0, milliseconds = 0.0;
500 for (
const char *strIndex = str;; ++strIndex) {
501 const char c = *strIndex;
502 if (c <=
'9' && c >=
'0') {
503 if (valueIndex > secondsIndex) {
504 milliseconds += (c -
'0') * millisecondsFact;
505 millisecondsFact /= 10;
507 Detail::raiseAndAdd(*valueIndex, 10, c);
509 }
else if ((c ==
'-' || c ==
':' || c ==
'/') || (c ==
'.' && (valueIndex == secondsIndex))
510 || ((c ==
' ' || c ==
'T') && (valueIndex == dayIndex))) {
511 if (++valueIndex == valuesEnd) {
514 }
else if (c ==
'\0') {
520 if (valueIndex < monthIndex && !*monthIndex) {
523 if (valueIndex < dayIndex && !*dayIndex) {
526 res.value =
DateTime::fromDateAndTime(values[0], values[1], *dayIndex, values[3], values[4], *secondsIndex, milliseconds);
527 res.parts = dateTimePartsFromParsingDistance(valueIndex, values);
537 auto s = std::stringstream(std::stringstream::in | std::stringstream::out);
576 s <<
'.' << setw(3) << milli;
578 s << setw(3) << micro;
586 if (d.isNegative()) {
593 s << setw(2) << d.hours();
597 s << timeZoneDelimiter;
599 s << setw(2) << d.minutes();
The ConversionException class is thrown by the various conversion functions of this library when a co...
Represents an instant in time, typically expressed as a date and time of day.
std::string toString(DateTimeOutputFormat format=DateTimeOutputFormat::DateAndTime, bool noMilliseconds=false) const
Returns the string representation of the current instance using the specified format.
int day() const
Returns the day component of the date represented by this instance.
constexpr DayOfWeek dayOfWeek() const
Returns the day of the week represented by this instance.
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,...
bool isLeapYear() const
Returns an indication whether the year represented by this instance is a leap year.
int month() const
Returns the month component of the date represented by this instance.
constexpr DateTime()
Constructs a DateTime.
std::string toIsoString(TimeSpan timeZoneDelta=TimeSpan()) const
Returns the string representation of the current instance in the ISO format, eg.
static constexpr DateTime unixEpochStart()
Returns the DateTime object for the "1970-01-01T00:00:00Z".
constexpr int microsecond() const
Returns the microsecond component of the date represented by this instance.
static std::pair< DateTime, TimeSpan > fromIsoString(const char *str)
Parses the specified ISO date time denotation provided as C-style string.
constexpr int hour() const
Returns the hour component of the date represented by this instance.
static DateTime fromString(const std::string &str)
Parses the given std::string as DateTime.
constexpr int second() const
Returns the second component of the date represented by this instance.
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.
constexpr TickType totalTicks() const
Returns the number of ticks which represent the value of the current instance.
static DateTime fromTimeStamp(std::time_t timeStamp)
Constructs a new DateTime object with the local time from the specified UNIX timeStamp.
constexpr int millisecond() const
Returns the millisecond component of the date represented by this instance.
static const char * printDayOfWeek(DayOfWeek dayOfWeek, bool abbreviation=false)
Returns the string representation as C-style string for the given day of week.
constexpr int nanosecond() const
Returns the nanosecond component of the date represented by this instance.
constexpr int minute() const
Returns the minute component of the date represented by this instance.
int year() const
Returns the year component of the date represented by this instance.
Represents a time interval.
constexpr bool isNull() const
Returns true if the time interval represented by the current TimeSpan class is null.
static constexpr TickType nanosecondsPerTick
static constexpr TickType ticksPerMillisecond
constexpr int minutes() const
Returns the minutes component of the time interval represented by the current TimeSpan class.
constexpr TickType totalTicks() const
Returns the number of ticks that represent the value of the current TimeSpan class.
static constexpr TickType ticksPerMinute
static constexpr TimeSpan fromMinutes(double minutes)
Constructs a new instance of the TimeSpan class with the specified number of minutes.
static constexpr TickType ticksPerSecond
static constexpr TickType ticksPerDay
static constexpr TickType ticksPerHour
constexpr bool isNegative() const
Returns true if the time interval represented by the current TimeSpan class is negative.
constexpr int hours() const
Returns the hours component of the time interval represented by the current TimeSpan class.
Contains all utilities provides by the c++utilities library.
DatePart
Specifies the date part.
constexpr bool inRangeExclMax(num1 val, num2 min, num3 max)
constexpr bool inRangeInclMax(num1 val, num2 min, num3 max)
StringType argsToString(Args &&...args)
constexpr T max(T first, T second)
Returns the greatest of the given items.
DateTimeParts
The DateTimeParts enum specifies which parts of a timestamp are present.
DateTimeOutputFormat
Specifies the output format.
@ DateTimeAndShortWeekday
@ IsoOmittingDefaultComponents
constexpr T min(T first, T second)
Returns the smallest of the given items.
DayOfWeek
Specifies the day of the week.
The DateTimeExpression struct holds information about a time expression (e.g.
static DateTimeExpression fromString(const char *str)
Parses the given C-style string.
std::string toIsoString(char dateDelimiter='-', char timeDelimiter=':', char timeZoneDelimiter=':') const
Returns the string representation of the current instance in the ISO format.
static DateTimeExpression fromIsoString(const char *str)
Parses the specified ISO date time denotation provided as C-style string.