year_month_day.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___CHRONO_YEAR_MONTH_DAY_H
  10. #define _LIBCPP___CHRONO_YEAR_MONTH_DAY_H
  11. #include <__chrono/calendar.h>
  12. #include <__chrono/day.h>
  13. #include <__chrono/duration.h>
  14. #include <__chrono/month.h>
  15. #include <__chrono/monthday.h>
  16. #include <__chrono/system_clock.h>
  17. #include <__chrono/time_point.h>
  18. #include <__chrono/year.h>
  19. #include <__chrono/year_month.h>
  20. #include <__config>
  21. #include <compare>
  22. #include <limits>
  23. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  24. # pragma GCC system_header
  25. #endif
  26. #if _LIBCPP_STD_VER > 17
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. namespace chrono
  29. {
  30. class year_month_day_last;
  31. class year_month_day {
  32. private:
  33. chrono::year __y_;
  34. chrono::month __m_;
  35. chrono::day __d_;
  36. public:
  37. _LIBCPP_HIDE_FROM_ABI year_month_day() = default;
  38. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day(
  39. const chrono::year& __yval, const chrono::month& __mval, const chrono::day& __dval) noexcept
  40. : __y_{__yval}, __m_{__mval}, __d_{__dval} {}
  41. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day(const year_month_day_last& __ymdl) noexcept;
  42. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day(const sys_days& __sysd) noexcept
  43. : year_month_day(__from_days(__sysd.time_since_epoch())) {}
  44. _LIBCPP_HIDE_FROM_ABI inline explicit constexpr year_month_day(const local_days& __locd) noexcept
  45. : year_month_day(__from_days(__locd.time_since_epoch())) {}
  46. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator+=(const months& __dm) noexcept;
  47. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator-=(const months& __dm) noexcept;
  48. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator+=(const years& __dy) noexcept;
  49. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator-=(const years& __dy) noexcept;
  50. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
  51. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
  52. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day day() const noexcept { return __d_; }
  53. _LIBCPP_HIDE_FROM_ABI inline constexpr operator sys_days() const noexcept { return sys_days{__to_days()}; }
  54. _LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept { return local_days{__to_days()}; }
  55. _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept;
  56. _LIBCPP_HIDE_FROM_ABI static constexpr year_month_day __from_days(days __d) noexcept;
  57. _LIBCPP_HIDE_FROM_ABI constexpr days __to_days() const noexcept;
  58. };
  59. // https://howardhinnant.github.io/date_algorithms.html#civil_from_days
  60. _LIBCPP_HIDE_FROM_ABI inline constexpr
  61. year_month_day year_month_day::__from_days(days __d) noexcept
  62. {
  63. static_assert(numeric_limits<unsigned>::digits >= 18, "");
  64. static_assert(numeric_limits<int>::digits >= 20 , "");
  65. const int __z = __d.count() + 719468;
  66. const int __era = (__z >= 0 ? __z : __z - 146096) / 146097;
  67. const unsigned __doe = static_cast<unsigned>(__z - __era * 146097); // [0, 146096]
  68. const unsigned __yoe = (__doe - __doe/1460 + __doe/36524 - __doe/146096) / 365; // [0, 399]
  69. const int __yr = static_cast<int>(__yoe) + __era * 400;
  70. const unsigned __doy = __doe - (365 * __yoe + __yoe/4 - __yoe/100); // [0, 365]
  71. const unsigned __mp = (5 * __doy + 2)/153; // [0, 11]
  72. const unsigned __dy = __doy - (153 * __mp + 2)/5 + 1; // [1, 31]
  73. const unsigned __mth = __mp + (__mp < 10 ? 3 : -9); // [1, 12]
  74. return year_month_day{chrono::year{__yr + (__mth <= 2)}, chrono::month{__mth}, chrono::day{__dy}};
  75. }
  76. // https://howardhinnant.github.io/date_algorithms.html#days_from_civil
  77. _LIBCPP_HIDE_FROM_ABI inline constexpr
  78. days year_month_day::__to_days() const noexcept
  79. {
  80. static_assert(numeric_limits<unsigned>::digits >= 18, "");
  81. static_assert(numeric_limits<int>::digits >= 20 , "");
  82. const int __yr = static_cast<int>(__y_) - (__m_ <= February);
  83. const unsigned __mth = static_cast<unsigned>(__m_);
  84. const unsigned __dy = static_cast<unsigned>(__d_);
  85. const int __era = (__yr >= 0 ? __yr : __yr - 399) / 400;
  86. const unsigned __yoe = static_cast<unsigned>(__yr - __era * 400); // [0, 399]
  87. const unsigned __doy = (153 * (__mth + (__mth > 2 ? -3 : 9)) + 2) / 5 + __dy-1; // [0, 365]
  88. const unsigned __doe = __yoe * 365 + __yoe/4 - __yoe/100 + __doy; // [0, 146096]
  89. return days{__era * 146097 + static_cast<int>(__doe) - 719468};
  90. }
  91. _LIBCPP_HIDE_FROM_ABI inline constexpr
  92. bool operator==(const year_month_day& __lhs, const year_month_day& __rhs) noexcept
  93. { return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month() && __lhs.day() == __rhs.day(); }
  94. _LIBCPP_HIDE_FROM_ABI constexpr strong_ordering
  95. operator<=>(const year_month_day& __lhs, const year_month_day& __rhs) noexcept {
  96. if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0)
  97. return __c;
  98. if (auto __c = __lhs.month() <=> __rhs.month(); __c != 0)
  99. return __c;
  100. return __lhs.day() <=> __rhs.day();
  101. }
  102. _LIBCPP_HIDE_FROM_ABI inline constexpr
  103. year_month_day operator/(const year_month& __lhs, const day& __rhs) noexcept
  104. { return year_month_day{__lhs.year(), __lhs.month(), __rhs}; }
  105. _LIBCPP_HIDE_FROM_ABI inline constexpr
  106. year_month_day operator/(const year_month& __lhs, int __rhs) noexcept
  107. { return __lhs / day(__rhs); }
  108. _LIBCPP_HIDE_FROM_ABI inline constexpr
  109. year_month_day operator/(const year& __lhs, const month_day& __rhs) noexcept
  110. { return __lhs / __rhs.month() / __rhs.day(); }
  111. _LIBCPP_HIDE_FROM_ABI inline constexpr
  112. year_month_day operator/(int __lhs, const month_day& __rhs) noexcept
  113. { return year(__lhs) / __rhs; }
  114. _LIBCPP_HIDE_FROM_ABI inline constexpr
  115. year_month_day operator/(const month_day& __lhs, const year& __rhs) noexcept
  116. { return __rhs / __lhs; }
  117. _LIBCPP_HIDE_FROM_ABI inline constexpr
  118. year_month_day operator/(const month_day& __lhs, int __rhs) noexcept
  119. { return year(__rhs) / __lhs; }
  120. _LIBCPP_HIDE_FROM_ABI inline constexpr
  121. year_month_day operator+(const year_month_day& __lhs, const months& __rhs) noexcept
  122. { return (__lhs.year()/__lhs.month() + __rhs)/__lhs.day(); }
  123. _LIBCPP_HIDE_FROM_ABI inline constexpr
  124. year_month_day operator+(const months& __lhs, const year_month_day& __rhs) noexcept
  125. { return __rhs + __lhs; }
  126. _LIBCPP_HIDE_FROM_ABI inline constexpr
  127. year_month_day operator-(const year_month_day& __lhs, const months& __rhs) noexcept
  128. { return __lhs + -__rhs; }
  129. _LIBCPP_HIDE_FROM_ABI inline constexpr
  130. year_month_day operator+(const year_month_day& __lhs, const years& __rhs) noexcept
  131. { return (__lhs.year() + __rhs) / __lhs.month() / __lhs.day(); }
  132. _LIBCPP_HIDE_FROM_ABI inline constexpr
  133. year_month_day operator+(const years& __lhs, const year_month_day& __rhs) noexcept
  134. { return __rhs + __lhs; }
  135. _LIBCPP_HIDE_FROM_ABI inline constexpr
  136. year_month_day operator-(const year_month_day& __lhs, const years& __rhs) noexcept
  137. { return __lhs + -__rhs; }
  138. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator+=(const months& __dm) noexcept { *this = *this + __dm; return *this; }
  139. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator-=(const months& __dm) noexcept { *this = *this - __dm; return *this; }
  140. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator+=(const years& __dy) noexcept { *this = *this + __dy; return *this; }
  141. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator-=(const years& __dy) noexcept { *this = *this - __dy; return *this; }
  142. class year_month_day_last {
  143. private:
  144. chrono::year __y_;
  145. chrono::month_day_last __mdl_;
  146. public:
  147. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last(const year& __yval, const month_day_last& __mdlval) noexcept
  148. : __y_{__yval}, __mdl_{__mdlval} {}
  149. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator+=(const months& __m) noexcept;
  150. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator-=(const months& __m) noexcept;
  151. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator+=(const years& __y) noexcept;
  152. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator-=(const years& __y) noexcept;
  153. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
  154. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __mdl_.month(); }
  155. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month_day_last month_day_last() const noexcept { return __mdl_; }
  156. _LIBCPP_HIDE_FROM_ABI constexpr chrono::day day() const noexcept;
  157. _LIBCPP_HIDE_FROM_ABI inline constexpr operator sys_days() const noexcept { return sys_days{year()/month()/day()}; }
  158. _LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept { return local_days{year()/month()/day()}; }
  159. _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __y_.ok() && __mdl_.ok(); }
  160. };
  161. _LIBCPP_HIDE_FROM_ABI inline constexpr
  162. chrono::day year_month_day_last::day() const noexcept
  163. {
  164. constexpr chrono::day __d[] =
  165. {
  166. chrono::day(31), chrono::day(28), chrono::day(31),
  167. chrono::day(30), chrono::day(31), chrono::day(30),
  168. chrono::day(31), chrono::day(31), chrono::day(30),
  169. chrono::day(31), chrono::day(30), chrono::day(31)
  170. };
  171. return (month() != February || !__y_.is_leap()) && month().ok() ?
  172. __d[static_cast<unsigned>(month()) - 1] : chrono::day{29};
  173. }
  174. _LIBCPP_HIDE_FROM_ABI inline constexpr
  175. bool operator==(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept
  176. { return __lhs.year() == __rhs.year() && __lhs.month_day_last() == __rhs.month_day_last(); }
  177. _LIBCPP_HIDE_FROM_ABI inline constexpr
  178. bool operator!=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept
  179. { return !(__lhs == __rhs); }
  180. _LIBCPP_HIDE_FROM_ABI inline constexpr
  181. bool operator< (const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept
  182. {
  183. if (__lhs.year() < __rhs.year()) return true;
  184. if (__lhs.year() > __rhs.year()) return false;
  185. return __lhs.month_day_last() < __rhs.month_day_last();
  186. }
  187. _LIBCPP_HIDE_FROM_ABI inline constexpr
  188. bool operator> (const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept
  189. { return __rhs < __lhs; }
  190. _LIBCPP_HIDE_FROM_ABI inline constexpr
  191. bool operator<=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept
  192. { return !(__rhs < __lhs);}
  193. _LIBCPP_HIDE_FROM_ABI inline constexpr
  194. bool operator>=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept
  195. { return !(__lhs < __rhs); }
  196. _LIBCPP_HIDE_FROM_ABI inline constexpr
  197. year_month_day_last operator/(const year_month& __lhs, last_spec) noexcept
  198. { return year_month_day_last{__lhs.year(), month_day_last{__lhs.month()}}; }
  199. _LIBCPP_HIDE_FROM_ABI inline constexpr
  200. year_month_day_last operator/(const year& __lhs, const month_day_last& __rhs) noexcept
  201. { return year_month_day_last{__lhs, __rhs}; }
  202. _LIBCPP_HIDE_FROM_ABI inline constexpr
  203. year_month_day_last operator/(int __lhs, const month_day_last& __rhs) noexcept
  204. { return year_month_day_last{year{__lhs}, __rhs}; }
  205. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
  206. operator/(const month_day_last& __lhs, const year& __rhs) noexcept
  207. { return __rhs / __lhs; }
  208. _LIBCPP_HIDE_FROM_ABI inline constexpr
  209. year_month_day_last operator/(const month_day_last& __lhs, int __rhs) noexcept
  210. { return year{__rhs} / __lhs; }
  211. _LIBCPP_HIDE_FROM_ABI inline constexpr
  212. year_month_day_last operator+(const year_month_day_last& __lhs, const months& __rhs) noexcept
  213. { return (__lhs.year() / __lhs.month() + __rhs) / last; }
  214. _LIBCPP_HIDE_FROM_ABI inline constexpr
  215. year_month_day_last operator+(const months& __lhs, const year_month_day_last& __rhs) noexcept
  216. { return __rhs + __lhs; }
  217. _LIBCPP_HIDE_FROM_ABI inline constexpr
  218. year_month_day_last operator-(const year_month_day_last& __lhs, const months& __rhs) noexcept
  219. { return __lhs + (-__rhs); }
  220. _LIBCPP_HIDE_FROM_ABI inline constexpr
  221. year_month_day_last operator+(const year_month_day_last& __lhs, const years& __rhs) noexcept
  222. { return year_month_day_last{__lhs.year() + __rhs, __lhs.month_day_last()}; }
  223. _LIBCPP_HIDE_FROM_ABI inline constexpr
  224. year_month_day_last operator+(const years& __lhs, const year_month_day_last& __rhs) noexcept
  225. { return __rhs + __lhs; }
  226. _LIBCPP_HIDE_FROM_ABI inline constexpr
  227. year_month_day_last operator-(const year_month_day_last& __lhs, const years& __rhs) noexcept
  228. { return __lhs + (-__rhs); }
  229. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last& year_month_day_last::operator+=(const months& __dm) noexcept { *this = *this + __dm; return *this; }
  230. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last& year_month_day_last::operator-=(const months& __dm) noexcept { *this = *this - __dm; return *this; }
  231. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last& year_month_day_last::operator+=(const years& __dy) noexcept { *this = *this + __dy; return *this; }
  232. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last& year_month_day_last::operator-=(const years& __dy) noexcept { *this = *this - __dy; return *this; }
  233. _LIBCPP_HIDE_FROM_ABI inline constexpr
  234. year_month_day::year_month_day(const year_month_day_last& __ymdl) noexcept
  235. : __y_{__ymdl.year()}, __m_{__ymdl.month()}, __d_{__ymdl.day()} {}
  236. _LIBCPP_HIDE_FROM_ABI inline constexpr
  237. bool year_month_day::ok() const noexcept
  238. {
  239. if (!__y_.ok() || !__m_.ok()) return false;
  240. return chrono::day{1} <= __d_ && __d_ <= (__y_ / __m_ / last).day();
  241. }
  242. } // namespace chrono
  243. _LIBCPP_END_NAMESPACE_STD
  244. #endif // _LIBCPP_STD_VER > 17
  245. #endif // _LIBCPP___CHRONO_YEAR_MONTH_DAY_H