year_month_day.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 >= 20
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. namespace chrono {
  29. class year_month_day_last;
  30. class year_month_day {
  31. private:
  32. chrono::year __y_;
  33. chrono::month __m_;
  34. chrono::day __d_;
  35. public:
  36. year_month_day() = default;
  37. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day(
  38. const chrono::year& __yval, const chrono::month& __mval, const chrono::day& __dval) noexcept
  39. : __y_{__yval}, __m_{__mval}, __d_{__dval} {}
  40. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day(const year_month_day_last& __ymdl) noexcept;
  41. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day(const sys_days& __sysd) noexcept
  42. : year_month_day(__from_days(__sysd.time_since_epoch())) {}
  43. _LIBCPP_HIDE_FROM_ABI inline explicit constexpr year_month_day(const local_days& __locd) noexcept
  44. : year_month_day(__from_days(__locd.time_since_epoch())) {}
  45. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator+=(const months& __dm) noexcept;
  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 years& __dy) noexcept;
  48. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day& operator-=(const years& __dy) noexcept;
  49. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
  50. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }
  51. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day day() const noexcept { return __d_; }
  52. _LIBCPP_HIDE_FROM_ABI inline constexpr operator sys_days() const noexcept { return sys_days{__to_days()}; }
  53. _LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept {
  54. return local_days{__to_days()};
  55. }
  56. _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept;
  57. _LIBCPP_HIDE_FROM_ABI static constexpr year_month_day __from_days(days __d) noexcept;
  58. _LIBCPP_HIDE_FROM_ABI constexpr days __to_days() const noexcept;
  59. };
  60. // https://howardhinnant.github.io/date_algorithms.html#civil_from_days
  61. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day year_month_day::__from_days(days __d) noexcept {
  62. static_assert(numeric_limits<unsigned>::digits >= 18, "");
  63. static_assert(numeric_limits<int>::digits >= 20, "");
  64. const int __z = __d.count() + 719468;
  65. const int __era = (__z >= 0 ? __z : __z - 146096) / 146097;
  66. const unsigned __doe = static_cast<unsigned>(__z - __era * 146097); // [0, 146096]
  67. const unsigned __yoe = (__doe - __doe / 1460 + __doe / 36524 - __doe / 146096) / 365; // [0, 399]
  68. const int __yr = static_cast<int>(__yoe) + __era * 400;
  69. const unsigned __doy = __doe - (365 * __yoe + __yoe / 4 - __yoe / 100); // [0, 365]
  70. const unsigned __mp = (5 * __doy + 2) / 153; // [0, 11]
  71. const unsigned __dy = __doy - (153 * __mp + 2) / 5 + 1; // [1, 31]
  72. const unsigned __mth = __mp + (__mp < 10 ? 3 : -9); // [1, 12]
  73. return year_month_day{chrono::year{__yr + (__mth <= 2)}, chrono::month{__mth}, chrono::day{__dy}};
  74. }
  75. // https://howardhinnant.github.io/date_algorithms.html#days_from_civil
  76. _LIBCPP_HIDE_FROM_ABI inline constexpr days year_month_day::__to_days() const noexcept {
  77. static_assert(numeric_limits<unsigned>::digits >= 18, "");
  78. static_assert(numeric_limits<int>::digits >= 20, "");
  79. const int __yr = static_cast<int>(__y_) - (__m_ <= February);
  80. const unsigned __mth = static_cast<unsigned>(__m_);
  81. const unsigned __dy = static_cast<unsigned>(__d_);
  82. const int __era = (__yr >= 0 ? __yr : __yr - 399) / 400;
  83. const unsigned __yoe = static_cast<unsigned>(__yr - __era * 400); // [0, 399]
  84. const unsigned __doy = (153 * (__mth + (__mth > 2 ? -3 : 9)) + 2) / 5 + __dy - 1; // [0, 365]
  85. const unsigned __doe = __yoe * 365 + __yoe / 4 - __yoe / 100 + __doy; // [0, 146096]
  86. return days{__era * 146097 + static_cast<int>(__doe) - 719468};
  87. }
  88. _LIBCPP_HIDE_FROM_ABI inline constexpr bool
  89. operator==(const year_month_day& __lhs, const year_month_day& __rhs) noexcept {
  90. return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month() && __lhs.day() == __rhs.day();
  91. }
  92. _LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering
  93. operator<=>(const year_month_day& __lhs, const year_month_day& __rhs) noexcept {
  94. if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0)
  95. return __c;
  96. if (auto __c = __lhs.month() <=> __rhs.month(); __c != 0)
  97. return __c;
  98. return __lhs.day() <=> __rhs.day();
  99. }
  100. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const year_month& __lhs, const day& __rhs) noexcept {
  101. return year_month_day{__lhs.year(), __lhs.month(), __rhs};
  102. }
  103. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const year_month& __lhs, int __rhs) noexcept {
  104. return __lhs / day(__rhs);
  105. }
  106. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const year& __lhs, const month_day& __rhs) noexcept {
  107. return __lhs / __rhs.month() / __rhs.day();
  108. }
  109. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(int __lhs, const month_day& __rhs) noexcept {
  110. return year(__lhs) / __rhs;
  111. }
  112. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const month_day& __lhs, const year& __rhs) noexcept {
  113. return __rhs / __lhs;
  114. }
  115. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day operator/(const month_day& __lhs, int __rhs) noexcept {
  116. return year(__rhs) / __lhs;
  117. }
  118. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
  119. operator+(const year_month_day& __lhs, const months& __rhs) noexcept {
  120. return (__lhs.year() / __lhs.month() + __rhs) / __lhs.day();
  121. }
  122. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
  123. operator+(const months& __lhs, const year_month_day& __rhs) noexcept {
  124. return __rhs + __lhs;
  125. }
  126. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
  127. operator-(const year_month_day& __lhs, const months& __rhs) noexcept {
  128. return __lhs + -__rhs;
  129. }
  130. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
  131. operator+(const year_month_day& __lhs, const years& __rhs) noexcept {
  132. return (__lhs.year() + __rhs) / __lhs.month() / __lhs.day();
  133. }
  134. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
  135. operator+(const years& __lhs, const year_month_day& __rhs) noexcept {
  136. return __rhs + __lhs;
  137. }
  138. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day
  139. operator-(const year_month_day& __lhs, const years& __rhs) noexcept {
  140. return __lhs + -__rhs;
  141. }
  142. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator+=(const months& __dm) noexcept {
  143. *this = *this + __dm;
  144. return *this;
  145. }
  146. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator-=(const months& __dm) noexcept {
  147. *this = *this - __dm;
  148. return *this;
  149. }
  150. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator+=(const years& __dy) noexcept {
  151. *this = *this + __dy;
  152. return *this;
  153. }
  154. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day& year_month_day::operator-=(const years& __dy) noexcept {
  155. *this = *this - __dy;
  156. return *this;
  157. }
  158. class year_month_day_last {
  159. private:
  160. chrono::year __y_;
  161. chrono::month_day_last __mdl_;
  162. public:
  163. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last(const year& __yval, const month_day_last& __mdlval) noexcept
  164. : __y_{__yval}, __mdl_{__mdlval} {}
  165. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator+=(const months& __m) noexcept;
  166. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator-=(const months& __m) noexcept;
  167. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator+=(const years& __y) noexcept;
  168. _LIBCPP_HIDE_FROM_ABI constexpr year_month_day_last& operator-=(const years& __y) noexcept;
  169. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }
  170. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __mdl_.month(); }
  171. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month_day_last month_day_last() const noexcept { return __mdl_; }
  172. _LIBCPP_HIDE_FROM_ABI constexpr chrono::day day() const noexcept;
  173. _LIBCPP_HIDE_FROM_ABI inline constexpr operator sys_days() const noexcept {
  174. return sys_days{year() / month() / day()};
  175. }
  176. _LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept {
  177. return local_days{year() / month() / day()};
  178. }
  179. _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __y_.ok() && __mdl_.ok(); }
  180. };
  181. _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::day year_month_day_last::day() const noexcept {
  182. constexpr chrono::day __d[] = {
  183. chrono::day(31),
  184. chrono::day(28),
  185. chrono::day(31),
  186. chrono::day(30),
  187. chrono::day(31),
  188. chrono::day(30),
  189. chrono::day(31),
  190. chrono::day(31),
  191. chrono::day(30),
  192. chrono::day(31),
  193. chrono::day(30),
  194. chrono::day(31)};
  195. return (month() != February || !__y_.is_leap()) && month().ok()
  196. ? __d[static_cast<unsigned>(month()) - 1]
  197. : chrono::day{29};
  198. }
  199. _LIBCPP_HIDE_FROM_ABI inline constexpr bool
  200. operator==(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
  201. return __lhs.year() == __rhs.year() && __lhs.month_day_last() == __rhs.month_day_last();
  202. }
  203. _LIBCPP_HIDE_FROM_ABI inline constexpr bool
  204. operator!=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
  205. return !(__lhs == __rhs);
  206. }
  207. _LIBCPP_HIDE_FROM_ABI inline constexpr bool
  208. operator<(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
  209. if (__lhs.year() < __rhs.year())
  210. return true;
  211. if (__lhs.year() > __rhs.year())
  212. return false;
  213. return __lhs.month_day_last() < __rhs.month_day_last();
  214. }
  215. _LIBCPP_HIDE_FROM_ABI inline constexpr bool
  216. operator>(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
  217. return __rhs < __lhs;
  218. }
  219. _LIBCPP_HIDE_FROM_ABI inline constexpr bool
  220. operator<=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
  221. return !(__rhs < __lhs);
  222. }
  223. _LIBCPP_HIDE_FROM_ABI inline constexpr bool
  224. operator>=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
  225. return !(__lhs < __rhs);
  226. }
  227. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last operator/(const year_month& __lhs, last_spec) noexcept {
  228. return year_month_day_last{__lhs.year(), month_day_last{__lhs.month()}};
  229. }
  230. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
  231. operator/(const year& __lhs, const month_day_last& __rhs) noexcept {
  232. return year_month_day_last{__lhs, __rhs};
  233. }
  234. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last operator/(int __lhs, const month_day_last& __rhs) noexcept {
  235. return year_month_day_last{year{__lhs}, __rhs};
  236. }
  237. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
  238. operator/(const month_day_last& __lhs, const year& __rhs) noexcept {
  239. return __rhs / __lhs;
  240. }
  241. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last operator/(const month_day_last& __lhs, int __rhs) noexcept {
  242. return year{__rhs} / __lhs;
  243. }
  244. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
  245. operator+(const year_month_day_last& __lhs, const months& __rhs) noexcept {
  246. return (__lhs.year() / __lhs.month() + __rhs) / last;
  247. }
  248. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
  249. operator+(const months& __lhs, const year_month_day_last& __rhs) noexcept {
  250. return __rhs + __lhs;
  251. }
  252. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
  253. operator-(const year_month_day_last& __lhs, const months& __rhs) noexcept {
  254. return __lhs + (-__rhs);
  255. }
  256. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
  257. operator+(const year_month_day_last& __lhs, const years& __rhs) noexcept {
  258. return year_month_day_last{__lhs.year() + __rhs, __lhs.month_day_last()};
  259. }
  260. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
  261. operator+(const years& __lhs, const year_month_day_last& __rhs) noexcept {
  262. return __rhs + __lhs;
  263. }
  264. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last
  265. operator-(const year_month_day_last& __lhs, const years& __rhs) noexcept {
  266. return __lhs + (-__rhs);
  267. }
  268. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last&
  269. year_month_day_last::operator+=(const months& __dm) noexcept {
  270. *this = *this + __dm;
  271. return *this;
  272. }
  273. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last&
  274. year_month_day_last::operator-=(const months& __dm) noexcept {
  275. *this = *this - __dm;
  276. return *this;
  277. }
  278. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last&
  279. year_month_day_last::operator+=(const years& __dy) noexcept {
  280. *this = *this + __dy;
  281. return *this;
  282. }
  283. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last&
  284. year_month_day_last::operator-=(const years& __dy) noexcept {
  285. *this = *this - __dy;
  286. return *this;
  287. }
  288. _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day::year_month_day(const year_month_day_last& __ymdl) noexcept
  289. : __y_{__ymdl.year()}, __m_{__ymdl.month()}, __d_{__ymdl.day()} {}
  290. _LIBCPP_HIDE_FROM_ABI inline constexpr bool year_month_day::ok() const noexcept {
  291. if (!__y_.ok() || !__m_.ok())
  292. return false;
  293. return chrono::day{1} <= __d_ && __d_ <= (__y_ / __m_ / last).day();
  294. }
  295. } // namespace chrono
  296. _LIBCPP_END_NAMESPACE_STD
  297. #endif // _LIBCPP_STD_VER >= 20
  298. #endif // _LIBCPP___CHRONO_YEAR_MONTH_DAY_H