chrono 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186
  1. // <chrono> -*- C++ -*-
  2. // Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/chrono
  21. * This is a Standard C++ Library header.
  22. * @ingroup chrono
  23. */
  24. #ifndef _GLIBCXX_CHRONO
  25. #define _GLIBCXX_CHRONO 1
  26. #pragma GCC system_header
  27. #if __cplusplus < 201103L
  28. # include <bits/c++0x_warning.h>
  29. #else
  30. #include <bits/chrono.h>
  31. #if __cplusplus > 201703L
  32. # include <sstream> // ostringstream
  33. # include <bits/charconv.h>
  34. #endif
  35. namespace std _GLIBCXX_VISIBILITY(default)
  36. {
  37. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  38. /**
  39. * @defgroup chrono Time
  40. * @ingroup utilities
  41. *
  42. * Classes and functions for time.
  43. *
  44. * @since C++11
  45. */
  46. /** @namespace std::chrono
  47. * @brief ISO C++ 2011 namespace for date and time utilities
  48. * @ingroup chrono
  49. */
  50. namespace chrono
  51. {
  52. #if __cplusplus >= 202002L
  53. /// @addtogroup chrono
  54. /// @{
  55. struct local_t { };
  56. template<typename _Duration>
  57. using local_time = time_point<local_t, _Duration>;
  58. using local_seconds = local_time<seconds>;
  59. using local_days = local_time<days>;
  60. class utc_clock;
  61. class tai_clock;
  62. class gps_clock;
  63. template<typename _Duration>
  64. using utc_time = time_point<utc_clock, _Duration>;
  65. using utc_seconds = utc_time<seconds>;
  66. template<typename _Duration>
  67. using tai_time = time_point<tai_clock, _Duration>;
  68. using tai_seconds = tai_time<seconds>;
  69. template<typename _Duration>
  70. using gps_time = time_point<gps_clock, _Duration>;
  71. using gps_seconds = gps_time<seconds>;
  72. template<> struct is_clock<utc_clock> : true_type { };
  73. template<> struct is_clock<tai_clock> : true_type { };
  74. template<> struct is_clock<gps_clock> : true_type { };
  75. template<> inline constexpr bool is_clock_v<utc_clock> = true;
  76. template<> inline constexpr bool is_clock_v<tai_clock> = true;
  77. template<> inline constexpr bool is_clock_v<gps_clock> = true;
  78. struct leap_second_info
  79. {
  80. bool is_leap_second;
  81. seconds elapsed;
  82. };
  83. // CALENDRICAL TYPES
  84. // CLASS DECLARATIONS
  85. class day;
  86. class month;
  87. class year;
  88. class weekday;
  89. class weekday_indexed;
  90. class weekday_last;
  91. class month_day;
  92. class month_day_last;
  93. class month_weekday;
  94. class month_weekday_last;
  95. class year_month;
  96. class year_month_day;
  97. class year_month_day_last;
  98. class year_month_weekday;
  99. class year_month_weekday_last;
  100. struct last_spec
  101. {
  102. explicit last_spec() = default;
  103. friend constexpr month_day_last
  104. operator/(int __m, last_spec) noexcept;
  105. friend constexpr month_day_last
  106. operator/(last_spec, int __m) noexcept;
  107. };
  108. inline constexpr last_spec last{};
  109. namespace __detail
  110. {
  111. // Compute the remainder of the Euclidean division of __n divided by __d.
  112. // Euclidean division truncates toward negative infinity and always
  113. // produces a remainder in the range of [0,__d-1] (whereas standard
  114. // division truncates toward zero and yields a nonpositive remainder
  115. // for negative __n).
  116. constexpr unsigned
  117. __modulo(long long __n, unsigned __d)
  118. {
  119. if (__n >= 0)
  120. return __n % __d;
  121. else
  122. return (__d + (__n % __d)) % __d;
  123. }
  124. inline constexpr unsigned __days_per_month[12]
  125. = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  126. }
  127. // DAY
  128. class day
  129. {
  130. private:
  131. unsigned char _M_d;
  132. public:
  133. day() = default;
  134. explicit constexpr
  135. day(unsigned __d) noexcept
  136. : _M_d(__d)
  137. { }
  138. constexpr day&
  139. operator++() noexcept
  140. {
  141. ++_M_d;
  142. return *this;
  143. }
  144. constexpr day
  145. operator++(int) noexcept
  146. {
  147. auto __ret = *this;
  148. ++(*this);
  149. return __ret;
  150. }
  151. constexpr day&
  152. operator--() noexcept
  153. {
  154. --_M_d;
  155. return *this;
  156. }
  157. constexpr day
  158. operator--(int) noexcept
  159. {
  160. auto __ret = *this;
  161. --(*this);
  162. return __ret;
  163. }
  164. constexpr day&
  165. operator+=(const days& __d) noexcept
  166. {
  167. *this = *this + __d;
  168. return *this;
  169. }
  170. constexpr day&
  171. operator-=(const days& __d) noexcept
  172. {
  173. *this = *this - __d;
  174. return *this;
  175. }
  176. constexpr explicit
  177. operator unsigned() const noexcept
  178. { return _M_d; }
  179. constexpr bool
  180. ok() const noexcept
  181. { return 1 <= _M_d && _M_d <= 31; }
  182. friend constexpr bool
  183. operator==(const day& __x, const day& __y) noexcept
  184. { return unsigned{__x} == unsigned{__y}; }
  185. friend constexpr strong_ordering
  186. operator<=>(const day& __x, const day& __y) noexcept
  187. { return unsigned{__x} <=> unsigned{__y}; }
  188. friend constexpr day
  189. operator+(const day& __x, const days& __y) noexcept
  190. { return day(unsigned{__x} + __y.count()); }
  191. friend constexpr day
  192. operator+(const days& __x, const day& __y) noexcept
  193. { return __y + __x; }
  194. friend constexpr day
  195. operator-(const day& __x, const days& __y) noexcept
  196. { return __x + -__y; }
  197. friend constexpr days
  198. operator-(const day& __x, const day& __y) noexcept
  199. { return days{int(unsigned{__x}) - int(unsigned{__y})}; }
  200. friend constexpr month_day
  201. operator/(const month& __m, const day& __d) noexcept;
  202. friend constexpr month_day
  203. operator/(int __m, const day& __d) noexcept;
  204. friend constexpr month_day
  205. operator/(const day& __d, const month& __m) noexcept;
  206. friend constexpr month_day
  207. operator/(const day& __d, int __m) noexcept;
  208. friend constexpr year_month_day
  209. operator/(const year_month& __ym, const day& __d) noexcept;
  210. // TODO: Implement operator<<, to_stream, from_stream.
  211. };
  212. // MONTH
  213. class month
  214. {
  215. private:
  216. unsigned char _M_m;
  217. public:
  218. month() = default;
  219. explicit constexpr
  220. month(unsigned __m) noexcept
  221. : _M_m(__m)
  222. { }
  223. constexpr month&
  224. operator++() noexcept
  225. {
  226. *this += months{1};
  227. return *this;
  228. }
  229. constexpr month
  230. operator++(int) noexcept
  231. {
  232. auto __ret = *this;
  233. ++(*this);
  234. return __ret;
  235. }
  236. constexpr month&
  237. operator--() noexcept
  238. {
  239. *this -= months{1};
  240. return *this;
  241. }
  242. constexpr month
  243. operator--(int) noexcept
  244. {
  245. auto __ret = *this;
  246. --(*this);
  247. return __ret;
  248. }
  249. constexpr month&
  250. operator+=(const months& __m) noexcept
  251. {
  252. *this = *this + __m;
  253. return *this;
  254. }
  255. constexpr month&
  256. operator-=(const months& __m) noexcept
  257. {
  258. *this = *this - __m;
  259. return *this;
  260. }
  261. explicit constexpr
  262. operator unsigned() const noexcept
  263. { return _M_m; }
  264. constexpr bool
  265. ok() const noexcept
  266. { return 1 <= _M_m && _M_m <= 12; }
  267. friend constexpr bool
  268. operator==(const month& __x, const month& __y) noexcept
  269. { return unsigned{__x} == unsigned{__y}; }
  270. friend constexpr strong_ordering
  271. operator<=>(const month& __x, const month& __y) noexcept
  272. { return unsigned{__x} <=> unsigned{__y}; }
  273. friend constexpr month
  274. operator+(const month& __x, const months& __y) noexcept
  275. {
  276. auto __n = static_cast<long long>(unsigned{__x}) + (__y.count() - 1);
  277. return month{__detail::__modulo(__n, 12) + 1};
  278. }
  279. friend constexpr month
  280. operator+(const months& __x, const month& __y) noexcept
  281. { return __y + __x; }
  282. friend constexpr month
  283. operator-(const month& __x, const months& __y) noexcept
  284. { return __x + -__y; }
  285. friend constexpr months
  286. operator-(const month& __x, const month& __y) noexcept
  287. {
  288. const auto __dm = int(unsigned(__x)) - int(unsigned(__y));
  289. return months{__dm < 0 ? 12 + __dm : __dm};
  290. }
  291. friend constexpr year_month
  292. operator/(const year& __y, const month& __m) noexcept;
  293. friend constexpr month_day
  294. operator/(const month& __m, int __d) noexcept;
  295. friend constexpr month_day_last
  296. operator/(const month& __m, last_spec) noexcept;
  297. friend constexpr month_day_last
  298. operator/(last_spec, const month& __m) noexcept;
  299. friend constexpr month_weekday
  300. operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
  301. friend constexpr month_weekday
  302. operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
  303. friend constexpr month_weekday_last
  304. operator/(const month& __m, const weekday_last& __wdl) noexcept;
  305. friend constexpr month_weekday_last
  306. operator/(const weekday_last& __wdl, const month& __m) noexcept;
  307. // TODO: Implement operator<<, to_stream, from_stream.
  308. };
  309. inline constexpr month January{1};
  310. inline constexpr month February{2};
  311. inline constexpr month March{3};
  312. inline constexpr month April{4};
  313. inline constexpr month May{5};
  314. inline constexpr month June{6};
  315. inline constexpr month July{7};
  316. inline constexpr month August{8};
  317. inline constexpr month September{9};
  318. inline constexpr month October{10};
  319. inline constexpr month November{11};
  320. inline constexpr month December{12};
  321. // YEAR
  322. class year
  323. {
  324. private:
  325. short _M_y;
  326. public:
  327. year() = default;
  328. explicit constexpr
  329. year(int __y) noexcept
  330. : _M_y{static_cast<short>(__y)}
  331. { }
  332. static constexpr year
  333. min() noexcept
  334. { return year{-32767}; }
  335. static constexpr year
  336. max() noexcept
  337. { return year{32767}; }
  338. constexpr year&
  339. operator++() noexcept
  340. {
  341. ++_M_y;
  342. return *this;
  343. }
  344. constexpr year
  345. operator++(int) noexcept
  346. {
  347. auto __ret = *this;
  348. ++(*this);
  349. return __ret;
  350. }
  351. constexpr year&
  352. operator--() noexcept
  353. {
  354. --_M_y;
  355. return *this;
  356. }
  357. constexpr year
  358. operator--(int) noexcept
  359. {
  360. auto __ret = *this;
  361. --(*this);
  362. return __ret;
  363. }
  364. constexpr year&
  365. operator+=(const years& __y) noexcept
  366. {
  367. *this = *this + __y;
  368. return *this;
  369. }
  370. constexpr year&
  371. operator-=(const years& __y) noexcept
  372. {
  373. *this = *this - __y;
  374. return *this;
  375. }
  376. constexpr year
  377. operator+() const noexcept
  378. { return *this; }
  379. constexpr year
  380. operator-() const noexcept
  381. { return year{-_M_y}; }
  382. constexpr bool
  383. is_leap() const noexcept
  384. {
  385. // Testing divisibility by 100 first gives better performance, that is,
  386. // return (_M_y % 100 != 0 || _M_y % 400 == 0) && _M_y % 4 == 0;
  387. // It gets even faster if _M_y is in [-536870800, 536870999]
  388. // (which is the case here) and _M_y % 100 is replaced by
  389. // __is_multiple_of_100 below.
  390. // References:
  391. // [1] https://github.com/cassioneri/calendar
  392. // [2] https://accu.org/journals/overload/28/155/overload155.pdf#page=16
  393. // Furthermore, if y%100 == 0, then y%400==0 is equivalent to y%16==0,
  394. // so we can simplify it to (!mult_100 && y % 4 == 0) || y % 16 == 0,
  395. // which is equivalent to (y & (mult_100 ? 15 : 3)) == 0.
  396. // See https://gcc.gnu.org/pipermail/libstdc++/2021-June/052815.html
  397. constexpr uint32_t __multiplier = 42949673;
  398. constexpr uint32_t __bound = 42949669;
  399. constexpr uint32_t __max_dividend = 1073741799;
  400. constexpr uint32_t __offset = __max_dividend / 2 / 100 * 100;
  401. const bool __is_multiple_of_100
  402. = __multiplier * (_M_y + __offset) < __bound;
  403. return (_M_y & (__is_multiple_of_100 ? 15 : 3)) == 0;
  404. }
  405. explicit constexpr
  406. operator int() const noexcept
  407. { return _M_y; }
  408. constexpr bool
  409. ok() const noexcept
  410. { return min()._M_y <= _M_y && _M_y <= max()._M_y; }
  411. friend constexpr bool
  412. operator==(const year& __x, const year& __y) noexcept
  413. { return int{__x} == int{__y}; }
  414. friend constexpr strong_ordering
  415. operator<=>(const year& __x, const year& __y) noexcept
  416. { return int{__x} <=> int{__y}; }
  417. friend constexpr year
  418. operator+(const year& __x, const years& __y) noexcept
  419. { return year{int{__x} + static_cast<int>(__y.count())}; }
  420. friend constexpr year
  421. operator+(const years& __x, const year& __y) noexcept
  422. { return __y + __x; }
  423. friend constexpr year
  424. operator-(const year& __x, const years& __y) noexcept
  425. { return __x + -__y; }
  426. friend constexpr years
  427. operator-(const year& __x, const year& __y) noexcept
  428. { return years{int{__x} - int{__y}}; }
  429. friend constexpr year_month
  430. operator/(const year& __y, int __m) noexcept;
  431. friend constexpr year_month_day
  432. operator/(const year& __y, const month_day& __md) noexcept;
  433. friend constexpr year_month_day
  434. operator/(const month_day& __md, const year& __y) noexcept;
  435. friend constexpr year_month_day_last
  436. operator/(const year& __y, const month_day_last& __mdl) noexcept;
  437. friend constexpr year_month_day_last
  438. operator/(const month_day_last& __mdl, const year& __y) noexcept;
  439. friend constexpr year_month_weekday
  440. operator/(const year& __y, const month_weekday& __mwd) noexcept;
  441. friend constexpr year_month_weekday
  442. operator/(const month_weekday& __mwd, const year& __y) noexcept;
  443. friend constexpr year_month_weekday_last
  444. operator/(const year& __y, const month_weekday_last& __mwdl) noexcept;
  445. friend constexpr year_month_weekday_last
  446. operator/(const month_weekday_last& __mwdl, const year& __y) noexcept;
  447. // TODO: Implement operator<<, to_stream, from_stream.
  448. };
  449. // WEEKDAY
  450. class weekday
  451. {
  452. private:
  453. unsigned char _M_wd;
  454. static constexpr weekday
  455. _S_from_days(const days& __d)
  456. {
  457. auto __n = __d.count();
  458. return weekday(__n >= -4 ? (__n + 4) % 7 : (__n + 5) % 7 + 6);
  459. }
  460. public:
  461. weekday() = default;
  462. explicit constexpr
  463. weekday(unsigned __wd) noexcept
  464. : _M_wd(__wd == 7 ? 0 : __wd) // __wd % 7 ?
  465. { }
  466. constexpr
  467. weekday(const sys_days& __dp) noexcept
  468. : weekday{_S_from_days(__dp.time_since_epoch())}
  469. { }
  470. explicit constexpr
  471. weekday(const local_days& __dp) noexcept
  472. : weekday{sys_days{__dp.time_since_epoch()}}
  473. { }
  474. constexpr weekday&
  475. operator++() noexcept
  476. {
  477. *this += days{1};
  478. return *this;
  479. }
  480. constexpr weekday
  481. operator++(int) noexcept
  482. {
  483. auto __ret = *this;
  484. ++(*this);
  485. return __ret;
  486. }
  487. constexpr weekday&
  488. operator--() noexcept
  489. {
  490. *this -= days{1};
  491. return *this;
  492. }
  493. constexpr weekday
  494. operator--(int) noexcept
  495. {
  496. auto __ret = *this;
  497. --(*this);
  498. return __ret;
  499. }
  500. constexpr weekday&
  501. operator+=(const days& __d) noexcept
  502. {
  503. *this = *this + __d;
  504. return *this;
  505. }
  506. constexpr weekday&
  507. operator-=(const days& __d) noexcept
  508. {
  509. *this = *this - __d;
  510. return *this;
  511. }
  512. constexpr unsigned
  513. c_encoding() const noexcept
  514. { return _M_wd; }
  515. constexpr unsigned
  516. iso_encoding() const noexcept
  517. { return _M_wd == 0u ? 7u : _M_wd; }
  518. constexpr bool
  519. ok() const noexcept
  520. { return _M_wd <= 6; }
  521. constexpr weekday_indexed
  522. operator[](unsigned __index) const noexcept;
  523. constexpr weekday_last
  524. operator[](last_spec) const noexcept;
  525. friend constexpr bool
  526. operator==(const weekday& __x, const weekday& __y) noexcept
  527. { return __x._M_wd == __y._M_wd; }
  528. friend constexpr weekday
  529. operator+(const weekday& __x, const days& __y) noexcept
  530. {
  531. auto __n = static_cast<long long>(__x._M_wd) + __y.count();
  532. return weekday{__detail::__modulo(__n, 7)};
  533. }
  534. friend constexpr weekday
  535. operator+(const days& __x, const weekday& __y) noexcept
  536. { return __y + __x; }
  537. friend constexpr weekday
  538. operator-(const weekday& __x, const days& __y) noexcept
  539. { return __x + -__y; }
  540. friend constexpr days
  541. operator-(const weekday& __x, const weekday& __y) noexcept
  542. {
  543. auto __n = static_cast<long long>(__x._M_wd) - __y._M_wd;
  544. return days{__detail::__modulo(__n, 7)};
  545. }
  546. // TODO: operator<<, from_stream.
  547. };
  548. inline constexpr weekday Sunday{0};
  549. inline constexpr weekday Monday{1};
  550. inline constexpr weekday Tuesday{2};
  551. inline constexpr weekday Wednesday{3};
  552. inline constexpr weekday Thursday{4};
  553. inline constexpr weekday Friday{5};
  554. inline constexpr weekday Saturday{6};
  555. // WEEKDAY_INDEXED
  556. class weekday_indexed
  557. {
  558. private:
  559. chrono::weekday _M_wd;
  560. unsigned char _M_index;
  561. public:
  562. weekday_indexed() = default;
  563. constexpr
  564. weekday_indexed(const chrono::weekday& __wd, unsigned __index) noexcept
  565. : _M_wd(__wd), _M_index(__index)
  566. { }
  567. constexpr chrono::weekday
  568. weekday() const noexcept
  569. { return _M_wd; }
  570. constexpr unsigned
  571. index() const noexcept
  572. { return _M_index; };
  573. constexpr bool
  574. ok() const noexcept
  575. { return _M_wd.ok() && 1 <= _M_index && _M_index <= 5; }
  576. friend constexpr bool
  577. operator==(const weekday_indexed& __x, const weekday_indexed& __y) noexcept
  578. { return __x.weekday() == __y.weekday() && __x.index() == __y.index(); }
  579. friend constexpr month_weekday
  580. operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
  581. friend constexpr month_weekday
  582. operator/(int __m, const weekday_indexed& __wdi) noexcept;
  583. friend constexpr month_weekday
  584. operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
  585. friend constexpr month_weekday
  586. operator/(const weekday_indexed& __wdi, int __m) noexcept;
  587. friend constexpr year_month_weekday
  588. operator/(const year_month& __ym, const weekday_indexed& __wdi) noexcept;
  589. // TODO: Implement operator<<.
  590. };
  591. constexpr weekday_indexed
  592. weekday::operator[](unsigned __index) const noexcept
  593. { return {*this, __index}; }
  594. // WEEKDAY_LAST
  595. class weekday_last
  596. {
  597. private:
  598. chrono::weekday _M_wd;
  599. public:
  600. explicit constexpr
  601. weekday_last(const chrono::weekday& __wd) noexcept
  602. : _M_wd{__wd}
  603. { }
  604. constexpr chrono::weekday
  605. weekday() const noexcept
  606. { return _M_wd; }
  607. constexpr bool
  608. ok() const noexcept
  609. { return _M_wd.ok(); }
  610. friend constexpr bool
  611. operator==(const weekday_last& __x, const weekday_last& __y) noexcept
  612. { return __x.weekday() == __y.weekday(); }
  613. friend constexpr month_weekday_last
  614. operator/(int __m, const weekday_last& __wdl) noexcept;
  615. friend constexpr month_weekday_last
  616. operator/(const weekday_last& __wdl, int __m) noexcept;
  617. friend constexpr year_month_weekday_last
  618. operator/(const year_month& __ym, const weekday_last& __wdl) noexcept;
  619. // TODO: Implement operator<<.
  620. };
  621. constexpr weekday_last
  622. weekday::operator[](last_spec) const noexcept
  623. { return weekday_last{*this}; }
  624. // MONTH_DAY
  625. class month_day
  626. {
  627. private:
  628. chrono::month _M_m;
  629. chrono::day _M_d;
  630. public:
  631. month_day() = default;
  632. constexpr
  633. month_day(const chrono::month& __m, const chrono::day& __d) noexcept
  634. : _M_m{__m}, _M_d{__d}
  635. { }
  636. constexpr chrono::month
  637. month() const noexcept
  638. { return _M_m; }
  639. constexpr chrono::day
  640. day() const noexcept
  641. { return _M_d; }
  642. constexpr bool
  643. ok() const noexcept
  644. {
  645. return _M_m.ok()
  646. && 1u <= unsigned(_M_d)
  647. && unsigned(_M_d) <= __detail::__days_per_month[unsigned(_M_m) - 1];
  648. }
  649. friend constexpr bool
  650. operator==(const month_day& __x, const month_day& __y) noexcept
  651. { return __x.month() == __y.month() && __x.day() == __y.day(); }
  652. friend constexpr strong_ordering
  653. operator<=>(const month_day& __x, const month_day& __y) noexcept
  654. = default;
  655. friend constexpr month_day
  656. operator/(const chrono::month& __m, const chrono::day& __d) noexcept
  657. { return {__m, __d}; }
  658. friend constexpr month_day
  659. operator/(const chrono::month& __m, int __d) noexcept
  660. { return {__m, chrono::day(unsigned(__d))}; }
  661. friend constexpr month_day
  662. operator/(int __m, const chrono::day& __d) noexcept
  663. { return {chrono::month(unsigned(__m)), __d}; }
  664. friend constexpr month_day
  665. operator/(const chrono::day& __d, const chrono::month& __m) noexcept
  666. { return {__m, __d}; }
  667. friend constexpr month_day
  668. operator/(const chrono::day& __d, int __m) noexcept
  669. { return {chrono::month(unsigned(__m)), __d}; }
  670. friend constexpr year_month_day
  671. operator/(int __y, const month_day& __md) noexcept;
  672. friend constexpr year_month_day
  673. operator/(const month_day& __md, int __y) noexcept;
  674. // TODO: Implement operator<<, from_stream.
  675. };
  676. // MONTH_DAY_LAST
  677. class month_day_last
  678. {
  679. private:
  680. chrono::month _M_m;
  681. public:
  682. explicit constexpr
  683. month_day_last(const chrono::month& __m) noexcept
  684. : _M_m{__m}
  685. { }
  686. constexpr chrono::month
  687. month() const noexcept
  688. { return _M_m; }
  689. constexpr bool
  690. ok() const noexcept
  691. { return _M_m.ok(); }
  692. friend constexpr bool
  693. operator==(const month_day_last& __x, const month_day_last& __y) noexcept
  694. { return __x.month() == __y.month(); }
  695. friend constexpr strong_ordering
  696. operator<=>(const month_day_last& __x, const month_day_last& __y) noexcept
  697. = default;
  698. friend constexpr month_day_last
  699. operator/(const chrono::month& __m, last_spec) noexcept
  700. { return month_day_last{__m}; }
  701. friend constexpr month_day_last
  702. operator/(int __m, last_spec) noexcept
  703. { return chrono::month(unsigned(__m)) / last; }
  704. friend constexpr month_day_last
  705. operator/(last_spec, const chrono::month& __m) noexcept
  706. { return __m / last; }
  707. friend constexpr month_day_last
  708. operator/(last_spec, int __m) noexcept
  709. { return __m / last; }
  710. friend constexpr year_month_day_last
  711. operator/(int __y, const month_day_last& __mdl) noexcept;
  712. friend constexpr year_month_day_last
  713. operator/(const month_day_last& __mdl, int __y) noexcept;
  714. // TODO: Implement operator<<.
  715. };
  716. // MONTH_WEEKDAY
  717. class month_weekday
  718. {
  719. private:
  720. chrono::month _M_m;
  721. chrono::weekday_indexed _M_wdi;
  722. public:
  723. constexpr
  724. month_weekday(const chrono::month& __m,
  725. const chrono::weekday_indexed& __wdi) noexcept
  726. : _M_m{__m}, _M_wdi{__wdi}
  727. { }
  728. constexpr chrono::month
  729. month() const noexcept
  730. { return _M_m; }
  731. constexpr chrono::weekday_indexed
  732. weekday_indexed() const noexcept
  733. { return _M_wdi; }
  734. constexpr bool
  735. ok() const noexcept
  736. { return _M_m.ok() && _M_wdi.ok(); }
  737. friend constexpr bool
  738. operator==(const month_weekday& __x, const month_weekday& __y) noexcept
  739. {
  740. return __x.month() == __y.month()
  741. && __x.weekday_indexed() == __y.weekday_indexed();
  742. }
  743. friend constexpr month_weekday
  744. operator/(const chrono::month& __m,
  745. const chrono::weekday_indexed& __wdi) noexcept
  746. { return {__m, __wdi}; }
  747. friend constexpr month_weekday
  748. operator/(int __m, const chrono::weekday_indexed& __wdi) noexcept
  749. { return chrono::month(unsigned(__m)) / __wdi; }
  750. friend constexpr month_weekday
  751. operator/(const chrono::weekday_indexed& __wdi,
  752. const chrono::month& __m) noexcept
  753. { return __m / __wdi; }
  754. friend constexpr month_weekday
  755. operator/(const chrono::weekday_indexed& __wdi, int __m) noexcept
  756. { return __m / __wdi; }
  757. friend constexpr year_month_weekday
  758. operator/(int __y, const month_weekday& __mwd) noexcept;
  759. friend constexpr year_month_weekday
  760. operator/(const month_weekday& __mwd, int __y) noexcept;
  761. // TODO: Implement operator<<.
  762. };
  763. // MONTH_WEEKDAY_LAST
  764. class month_weekday_last
  765. {
  766. private:
  767. chrono::month _M_m;
  768. chrono::weekday_last _M_wdl;
  769. public:
  770. constexpr
  771. month_weekday_last(const chrono::month& __m,
  772. const chrono::weekday_last& __wdl) noexcept
  773. :_M_m{__m}, _M_wdl{__wdl}
  774. { }
  775. constexpr chrono::month
  776. month() const noexcept
  777. { return _M_m; }
  778. constexpr chrono::weekday_last
  779. weekday_last() const noexcept
  780. { return _M_wdl; }
  781. constexpr bool
  782. ok() const noexcept
  783. { return _M_m.ok() && _M_wdl.ok(); }
  784. friend constexpr bool
  785. operator==(const month_weekday_last& __x,
  786. const month_weekday_last& __y) noexcept
  787. {
  788. return __x.month() == __y.month()
  789. && __x.weekday_last() == __y.weekday_last();
  790. }
  791. friend constexpr month_weekday_last
  792. operator/(const chrono::month& __m,
  793. const chrono::weekday_last& __wdl) noexcept
  794. { return {__m, __wdl}; }
  795. friend constexpr month_weekday_last
  796. operator/(int __m, const chrono::weekday_last& __wdl) noexcept
  797. { return chrono::month(unsigned(__m)) / __wdl; }
  798. friend constexpr month_weekday_last
  799. operator/(const chrono::weekday_last& __wdl,
  800. const chrono::month& __m) noexcept
  801. { return __m / __wdl; }
  802. friend constexpr month_weekday_last
  803. operator/(const chrono::weekday_last& __wdl, int __m) noexcept
  804. { return chrono::month(unsigned(__m)) / __wdl; }
  805. friend constexpr year_month_weekday_last
  806. operator/(int __y, const month_weekday_last& __mwdl) noexcept;
  807. friend constexpr year_month_weekday_last
  808. operator/(const month_weekday_last& __mwdl, int __y) noexcept;
  809. // TODO: Implement operator<<.
  810. };
  811. // YEAR_MONTH
  812. namespace __detail
  813. {
  814. // [time.cal.ym], [time.cal.ymd], etc constrain the 'months'-based
  815. // addition/subtraction operator overloads like so:
  816. //
  817. // Constraints: if the argument supplied by the caller for the months
  818. // parameter is convertible to years, its implicit conversion sequence
  819. // to years is worse than its implicit conversion sequence to months.
  820. //
  821. // We realize this constraint by templatizing the 'months'-based
  822. // overloads (using a dummy defaulted template parameter), so that
  823. // overload resolution doesn't select the 'months'-based overload unless
  824. // the implicit conversion sequence to 'months' is better than that to
  825. // 'years'.
  826. using __months_years_conversion_disambiguator = void;
  827. }
  828. class year_month
  829. {
  830. private:
  831. chrono::year _M_y;
  832. chrono::month _M_m;
  833. public:
  834. year_month() = default;
  835. constexpr
  836. year_month(const chrono::year& __y, const chrono::month& __m) noexcept
  837. : _M_y{__y}, _M_m{__m}
  838. { }
  839. constexpr chrono::year
  840. year() const noexcept
  841. { return _M_y; }
  842. constexpr chrono::month
  843. month() const noexcept
  844. { return _M_m; }
  845. template<typename = __detail::__months_years_conversion_disambiguator>
  846. constexpr year_month&
  847. operator+=(const months& __dm) noexcept
  848. {
  849. *this = *this + __dm;
  850. return *this;
  851. }
  852. template<typename = __detail::__months_years_conversion_disambiguator>
  853. constexpr year_month&
  854. operator-=(const months& __dm) noexcept
  855. {
  856. *this = *this - __dm;
  857. return *this;
  858. }
  859. constexpr year_month&
  860. operator+=(const years& __dy) noexcept
  861. {
  862. *this = *this + __dy;
  863. return *this;
  864. }
  865. constexpr year_month&
  866. operator-=(const years& __dy) noexcept
  867. {
  868. *this = *this - __dy;
  869. return *this;
  870. }
  871. constexpr bool
  872. ok() const noexcept
  873. { return _M_y.ok() && _M_m.ok(); }
  874. friend constexpr bool
  875. operator==(const year_month& __x, const year_month& __y) noexcept
  876. { return __x.year() == __y.year() && __x.month() == __y.month(); }
  877. friend constexpr strong_ordering
  878. operator<=>(const year_month& __x, const year_month& __y) noexcept
  879. = default;
  880. template<typename = __detail::__months_years_conversion_disambiguator>
  881. friend constexpr year_month
  882. operator+(const year_month& __ym, const months& __dm) noexcept
  883. {
  884. // TODO: Optimize?
  885. auto __m = __ym.month() + __dm;
  886. auto __i = int(unsigned(__ym.month())) - 1 + __dm.count();
  887. auto __y = (__i < 0
  888. ? __ym.year() + years{(__i - 11) / 12}
  889. : __ym.year() + years{__i / 12});
  890. return __y / __m;
  891. }
  892. template<typename = __detail::__months_years_conversion_disambiguator>
  893. friend constexpr year_month
  894. operator+(const months& __dm, const year_month& __ym) noexcept
  895. { return __ym + __dm; }
  896. template<typename = __detail::__months_years_conversion_disambiguator>
  897. friend constexpr year_month
  898. operator-(const year_month& __ym, const months& __dm) noexcept
  899. { return __ym + -__dm; }
  900. friend constexpr months
  901. operator-(const year_month& __x, const year_month& __y) noexcept
  902. {
  903. return (__x.year() - __y.year()
  904. + months{static_cast<int>(unsigned{__x.month()})
  905. - static_cast<int>(unsigned{__y.month()})});
  906. }
  907. friend constexpr year_month
  908. operator+(const year_month& __ym, const years& __dy) noexcept
  909. { return (__ym.year() + __dy) / __ym.month(); }
  910. friend constexpr year_month
  911. operator+(const years& __dy, const year_month& __ym) noexcept
  912. { return __ym + __dy; }
  913. friend constexpr year_month
  914. operator-(const year_month& __ym, const years& __dy) noexcept
  915. { return __ym + -__dy; }
  916. friend constexpr year_month
  917. operator/(const chrono::year& __y, const chrono::month& __m) noexcept
  918. { return {__y, __m}; }
  919. friend constexpr year_month
  920. operator/(const chrono::year& __y, int __m) noexcept
  921. { return {__y, chrono::month(unsigned(__m))}; }
  922. friend constexpr year_month_day
  923. operator/(const year_month& __ym, int __d) noexcept;
  924. friend constexpr year_month_day_last
  925. operator/(const year_month& __ym, last_spec) noexcept;
  926. // TODO: Implement operator<<, from_stream.
  927. };
  928. // YEAR_MONTH_DAY
  929. class year_month_day
  930. {
  931. private:
  932. chrono::year _M_y;
  933. chrono::month _M_m;
  934. chrono::day _M_d;
  935. static constexpr year_month_day _S_from_days(const days& __dp) noexcept;
  936. constexpr days _M_days_since_epoch() const noexcept;
  937. public:
  938. year_month_day() = default;
  939. constexpr
  940. year_month_day(const chrono::year& __y, const chrono::month& __m,
  941. const chrono::day& __d) noexcept
  942. : _M_y{__y}, _M_m{__m}, _M_d{__d}
  943. { }
  944. constexpr
  945. year_month_day(const year_month_day_last& __ymdl) noexcept;
  946. constexpr
  947. year_month_day(const sys_days& __dp) noexcept
  948. : year_month_day(_S_from_days(__dp.time_since_epoch()))
  949. { }
  950. explicit constexpr
  951. year_month_day(const local_days& __dp) noexcept
  952. : year_month_day(sys_days{__dp.time_since_epoch()})
  953. { }
  954. template<typename = __detail::__months_years_conversion_disambiguator>
  955. constexpr year_month_day&
  956. operator+=(const months& __m) noexcept
  957. {
  958. *this = *this + __m;
  959. return *this;
  960. }
  961. template<typename = __detail::__months_years_conversion_disambiguator>
  962. constexpr year_month_day&
  963. operator-=(const months& __m) noexcept
  964. {
  965. *this = *this - __m;
  966. return *this;
  967. }
  968. constexpr year_month_day&
  969. operator+=(const years& __y) noexcept
  970. {
  971. *this = *this + __y;
  972. return *this;
  973. }
  974. constexpr year_month_day&
  975. operator-=(const years& __y) noexcept
  976. {
  977. *this = *this - __y;
  978. return *this;
  979. }
  980. constexpr chrono::year
  981. year() const noexcept
  982. { return _M_y; }
  983. constexpr chrono::month
  984. month() const noexcept
  985. { return _M_m; }
  986. constexpr chrono::day
  987. day() const noexcept
  988. { return _M_d; }
  989. constexpr
  990. operator sys_days() const noexcept
  991. { return sys_days{_M_days_since_epoch()}; }
  992. explicit constexpr
  993. operator local_days() const noexcept
  994. { return local_days{sys_days{*this}.time_since_epoch()}; }
  995. constexpr bool ok() const noexcept;
  996. friend constexpr bool
  997. operator==(const year_month_day& __x, const year_month_day& __y) noexcept
  998. {
  999. return __x.year() == __y.year()
  1000. && __x.month() == __y.month()
  1001. && __x.day() == __y.day();
  1002. }
  1003. friend constexpr strong_ordering
  1004. operator<=>(const year_month_day& __x, const year_month_day& __y) noexcept
  1005. = default;
  1006. template<typename = __detail::__months_years_conversion_disambiguator>
  1007. friend constexpr year_month_day
  1008. operator+(const year_month_day& __ymd, const months& __dm) noexcept
  1009. { return (__ymd.year() / __ymd.month() + __dm) / __ymd.day(); }
  1010. template<typename = __detail::__months_years_conversion_disambiguator>
  1011. friend constexpr year_month_day
  1012. operator+(const months& __dm, const year_month_day& __ymd) noexcept
  1013. { return __ymd + __dm; }
  1014. friend constexpr year_month_day
  1015. operator+(const year_month_day& __ymd, const years& __dy) noexcept
  1016. { return (__ymd.year() + __dy) / __ymd.month() / __ymd.day(); }
  1017. friend constexpr year_month_day
  1018. operator+(const years& __dy, const year_month_day& __ymd) noexcept
  1019. { return __ymd + __dy; }
  1020. template<typename = __detail::__months_years_conversion_disambiguator>
  1021. friend constexpr year_month_day
  1022. operator-(const year_month_day& __ymd, const months& __dm) noexcept
  1023. { return __ymd + -__dm; }
  1024. friend constexpr year_month_day
  1025. operator-(const year_month_day& __ymd, const years& __dy) noexcept
  1026. { return __ymd + -__dy; }
  1027. friend constexpr year_month_day
  1028. operator/(const year_month& __ym, const chrono::day& __d) noexcept
  1029. { return {__ym.year(), __ym.month(), __d}; }
  1030. friend constexpr year_month_day
  1031. operator/(const year_month& __ym, int __d) noexcept
  1032. { return __ym / chrono::day{unsigned(__d)}; }
  1033. friend constexpr year_month_day
  1034. operator/(const chrono::year& __y, const month_day& __md) noexcept
  1035. { return __y / __md.month() / __md.day(); }
  1036. friend constexpr year_month_day
  1037. operator/(int __y, const month_day& __md) noexcept
  1038. { return chrono::year{__y} / __md; }
  1039. friend constexpr year_month_day
  1040. operator/(const month_day& __md, const chrono::year& __y) noexcept
  1041. { return __y / __md; }
  1042. friend constexpr year_month_day
  1043. operator/(const month_day& __md, int __y) noexcept
  1044. { return chrono::year(__y) / __md; }
  1045. // TODO: Implement operator<<, from_stream.
  1046. };
  1047. // Construct from days since 1970/01/01.
  1048. // Proposition 6.3 of Neri and Schneider,
  1049. // "Euclidean Affine Functions and Applications to Calendar Algorithms".
  1050. // https://arxiv.org/abs/2102.06959
  1051. constexpr year_month_day
  1052. year_month_day::_S_from_days(const days& __dp) noexcept
  1053. {
  1054. constexpr auto __z2 = static_cast<uint32_t>(-1468000);
  1055. constexpr auto __r2_e3 = static_cast<uint32_t>(536895458);
  1056. const auto __r0 = static_cast<uint32_t>(__dp.count()) + __r2_e3;
  1057. const auto __n1 = 4 * __r0 + 3;
  1058. const auto __q1 = __n1 / 146097;
  1059. const auto __r1 = __n1 % 146097 / 4;
  1060. constexpr auto __p32 = static_cast<uint64_t>(1) << 32;
  1061. const auto __n2 = 4 * __r1 + 3;
  1062. const auto __u2 = static_cast<uint64_t>(2939745) * __n2;
  1063. const auto __q2 = static_cast<uint32_t>(__u2 / __p32);
  1064. const auto __r2 = static_cast<uint32_t>(__u2 % __p32) / 2939745 / 4;
  1065. constexpr auto __p16 = static_cast<uint32_t>(1) << 16;
  1066. const auto __n3 = 2141 * __r2 + 197913;
  1067. const auto __q3 = __n3 / __p16;
  1068. const auto __r3 = __n3 % __p16 / 2141;
  1069. const auto __y0 = 100 * __q1 + __q2;
  1070. const auto __m0 = __q3;
  1071. const auto __d0 = __r3;
  1072. const auto __j = __r2 >= 306;
  1073. const auto __y1 = __y0 + __j;
  1074. const auto __m1 = __j ? __m0 - 12 : __m0;
  1075. const auto __d1 = __d0 + 1;
  1076. return year_month_day{chrono::year{static_cast<int>(__y1 + __z2)},
  1077. chrono::month{__m1}, chrono::day{__d1}};
  1078. }
  1079. // Days since 1970/01/01.
  1080. // Proposition 6.2 of Neri and Schneider,
  1081. // "Euclidean Affine Functions and Applications to Calendar Algorithms".
  1082. // https://arxiv.org/abs/2102.06959
  1083. constexpr days
  1084. year_month_day::_M_days_since_epoch() const noexcept
  1085. {
  1086. auto constexpr __z2 = static_cast<uint32_t>(-1468000);
  1087. auto constexpr __r2_e3 = static_cast<uint32_t>(536895458);
  1088. const auto __y1 = static_cast<uint32_t>(static_cast<int>(_M_y)) - __z2;
  1089. const auto __m1 = static_cast<uint32_t>(static_cast<unsigned>(_M_m));
  1090. const auto __d1 = static_cast<uint32_t>(static_cast<unsigned>(_M_d));
  1091. const auto __j = static_cast<uint32_t>(__m1 < 3);
  1092. const auto __y0 = __y1 - __j;
  1093. const auto __m0 = __j ? __m1 + 12 : __m1;
  1094. const auto __d0 = __d1 - 1;
  1095. const auto __q1 = __y0 / 100;
  1096. const auto __yc = 1461 * __y0 / 4 - __q1 + __q1 / 4;
  1097. const auto __mc = (979 *__m0 - 2919) / 32;
  1098. const auto __dc = __d0;
  1099. return days{static_cast<int32_t>(__yc + __mc + __dc - __r2_e3)};
  1100. }
  1101. // YEAR_MONTH_DAY_LAST
  1102. class year_month_day_last
  1103. {
  1104. private:
  1105. chrono::year _M_y;
  1106. chrono::month_day_last _M_mdl;
  1107. public:
  1108. constexpr
  1109. year_month_day_last(const chrono::year& __y,
  1110. const chrono::month_day_last& __mdl) noexcept
  1111. : _M_y{__y}, _M_mdl{__mdl}
  1112. { }
  1113. template<typename = __detail::__months_years_conversion_disambiguator>
  1114. constexpr year_month_day_last&
  1115. operator+=(const months& __m) noexcept
  1116. {
  1117. *this = *this + __m;
  1118. return *this;
  1119. }
  1120. template<typename = __detail::__months_years_conversion_disambiguator>
  1121. constexpr year_month_day_last&
  1122. operator-=(const months& __m) noexcept
  1123. {
  1124. *this = *this - __m;
  1125. return *this;
  1126. }
  1127. constexpr year_month_day_last&
  1128. operator+=(const years& __y) noexcept
  1129. {
  1130. *this = *this + __y;
  1131. return *this;
  1132. }
  1133. constexpr year_month_day_last&
  1134. operator-=(const years& __y) noexcept
  1135. {
  1136. *this = *this - __y;
  1137. return *this;
  1138. }
  1139. constexpr chrono::year
  1140. year() const noexcept
  1141. { return _M_y; }
  1142. constexpr chrono::month
  1143. month() const noexcept
  1144. { return _M_mdl.month(); }
  1145. constexpr chrono::month_day_last
  1146. month_day_last() const noexcept
  1147. { return _M_mdl; }
  1148. // Return A day representing the last day of this year, month pair.
  1149. constexpr chrono::day
  1150. day() const noexcept
  1151. {
  1152. const auto __m = static_cast<unsigned>(month());
  1153. // Excluding February, the last day of month __m is either 30 or 31 or,
  1154. // in another words, it is 30 + b = 30 | b, where b is in {0, 1}.
  1155. // If __m in {1, 3, 4, 5, 6, 7}, then b is 1 if, and only if __m is odd.
  1156. // Hence, b = __m & 1 = (__m ^ 0) & 1.
  1157. // If __m in {8, 9, 10, 11, 12}, then b is 1 if, and only if __m is even.
  1158. // Hence, b = (__m ^ 1) & 1.
  1159. // Therefore, b = (__m ^ c) & 1, where c = 0, if __m < 8, or c = 1 if
  1160. // __m >= 8, that is, c = __m >> 3.
  1161. // The above mathematically justifies this implementation whose
  1162. // performance does not depend on look-up tables being on the L1 cache.
  1163. return chrono::day{__m != 2 ? ((__m ^ (__m >> 3)) & 1) | 30
  1164. : _M_y.is_leap() ? 29 : 28};
  1165. }
  1166. constexpr
  1167. operator sys_days() const noexcept
  1168. { return sys_days{year() / month() / day()}; }
  1169. explicit constexpr
  1170. operator local_days() const noexcept
  1171. { return local_days{sys_days{*this}.time_since_epoch()}; }
  1172. constexpr bool
  1173. ok() const noexcept
  1174. { return _M_y.ok() && _M_mdl.ok(); }
  1175. friend constexpr bool
  1176. operator==(const year_month_day_last& __x,
  1177. const year_month_day_last& __y) noexcept
  1178. {
  1179. return __x.year() == __y.year()
  1180. && __x.month_day_last() == __y.month_day_last();
  1181. }
  1182. friend constexpr strong_ordering
  1183. operator<=>(const year_month_day_last& __x,
  1184. const year_month_day_last& __y) noexcept
  1185. = default;
  1186. template<typename = __detail::__months_years_conversion_disambiguator>
  1187. friend constexpr year_month_day_last
  1188. operator+(const year_month_day_last& __ymdl,
  1189. const months& __dm) noexcept
  1190. { return (__ymdl.year() / __ymdl.month() + __dm) / last; }
  1191. template<typename = __detail::__months_years_conversion_disambiguator>
  1192. friend constexpr year_month_day_last
  1193. operator+(const months& __dm,
  1194. const year_month_day_last& __ymdl) noexcept
  1195. { return __ymdl + __dm; }
  1196. template<typename = __detail::__months_years_conversion_disambiguator>
  1197. friend constexpr year_month_day_last
  1198. operator-(const year_month_day_last& __ymdl,
  1199. const months& __dm) noexcept
  1200. { return __ymdl + -__dm; }
  1201. friend constexpr year_month_day_last
  1202. operator+(const year_month_day_last& __ymdl,
  1203. const years& __dy) noexcept
  1204. { return {__ymdl.year() + __dy, __ymdl.month_day_last()}; }
  1205. friend constexpr year_month_day_last
  1206. operator+(const years& __dy,
  1207. const year_month_day_last& __ymdl) noexcept
  1208. { return __ymdl + __dy; }
  1209. friend constexpr year_month_day_last
  1210. operator-(const year_month_day_last& __ymdl,
  1211. const years& __dy) noexcept
  1212. { return __ymdl + -__dy; }
  1213. friend constexpr year_month_day_last
  1214. operator/(const year_month& __ym, last_spec) noexcept
  1215. { return {__ym.year(), chrono::month_day_last{__ym.month()}}; }
  1216. friend constexpr year_month_day_last
  1217. operator/(const chrono::year& __y,
  1218. const chrono::month_day_last& __mdl) noexcept
  1219. { return {__y, __mdl}; }
  1220. friend constexpr year_month_day_last
  1221. operator/(int __y, const chrono::month_day_last& __mdl) noexcept
  1222. { return chrono::year(__y) / __mdl; }
  1223. friend constexpr year_month_day_last
  1224. operator/(const chrono::month_day_last& __mdl,
  1225. const chrono::year& __y) noexcept
  1226. { return __y / __mdl; }
  1227. friend constexpr year_month_day_last
  1228. operator/(const chrono::month_day_last& __mdl, int __y) noexcept
  1229. { return chrono::year(__y) / __mdl; }
  1230. // TODO: Implement operator<<.
  1231. };
  1232. // year_month_day ctor from year_month_day_last
  1233. constexpr
  1234. year_month_day::year_month_day(const year_month_day_last& __ymdl) noexcept
  1235. : _M_y{__ymdl.year()}, _M_m{__ymdl.month()}, _M_d{__ymdl.day()}
  1236. { }
  1237. constexpr bool
  1238. year_month_day::ok() const noexcept
  1239. {
  1240. if (!_M_y.ok() || !_M_m.ok())
  1241. return false;
  1242. return chrono::day{1} <= _M_d && _M_d <= (_M_y / _M_m / last).day();
  1243. }
  1244. // YEAR_MONTH_WEEKDAY
  1245. class year_month_weekday
  1246. {
  1247. private:
  1248. chrono::year _M_y;
  1249. chrono::month _M_m;
  1250. chrono::weekday_indexed _M_wdi;
  1251. static constexpr year_month_weekday
  1252. _S_from_sys_days(const sys_days& __dp)
  1253. {
  1254. year_month_day __ymd{__dp};
  1255. chrono::weekday __wd{__dp};
  1256. auto __index = __wd[(unsigned{__ymd.day()} - 1) / 7 + 1];
  1257. return {__ymd.year(), __ymd.month(), __index};
  1258. }
  1259. public:
  1260. year_month_weekday() = default;
  1261. constexpr
  1262. year_month_weekday(const chrono::year& __y, const chrono::month& __m,
  1263. const chrono::weekday_indexed& __wdi) noexcept
  1264. : _M_y{__y}, _M_m{__m}, _M_wdi{__wdi}
  1265. { }
  1266. constexpr
  1267. year_month_weekday(const sys_days& __dp) noexcept
  1268. : year_month_weekday{_S_from_sys_days(__dp)}
  1269. { }
  1270. explicit constexpr
  1271. year_month_weekday(const local_days& __dp) noexcept
  1272. : year_month_weekday{sys_days{__dp.time_since_epoch()}}
  1273. { }
  1274. template<typename = __detail::__months_years_conversion_disambiguator>
  1275. constexpr year_month_weekday&
  1276. operator+=(const months& __m) noexcept
  1277. {
  1278. *this = *this + __m;
  1279. return *this;
  1280. }
  1281. template<typename = __detail::__months_years_conversion_disambiguator>
  1282. constexpr year_month_weekday&
  1283. operator-=(const months& __m) noexcept
  1284. {
  1285. *this = *this - __m;
  1286. return *this;
  1287. }
  1288. constexpr year_month_weekday&
  1289. operator+=(const years& __y) noexcept
  1290. {
  1291. *this = *this + __y;
  1292. return *this;
  1293. }
  1294. constexpr year_month_weekday&
  1295. operator-=(const years& __y) noexcept
  1296. {
  1297. *this = *this - __y;
  1298. return *this;
  1299. }
  1300. constexpr chrono::year
  1301. year() const noexcept
  1302. { return _M_y; }
  1303. constexpr chrono::month
  1304. month() const noexcept
  1305. { return _M_m; }
  1306. constexpr chrono::weekday
  1307. weekday() const noexcept
  1308. { return _M_wdi.weekday(); }
  1309. constexpr unsigned
  1310. index() const noexcept
  1311. { return _M_wdi.index(); }
  1312. constexpr chrono::weekday_indexed
  1313. weekday_indexed() const noexcept
  1314. { return _M_wdi; }
  1315. constexpr
  1316. operator sys_days() const noexcept
  1317. {
  1318. auto __d = sys_days{year() / month() / 1};
  1319. return __d + (weekday() - chrono::weekday(__d)
  1320. + days{(static_cast<int>(index())-1)*7});
  1321. }
  1322. explicit constexpr
  1323. operator local_days() const noexcept
  1324. { return local_days{sys_days{*this}.time_since_epoch()}; }
  1325. constexpr bool
  1326. ok() const noexcept
  1327. {
  1328. if (!_M_y.ok() || !_M_m.ok() || !_M_wdi.ok())
  1329. return false;
  1330. if (_M_wdi.index() <= 4)
  1331. return true;
  1332. days __d = (_M_wdi.weekday()
  1333. - chrono::weekday{sys_days{_M_y / _M_m / 1}}
  1334. + days((_M_wdi.index()-1)*7 + 1));
  1335. __glibcxx_assert(__d.count() >= 1);
  1336. return __d.count() <= unsigned{(_M_y / _M_m / last).day()};
  1337. }
  1338. friend constexpr bool
  1339. operator==(const year_month_weekday& __x,
  1340. const year_month_weekday& __y) noexcept
  1341. {
  1342. return __x.year() == __y.year()
  1343. && __x.month() == __y.month()
  1344. && __x.weekday_indexed() == __y.weekday_indexed();
  1345. }
  1346. template<typename = __detail::__months_years_conversion_disambiguator>
  1347. friend constexpr year_month_weekday
  1348. operator+(const year_month_weekday& __ymwd, const months& __dm) noexcept
  1349. {
  1350. return ((__ymwd.year() / __ymwd.month() + __dm)
  1351. / __ymwd.weekday_indexed());
  1352. }
  1353. template<typename = __detail::__months_years_conversion_disambiguator>
  1354. friend constexpr year_month_weekday
  1355. operator+(const months& __dm, const year_month_weekday& __ymwd) noexcept
  1356. { return __ymwd + __dm; }
  1357. friend constexpr year_month_weekday
  1358. operator+(const year_month_weekday& __ymwd, const years& __dy) noexcept
  1359. { return {__ymwd.year() + __dy, __ymwd.month(), __ymwd.weekday_indexed()}; }
  1360. friend constexpr year_month_weekday
  1361. operator+(const years& __dy, const year_month_weekday& __ymwd) noexcept
  1362. { return __ymwd + __dy; }
  1363. template<typename = __detail::__months_years_conversion_disambiguator>
  1364. friend constexpr year_month_weekday
  1365. operator-(const year_month_weekday& __ymwd, const months& __dm) noexcept
  1366. { return __ymwd + -__dm; }
  1367. friend constexpr year_month_weekday
  1368. operator-(const year_month_weekday& __ymwd, const years& __dy) noexcept
  1369. { return __ymwd + -__dy; }
  1370. friend constexpr year_month_weekday
  1371. operator/(const year_month& __ym,
  1372. const chrono::weekday_indexed& __wdi) noexcept
  1373. { return {__ym.year(), __ym.month(), __wdi}; }
  1374. friend constexpr year_month_weekday
  1375. operator/(const chrono::year& __y, const month_weekday& __mwd) noexcept
  1376. { return {__y, __mwd.month(), __mwd.weekday_indexed()}; }
  1377. friend constexpr year_month_weekday
  1378. operator/(int __y, const month_weekday& __mwd) noexcept
  1379. { return chrono::year(__y) / __mwd; }
  1380. friend constexpr year_month_weekday
  1381. operator/(const month_weekday& __mwd, const chrono::year& __y) noexcept
  1382. { return __y / __mwd; }
  1383. friend constexpr year_month_weekday
  1384. operator/(const month_weekday& __mwd, int __y) noexcept
  1385. { return chrono::year(__y) / __mwd; }
  1386. // TODO: Implement operator<<.
  1387. };
  1388. // YEAR_MONTH_WEEKDAY_LAST
  1389. class year_month_weekday_last
  1390. {
  1391. private:
  1392. chrono::year _M_y;
  1393. chrono::month _M_m;
  1394. chrono::weekday_last _M_wdl;
  1395. public:
  1396. constexpr
  1397. year_month_weekday_last(const chrono::year& __y, const chrono::month& __m,
  1398. const chrono::weekday_last& __wdl) noexcept
  1399. : _M_y{__y}, _M_m{__m}, _M_wdl{__wdl}
  1400. { }
  1401. template<typename = __detail::__months_years_conversion_disambiguator>
  1402. constexpr year_month_weekday_last&
  1403. operator+=(const months& __m) noexcept
  1404. {
  1405. *this = *this + __m;
  1406. return *this;
  1407. }
  1408. template<typename = __detail::__months_years_conversion_disambiguator>
  1409. constexpr year_month_weekday_last&
  1410. operator-=(const months& __m) noexcept
  1411. {
  1412. *this = *this - __m;
  1413. return *this;
  1414. }
  1415. constexpr year_month_weekday_last&
  1416. operator+=(const years& __y) noexcept
  1417. {
  1418. *this = *this + __y;
  1419. return *this;
  1420. }
  1421. constexpr year_month_weekday_last&
  1422. operator-=(const years& __y) noexcept
  1423. {
  1424. *this = *this - __y;
  1425. return *this;
  1426. }
  1427. constexpr chrono::year
  1428. year() const noexcept
  1429. { return _M_y; }
  1430. constexpr chrono::month
  1431. month() const noexcept
  1432. { return _M_m; }
  1433. constexpr chrono::weekday
  1434. weekday() const noexcept
  1435. { return _M_wdl.weekday(); }
  1436. constexpr chrono::weekday_last
  1437. weekday_last() const noexcept
  1438. { return _M_wdl; }
  1439. constexpr
  1440. operator sys_days() const noexcept
  1441. {
  1442. const auto __d = sys_days{_M_y / _M_m / last};
  1443. return sys_days{(__d - (chrono::weekday{__d}
  1444. - _M_wdl.weekday())).time_since_epoch()};
  1445. }
  1446. explicit constexpr
  1447. operator local_days() const noexcept
  1448. { return local_days{sys_days{*this}.time_since_epoch()}; }
  1449. constexpr bool
  1450. ok() const noexcept
  1451. { return _M_y.ok() && _M_m.ok() && _M_wdl.ok(); }
  1452. friend constexpr bool
  1453. operator==(const year_month_weekday_last& __x,
  1454. const year_month_weekday_last& __y) noexcept
  1455. {
  1456. return __x.year() == __y.year()
  1457. && __x.month() == __y.month()
  1458. && __x.weekday_last() == __y.weekday_last();
  1459. }
  1460. template<typename = __detail::__months_years_conversion_disambiguator>
  1461. friend constexpr year_month_weekday_last
  1462. operator+(const year_month_weekday_last& __ymwdl,
  1463. const months& __dm) noexcept
  1464. {
  1465. return ((__ymwdl.year() / __ymwdl.month() + __dm)
  1466. / __ymwdl.weekday_last());
  1467. }
  1468. template<typename = __detail::__months_years_conversion_disambiguator>
  1469. friend constexpr year_month_weekday_last
  1470. operator+(const months& __dm,
  1471. const year_month_weekday_last& __ymwdl) noexcept
  1472. { return __ymwdl + __dm; }
  1473. friend constexpr year_month_weekday_last
  1474. operator+(const year_month_weekday_last& __ymwdl,
  1475. const years& __dy) noexcept
  1476. { return {__ymwdl.year() + __dy, __ymwdl.month(), __ymwdl.weekday_last()}; }
  1477. friend constexpr year_month_weekday_last
  1478. operator+(const years& __dy,
  1479. const year_month_weekday_last& __ymwdl) noexcept
  1480. { return __ymwdl + __dy; }
  1481. template<typename = __detail::__months_years_conversion_disambiguator>
  1482. friend constexpr year_month_weekday_last
  1483. operator-(const year_month_weekday_last& __ymwdl,
  1484. const months& __dm) noexcept
  1485. { return __ymwdl + -__dm; }
  1486. friend constexpr year_month_weekday_last
  1487. operator-(const year_month_weekday_last& __ymwdl,
  1488. const years& __dy) noexcept
  1489. { return __ymwdl + -__dy; }
  1490. friend constexpr year_month_weekday_last
  1491. operator/(const year_month& __ym,
  1492. const chrono::weekday_last& __wdl) noexcept
  1493. { return {__ym.year(), __ym.month(), __wdl}; }
  1494. friend constexpr year_month_weekday_last
  1495. operator/(const chrono::year& __y,
  1496. const chrono::month_weekday_last& __mwdl) noexcept
  1497. { return {__y, __mwdl.month(), __mwdl.weekday_last()}; }
  1498. friend constexpr year_month_weekday_last
  1499. operator/(int __y, const chrono::month_weekday_last& __mwdl) noexcept
  1500. { return chrono::year(__y) / __mwdl; }
  1501. friend constexpr year_month_weekday_last
  1502. operator/(const chrono::month_weekday_last& __mwdl,
  1503. const chrono::year& __y) noexcept
  1504. { return __y / __mwdl; }
  1505. friend constexpr year_month_weekday_last
  1506. operator/(const chrono::month_weekday_last& __mwdl, int __y) noexcept
  1507. { return chrono::year(__y) / __mwdl; }
  1508. // TODO: Implement operator<<.
  1509. };
  1510. // HH_MM_SS
  1511. namespace __detail
  1512. {
  1513. consteval long long
  1514. __pow10(unsigned __n)
  1515. {
  1516. long long __r = 1;
  1517. while (__n-- > 0)
  1518. __r *= 10;
  1519. return __r;
  1520. }
  1521. }
  1522. template<typename _Duration>
  1523. class hh_mm_ss
  1524. {
  1525. private:
  1526. static constexpr int
  1527. _S_fractional_width()
  1528. {
  1529. int __multiplicity_2 = 0;
  1530. int __multiplicity_5 = 0;
  1531. auto __den = _Duration::period::den;
  1532. while ((__den % 2) == 0)
  1533. {
  1534. ++__multiplicity_2;
  1535. __den /= 2;
  1536. }
  1537. while ((__den % 5) == 0)
  1538. {
  1539. ++__multiplicity_5;
  1540. __den /= 5;
  1541. }
  1542. if (__den != 1)
  1543. return 6;
  1544. int __width = (__multiplicity_2 > __multiplicity_5
  1545. ? __multiplicity_2 : __multiplicity_5);
  1546. if (__width > 18)
  1547. __width = 18;
  1548. return __width;
  1549. }
  1550. public:
  1551. static constexpr unsigned fractional_width = {_S_fractional_width()};
  1552. using precision
  1553. = duration<common_type_t<typename _Duration::rep,
  1554. chrono::seconds::rep>,
  1555. ratio<1, __detail::__pow10(fractional_width)>>;
  1556. constexpr
  1557. hh_mm_ss() noexcept
  1558. : hh_mm_ss{_Duration::zero()}
  1559. { }
  1560. constexpr explicit
  1561. hh_mm_ss(_Duration __d) noexcept
  1562. : _M_is_neg (__d < _Duration::zero()),
  1563. _M_h (duration_cast<chrono::hours>(abs(__d))),
  1564. _M_m (duration_cast<chrono::minutes>(abs(__d) - hours())),
  1565. _M_s (duration_cast<chrono::seconds>(abs(__d) - hours() - minutes()))
  1566. {
  1567. if constexpr (treat_as_floating_point_v<typename precision::rep>)
  1568. _M_ss = abs(__d) - hours() - minutes() - seconds();
  1569. else
  1570. _M_ss = duration_cast<precision>(abs(__d) - hours()
  1571. - minutes() - seconds());
  1572. }
  1573. constexpr bool
  1574. is_negative() const noexcept
  1575. { return _M_is_neg; }
  1576. constexpr chrono::hours
  1577. hours() const noexcept
  1578. { return _M_h; }
  1579. constexpr chrono::minutes
  1580. minutes() const noexcept
  1581. { return _M_m; }
  1582. constexpr chrono::seconds
  1583. seconds() const noexcept
  1584. { return _M_s; }
  1585. constexpr precision
  1586. subseconds() const noexcept
  1587. { return _M_ss; }
  1588. constexpr explicit
  1589. operator precision() const noexcept
  1590. { return to_duration(); }
  1591. constexpr precision
  1592. to_duration() const noexcept
  1593. {
  1594. if (_M_is_neg)
  1595. return -(_M_h + _M_m + _M_s + _M_ss);
  1596. else
  1597. return _M_h + _M_m + _M_s + _M_ss;
  1598. }
  1599. // TODO: Implement operator<<.
  1600. private:
  1601. bool _M_is_neg;
  1602. chrono::hours _M_h;
  1603. chrono::minutes _M_m;
  1604. chrono::seconds _M_s;
  1605. precision _M_ss;
  1606. };
  1607. // 12/24 HOURS FUNCTIONS
  1608. constexpr bool
  1609. is_am(const hours& __h) noexcept
  1610. { return 0h <= __h && __h <= 11h; }
  1611. constexpr bool
  1612. is_pm(const hours& __h) noexcept
  1613. { return 12h <= __h && __h <= 23h; }
  1614. constexpr hours
  1615. make12(const hours& __h) noexcept
  1616. {
  1617. if (__h == 0h)
  1618. return 12h;
  1619. else if (__h > 12h)
  1620. return __h - 12h;
  1621. return __h;
  1622. }
  1623. constexpr hours
  1624. make24(const hours& __h, bool __is_pm) noexcept
  1625. {
  1626. if (!__is_pm)
  1627. {
  1628. if (__h == 12h)
  1629. return 0h;
  1630. else
  1631. return __h;
  1632. }
  1633. else
  1634. {
  1635. if (__h == 12h)
  1636. return __h;
  1637. else
  1638. return __h + 12h;
  1639. }
  1640. }
  1641. /// @} group chrono
  1642. #endif // C++20
  1643. } // namespace chrono
  1644. #if __cplusplus >= 202002L
  1645. inline namespace literals
  1646. {
  1647. inline namespace chrono_literals
  1648. {
  1649. /// @addtogroup chrono
  1650. /// @{
  1651. #pragma GCC diagnostic push
  1652. #pragma GCC diagnostic ignored "-Wliteral-suffix"
  1653. /// Literal suffix for creating chrono::day objects.
  1654. /// @since C++20
  1655. constexpr chrono::day
  1656. operator""d(unsigned long long __d) noexcept
  1657. { return chrono::day{static_cast<unsigned>(__d)}; }
  1658. /// Literal suffix for creating chrono::year objects.
  1659. /// @since C++20
  1660. constexpr chrono::year
  1661. operator""y(unsigned long long __y) noexcept
  1662. { return chrono::year{static_cast<int>(__y)}; }
  1663. #pragma GCC diagnostic pop
  1664. /// @}
  1665. } // inline namespace chrono_literals
  1666. } // inline namespace literals
  1667. namespace chrono
  1668. {
  1669. /// @addtogroup chrono
  1670. /// @{
  1671. /// @cond undocumented
  1672. namespace __detail
  1673. {
  1674. template<typename _Period>
  1675. const char*
  1676. __units_suffix_misc(char* __buf, size_t __n) noexcept
  1677. {
  1678. namespace __tc = std::__detail;
  1679. char* __p = __buf;
  1680. __p[0] = '[';
  1681. unsigned __nlen = __tc::__to_chars_len((uintmax_t)_Period::num);
  1682. __tc::__to_chars_10_impl(__p + 1, __nlen, (uintmax_t)_Period::num);
  1683. __p += 1 + __nlen;
  1684. if constexpr (_Period::den != 1)
  1685. {
  1686. __p[0] = '/';
  1687. unsigned __dlen = __tc::__to_chars_len((uintmax_t)_Period::den);
  1688. __tc::__to_chars_10_impl(__p + 1, __dlen, (uintmax_t)_Period::den);
  1689. __p += 1 + __dlen;
  1690. }
  1691. __p[0] = ']';
  1692. __p[1] = 's';
  1693. __p[2] = '\0';
  1694. return __buf;
  1695. }
  1696. template<typename _Period, typename _CharT>
  1697. auto
  1698. __units_suffix(char* __buf, size_t __n) noexcept
  1699. {
  1700. #define _GLIBCXX_UNITS_SUFFIX(period, suffix) \
  1701. if constexpr (is_same_v<_Period, period>) \
  1702. { \
  1703. if constexpr (is_same_v<_CharT, wchar_t>) \
  1704. return L##suffix; \
  1705. else \
  1706. return suffix; \
  1707. } \
  1708. else
  1709. _GLIBCXX_UNITS_SUFFIX(atto, "as")
  1710. _GLIBCXX_UNITS_SUFFIX(femto, "fs")
  1711. _GLIBCXX_UNITS_SUFFIX(pico, "ps")
  1712. _GLIBCXX_UNITS_SUFFIX(nano, "ns")
  1713. _GLIBCXX_UNITS_SUFFIX(micro, "\u00b5s")
  1714. _GLIBCXX_UNITS_SUFFIX(milli, "ms")
  1715. _GLIBCXX_UNITS_SUFFIX(centi, "cs")
  1716. _GLIBCXX_UNITS_SUFFIX(deci, "ds")
  1717. _GLIBCXX_UNITS_SUFFIX(ratio<1>, "s")
  1718. _GLIBCXX_UNITS_SUFFIX(deca, "das")
  1719. _GLIBCXX_UNITS_SUFFIX(hecto, "hs")
  1720. _GLIBCXX_UNITS_SUFFIX(kilo, "ks")
  1721. _GLIBCXX_UNITS_SUFFIX(mega, "Ms")
  1722. _GLIBCXX_UNITS_SUFFIX(giga, "Gs")
  1723. _GLIBCXX_UNITS_SUFFIX(tera, "Ts")
  1724. _GLIBCXX_UNITS_SUFFIX(tera, "Ts")
  1725. _GLIBCXX_UNITS_SUFFIX(peta, "Ps")
  1726. _GLIBCXX_UNITS_SUFFIX(exa, "Es")
  1727. _GLIBCXX_UNITS_SUFFIX(ratio<60>, "min")
  1728. _GLIBCXX_UNITS_SUFFIX(ratio<3600>, "h")
  1729. _GLIBCXX_UNITS_SUFFIX(ratio<86400>, "d")
  1730. #undef _GLIBCXX_UNITS_SUFFIX
  1731. return __detail::__units_suffix_misc<_Period>(__buf, __n);
  1732. }
  1733. } // namespace __detail
  1734. /// @endcond
  1735. template<typename _CharT, typename _Traits,
  1736. typename _Rep, typename _Period>
  1737. inline basic_ostream<_CharT, _Traits>&
  1738. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1739. const duration<_Rep, _Period>& __d)
  1740. {
  1741. using period = typename _Period::type;
  1742. char __buf[sizeof("[/]s") + 2 * numeric_limits<intmax_t>::digits10];
  1743. std::basic_ostringstream<_CharT, _Traits> __s;
  1744. __s.flags(__os.flags());
  1745. __s.imbue(__os.getloc());
  1746. __s.precision(__os.precision());
  1747. __s << __d.count();
  1748. __s << __detail::__units_suffix<period, _CharT>(__buf, sizeof(__buf));
  1749. __os << std::move(__s).str();
  1750. return __os;
  1751. }
  1752. // TODO: from_stream for duration
  1753. /// @} group chrono
  1754. } // namespace chrono
  1755. #endif // C++20
  1756. _GLIBCXX_END_NAMESPACE_VERSION
  1757. } // namespace std
  1758. #endif // C++11
  1759. #endif //_GLIBCXX_CHRONO