datetime.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MYSQL_DETAIL_DATETIME_HPP
  8. #define BOOST_MYSQL_DETAIL_DATETIME_HPP
  9. // All these algorithms have been taken from:
  10. // http://howardhinnant.github.io/date_algorithms.html
  11. #include <boost/assert.hpp>
  12. #include <boost/config.hpp>
  13. #include <cstdint>
  14. #include <limits>
  15. namespace boost {
  16. namespace mysql {
  17. namespace detail {
  18. // Helpers
  19. BOOST_INLINE_CONSTEXPR unsigned char last_month_day_arr[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  20. constexpr bool is_leap(std::uint16_t y) noexcept { return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0); }
  21. constexpr inline std::uint8_t last_month_day(std::uint16_t y, std::uint8_t m) noexcept
  22. {
  23. return m != 2 || !is_leap(y) ? last_month_day_arr[m - 1] : 29u;
  24. }
  25. // Interface
  26. BOOST_INLINE_CONSTEXPR std::uint16_t max_year = 9999;
  27. BOOST_INLINE_CONSTEXPR std::uint8_t max_month = 12;
  28. BOOST_INLINE_CONSTEXPR std::uint8_t max_day = 31;
  29. BOOST_INLINE_CONSTEXPR std::uint8_t max_hour = 23;
  30. BOOST_INLINE_CONSTEXPR std::uint8_t max_min = 59;
  31. BOOST_INLINE_CONSTEXPR std::uint8_t max_sec = 59;
  32. BOOST_INLINE_CONSTEXPR std::uint32_t max_micro = 999999;
  33. constexpr inline bool is_valid(std::uint16_t years, std::uint8_t month, std::uint8_t day) noexcept
  34. {
  35. return years <= max_year && month > 0 && month <= max_month && day > 0 &&
  36. day <= last_month_day(years, month);
  37. }
  38. BOOST_CXX14_CONSTEXPR inline int ymd_to_days(
  39. std::uint16_t years,
  40. std::uint8_t month,
  41. std::uint8_t day
  42. ) noexcept
  43. {
  44. BOOST_ASSERT(is_valid(years, month, day));
  45. int y = years;
  46. const int m = month;
  47. const int d = day;
  48. y -= m <= 2;
  49. const int era = (y >= 0 ? y : y - 399) / 400;
  50. const unsigned yoe = static_cast<unsigned>(y - era * 400); // [0, 399]
  51. const unsigned doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1; // [0, 365]
  52. const unsigned doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096]
  53. return era * 146097 + static_cast<int>(doe) - 719468;
  54. }
  55. BOOST_CXX14_CONSTEXPR inline bool days_to_ymd(
  56. int num_days,
  57. std::uint16_t& years,
  58. std::uint8_t& month,
  59. std::uint8_t& day
  60. ) noexcept
  61. {
  62. // Prevent overflow
  63. constexpr int days_magic = 719468;
  64. if (num_days > (std::numeric_limits<int>::max)() - days_magic)
  65. return false;
  66. num_days += days_magic;
  67. const int era = (num_days >= 0 ? num_days : num_days - 146096) / 146097;
  68. const unsigned doe = static_cast<unsigned>(num_days - era * 146097); // [0, 146096]
  69. const unsigned yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; // [0, 399]
  70. const int y = static_cast<int>(yoe) + era * 400;
  71. const unsigned doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365]
  72. const unsigned mp = (5 * doy + 2) / 153; // [0, 11]
  73. const unsigned d = doy - (153 * mp + 2) / 5 + 1; // [1, 31]
  74. const unsigned m = mp + (mp < 10 ? 3 : -9); // [1, 12]
  75. const int final_year = y + (m <= 2);
  76. if (final_year < 0 || final_year > static_cast<int>(max_year))
  77. return false;
  78. else
  79. {
  80. years = static_cast<std::uint16_t>(final_year);
  81. month = static_cast<std::uint8_t>(m);
  82. day = static_cast<std::uint8_t>(d);
  83. return true;
  84. }
  85. }
  86. } // namespace detail
  87. } // namespace mysql
  88. } // namespace boost
  89. #endif