1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
|
#ifndef CHRONO_UTILITIES_DATETIME_H
#define CHRONO_UTILITIES_DATETIME_H
#include "./timespan.h"
#include "../misc/flagenumclass.h"
#include <cstdint>
#include <ctime>
#include <limits>
#include <string>
namespace CppUtilities {
/*!
* \brief Specifies the output format.
* \sa DateTime::toString()
*/
enum class DateTimeOutputFormat {
DateAndTime, /**< date and time */
DateOnly, /**< date only */
TimeOnly, /**< time only */
DateTimeAndWeekday, /**< date with weekday and time */
DateTimeAndShortWeekday, /**< date with abbreviated weekday and time */
Iso, /**< ISO format like DateTime::toIsoString() */
IsoOmittingDefaultComponents, /**< ISO format like DateTime::toIsoString() omitting default components, e.g. just "2017" instead of "2017-01-01T00:00:00" */
};
/*!
* \brief Specifies the day of the week.
* \sa DateTime::dayOfWeek()
*/
enum class DayOfWeek {
Monday, /**< Monday */
Tuesday, /**< Tuesday */
Wednesday, /**< Wednesday */
Thursday, /**< Thursday */
Friday, /**< Friday */
Saturday, /**< Saturday */
Sunday /**< Sunday */
};
/*!
* \brief Specifies the date part.
* \remarks Intended for internal use only.
* \sa DateTime::getDatePart()
*/
enum class DatePart {
Year, /**< year */
Month, /**< month */
DayOfYear, /**< day of year */
Day /**< day */
};
class CPP_UTILITIES_EXPORT DateTime {
public:
using TickType = std::uint64_t;
explicit constexpr DateTime();
explicit constexpr DateTime(TickType ticks);
static DateTime fromDate(int year = 1, int month = 1, int day = 1);
static DateTime fromTime(int hour = 0, int minute = 0, int second = 0, double millisecond = 0.0);
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);
static DateTime fromString(const std::string &str);
static DateTime fromString(const char *str);
static std::pair<DateTime, TimeSpan> fromIsoString(const char *str);
static DateTime fromIsoStringGmt(const char *str);
static DateTime fromIsoStringLocal(const char *str);
static DateTime fromTimeStamp(std::time_t timeStamp);
constexpr static DateTime fromTimeStampGmt(std::time_t timeStamp);
template <typename TimePoint> static DateTime fromChronoTimePoint(TimePoint timePoint);
template <typename TimePoint> constexpr static DateTime fromChronoTimePointGmt(TimePoint timePoint);
constexpr TickType &ticks();
constexpr TickType totalTicks() const;
int year() const;
int month() const;
int day() const;
int dayOfYear() const;
constexpr DayOfWeek dayOfWeek() const;
constexpr int hour() const;
constexpr int minute() const;
constexpr int second() const;
constexpr int millisecond() const;
constexpr int microsecond() const;
constexpr int nanosecond() const;
constexpr bool isNull() const;
constexpr TimeSpan timeOfDay() const;
bool isLeapYear() const;
constexpr bool isEternity() const;
constexpr bool isSameDay(const DateTime &other) const;
std::string toString(DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime, bool noMilliseconds = false) const;
void toString(std::string &result, DateTimeOutputFormat format = DateTimeOutputFormat::DateAndTime, bool noMilliseconds = false) const;
std::string toIsoStringWithCustomDelimiters(
TimeSpan timeZoneDelta = TimeSpan(), char dateDelimiter = '-', char timeDelimiter = ':', char timeZoneDelimiter = ':') const;
std::string toIsoString(TimeSpan timeZoneDelta = TimeSpan()) const;
constexpr std::time_t toTimeStamp() const;
static const char *printDayOfWeek(DayOfWeek dayOfWeek, bool abbreviation = false);
static constexpr DateTime eternity();
static constexpr DateTime unixEpochStart();
static DateTime now();
static DateTime gmtNow();
#if defined(PLATFORM_UNIX) && !defined(PLATFORM_MAC)
static DateTime exactGmtNow();
#endif
constexpr static bool isLeapYear(int year);
static int daysInMonth(int year, int month);
constexpr bool operator==(const DateTime &other) const;
constexpr bool operator!=(const DateTime &other) const;
constexpr bool operator<(const DateTime &other) const;
constexpr bool operator>(const DateTime &other) const;
constexpr bool operator<=(const DateTime &other) const;
constexpr bool operator>=(const DateTime &other) const;
constexpr DateTime operator+(const TimeSpan &timeSpan) const;
constexpr DateTime operator-(const TimeSpan &timeSpan) const;
constexpr TimeSpan operator+(const DateTime &other) const;
constexpr TimeSpan operator-(const DateTime &other) const;
DateTime &operator+=(const TimeSpan &timeSpan);
DateTime &operator-=(const TimeSpan &timeSpan);
private:
static TickType dateToTicks(int year, int month, int day);
static TickType timeToTicks(int hour, int minute, int second, double millisecond);
int getDatePart(DatePart part) const;
TickType m_ticks;
static const int m_daysPerYear;
static const int m_daysPer4Years;
static const int m_daysPer100Years;
static const int m_daysPer400Years;
static const int m_daysTo1601;
static const int m_daysTo1899;
static const int m_daysTo10000;
static const int m_daysToMonth365[13];
static const int m_daysToMonth366[13];
static const int m_daysInMonth365[12];
static const int m_daysInMonth366[12];
};
/*!
* \brief The DateTimeParts enum specifies which parts of a timestamp are present.
*/
enum class DateTimeParts : std::uint64_t {
None = 0, /**< no parts are present */
Year = (1 << 0), /**< the year is present */
Month = (1 << 1), /**< the month is present */
Day = (1 << 2), /**< the day is present */
Hour = (1 << 3), /**< the hour is present */
Minute = (1 << 4), /**< the minute is present */
Second = (1 << 5), /**< the second is present */
SubSecond = (1 << 6), /**< the second contains a fractional part */
DeltaHour = (1 << 7), /**< the timezone-delta hour is present */
DeltaMinute = (1 << 8), /**< the timezone-delta minute is present */
Date = Year | Month | Day, /**< year, month and day are present */
Time = Hour | Minute | Second | SubSecond, /**< hour, minute and (sub)second are present */
DateTime = Date | Time, /**< all parts a DateTime object can represent are present */
TimeZoneDelta = DeltaHour | DeltaMinute, /**< the timezone-delta hour and minute are present */
All = DateTime | TimeZoneDelta, /**< all parts a DateTime object can represent plus the full timezone-delta are present */
};
struct CPP_UTILITIES_EXPORT DateTimeExpression {
DateTime value; /**< The value of the time expression as DateTime object. */
TimeSpan delta; /**< The delta of \a value from UTC as TimeSpan object. */
DateTimeParts parts = DateTimeParts::None; /**< The parts present in the expression as flag enum. */
constexpr DateTime gmt() const;
constexpr bool operator==(const DateTimeExpression &other) const;
std::string toIsoString(char dateDelimiter = '-', char timeDelimiter = ':', char timeZoneDelimiter = ':') const;
static DateTimeExpression fromIsoString(const char *str);
static DateTimeExpression fromString(const char *str);
};
/*!
* \brief Returns the value in UTC time.
*/
constexpr DateTime DateTimeExpression::gmt() const
{
return value - delta;
}
/*!
* \brief Returns whether the expressions are equivalent.
*/
constexpr bool DateTimeExpression::operator==(const DateTimeExpression &other) const
{
return value == other.value && delta == other.delta && parts == other.parts;
}
/*!
* \brief Constructs a DateTime.
*/
constexpr inline DateTime::DateTime()
: m_ticks(0)
{
}
/*!
* \brief Constructs a DateTime with the specified number of \a ticks.
*/
constexpr inline DateTime::DateTime(TickType ticks)
: m_ticks(ticks)
{
}
/*!
* \brief Constructs a DateTime to the specified \a year, \a month, and \a day.
* \throws Throws a ConversionException if the specified \a year, \a month or \a day is out-of-range.
*/
inline DateTime DateTime::fromDate(int year, int month, int day)
{
return DateTime(dateToTicks(year, month, day));
}
/*!
* \brief Constructs a DateTime to the specified \a hour, \a minute, \a second and \a millisecond.
* \throws Throws a ConversionException if the specified \a hour, \a minute, \a second or \a millisecond is out-of-range.
*/
inline DateTime DateTime::fromTime(int hour, int minute, int second, double millisecond)
{
return DateTime(timeToTicks(hour, minute, second, millisecond));
}
/*!
* \brief Constructs a DateTime to the specified \a year, \a month, \a day, \a hour, \a minute, \a second and \a millisecond.
* \throws Throws a ConversionException if the specified \a year, \a month, \a day, \a hour, \a minute, \a second or \a millisecond
* is out-of-range.
*/
inline DateTime DateTime::fromDateAndTime(int year, int month, int day, int hour, int minute, int second, double millisecond)
{
return DateTime(dateToTicks(year, month, day) + timeToTicks(hour, minute, second, millisecond));
}
/*!
* \brief Parses the given std::string as DateTime.
* \throws Throws a ConversionException if the specified \a str does not match the expected time format.
*
* The expected format is something like "2012-02-29 15:34:20.033" or "2012/02/29 15:34:20.033". The
* delimiters '-', ':' and '/' are exchangeable.
*
* \sa DateTime::fromIsoString()
*/
inline DateTime DateTime::fromString(const std::string &str)
{
return fromString(str.data());
}
/*!
* \brief Parses the specified ISO date time denotation provided as C-style string.
* \returns Returns the parsed UTC time. That means a possibly denoted time zone delta is subtracted from the time stamp.
* \throws Throws a ConversionException if the specified \a str does not match the expected time format.
* \sa fromIsoString()
*/
inline DateTime DateTime::fromIsoStringGmt(const char *str)
{
return DateTimeExpression::fromIsoString(str).gmt();
}
/*!
* \brief Parses the specified ISO date time denotation provided as C-style string.
* \returns Returns the parsed local time. That means a possibly denoted time zone delta is discarded.
* \throws Throws a ConversionException if the specified \a str does not match the expected time format.
* \sa fromIsoString()
*/
inline DateTime DateTime::fromIsoStringLocal(const char *str)
{
return DateTimeExpression::fromIsoString(str).value;
}
/*!
* \brief Constructs a new DateTime object with the GMT time from the specified UNIX \a timeStamp.
*/
constexpr inline DateTime DateTime::fromTimeStampGmt(std::time_t timeStamp)
{
return DateTime(DateTime::unixEpochStart().totalTicks() + static_cast<std::uint64_t>(timeStamp) * TimeSpan::ticksPerSecond);
}
/*!
* \brief Constructs a new DateTime object with the local time from the specified std::chrono::time_point.
* \remarks Works only with time points of std::chrono::system_clock so far. C++20 will fix this. Until then this function
* should be considered experimental.
*/
template <typename TimePoint> inline DateTime DateTime::fromChronoTimePoint(TimePoint timePoint)
{
return DateTime::fromTimeStamp(decltype(timePoint)::clock::to_time_t(timePoint));
}
/*!
* \brief Constructs a new DateTime object with the GMT time from the specified std::chrono::time_point.
* \remarks Works only with time points of std::chrono::system_clock so far. C++20 will fix this. Until then this function
* should be considered experimental.
*/
template <typename TimePoint> constexpr DateTime DateTime::fromChronoTimePointGmt(TimePoint timePoint)
{
return DateTime::fromTimeStampGmt(decltype(timePoint)::clock::to_time_t(timePoint));
}
/*!
* \brief Returns a mutable reference to the total ticks.
*/
constexpr inline DateTime::TickType &DateTime::ticks()
{
return m_ticks;
}
/*!
* \brief Returns the number of ticks which represent the value of the current instance.
*/
constexpr inline DateTime::TickType DateTime::totalTicks() const
{
return m_ticks;
}
/*!
* \brief Returns the year component of the date represented by this instance.
*/
inline int DateTime::year() const
{
return getDatePart(DatePart::Year);
}
/*!
* \brief Returns the month component of the date represented by this instance.
*/
inline int DateTime::month() const
{
return getDatePart(DatePart::Month);
}
/*!
* \brief Returns the day component of the date represented by this instance.
*/
inline int DateTime::day() const
{
return getDatePart(DatePart::Day);
}
/*!
* \brief Returns the day of the year represented by this instance.
*/
inline int DateTime::dayOfYear() const
{
return getDatePart(DatePart::DayOfYear);
}
/*!
* \brief Returns the day of the week represented by this instance.
* \sa DayOfWeek
*/
constexpr inline DayOfWeek DateTime::dayOfWeek() const
{
return static_cast<DayOfWeek>((m_ticks / TimeSpan::ticksPerDay) % 7l);
}
/*!
* \brief Returns the hour component of the date represented by this instance.
*/
constexpr inline int DateTime::hour() const
{
return static_cast<int>(m_ticks / TimeSpan::ticksPerHour % 24ul);
}
/*!
*\brief Returns the minute component of the date represented by this instance.
*/
constexpr inline int DateTime::minute() const
{
return static_cast<int>(m_ticks / TimeSpan::ticksPerMinute % 60ul);
}
/*!
* \brief Returns the second component of the date represented by this instance.
*/
constexpr inline int DateTime::second() const
{
return static_cast<int>(m_ticks / TimeSpan::ticksPerSecond % 60ul);
}
/*!
* \brief Returns the millisecond component of the date represented by this instance.
*/
constexpr inline int DateTime::millisecond() const
{
return static_cast<int>(m_ticks / TimeSpan::ticksPerMillisecond % 1000ul);
}
/*!
* \brief Returns the microsecond component of the date represented by this instance.
*/
constexpr inline int DateTime::microsecond() const
{
return static_cast<int>(m_ticks / TimeSpan::ticksPerMicrosecond % 1000ul);
}
/*!
* \brief Returns the nanosecond component of the date represented by this instance.
* \remarks The accuracy of the DateTime class is 100-nanoseconds. Hence the returned value
* will always have two zeros at the end (in decimal representation).
*/
constexpr inline int DateTime::nanosecond() const
{
return static_cast<int>(m_ticks % 10ul * TimeSpan::nanosecondsPerTick);
}
/*!
* \brief Returns true if the date represented by the current DateTime class is null.
* \sa DateTime
*/
constexpr inline bool DateTime::isNull() const
{
return m_ticks == 0;
}
/*!
* \brief Returns the time of day as TimeSpan for this instance.
*/
constexpr inline TimeSpan DateTime::timeOfDay() const
{
return TimeSpan(static_cast<TimeSpan::TickType>(m_ticks % TimeSpan::ticksPerDay));
}
/*!
* \brief Returns an indication whether the year represented by this instance is a leap year.
*/
inline bool DateTime::isLeapYear() const
{
return isLeapYear(year());
}
/*!
* \brief Returns whether the instance has the maximal number of ticks.
*/
constexpr inline bool DateTime::isEternity() const
{
return m_ticks == std::numeric_limits<TickType>::max();
}
/*!
* \brief Returns an indication whether the specified \a year is a leap year.
*/
constexpr inline bool DateTime::isLeapYear(int year)
{
return (year % 4 != 0) ? false : ((year % 100 == 0) ? (year % 400 == 0) : true);
}
/*!
* \brief Returns the number of days in the specified \a month and \a year.
*/
inline int DateTime::daysInMonth(int year, int month)
{
return (month >= 1 && month <= 12) ? (isLeapYear(year) ? m_daysInMonth366[month - 1] : m_daysInMonth365[month - 1]) : (0);
}
/*!
* \brief Returns and indication whether two DateTime instances represent the same day.
*/
constexpr inline bool DateTime::isSameDay(const DateTime &other) const
{
return (m_ticks / TimeSpan::ticksPerDay) == (other.m_ticks / TimeSpan::ticksPerDay);
}
/*!
* \brief Returns the string representation of the current instance using the specified \a format.
* \remarks If \a noMilliseconds is true the date will be rounded to full seconds.
* \sa toIsoString() for ISO format
*/
inline std::string DateTime::toString(DateTimeOutputFormat format, bool noMilliseconds) const
{
std::string result;
toString(result, format, noMilliseconds);
return result;
}
/*!
* \brief Returns the UNIX timestamp for the current instance.
*/
constexpr std::time_t DateTime::toTimeStamp() const
{
return static_cast<std::time_t>((totalTicks() - DateTime::unixEpochStart().totalTicks()) / TimeSpan::ticksPerSecond);
}
/*!
* \brief Constructs a new instance of the DateTime class with the maximal number of ticks.
*/
constexpr inline DateTime DateTime::eternity()
{
return DateTime(std::numeric_limits<TickType>::max());
}
/*!
* \brief Returns the DateTime object for the "1970-01-01T00:00:00Z".
*/
constexpr inline DateTime DateTime::unixEpochStart()
{
return DateTime(621355968000000000);
}
/*!
* \brief Returns a DateTime object that is set to the current date and time on this computer, expressed as the local time.
* \remarks The time might be rounded to full seconds. Use exactGmtNow() for better precision.
*/
inline DateTime DateTime::now()
{
return DateTime::fromTimeStamp(std::time(nullptr));
}
/*!
* \brief Returns a DateTime object that is set to the current date and time on this computer, expressed as the GMT time.
* \remarks The time might be rounded to full seconds. Use exactGmtNow() for better precision.
*/
inline DateTime DateTime::gmtNow()
{
return DateTime::fromTimeStampGmt(std::time(nullptr));
}
/*!
* \brief Indicates whether two DateTime instances are equal.
*/
constexpr inline bool DateTime::operator==(const DateTime &other) const
{
return m_ticks == other.m_ticks;
}
/*!
* \brief Indicates whether two DateTime instances are not equal.
*/
constexpr inline bool DateTime::operator!=(const DateTime &other) const
{
return m_ticks != other.m_ticks;
}
/*!
* \brief Indicates whether a specified DateTime is less than another specified DateTime.
*/
constexpr inline bool DateTime::operator<(const DateTime &other) const
{
return m_ticks < other.m_ticks;
}
/*!
* \brief Indicates whether a specified DateTime is greater than another specified DateTime.
*/
constexpr inline bool DateTime::operator>(const DateTime &other) const
{
return m_ticks > other.m_ticks;
}
/*!
* \brief Indicates whether a specified DateTime is less or equal than another specified DateTime.
*/
constexpr inline bool DateTime::operator<=(const DateTime &other) const
{
return m_ticks <= other.m_ticks;
}
/*!
* \brief Indicates whether a specified DateTime is greater or equal than another specified DateTime.
*/
constexpr inline bool DateTime::operator>=(const DateTime &other) const
{
return m_ticks >= other.m_ticks;
}
/*!
* \brief Adds another instance.
* \returns The result is another DateTime.
*/
constexpr inline DateTime DateTime::operator+(const TimeSpan &timeSpan) const
{
return DateTime(m_ticks + static_cast<TickType>(timeSpan.m_ticks));
}
/*!
* \brief Subtracts another instance.
* \returns The result is another DateTime.
*/
constexpr inline DateTime DateTime::operator-(const TimeSpan &timeSpan) const
{
return DateTime(m_ticks - static_cast<TickType>(timeSpan.m_ticks));
}
/*!
* \brief Adds two instances.
* \returns The result is a TimeSpan.
*/
constexpr inline TimeSpan DateTime::operator+(const DateTime &other) const
{
return TimeSpan(static_cast<TimeSpan::TickType>(m_ticks + other.m_ticks));
}
/*!
* \brief Subtracts two DateTime instances.
* \returns The result is a TimeSpan.
* \remarks For expressing the delta between two concrete DateTime instances in terms of
* years, month and days, use Period::Period instead.
*/
constexpr inline TimeSpan DateTime::operator-(const DateTime &other) const
{
return TimeSpan(static_cast<TimeSpan::TickType>(m_ticks - other.m_ticks));
}
/*!
* \brief Adds a TimeSpan to the current instance.
*/
inline DateTime &DateTime::operator+=(const TimeSpan &timeSpan)
{
m_ticks += static_cast<TickType>(timeSpan.m_ticks);
return *this;
}
/*!
* \brief Subtracts a TimeSpan from the current instance.
*/
inline DateTime &DateTime::operator-=(const TimeSpan &timeSpan)
{
m_ticks += static_cast<TickType>(timeSpan.m_ticks);
return *this;
}
} // namespace CppUtilities
namespace std {
/// \brief Computes the hash for the CppUtilities::DateTime instance.
template <> struct hash<CppUtilities::DateTime> {
inline size_t operator()(const CppUtilities::DateTime &dateTime) const
{
return hash<CppUtilities::DateTime::TickType>()(dateTime.totalTicks());
}
};
} // namespace std
CPP_UTILITIES_MARK_FLAG_ENUM_CLASS(CppUtilities, CppUtilities::DateTimeParts);
#endif // CHRONO_UTILITIES_DATETIME_H
|