chrono 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378
  1. // <chrono> -*- C++ -*-
  2. // Copyright (C) 2008-2021 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 <ratio>
  31. #include <type_traits>
  32. #include <limits>
  33. #include <ctime>
  34. #include <bits/parse_numbers.h> // for literals support.
  35. #if __cplusplus > 201703L
  36. # include <concepts>
  37. # include <compare>
  38. #endif
  39. namespace std _GLIBCXX_VISIBILITY(default)
  40. {
  41. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  42. #if __cplusplus >= 201703L
  43. namespace filesystem { struct __file_clock; };
  44. #endif
  45. /**
  46. * @defgroup chrono Time
  47. * @ingroup utilities
  48. *
  49. * Classes and functions for time.
  50. * @{
  51. */
  52. /** @namespace std::chrono
  53. * @brief ISO C++ 2011 namespace for date and time utilities
  54. */
  55. namespace chrono
  56. {
  57. template<typename _Rep, typename _Period = ratio<1>>
  58. struct duration;
  59. template<typename _Clock, typename _Dur = typename _Clock::duration>
  60. struct time_point;
  61. }
  62. // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly)
  63. /// @cond undocumented
  64. template<typename _CT, typename _Period1, typename _Period2, typename = void>
  65. struct __duration_common_type
  66. { };
  67. template<typename _CT, typename _Period1, typename _Period2>
  68. struct __duration_common_type<_CT, _Period1, _Period2,
  69. __void_t<typename _CT::type>>
  70. {
  71. private:
  72. using __gcd_num = __static_gcd<_Period1::num, _Period2::num>;
  73. using __gcd_den = __static_gcd<_Period1::den, _Period2::den>;
  74. using __cr = typename _CT::type;
  75. using __r = ratio<__gcd_num::value,
  76. (_Period1::den / __gcd_den::value) * _Period2::den>;
  77. public:
  78. using type = chrono::duration<__cr, typename __r::type>;
  79. };
  80. /// @endcond
  81. /// Specialization of common_type for chrono::duration types.
  82. /// @relates duration
  83. template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
  84. struct common_type<chrono::duration<_Rep1, _Period1>,
  85. chrono::duration<_Rep2, _Period2>>
  86. : __duration_common_type<common_type<_Rep1, _Rep2>,
  87. typename _Period1::type,
  88. typename _Period2::type>
  89. { };
  90. /// Specialization of common_type for two identical chrono::duration types.
  91. /// @relates duration
  92. template<typename _Rep, typename _Period>
  93. struct common_type<chrono::duration<_Rep, _Period>,
  94. chrono::duration<_Rep, _Period>>
  95. {
  96. using type = chrono::duration<typename common_type<_Rep>::type,
  97. typename _Period::type>;
  98. };
  99. /// Specialization of common_type for one chrono::duration type.
  100. /// @relates duration
  101. template<typename _Rep, typename _Period>
  102. struct common_type<chrono::duration<_Rep, _Period>>
  103. {
  104. using type = chrono::duration<typename common_type<_Rep>::type,
  105. typename _Period::type>;
  106. };
  107. // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly)
  108. /// @cond undocumented
  109. template<typename _CT, typename _Clock, typename = void>
  110. struct __timepoint_common_type
  111. { };
  112. template<typename _CT, typename _Clock>
  113. struct __timepoint_common_type<_CT, _Clock, __void_t<typename _CT::type>>
  114. {
  115. using type = chrono::time_point<_Clock, typename _CT::type>;
  116. };
  117. /// @endcond
  118. /// Specialization of common_type for chrono::time_point types.
  119. /// @relates time_point
  120. template<typename _Clock, typename _Duration1, typename _Duration2>
  121. struct common_type<chrono::time_point<_Clock, _Duration1>,
  122. chrono::time_point<_Clock, _Duration2>>
  123. : __timepoint_common_type<common_type<_Duration1, _Duration2>, _Clock>
  124. { };
  125. /// Specialization of common_type for two identical chrono::time_point types.
  126. /// @relates time_point
  127. template<typename _Clock, typename _Duration>
  128. struct common_type<chrono::time_point<_Clock, _Duration>,
  129. chrono::time_point<_Clock, _Duration>>
  130. { using type = chrono::time_point<_Clock, _Duration>; };
  131. /// Specialization of common_type for one chrono::time_point type.
  132. /// @relates time_point
  133. template<typename _Clock, typename _Duration>
  134. struct common_type<chrono::time_point<_Clock, _Duration>>
  135. { using type = chrono::time_point<_Clock, _Duration>; };
  136. /// @} group chrono
  137. namespace chrono
  138. {
  139. /// @addtogroup chrono
  140. /// @{
  141. /// @cond undocumented
  142. // Primary template for duration_cast impl.
  143. template<typename _ToDur, typename _CF, typename _CR,
  144. bool _NumIsOne = false, bool _DenIsOne = false>
  145. struct __duration_cast_impl
  146. {
  147. template<typename _Rep, typename _Period>
  148. static constexpr _ToDur
  149. __cast(const duration<_Rep, _Period>& __d)
  150. {
  151. typedef typename _ToDur::rep __to_rep;
  152. return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count())
  153. * static_cast<_CR>(_CF::num)
  154. / static_cast<_CR>(_CF::den)));
  155. }
  156. };
  157. template<typename _ToDur, typename _CF, typename _CR>
  158. struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
  159. {
  160. template<typename _Rep, typename _Period>
  161. static constexpr _ToDur
  162. __cast(const duration<_Rep, _Period>& __d)
  163. {
  164. typedef typename _ToDur::rep __to_rep;
  165. return _ToDur(static_cast<__to_rep>(__d.count()));
  166. }
  167. };
  168. template<typename _ToDur, typename _CF, typename _CR>
  169. struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
  170. {
  171. template<typename _Rep, typename _Period>
  172. static constexpr _ToDur
  173. __cast(const duration<_Rep, _Period>& __d)
  174. {
  175. typedef typename _ToDur::rep __to_rep;
  176. return _ToDur(static_cast<__to_rep>(
  177. static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
  178. }
  179. };
  180. template<typename _ToDur, typename _CF, typename _CR>
  181. struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
  182. {
  183. template<typename _Rep, typename _Period>
  184. static constexpr _ToDur
  185. __cast(const duration<_Rep, _Period>& __d)
  186. {
  187. typedef typename _ToDur::rep __to_rep;
  188. return _ToDur(static_cast<__to_rep>(
  189. static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
  190. }
  191. };
  192. template<typename _Tp>
  193. struct __is_duration
  194. : std::false_type
  195. { };
  196. template<typename _Rep, typename _Period>
  197. struct __is_duration<duration<_Rep, _Period>>
  198. : std::true_type
  199. { };
  200. template<typename _Tp>
  201. using __enable_if_is_duration
  202. = typename enable_if<__is_duration<_Tp>::value, _Tp>::type;
  203. template<typename _Tp>
  204. using __disable_if_is_duration
  205. = typename enable_if<!__is_duration<_Tp>::value, _Tp>::type;
  206. /// @endcond
  207. /// duration_cast
  208. template<typename _ToDur, typename _Rep, typename _Period>
  209. constexpr __enable_if_is_duration<_ToDur>
  210. duration_cast(const duration<_Rep, _Period>& __d)
  211. {
  212. typedef typename _ToDur::period __to_period;
  213. typedef typename _ToDur::rep __to_rep;
  214. typedef ratio_divide<_Period, __to_period> __cf;
  215. typedef typename common_type<__to_rep, _Rep, intmax_t>::type
  216. __cr;
  217. typedef __duration_cast_impl<_ToDur, __cf, __cr,
  218. __cf::num == 1, __cf::den == 1> __dc;
  219. return __dc::__cast(__d);
  220. }
  221. /// treat_as_floating_point
  222. template<typename _Rep>
  223. struct treat_as_floating_point
  224. : is_floating_point<_Rep>
  225. { };
  226. #if __cplusplus > 201402L
  227. template <typename _Rep>
  228. inline constexpr bool treat_as_floating_point_v =
  229. treat_as_floating_point<_Rep>::value;
  230. #endif // C++17
  231. #if __cplusplus > 201703L
  232. template<typename _Tp>
  233. struct is_clock;
  234. template<typename _Tp>
  235. inline constexpr bool is_clock_v = is_clock<_Tp>::value;
  236. #if __cpp_lib_concepts
  237. template<typename _Tp>
  238. struct is_clock : false_type
  239. { };
  240. template<typename _Tp>
  241. requires requires {
  242. typename _Tp::rep;
  243. typename _Tp::period;
  244. typename _Tp::duration;
  245. typename _Tp::time_point::clock;
  246. typename _Tp::time_point::duration;
  247. { &_Tp::is_steady } -> same_as<const bool*>;
  248. { _Tp::now() } -> same_as<typename _Tp::time_point>;
  249. requires same_as<typename _Tp::duration,
  250. duration<typename _Tp::rep, typename _Tp::period>>;
  251. requires same_as<typename _Tp::time_point::duration,
  252. typename _Tp::duration>;
  253. }
  254. struct is_clock<_Tp> : true_type
  255. { };
  256. #else
  257. template<typename _Tp, typename = void>
  258. struct __is_clock_impl : false_type
  259. { };
  260. template<typename _Tp>
  261. struct __is_clock_impl<_Tp,
  262. void_t<typename _Tp::rep, typename _Tp::period,
  263. typename _Tp::duration,
  264. typename _Tp::time_point::duration,
  265. decltype(_Tp::is_steady),
  266. decltype(_Tp::now())>>
  267. : __and_<is_same<typename _Tp::duration,
  268. duration<typename _Tp::rep, typename _Tp::period>>,
  269. is_same<typename _Tp::time_point::duration,
  270. typename _Tp::duration>,
  271. is_same<decltype(&_Tp::is_steady), const bool*>,
  272. is_same<decltype(_Tp::now()), typename _Tp::time_point>>::type
  273. { };
  274. template<typename _Tp>
  275. struct is_clock : __is_clock_impl<_Tp>::type
  276. { };
  277. #endif
  278. #endif // C++20
  279. #if __cplusplus >= 201703L
  280. # define __cpp_lib_chrono 201611
  281. template<typename _ToDur, typename _Rep, typename _Period>
  282. constexpr __enable_if_is_duration<_ToDur>
  283. floor(const duration<_Rep, _Period>& __d)
  284. {
  285. auto __to = chrono::duration_cast<_ToDur>(__d);
  286. if (__to > __d)
  287. return __to - _ToDur{1};
  288. return __to;
  289. }
  290. template<typename _ToDur, typename _Rep, typename _Period>
  291. constexpr __enable_if_is_duration<_ToDur>
  292. ceil(const duration<_Rep, _Period>& __d)
  293. {
  294. auto __to = chrono::duration_cast<_ToDur>(__d);
  295. if (__to < __d)
  296. return __to + _ToDur{1};
  297. return __to;
  298. }
  299. template <typename _ToDur, typename _Rep, typename _Period>
  300. constexpr enable_if_t<
  301. __and_<__is_duration<_ToDur>,
  302. __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
  303. _ToDur>
  304. round(const duration<_Rep, _Period>& __d)
  305. {
  306. _ToDur __t0 = chrono::floor<_ToDur>(__d);
  307. _ToDur __t1 = __t0 + _ToDur{1};
  308. auto __diff0 = __d - __t0;
  309. auto __diff1 = __t1 - __d;
  310. if (__diff0 == __diff1)
  311. {
  312. if (__t0.count() & 1)
  313. return __t1;
  314. return __t0;
  315. }
  316. else if (__diff0 < __diff1)
  317. return __t0;
  318. return __t1;
  319. }
  320. template<typename _Rep, typename _Period>
  321. constexpr
  322. enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>>
  323. abs(duration<_Rep, _Period> __d)
  324. {
  325. if (__d >= __d.zero())
  326. return __d;
  327. return -__d;
  328. }
  329. // Make chrono::ceil<D> also usable as chrono::__detail::ceil<D>.
  330. namespace __detail { using chrono::ceil; }
  331. #else // ! C++17
  332. // We want to use ceil even when compiling for earlier standards versions.
  333. // C++11 only allows a single statement in a constexpr function, so we
  334. // need to move the comparison into a separate function, __ceil_impl.
  335. namespace __detail
  336. {
  337. template<typename _Tp, typename _Up>
  338. constexpr _Tp
  339. __ceil_impl(const _Tp& __t, const _Up& __u)
  340. {
  341. return (__t < __u) ? (__t + _Tp{1}) : __t;
  342. }
  343. // C++11-friendly version of std::chrono::ceil<D> for internal use.
  344. template<typename _ToDur, typename _Rep, typename _Period>
  345. constexpr _ToDur
  346. ceil(const duration<_Rep, _Period>& __d)
  347. {
  348. return __detail::__ceil_impl(chrono::duration_cast<_ToDur>(__d), __d);
  349. }
  350. }
  351. #endif // C++17
  352. /// duration_values
  353. template<typename _Rep>
  354. struct duration_values
  355. {
  356. static constexpr _Rep
  357. zero() noexcept
  358. { return _Rep(0); }
  359. static constexpr _Rep
  360. max() noexcept
  361. { return numeric_limits<_Rep>::max(); }
  362. static constexpr _Rep
  363. min() noexcept
  364. { return numeric_limits<_Rep>::lowest(); }
  365. };
  366. /// @cond undocumented
  367. template<typename _Tp>
  368. struct __is_ratio
  369. : std::false_type
  370. { };
  371. template<intmax_t _Num, intmax_t _Den>
  372. struct __is_ratio<ratio<_Num, _Den>>
  373. : std::true_type
  374. { };
  375. /// @endcond
  376. /// duration
  377. template<typename _Rep, typename _Period>
  378. struct duration
  379. {
  380. private:
  381. template<typename _Rep2>
  382. using __is_float = treat_as_floating_point<_Rep2>;
  383. static constexpr intmax_t
  384. _S_gcd(intmax_t __m, intmax_t __n) noexcept
  385. {
  386. // Duration only allows positive periods so we don't need to
  387. // handle negative values here (unlike __static_gcd and std::gcd).
  388. #if __cplusplus >= 201402L
  389. do
  390. {
  391. intmax_t __rem = __m % __n;
  392. __m = __n;
  393. __n = __rem;
  394. }
  395. while (__n != 0);
  396. return __m;
  397. #else
  398. // C++11 doesn't allow loops in constexpr functions, but this
  399. // recursive version can be more expensive to evaluate.
  400. return (__n == 0) ? __m : _S_gcd(__n, __m % __n);
  401. #endif
  402. }
  403. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  404. // 2094. overflow shouldn't participate in overload resolution
  405. // 3090. What is [2094] intended to mean?
  406. // This only produces a valid type if no overflow occurs.
  407. template<typename _R1, typename _R2,
  408. intmax_t __gcd1 = _S_gcd(_R1::num, _R2::num),
  409. intmax_t __gcd2 = _S_gcd(_R1::den, _R2::den)>
  410. using __divide = ratio<(_R1::num / __gcd1) * (_R2::den / __gcd2),
  411. (_R1::den / __gcd2) * (_R2::num / __gcd1)>;
  412. // _Period2 is an exact multiple of _Period
  413. template<typename _Period2>
  414. using __is_harmonic
  415. = __bool_constant<__divide<_Period2, _Period>::den == 1>;
  416. public:
  417. using rep = _Rep;
  418. using period = typename _Period::type;
  419. static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
  420. static_assert(__is_ratio<_Period>::value,
  421. "period must be a specialization of ratio");
  422. static_assert(_Period::num > 0, "period must be positive");
  423. // 20.11.5.1 construction / copy / destroy
  424. constexpr duration() = default;
  425. duration(const duration&) = default;
  426. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  427. // 3050. Conversion specification problem in chrono::duration
  428. template<typename _Rep2, typename = _Require<
  429. is_convertible<const _Rep2&, rep>,
  430. __or_<__is_float<rep>, __not_<__is_float<_Rep2>>>>>
  431. constexpr explicit duration(const _Rep2& __rep)
  432. : __r(static_cast<rep>(__rep)) { }
  433. template<typename _Rep2, typename _Period2, typename = _Require<
  434. is_convertible<const _Rep2&, rep>,
  435. __or_<__is_float<rep>,
  436. __and_<__is_harmonic<_Period2>,
  437. __not_<__is_float<_Rep2>>>>>>
  438. constexpr duration(const duration<_Rep2, _Period2>& __d)
  439. : __r(duration_cast<duration>(__d).count()) { }
  440. ~duration() = default;
  441. duration& operator=(const duration&) = default;
  442. // 20.11.5.2 observer
  443. constexpr rep
  444. count() const
  445. { return __r; }
  446. // 20.11.5.3 arithmetic
  447. constexpr duration<typename common_type<rep>::type, period>
  448. operator+() const
  449. { return duration<typename common_type<rep>::type, period>(__r); }
  450. constexpr duration<typename common_type<rep>::type, period>
  451. operator-() const
  452. { return duration<typename common_type<rep>::type, period>(-__r); }
  453. _GLIBCXX17_CONSTEXPR duration&
  454. operator++()
  455. {
  456. ++__r;
  457. return *this;
  458. }
  459. _GLIBCXX17_CONSTEXPR duration
  460. operator++(int)
  461. { return duration(__r++); }
  462. _GLIBCXX17_CONSTEXPR duration&
  463. operator--()
  464. {
  465. --__r;
  466. return *this;
  467. }
  468. _GLIBCXX17_CONSTEXPR duration
  469. operator--(int)
  470. { return duration(__r--); }
  471. _GLIBCXX17_CONSTEXPR duration&
  472. operator+=(const duration& __d)
  473. {
  474. __r += __d.count();
  475. return *this;
  476. }
  477. _GLIBCXX17_CONSTEXPR duration&
  478. operator-=(const duration& __d)
  479. {
  480. __r -= __d.count();
  481. return *this;
  482. }
  483. _GLIBCXX17_CONSTEXPR duration&
  484. operator*=(const rep& __rhs)
  485. {
  486. __r *= __rhs;
  487. return *this;
  488. }
  489. _GLIBCXX17_CONSTEXPR duration&
  490. operator/=(const rep& __rhs)
  491. {
  492. __r /= __rhs;
  493. return *this;
  494. }
  495. // DR 934.
  496. template<typename _Rep2 = rep>
  497. _GLIBCXX17_CONSTEXPR
  498. typename enable_if<!treat_as_floating_point<_Rep2>::value,
  499. duration&>::type
  500. operator%=(const rep& __rhs)
  501. {
  502. __r %= __rhs;
  503. return *this;
  504. }
  505. template<typename _Rep2 = rep>
  506. _GLIBCXX17_CONSTEXPR
  507. typename enable_if<!treat_as_floating_point<_Rep2>::value,
  508. duration&>::type
  509. operator%=(const duration& __d)
  510. {
  511. __r %= __d.count();
  512. return *this;
  513. }
  514. // 20.11.5.4 special values
  515. static constexpr duration
  516. zero() noexcept
  517. { return duration(duration_values<rep>::zero()); }
  518. static constexpr duration
  519. min() noexcept
  520. { return duration(duration_values<rep>::min()); }
  521. static constexpr duration
  522. max() noexcept
  523. { return duration(duration_values<rep>::max()); }
  524. private:
  525. rep __r;
  526. };
  527. /// @relates duration @{
  528. /// The sum of two durations.
  529. template<typename _Rep1, typename _Period1,
  530. typename _Rep2, typename _Period2>
  531. constexpr typename common_type<duration<_Rep1, _Period1>,
  532. duration<_Rep2, _Period2>>::type
  533. operator+(const duration<_Rep1, _Period1>& __lhs,
  534. const duration<_Rep2, _Period2>& __rhs)
  535. {
  536. typedef duration<_Rep1, _Period1> __dur1;
  537. typedef duration<_Rep2, _Period2> __dur2;
  538. typedef typename common_type<__dur1,__dur2>::type __cd;
  539. return __cd(__cd(__lhs).count() + __cd(__rhs).count());
  540. }
  541. /// The difference between two durations.
  542. template<typename _Rep1, typename _Period1,
  543. typename _Rep2, typename _Period2>
  544. constexpr typename common_type<duration<_Rep1, _Period1>,
  545. duration<_Rep2, _Period2>>::type
  546. operator-(const duration<_Rep1, _Period1>& __lhs,
  547. const duration<_Rep2, _Period2>& __rhs)
  548. {
  549. typedef duration<_Rep1, _Period1> __dur1;
  550. typedef duration<_Rep2, _Period2> __dur2;
  551. typedef typename common_type<__dur1,__dur2>::type __cd;
  552. return __cd(__cd(__lhs).count() - __cd(__rhs).count());
  553. }
  554. /// @}
  555. /// @cond undocumented
  556. // SFINAE helper to obtain common_type<_Rep1, _Rep2> only if _Rep2
  557. // is implicitly convertible to it.
  558. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  559. // 3050. Conversion specification problem in chrono::duration constructor
  560. template<typename _Rep1, typename _Rep2,
  561. typename _CRep = typename common_type<_Rep1, _Rep2>::type>
  562. using __common_rep_t = typename
  563. enable_if<is_convertible<const _Rep2&, _CRep>::value, _CRep>::type;
  564. /// @endcond
  565. /// @relates duration @{
  566. /// Multiply a duration by a scalar value.
  567. template<typename _Rep1, typename _Period, typename _Rep2>
  568. constexpr duration<__common_rep_t<_Rep1, _Rep2>, _Period>
  569. operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
  570. {
  571. typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
  572. __cd;
  573. return __cd(__cd(__d).count() * __s);
  574. }
  575. /// Multiply a duration by a scalar value.
  576. template<typename _Rep1, typename _Rep2, typename _Period>
  577. constexpr duration<__common_rep_t<_Rep2, _Rep1>, _Period>
  578. operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
  579. { return __d * __s; }
  580. template<typename _Rep1, typename _Period, typename _Rep2>
  581. constexpr
  582. duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
  583. operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
  584. {
  585. typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
  586. __cd;
  587. return __cd(__cd(__d).count() / __s);
  588. }
  589. template<typename _Rep1, typename _Period1,
  590. typename _Rep2, typename _Period2>
  591. constexpr typename common_type<_Rep1, _Rep2>::type
  592. operator/(const duration<_Rep1, _Period1>& __lhs,
  593. const duration<_Rep2, _Period2>& __rhs)
  594. {
  595. typedef duration<_Rep1, _Period1> __dur1;
  596. typedef duration<_Rep2, _Period2> __dur2;
  597. typedef typename common_type<__dur1,__dur2>::type __cd;
  598. return __cd(__lhs).count() / __cd(__rhs).count();
  599. }
  600. // DR 934.
  601. template<typename _Rep1, typename _Period, typename _Rep2>
  602. constexpr
  603. duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
  604. operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
  605. {
  606. typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
  607. __cd;
  608. return __cd(__cd(__d).count() % __s);
  609. }
  610. template<typename _Rep1, typename _Period1,
  611. typename _Rep2, typename _Period2>
  612. constexpr typename common_type<duration<_Rep1, _Period1>,
  613. duration<_Rep2, _Period2>>::type
  614. operator%(const duration<_Rep1, _Period1>& __lhs,
  615. const duration<_Rep2, _Period2>& __rhs)
  616. {
  617. typedef duration<_Rep1, _Period1> __dur1;
  618. typedef duration<_Rep2, _Period2> __dur2;
  619. typedef typename common_type<__dur1,__dur2>::type __cd;
  620. return __cd(__cd(__lhs).count() % __cd(__rhs).count());
  621. }
  622. // comparisons
  623. template<typename _Rep1, typename _Period1,
  624. typename _Rep2, typename _Period2>
  625. constexpr bool
  626. operator==(const duration<_Rep1, _Period1>& __lhs,
  627. const duration<_Rep2, _Period2>& __rhs)
  628. {
  629. typedef duration<_Rep1, _Period1> __dur1;
  630. typedef duration<_Rep2, _Period2> __dur2;
  631. typedef typename common_type<__dur1,__dur2>::type __ct;
  632. return __ct(__lhs).count() == __ct(__rhs).count();
  633. }
  634. template<typename _Rep1, typename _Period1,
  635. typename _Rep2, typename _Period2>
  636. constexpr bool
  637. operator<(const duration<_Rep1, _Period1>& __lhs,
  638. const duration<_Rep2, _Period2>& __rhs)
  639. {
  640. typedef duration<_Rep1, _Period1> __dur1;
  641. typedef duration<_Rep2, _Period2> __dur2;
  642. typedef typename common_type<__dur1,__dur2>::type __ct;
  643. return __ct(__lhs).count() < __ct(__rhs).count();
  644. }
  645. #if __cpp_lib_three_way_comparison
  646. template<typename _Rep1, typename _Period1,
  647. typename _Rep2, typename _Period2>
  648. requires three_way_comparable<common_type_t<_Rep1, _Rep2>>
  649. constexpr auto
  650. operator<=>(const duration<_Rep1, _Period1>& __lhs,
  651. const duration<_Rep2, _Period2>& __rhs)
  652. {
  653. using __ct = common_type_t<duration<_Rep1, _Period1>,
  654. duration<_Rep2, _Period2>>;
  655. return __ct(__lhs).count() <=> __ct(__rhs).count();
  656. }
  657. #else
  658. template<typename _Rep1, typename _Period1,
  659. typename _Rep2, typename _Period2>
  660. constexpr bool
  661. operator!=(const duration<_Rep1, _Period1>& __lhs,
  662. const duration<_Rep2, _Period2>& __rhs)
  663. { return !(__lhs == __rhs); }
  664. #endif
  665. template<typename _Rep1, typename _Period1,
  666. typename _Rep2, typename _Period2>
  667. constexpr bool
  668. operator<=(const duration<_Rep1, _Period1>& __lhs,
  669. const duration<_Rep2, _Period2>& __rhs)
  670. { return !(__rhs < __lhs); }
  671. template<typename _Rep1, typename _Period1,
  672. typename _Rep2, typename _Period2>
  673. constexpr bool
  674. operator>(const duration<_Rep1, _Period1>& __lhs,
  675. const duration<_Rep2, _Period2>& __rhs)
  676. { return __rhs < __lhs; }
  677. template<typename _Rep1, typename _Period1,
  678. typename _Rep2, typename _Period2>
  679. constexpr bool
  680. operator>=(const duration<_Rep1, _Period1>& __lhs,
  681. const duration<_Rep2, _Period2>& __rhs)
  682. { return !(__lhs < __rhs); }
  683. /// @}
  684. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  685. # define _GLIBCXX_CHRONO_INT64_T int64_t
  686. #elif defined __INT64_TYPE__
  687. # define _GLIBCXX_CHRONO_INT64_T __INT64_TYPE__
  688. #else
  689. static_assert(std::numeric_limits<unsigned long long>::digits >= 64,
  690. "Representation type for nanoseconds must have at least 64 bits");
  691. # define _GLIBCXX_CHRONO_INT64_T long long
  692. #endif
  693. /// nanoseconds
  694. using nanoseconds = duration<_GLIBCXX_CHRONO_INT64_T, nano>;
  695. /// microseconds
  696. using microseconds = duration<_GLIBCXX_CHRONO_INT64_T, micro>;
  697. /// milliseconds
  698. using milliseconds = duration<_GLIBCXX_CHRONO_INT64_T, milli>;
  699. /// seconds
  700. using seconds = duration<_GLIBCXX_CHRONO_INT64_T>;
  701. /// minutes
  702. using minutes = duration<_GLIBCXX_CHRONO_INT64_T, ratio< 60>>;
  703. /// hours
  704. using hours = duration<_GLIBCXX_CHRONO_INT64_T, ratio<3600>>;
  705. #if __cplusplus > 201703L
  706. /// days
  707. using days = duration<_GLIBCXX_CHRONO_INT64_T, ratio<86400>>;
  708. /// weeks
  709. using weeks = duration<_GLIBCXX_CHRONO_INT64_T, ratio<604800>>;
  710. /// years
  711. using years = duration<_GLIBCXX_CHRONO_INT64_T, ratio<31556952>>;
  712. /// months
  713. using months = duration<_GLIBCXX_CHRONO_INT64_T, ratio<2629746>>;
  714. #endif // C++20
  715. #undef _GLIBCXX_CHRONO_INT64_T
  716. /// time_point
  717. template<typename _Clock, typename _Dur>
  718. struct time_point
  719. {
  720. static_assert(__is_duration<_Dur>::value,
  721. "duration must be a specialization of std::chrono::duration");
  722. typedef _Clock clock;
  723. typedef _Dur duration;
  724. typedef typename duration::rep rep;
  725. typedef typename duration::period period;
  726. constexpr time_point() : __d(duration::zero())
  727. { }
  728. constexpr explicit time_point(const duration& __dur)
  729. : __d(__dur)
  730. { }
  731. // conversions
  732. template<typename _Dur2,
  733. typename = _Require<is_convertible<_Dur2, _Dur>>>
  734. constexpr time_point(const time_point<clock, _Dur2>& __t)
  735. : __d(__t.time_since_epoch())
  736. { }
  737. // observer
  738. constexpr duration
  739. time_since_epoch() const
  740. { return __d; }
  741. #if __cplusplus > 201703L
  742. constexpr time_point&
  743. operator++()
  744. {
  745. ++__d;
  746. return *this;
  747. }
  748. constexpr time_point
  749. operator++(int)
  750. { return time_point{__d++}; }
  751. constexpr time_point&
  752. operator--()
  753. {
  754. --__d;
  755. return *this;
  756. }
  757. constexpr time_point
  758. operator--(int)
  759. { return time_point{__d--}; }
  760. #endif
  761. // arithmetic
  762. _GLIBCXX17_CONSTEXPR time_point&
  763. operator+=(const duration& __dur)
  764. {
  765. __d += __dur;
  766. return *this;
  767. }
  768. _GLIBCXX17_CONSTEXPR time_point&
  769. operator-=(const duration& __dur)
  770. {
  771. __d -= __dur;
  772. return *this;
  773. }
  774. // special values
  775. static constexpr time_point
  776. min() noexcept
  777. { return time_point(duration::min()); }
  778. static constexpr time_point
  779. max() noexcept
  780. { return time_point(duration::max()); }
  781. private:
  782. duration __d;
  783. };
  784. /// time_point_cast
  785. template<typename _ToDur, typename _Clock, typename _Dur>
  786. constexpr typename enable_if<__is_duration<_ToDur>::value,
  787. time_point<_Clock, _ToDur>>::type
  788. time_point_cast(const time_point<_Clock, _Dur>& __t)
  789. {
  790. typedef time_point<_Clock, _ToDur> __time_point;
  791. return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
  792. }
  793. #if __cplusplus > 201402L
  794. template<typename _ToDur, typename _Clock, typename _Dur>
  795. constexpr
  796. enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
  797. floor(const time_point<_Clock, _Dur>& __tp)
  798. {
  799. return time_point<_Clock, _ToDur>{
  800. chrono::floor<_ToDur>(__tp.time_since_epoch())};
  801. }
  802. template<typename _ToDur, typename _Clock, typename _Dur>
  803. constexpr
  804. enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
  805. ceil(const time_point<_Clock, _Dur>& __tp)
  806. {
  807. return time_point<_Clock, _ToDur>{
  808. chrono::ceil<_ToDur>(__tp.time_since_epoch())};
  809. }
  810. template<typename _ToDur, typename _Clock, typename _Dur>
  811. constexpr enable_if_t<
  812. __and_<__is_duration<_ToDur>,
  813. __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
  814. time_point<_Clock, _ToDur>>
  815. round(const time_point<_Clock, _Dur>& __tp)
  816. {
  817. return time_point<_Clock, _ToDur>{
  818. chrono::round<_ToDur>(__tp.time_since_epoch())};
  819. }
  820. #endif // C++17
  821. /// @relates time_point @{
  822. /// Adjust a time point forwards by the given duration.
  823. template<typename _Clock, typename _Dur1,
  824. typename _Rep2, typename _Period2>
  825. constexpr time_point<_Clock,
  826. typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
  827. operator+(const time_point<_Clock, _Dur1>& __lhs,
  828. const duration<_Rep2, _Period2>& __rhs)
  829. {
  830. typedef duration<_Rep2, _Period2> __dur2;
  831. typedef typename common_type<_Dur1,__dur2>::type __ct;
  832. typedef time_point<_Clock, __ct> __time_point;
  833. return __time_point(__lhs.time_since_epoch() + __rhs);
  834. }
  835. /// Adjust a time point forwards by the given duration.
  836. template<typename _Rep1, typename _Period1,
  837. typename _Clock, typename _Dur2>
  838. constexpr time_point<_Clock,
  839. typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
  840. operator+(const duration<_Rep1, _Period1>& __lhs,
  841. const time_point<_Clock, _Dur2>& __rhs)
  842. {
  843. typedef duration<_Rep1, _Period1> __dur1;
  844. typedef typename common_type<__dur1,_Dur2>::type __ct;
  845. typedef time_point<_Clock, __ct> __time_point;
  846. return __time_point(__rhs.time_since_epoch() + __lhs);
  847. }
  848. /// Adjust a time point backwards by the given duration.
  849. template<typename _Clock, typename _Dur1,
  850. typename _Rep2, typename _Period2>
  851. constexpr time_point<_Clock,
  852. typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
  853. operator-(const time_point<_Clock, _Dur1>& __lhs,
  854. const duration<_Rep2, _Period2>& __rhs)
  855. {
  856. typedef duration<_Rep2, _Period2> __dur2;
  857. typedef typename common_type<_Dur1,__dur2>::type __ct;
  858. typedef time_point<_Clock, __ct> __time_point;
  859. return __time_point(__lhs.time_since_epoch() -__rhs);
  860. }
  861. /// @}
  862. /// @relates time_point @{
  863. /// The difference between two time points (as a duration)
  864. template<typename _Clock, typename _Dur1, typename _Dur2>
  865. constexpr typename common_type<_Dur1, _Dur2>::type
  866. operator-(const time_point<_Clock, _Dur1>& __lhs,
  867. const time_point<_Clock, _Dur2>& __rhs)
  868. { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
  869. template<typename _Clock, typename _Dur1, typename _Dur2>
  870. constexpr bool
  871. operator==(const time_point<_Clock, _Dur1>& __lhs,
  872. const time_point<_Clock, _Dur2>& __rhs)
  873. { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
  874. #if __cpp_lib_three_way_comparison
  875. template<typename _Clock, typename _Dur1,
  876. three_way_comparable_with<_Dur1> _Dur2>
  877. constexpr auto
  878. operator<=>(const time_point<_Clock, _Dur1>& __lhs,
  879. const time_point<_Clock, _Dur2>& __rhs)
  880. { return __lhs.time_since_epoch() <=> __rhs.time_since_epoch(); }
  881. #else
  882. template<typename _Clock, typename _Dur1, typename _Dur2>
  883. constexpr bool
  884. operator!=(const time_point<_Clock, _Dur1>& __lhs,
  885. const time_point<_Clock, _Dur2>& __rhs)
  886. { return !(__lhs == __rhs); }
  887. #endif
  888. template<typename _Clock, typename _Dur1, typename _Dur2>
  889. constexpr bool
  890. operator<(const time_point<_Clock, _Dur1>& __lhs,
  891. const time_point<_Clock, _Dur2>& __rhs)
  892. { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
  893. template<typename _Clock, typename _Dur1, typename _Dur2>
  894. constexpr bool
  895. operator<=(const time_point<_Clock, _Dur1>& __lhs,
  896. const time_point<_Clock, _Dur2>& __rhs)
  897. { return !(__rhs < __lhs); }
  898. template<typename _Clock, typename _Dur1, typename _Dur2>
  899. constexpr bool
  900. operator>(const time_point<_Clock, _Dur1>& __lhs,
  901. const time_point<_Clock, _Dur2>& __rhs)
  902. { return __rhs < __lhs; }
  903. template<typename _Clock, typename _Dur1, typename _Dur2>
  904. constexpr bool
  905. operator>=(const time_point<_Clock, _Dur1>& __lhs,
  906. const time_point<_Clock, _Dur2>& __rhs)
  907. { return !(__lhs < __rhs); }
  908. /// @}
  909. // Clocks.
  910. // Why nanosecond resolution as the default?
  911. // Why have std::system_clock always count in the highest
  912. // resolution (ie nanoseconds), even if on some OSes the low 3
  913. // or 9 decimal digits will be always zero? This allows later
  914. // implementations to change the system_clock::now()
  915. // implementation any time to provide better resolution without
  916. // changing function signature or units.
  917. // To support the (forward) evolution of the library's defined
  918. // clocks, wrap inside inline namespace so that the current
  919. // defintions of system_clock, steady_clock, and
  920. // high_resolution_clock types are uniquely mangled. This way, new
  921. // code can use the latests clocks, while the library can contain
  922. // compatibility definitions for previous versions. At some
  923. // point, when these clocks settle down, the inlined namespaces
  924. // can be removed. XXX GLIBCXX_ABI Deprecated
  925. inline namespace _V2 {
  926. /**
  927. * @brief System clock.
  928. *
  929. * Time returned represents wall time from the system-wide clock.
  930. * @ingroup chrono
  931. */
  932. struct system_clock
  933. {
  934. typedef chrono::nanoseconds duration;
  935. typedef duration::rep rep;
  936. typedef duration::period period;
  937. typedef chrono::time_point<system_clock, duration> time_point;
  938. static_assert(system_clock::duration::min()
  939. < system_clock::duration::zero(),
  940. "a clock's minimum duration cannot be less than its epoch");
  941. static constexpr bool is_steady = false;
  942. static time_point
  943. now() noexcept;
  944. // Map to C API
  945. static std::time_t
  946. to_time_t(const time_point& __t) noexcept
  947. {
  948. return std::time_t(duration_cast<chrono::seconds>
  949. (__t.time_since_epoch()).count());
  950. }
  951. static time_point
  952. from_time_t(std::time_t __t) noexcept
  953. {
  954. typedef chrono::time_point<system_clock, seconds> __from;
  955. return time_point_cast<system_clock::duration>
  956. (__from(chrono::seconds(__t)));
  957. }
  958. };
  959. /**
  960. * @brief Monotonic clock
  961. *
  962. * Time returned has the property of only increasing at a uniform rate.
  963. * @ingroup chrono
  964. */
  965. struct steady_clock
  966. {
  967. typedef chrono::nanoseconds duration;
  968. typedef duration::rep rep;
  969. typedef duration::period period;
  970. typedef chrono::time_point<steady_clock, duration> time_point;
  971. static constexpr bool is_steady = true;
  972. static time_point
  973. now() noexcept;
  974. };
  975. /**
  976. * @brief Highest-resolution clock
  977. *
  978. * This is the clock "with the shortest tick period." Alias to
  979. * std::system_clock until higher-than-nanosecond definitions
  980. * become feasible.
  981. * @ingroup chrono
  982. */
  983. using high_resolution_clock = system_clock;
  984. } // end inline namespace _V2
  985. #if __cplusplus > 201703L
  986. template<typename _Duration>
  987. using sys_time = time_point<system_clock, _Duration>;
  988. using sys_seconds = sys_time<seconds>;
  989. using sys_days = sys_time<days>;
  990. using file_clock = ::std::filesystem::__file_clock;
  991. template<typename _Duration>
  992. using file_time = time_point<file_clock, _Duration>;
  993. template<> struct is_clock<system_clock> : true_type { };
  994. template<> struct is_clock<steady_clock> : true_type { };
  995. template<> struct is_clock<file_clock> : true_type { };
  996. template<> inline constexpr bool is_clock_v<system_clock> = true;
  997. template<> inline constexpr bool is_clock_v<steady_clock> = true;
  998. template<> inline constexpr bool is_clock_v<file_clock> = true;
  999. struct local_t { };
  1000. template<typename _Duration>
  1001. using local_time = time_point<local_t, _Duration>;
  1002. using local_seconds = local_time<seconds>;
  1003. using local_days = local_time<days>;
  1004. class utc_clock;
  1005. class tai_clock;
  1006. class gps_clock;
  1007. template<typename _Duration>
  1008. using utc_time = time_point<utc_clock, _Duration>;
  1009. using utc_seconds = utc_time<seconds>;
  1010. template<typename _Duration>
  1011. using tai_time = time_point<tai_clock, _Duration>;
  1012. using tai_seconds = tai_time<seconds>;
  1013. template<typename _Duration>
  1014. using gps_time = time_point<gps_clock, _Duration>;
  1015. using gps_seconds = gps_time<seconds>;
  1016. template<> struct is_clock<utc_clock> : true_type { };
  1017. template<> struct is_clock<tai_clock> : true_type { };
  1018. template<> struct is_clock<gps_clock> : true_type { };
  1019. template<> inline constexpr bool is_clock_v<utc_clock> = true;
  1020. template<> inline constexpr bool is_clock_v<tai_clock> = true;
  1021. template<> inline constexpr bool is_clock_v<gps_clock> = true;
  1022. struct leap_second_info
  1023. {
  1024. bool is_leap_second;
  1025. seconds elapsed;
  1026. };
  1027. // CALENDRICAL TYPES
  1028. // CLASS DECLARATIONS
  1029. class day;
  1030. class month;
  1031. class year;
  1032. class weekday;
  1033. class weekday_indexed;
  1034. class weekday_last;
  1035. class month_day;
  1036. class month_day_last;
  1037. class month_weekday;
  1038. class month_weekday_last;
  1039. class year_month;
  1040. class year_month_day;
  1041. class year_month_day_last;
  1042. class year_month_weekday;
  1043. class year_month_weekday_last;
  1044. struct last_spec
  1045. {
  1046. explicit last_spec() = default;
  1047. friend constexpr month_day_last
  1048. operator/(int __m, last_spec) noexcept;
  1049. friend constexpr month_day_last
  1050. operator/(last_spec, int __m) noexcept;
  1051. };
  1052. inline constexpr last_spec last{};
  1053. namespace __detail
  1054. {
  1055. // Compute the remainder of the Euclidean division of __n divided by __d.
  1056. // Euclidean division truncates toward negative infinity and always
  1057. // produces a remainder in the range of [0,__d-1] (whereas standard
  1058. // division truncates toward zero and yields a nonpositive remainder
  1059. // for negative __n).
  1060. constexpr unsigned
  1061. __modulo(long long __n, unsigned __d)
  1062. {
  1063. if (__n >= 0)
  1064. return __n % __d;
  1065. else
  1066. return (__d + (__n % __d)) % __d;
  1067. }
  1068. inline constexpr unsigned __days_per_month[12]
  1069. = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  1070. }
  1071. // DAY
  1072. class day
  1073. {
  1074. private:
  1075. unsigned char _M_d;
  1076. public:
  1077. day() = default;
  1078. explicit constexpr
  1079. day(unsigned __d) noexcept
  1080. : _M_d(__d)
  1081. { }
  1082. constexpr day&
  1083. operator++() noexcept
  1084. {
  1085. ++_M_d;
  1086. return *this;
  1087. }
  1088. constexpr day
  1089. operator++(int) noexcept
  1090. {
  1091. auto __ret = *this;
  1092. ++(*this);
  1093. return __ret;
  1094. }
  1095. constexpr day&
  1096. operator--() noexcept
  1097. {
  1098. --_M_d;
  1099. return *this;
  1100. }
  1101. constexpr day
  1102. operator--(int) noexcept
  1103. {
  1104. auto __ret = *this;
  1105. --(*this);
  1106. return __ret;
  1107. }
  1108. constexpr day&
  1109. operator+=(const days& __d) noexcept
  1110. {
  1111. *this = *this + __d;
  1112. return *this;
  1113. }
  1114. constexpr day&
  1115. operator-=(const days& __d) noexcept
  1116. {
  1117. *this = *this - __d;
  1118. return *this;
  1119. }
  1120. constexpr explicit
  1121. operator unsigned() const noexcept
  1122. { return _M_d; }
  1123. constexpr bool
  1124. ok() const noexcept
  1125. { return 1 <= _M_d && _M_d <= 31; }
  1126. friend constexpr bool
  1127. operator==(const day& __x, const day& __y) noexcept
  1128. { return unsigned{__x} == unsigned{__y}; }
  1129. friend constexpr strong_ordering
  1130. operator<=>(const day& __x, const day& __y) noexcept
  1131. { return unsigned{__x} <=> unsigned{__y}; }
  1132. friend constexpr day
  1133. operator+(const day& __x, const days& __y) noexcept
  1134. { return day(unsigned{__x} + __y.count()); }
  1135. friend constexpr day
  1136. operator+(const days& __x, const day& __y) noexcept
  1137. { return __y + __x; }
  1138. friend constexpr day
  1139. operator-(const day& __x, const days& __y) noexcept
  1140. { return __x + -__y; }
  1141. friend constexpr days
  1142. operator-(const day& __x, const day& __y) noexcept
  1143. { return days{int(unsigned{__x}) - int(unsigned{__y})}; }
  1144. friend constexpr month_day
  1145. operator/(const month& __m, const day& __d) noexcept;
  1146. friend constexpr month_day
  1147. operator/(int __m, const day& __d) noexcept;
  1148. friend constexpr month_day
  1149. operator/(const day& __d, const month& __m) noexcept;
  1150. friend constexpr month_day
  1151. operator/(const day& __d, int __m) noexcept;
  1152. friend constexpr year_month_day
  1153. operator/(const year_month& __ym, const day& __d) noexcept;
  1154. // TODO: Implement operator<<, to_stream, from_stream.
  1155. };
  1156. // MONTH
  1157. class month
  1158. {
  1159. private:
  1160. unsigned char _M_m;
  1161. public:
  1162. month() = default;
  1163. explicit constexpr
  1164. month(unsigned __m) noexcept
  1165. : _M_m(__m)
  1166. { }
  1167. constexpr month&
  1168. operator++() noexcept
  1169. {
  1170. *this += months{1};
  1171. return *this;
  1172. }
  1173. constexpr month
  1174. operator++(int) noexcept
  1175. {
  1176. auto __ret = *this;
  1177. ++(*this);
  1178. return __ret;
  1179. }
  1180. constexpr month&
  1181. operator--() noexcept
  1182. {
  1183. *this -= months{1};
  1184. return *this;
  1185. }
  1186. constexpr month
  1187. operator--(int) noexcept
  1188. {
  1189. auto __ret = *this;
  1190. --(*this);
  1191. return __ret;
  1192. }
  1193. constexpr month&
  1194. operator+=(const months& __m) noexcept
  1195. {
  1196. *this = *this + __m;
  1197. return *this;
  1198. }
  1199. constexpr month&
  1200. operator-=(const months& __m) noexcept
  1201. {
  1202. *this = *this - __m;
  1203. return *this;
  1204. }
  1205. explicit constexpr
  1206. operator unsigned() const noexcept
  1207. { return _M_m; }
  1208. constexpr bool
  1209. ok() const noexcept
  1210. { return 1 <= _M_m && _M_m <= 12; }
  1211. friend constexpr bool
  1212. operator==(const month& __x, const month& __y) noexcept
  1213. { return unsigned{__x} == unsigned{__y}; }
  1214. friend constexpr strong_ordering
  1215. operator<=>(const month& __x, const month& __y) noexcept
  1216. { return unsigned{__x} <=> unsigned{__y}; }
  1217. friend constexpr month
  1218. operator+(const month& __x, const months& __y) noexcept
  1219. {
  1220. auto __n = static_cast<long long>(unsigned{__x}) + (__y.count() - 1);
  1221. return month{__detail::__modulo(__n, 12) + 1};
  1222. }
  1223. friend constexpr month
  1224. operator+(const months& __x, const month& __y) noexcept
  1225. { return __y + __x; }
  1226. friend constexpr month
  1227. operator-(const month& __x, const months& __y) noexcept
  1228. { return __x + -__y; }
  1229. friend constexpr months
  1230. operator-(const month& __x, const month& __y) noexcept
  1231. {
  1232. const auto __dm = int(unsigned(__x)) - int(unsigned(__y));
  1233. return months{__dm < 0 ? 12 + __dm : __dm};
  1234. }
  1235. friend constexpr year_month
  1236. operator/(const year& __y, const month& __m) noexcept;
  1237. friend constexpr month_day
  1238. operator/(const month& __m, int __d) noexcept;
  1239. friend constexpr month_day_last
  1240. operator/(const month& __m, last_spec) noexcept;
  1241. friend constexpr month_day_last
  1242. operator/(last_spec, const month& __m) noexcept;
  1243. friend constexpr month_weekday
  1244. operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
  1245. friend constexpr month_weekday
  1246. operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
  1247. friend constexpr month_weekday_last
  1248. operator/(const month& __m, const weekday_last& __wdl) noexcept;
  1249. friend constexpr month_weekday_last
  1250. operator/(const weekday_last& __wdl, const month& __m) noexcept;
  1251. // TODO: Implement operator<<, to_stream, from_stream.
  1252. };
  1253. inline constexpr month January{1};
  1254. inline constexpr month February{2};
  1255. inline constexpr month March{3};
  1256. inline constexpr month April{4};
  1257. inline constexpr month May{5};
  1258. inline constexpr month June{6};
  1259. inline constexpr month July{7};
  1260. inline constexpr month August{8};
  1261. inline constexpr month September{9};
  1262. inline constexpr month October{10};
  1263. inline constexpr month November{11};
  1264. inline constexpr month December{12};
  1265. // YEAR
  1266. class year
  1267. {
  1268. private:
  1269. short _M_y;
  1270. public:
  1271. year() = default;
  1272. explicit constexpr
  1273. year(int __y) noexcept
  1274. : _M_y{static_cast<short>(__y)}
  1275. { }
  1276. static constexpr year
  1277. min() noexcept
  1278. { return year{-32767}; }
  1279. static constexpr year
  1280. max() noexcept
  1281. { return year{32767}; }
  1282. constexpr year&
  1283. operator++() noexcept
  1284. {
  1285. ++_M_y;
  1286. return *this;
  1287. }
  1288. constexpr year
  1289. operator++(int) noexcept
  1290. {
  1291. auto __ret = *this;
  1292. ++(*this);
  1293. return __ret;
  1294. }
  1295. constexpr year&
  1296. operator--() noexcept
  1297. {
  1298. --_M_y;
  1299. return *this;
  1300. }
  1301. constexpr year
  1302. operator--(int) noexcept
  1303. {
  1304. auto __ret = *this;
  1305. --(*this);
  1306. return __ret;
  1307. }
  1308. constexpr year&
  1309. operator+=(const years& __y) noexcept
  1310. {
  1311. *this = *this + __y;
  1312. return *this;
  1313. }
  1314. constexpr year&
  1315. operator-=(const years& __y) noexcept
  1316. {
  1317. *this = *this - __y;
  1318. return *this;
  1319. }
  1320. constexpr year
  1321. operator+() const noexcept
  1322. { return *this; }
  1323. constexpr year
  1324. operator-() const noexcept
  1325. { return year{-_M_y}; }
  1326. constexpr bool
  1327. is_leap() const noexcept
  1328. {
  1329. // Testing divisibility by 100 first gives better performance, that is,
  1330. // return (_M_y % 100 != 0 || _M_y % 400 == 0) && _M_y % 4 == 0;
  1331. // It gets even faster if _M_y is in [-536870800, 536870999]
  1332. // (which is the case here) and _M_y % 100 is replaced by
  1333. // __is_multiple_of_100 below.
  1334. // References:
  1335. // [1] https://github.com/cassioneri/calendar
  1336. // [2] https://accu.org/journals/overload/28/155/overload155.pdf#page=16
  1337. constexpr uint32_t __multiplier = 42949673;
  1338. constexpr uint32_t __bound = 42949669;
  1339. constexpr uint32_t __max_dividend = 1073741799;
  1340. constexpr uint32_t __offset = __max_dividend / 2 / 100 * 100;
  1341. const bool __is_multiple_of_100
  1342. = __multiplier * (_M_y + __offset) < __bound;
  1343. return (!__is_multiple_of_100 || _M_y % 400 == 0) && _M_y % 4 == 0;
  1344. }
  1345. explicit constexpr
  1346. operator int() const noexcept
  1347. { return _M_y; }
  1348. constexpr bool
  1349. ok() const noexcept
  1350. { return min()._M_y <= _M_y && _M_y <= max()._M_y; }
  1351. friend constexpr bool
  1352. operator==(const year& __x, const year& __y) noexcept
  1353. { return int{__x} == int{__y}; }
  1354. friend constexpr strong_ordering
  1355. operator<=>(const year& __x, const year& __y) noexcept
  1356. { return int{__x} <=> int{__y}; }
  1357. friend constexpr year
  1358. operator+(const year& __x, const years& __y) noexcept
  1359. { return year{int{__x} + static_cast<int>(__y.count())}; }
  1360. friend constexpr year
  1361. operator+(const years& __x, const year& __y) noexcept
  1362. { return __y + __x; }
  1363. friend constexpr year
  1364. operator-(const year& __x, const years& __y) noexcept
  1365. { return __x + -__y; }
  1366. friend constexpr years
  1367. operator-(const year& __x, const year& __y) noexcept
  1368. { return years{int{__x} - int{__y}}; }
  1369. friend constexpr year_month
  1370. operator/(const year& __y, int __m) noexcept;
  1371. friend constexpr year_month_day
  1372. operator/(const year& __y, const month_day& __md) noexcept;
  1373. friend constexpr year_month_day
  1374. operator/(const month_day& __md, const year& __y) noexcept;
  1375. friend constexpr year_month_day_last
  1376. operator/(const year& __y, const month_day_last& __mdl) noexcept;
  1377. friend constexpr year_month_day_last
  1378. operator/(const month_day_last& __mdl, const year& __y) noexcept;
  1379. friend constexpr year_month_weekday
  1380. operator/(const year& __y, const month_weekday& __mwd) noexcept;
  1381. friend constexpr year_month_weekday
  1382. operator/(const month_weekday& __mwd, const year& __y) noexcept;
  1383. friend constexpr year_month_weekday_last
  1384. operator/(const year& __y, const month_weekday_last& __mwdl) noexcept;
  1385. friend constexpr year_month_weekday_last
  1386. operator/(const month_weekday_last& __mwdl, const year& __y) noexcept;
  1387. // TODO: Implement operator<<, to_stream, from_stream.
  1388. };
  1389. // WEEKDAY
  1390. class weekday
  1391. {
  1392. private:
  1393. unsigned char _M_wd;
  1394. static constexpr weekday
  1395. _S_from_days(const days& __d)
  1396. {
  1397. auto __n = __d.count();
  1398. return weekday(__n >= -4 ? (__n + 4) % 7 : (__n + 5) % 7 + 6);
  1399. }
  1400. public:
  1401. weekday() = default;
  1402. explicit constexpr
  1403. weekday(unsigned __wd) noexcept
  1404. : _M_wd(__wd == 7 ? 0 : __wd) // __wd % 7 ?
  1405. { }
  1406. constexpr
  1407. weekday(const sys_days& __dp) noexcept
  1408. : weekday{_S_from_days(__dp.time_since_epoch())}
  1409. { }
  1410. explicit constexpr
  1411. weekday(const local_days& __dp) noexcept
  1412. : weekday{sys_days{__dp.time_since_epoch()}}
  1413. { }
  1414. constexpr weekday&
  1415. operator++() noexcept
  1416. {
  1417. *this += days{1};
  1418. return *this;
  1419. }
  1420. constexpr weekday
  1421. operator++(int) noexcept
  1422. {
  1423. auto __ret = *this;
  1424. ++(*this);
  1425. return __ret;
  1426. }
  1427. constexpr weekday&
  1428. operator--() noexcept
  1429. {
  1430. *this -= days{1};
  1431. return *this;
  1432. }
  1433. constexpr weekday
  1434. operator--(int) noexcept
  1435. {
  1436. auto __ret = *this;
  1437. --(*this);
  1438. return __ret;
  1439. }
  1440. constexpr weekday&
  1441. operator+=(const days& __d) noexcept
  1442. {
  1443. *this = *this + __d;
  1444. return *this;
  1445. }
  1446. constexpr weekday&
  1447. operator-=(const days& __d) noexcept
  1448. {
  1449. *this = *this - __d;
  1450. return *this;
  1451. }
  1452. constexpr unsigned
  1453. c_encoding() const noexcept
  1454. { return _M_wd; }
  1455. constexpr unsigned
  1456. iso_encoding() const noexcept
  1457. { return _M_wd == 0u ? 7u : _M_wd; }
  1458. constexpr bool
  1459. ok() const noexcept
  1460. { return _M_wd <= 6; }
  1461. constexpr weekday_indexed
  1462. operator[](unsigned __index) const noexcept;
  1463. constexpr weekday_last
  1464. operator[](last_spec) const noexcept;
  1465. friend constexpr bool
  1466. operator==(const weekday& __x, const weekday& __y) noexcept
  1467. { return __x._M_wd == __y._M_wd; }
  1468. friend constexpr weekday
  1469. operator+(const weekday& __x, const days& __y) noexcept
  1470. {
  1471. auto __n = static_cast<long long>(__x._M_wd) + __y.count();
  1472. return weekday{__detail::__modulo(__n, 7)};
  1473. }
  1474. friend constexpr weekday
  1475. operator+(const days& __x, const weekday& __y) noexcept
  1476. { return __y + __x; }
  1477. friend constexpr weekday
  1478. operator-(const weekday& __x, const days& __y) noexcept
  1479. { return __x + -__y; }
  1480. friend constexpr days
  1481. operator-(const weekday& __x, const weekday& __y) noexcept
  1482. {
  1483. auto __n = static_cast<long long>(__x._M_wd) - __y._M_wd;
  1484. return days{__detail::__modulo(__n, 7)};
  1485. }
  1486. // TODO: operator<<, from_stream.
  1487. };
  1488. inline constexpr weekday Sunday{0};
  1489. inline constexpr weekday Monday{1};
  1490. inline constexpr weekday Tuesday{2};
  1491. inline constexpr weekday Wednesday{3};
  1492. inline constexpr weekday Thursday{4};
  1493. inline constexpr weekday Friday{5};
  1494. inline constexpr weekday Saturday{6};
  1495. // WEEKDAY_INDEXED
  1496. class weekday_indexed
  1497. {
  1498. private:
  1499. chrono::weekday _M_wd;
  1500. unsigned char _M_index;
  1501. public:
  1502. weekday_indexed() = default;
  1503. constexpr
  1504. weekday_indexed(const chrono::weekday& __wd, unsigned __index) noexcept
  1505. : _M_wd(__wd), _M_index(__index)
  1506. { }
  1507. constexpr chrono::weekday
  1508. weekday() const noexcept
  1509. { return _M_wd; }
  1510. constexpr unsigned
  1511. index() const noexcept
  1512. { return _M_index; };
  1513. constexpr bool
  1514. ok() const noexcept
  1515. { return _M_wd.ok() && 1 <= _M_index && _M_index <= 5; }
  1516. friend constexpr bool
  1517. operator==(const weekday_indexed& __x, const weekday_indexed& __y) noexcept
  1518. { return __x.weekday() == __y.weekday() && __x.index() == __y.index(); }
  1519. friend constexpr month_weekday
  1520. operator/(const month& __m, const weekday_indexed& __wdi) noexcept;
  1521. friend constexpr month_weekday
  1522. operator/(int __m, const weekday_indexed& __wdi) noexcept;
  1523. friend constexpr month_weekday
  1524. operator/(const weekday_indexed& __wdi, const month& __m) noexcept;
  1525. friend constexpr month_weekday
  1526. operator/(const weekday_indexed& __wdi, int __m) noexcept;
  1527. friend constexpr year_month_weekday
  1528. operator/(const year_month& __ym, const weekday_indexed& __wdi) noexcept;
  1529. // TODO: Implement operator<<.
  1530. };
  1531. constexpr weekday_indexed
  1532. weekday::operator[](unsigned __index) const noexcept
  1533. { return {*this, __index}; }
  1534. // WEEKDAY_LAST
  1535. class weekday_last
  1536. {
  1537. private:
  1538. chrono::weekday _M_wd;
  1539. public:
  1540. explicit constexpr
  1541. weekday_last(const chrono::weekday& __wd) noexcept
  1542. : _M_wd{__wd}
  1543. { }
  1544. constexpr chrono::weekday
  1545. weekday() const noexcept
  1546. { return _M_wd; }
  1547. constexpr bool
  1548. ok() const noexcept
  1549. { return _M_wd.ok(); }
  1550. friend constexpr bool
  1551. operator==(const weekday_last& __x, const weekday_last& __y) noexcept
  1552. { return __x.weekday() == __y.weekday(); }
  1553. friend constexpr month_weekday_last
  1554. operator/(int __m, const weekday_last& __wdl) noexcept;
  1555. friend constexpr month_weekday_last
  1556. operator/(const weekday_last& __wdl, int __m) noexcept;
  1557. friend constexpr year_month_weekday_last
  1558. operator/(const year_month& __ym, const weekday_last& __wdl) noexcept;
  1559. // TODO: Implement operator<<.
  1560. };
  1561. constexpr weekday_last
  1562. weekday::operator[](last_spec) const noexcept
  1563. { return weekday_last{*this}; }
  1564. // MONTH_DAY
  1565. class month_day
  1566. {
  1567. private:
  1568. chrono::month _M_m;
  1569. chrono::day _M_d;
  1570. public:
  1571. month_day() = default;
  1572. constexpr
  1573. month_day(const chrono::month& __m, const chrono::day& __d) noexcept
  1574. : _M_m{__m}, _M_d{__d}
  1575. { }
  1576. constexpr chrono::month
  1577. month() const noexcept
  1578. { return _M_m; }
  1579. constexpr chrono::day
  1580. day() const noexcept
  1581. { return _M_d; }
  1582. constexpr bool
  1583. ok() const noexcept
  1584. {
  1585. return _M_m.ok()
  1586. && 1u <= unsigned(_M_d)
  1587. && unsigned(_M_d) <= __detail::__days_per_month[unsigned(_M_m) - 1];
  1588. }
  1589. friend constexpr bool
  1590. operator==(const month_day& __x, const month_day& __y) noexcept
  1591. { return __x.month() == __y.month() && __x.day() == __y.day(); }
  1592. friend constexpr strong_ordering
  1593. operator<=>(const month_day& __x, const month_day& __y) noexcept
  1594. = default;
  1595. friend constexpr month_day
  1596. operator/(const chrono::month& __m, const chrono::day& __d) noexcept
  1597. { return {__m, __d}; }
  1598. friend constexpr month_day
  1599. operator/(const chrono::month& __m, int __d) noexcept
  1600. { return {__m, chrono::day(unsigned(__d))}; }
  1601. friend constexpr month_day
  1602. operator/(int __m, const chrono::day& __d) noexcept
  1603. { return {chrono::month(unsigned(__m)), __d}; }
  1604. friend constexpr month_day
  1605. operator/(const chrono::day& __d, const chrono::month& __m) noexcept
  1606. { return {__m, __d}; }
  1607. friend constexpr month_day
  1608. operator/(const chrono::day& __d, int __m) noexcept
  1609. { return {chrono::month(unsigned(__m)), __d}; }
  1610. friend constexpr year_month_day
  1611. operator/(int __y, const month_day& __md) noexcept;
  1612. friend constexpr year_month_day
  1613. operator/(const month_day& __md, int __y) noexcept;
  1614. // TODO: Implement operator<<, from_stream.
  1615. };
  1616. // MONTH_DAY_LAST
  1617. class month_day_last
  1618. {
  1619. private:
  1620. chrono::month _M_m;
  1621. public:
  1622. explicit constexpr
  1623. month_day_last(const chrono::month& __m) noexcept
  1624. : _M_m{__m}
  1625. { }
  1626. constexpr chrono::month
  1627. month() const noexcept
  1628. { return _M_m; }
  1629. constexpr bool
  1630. ok() const noexcept
  1631. { return _M_m.ok(); }
  1632. friend constexpr bool
  1633. operator==(const month_day_last& __x, const month_day_last& __y) noexcept
  1634. { return __x.month() == __y.month(); }
  1635. friend constexpr strong_ordering
  1636. operator<=>(const month_day_last& __x, const month_day_last& __y) noexcept
  1637. = default;
  1638. friend constexpr month_day_last
  1639. operator/(const chrono::month& __m, last_spec) noexcept
  1640. { return month_day_last{__m}; }
  1641. friend constexpr month_day_last
  1642. operator/(int __m, last_spec) noexcept
  1643. { return chrono::month(unsigned(__m)) / last; }
  1644. friend constexpr month_day_last
  1645. operator/(last_spec, const chrono::month& __m) noexcept
  1646. { return __m / last; }
  1647. friend constexpr month_day_last
  1648. operator/(last_spec, int __m) noexcept
  1649. { return __m / last; }
  1650. friend constexpr year_month_day_last
  1651. operator/(int __y, const month_day_last& __mdl) noexcept;
  1652. friend constexpr year_month_day_last
  1653. operator/(const month_day_last& __mdl, int __y) noexcept;
  1654. // TODO: Implement operator<<.
  1655. };
  1656. // MONTH_WEEKDAY
  1657. class month_weekday
  1658. {
  1659. private:
  1660. chrono::month _M_m;
  1661. chrono::weekday_indexed _M_wdi;
  1662. public:
  1663. constexpr
  1664. month_weekday(const chrono::month& __m,
  1665. const chrono::weekday_indexed& __wdi) noexcept
  1666. : _M_m{__m}, _M_wdi{__wdi}
  1667. { }
  1668. constexpr chrono::month
  1669. month() const noexcept
  1670. { return _M_m; }
  1671. constexpr chrono::weekday_indexed
  1672. weekday_indexed() const noexcept
  1673. { return _M_wdi; }
  1674. constexpr bool
  1675. ok() const noexcept
  1676. { return _M_m.ok() && _M_wdi.ok(); }
  1677. friend constexpr bool
  1678. operator==(const month_weekday& __x, const month_weekday& __y) noexcept
  1679. {
  1680. return __x.month() == __y.month()
  1681. && __x.weekday_indexed() == __y.weekday_indexed();
  1682. }
  1683. friend constexpr month_weekday
  1684. operator/(const chrono::month& __m,
  1685. const chrono::weekday_indexed& __wdi) noexcept
  1686. { return {__m, __wdi}; }
  1687. friend constexpr month_weekday
  1688. operator/(int __m, const chrono::weekday_indexed& __wdi) noexcept
  1689. { return chrono::month(unsigned(__m)) / __wdi; }
  1690. friend constexpr month_weekday
  1691. operator/(const chrono::weekday_indexed& __wdi,
  1692. const chrono::month& __m) noexcept
  1693. { return __m / __wdi; }
  1694. friend constexpr month_weekday
  1695. operator/(const chrono::weekday_indexed& __wdi, int __m) noexcept
  1696. { return __m / __wdi; }
  1697. friend constexpr year_month_weekday
  1698. operator/(int __y, const month_weekday& __mwd) noexcept;
  1699. friend constexpr year_month_weekday
  1700. operator/(const month_weekday& __mwd, int __y) noexcept;
  1701. // TODO: Implement operator<<.
  1702. };
  1703. // MONTH_WEEKDAY_LAST
  1704. class month_weekday_last
  1705. {
  1706. private:
  1707. chrono::month _M_m;
  1708. chrono::weekday_last _M_wdl;
  1709. public:
  1710. constexpr
  1711. month_weekday_last(const chrono::month& __m,
  1712. const chrono::weekday_last& __wdl) noexcept
  1713. :_M_m{__m}, _M_wdl{__wdl}
  1714. { }
  1715. constexpr chrono::month
  1716. month() const noexcept
  1717. { return _M_m; }
  1718. constexpr chrono::weekday_last
  1719. weekday_last() const noexcept
  1720. { return _M_wdl; }
  1721. constexpr bool
  1722. ok() const noexcept
  1723. { return _M_m.ok() && _M_wdl.ok(); }
  1724. friend constexpr bool
  1725. operator==(const month_weekday_last& __x,
  1726. const month_weekday_last& __y) noexcept
  1727. {
  1728. return __x.month() == __y.month()
  1729. && __x.weekday_last() == __y.weekday_last();
  1730. }
  1731. friend constexpr month_weekday_last
  1732. operator/(const chrono::month& __m,
  1733. const chrono::weekday_last& __wdl) noexcept
  1734. { return {__m, __wdl}; }
  1735. friend constexpr month_weekday_last
  1736. operator/(int __m, const chrono::weekday_last& __wdl) noexcept
  1737. { return chrono::month(unsigned(__m)) / __wdl; }
  1738. friend constexpr month_weekday_last
  1739. operator/(const chrono::weekday_last& __wdl,
  1740. const chrono::month& __m) noexcept
  1741. { return __m / __wdl; }
  1742. friend constexpr month_weekday_last
  1743. operator/(const chrono::weekday_last& __wdl, int __m) noexcept
  1744. { return chrono::month(unsigned(__m)) / __wdl; }
  1745. friend constexpr year_month_weekday_last
  1746. operator/(int __y, const month_weekday_last& __mwdl) noexcept;
  1747. friend constexpr year_month_weekday_last
  1748. operator/(const month_weekday_last& __mwdl, int __y) noexcept;
  1749. // TODO: Implement operator<<.
  1750. };
  1751. // YEAR_MONTH
  1752. namespace __detail
  1753. {
  1754. // [time.cal.ym], [time.cal.ymd], etc constrain the 'months'-based
  1755. // addition/subtraction operator overloads like so:
  1756. //
  1757. // Constraints: if the argument supplied by the caller for the months
  1758. // parameter is convertible to years, its implicit conversion sequence
  1759. // to years is worse than its implicit conversion sequence to months.
  1760. //
  1761. // We realize this constraint by templatizing the 'months'-based
  1762. // overloads (using a dummy defaulted template parameter), so that
  1763. // overload resolution doesn't select the 'months'-based overload unless
  1764. // the implicit conversion sequence to 'months' is better than that to
  1765. // 'years'.
  1766. using __months_years_conversion_disambiguator = void;
  1767. }
  1768. class year_month
  1769. {
  1770. private:
  1771. chrono::year _M_y;
  1772. chrono::month _M_m;
  1773. public:
  1774. year_month() = default;
  1775. constexpr
  1776. year_month(const chrono::year& __y, const chrono::month& __m) noexcept
  1777. : _M_y{__y}, _M_m{__m}
  1778. { }
  1779. constexpr chrono::year
  1780. year() const noexcept
  1781. { return _M_y; }
  1782. constexpr chrono::month
  1783. month() const noexcept
  1784. { return _M_m; }
  1785. template<typename = __detail::__months_years_conversion_disambiguator>
  1786. constexpr year_month&
  1787. operator+=(const months& __dm) noexcept
  1788. {
  1789. *this = *this + __dm;
  1790. return *this;
  1791. }
  1792. template<typename = __detail::__months_years_conversion_disambiguator>
  1793. constexpr year_month&
  1794. operator-=(const months& __dm) noexcept
  1795. {
  1796. *this = *this - __dm;
  1797. return *this;
  1798. }
  1799. constexpr year_month&
  1800. operator+=(const years& __dy) noexcept
  1801. {
  1802. *this = *this + __dy;
  1803. return *this;
  1804. }
  1805. constexpr year_month&
  1806. operator-=(const years& __dy) noexcept
  1807. {
  1808. *this = *this - __dy;
  1809. return *this;
  1810. }
  1811. constexpr bool
  1812. ok() const noexcept
  1813. { return _M_y.ok() && _M_m.ok(); }
  1814. friend constexpr bool
  1815. operator==(const year_month& __x, const year_month& __y) noexcept
  1816. { return __x.year() == __y.year() && __x.month() == __y.month(); }
  1817. friend constexpr strong_ordering
  1818. operator<=>(const year_month& __x, const year_month& __y) noexcept
  1819. = default;
  1820. template<typename = __detail::__months_years_conversion_disambiguator>
  1821. friend constexpr year_month
  1822. operator+(const year_month& __ym, const months& __dm) noexcept
  1823. {
  1824. // TODO: Optimize?
  1825. auto __m = __ym.month() + __dm;
  1826. auto __i = int(unsigned(__ym.month())) - 1 + __dm.count();
  1827. auto __y = (__i < 0
  1828. ? __ym.year() + years{(__i - 11) / 12}
  1829. : __ym.year() + years{__i / 12});
  1830. return __y / __m;
  1831. }
  1832. template<typename = __detail::__months_years_conversion_disambiguator>
  1833. friend constexpr year_month
  1834. operator+(const months& __dm, const year_month& __ym) noexcept
  1835. { return __ym + __dm; }
  1836. template<typename = __detail::__months_years_conversion_disambiguator>
  1837. friend constexpr year_month
  1838. operator-(const year_month& __ym, const months& __dm) noexcept
  1839. { return __ym + -__dm; }
  1840. friend constexpr months
  1841. operator-(const year_month& __x, const year_month& __y) noexcept
  1842. {
  1843. return (__x.year() - __y.year()
  1844. + months{static_cast<int>(unsigned{__x.month()})
  1845. - static_cast<int>(unsigned{__y.month()})});
  1846. }
  1847. friend constexpr year_month
  1848. operator+(const year_month& __ym, const years& __dy) noexcept
  1849. { return (__ym.year() + __dy) / __ym.month(); }
  1850. friend constexpr year_month
  1851. operator+(const years& __dy, const year_month& __ym) noexcept
  1852. { return __ym + __dy; }
  1853. friend constexpr year_month
  1854. operator-(const year_month& __ym, const years& __dy) noexcept
  1855. { return __ym + -__dy; }
  1856. friend constexpr year_month
  1857. operator/(const chrono::year& __y, const chrono::month& __m) noexcept
  1858. { return {__y, __m}; }
  1859. friend constexpr year_month
  1860. operator/(const chrono::year& __y, int __m) noexcept
  1861. { return {__y, chrono::month(unsigned(__m))}; }
  1862. friend constexpr year_month_day
  1863. operator/(const year_month& __ym, int __d) noexcept;
  1864. friend constexpr year_month_day_last
  1865. operator/(const year_month& __ym, last_spec) noexcept;
  1866. // TODO: Implement operator<<, from_stream.
  1867. };
  1868. // YEAR_MONTH_DAY
  1869. class year_month_day
  1870. {
  1871. private:
  1872. chrono::year _M_y;
  1873. chrono::month _M_m;
  1874. chrono::day _M_d;
  1875. static constexpr year_month_day _S_from_days(const days& __dp) noexcept;
  1876. constexpr days _M_days_since_epoch() const noexcept;
  1877. public:
  1878. year_month_day() = default;
  1879. constexpr
  1880. year_month_day(const chrono::year& __y, const chrono::month& __m,
  1881. const chrono::day& __d) noexcept
  1882. : _M_y{__y}, _M_m{__m}, _M_d{__d}
  1883. { }
  1884. constexpr
  1885. year_month_day(const year_month_day_last& __ymdl) noexcept;
  1886. constexpr
  1887. year_month_day(const sys_days& __dp) noexcept
  1888. : year_month_day(_S_from_days(__dp.time_since_epoch()))
  1889. { }
  1890. explicit constexpr
  1891. year_month_day(const local_days& __dp) noexcept
  1892. : year_month_day(sys_days{__dp.time_since_epoch()})
  1893. { }
  1894. template<typename = __detail::__months_years_conversion_disambiguator>
  1895. constexpr year_month_day&
  1896. operator+=(const months& __m) noexcept
  1897. {
  1898. *this = *this + __m;
  1899. return *this;
  1900. }
  1901. template<typename = __detail::__months_years_conversion_disambiguator>
  1902. constexpr year_month_day&
  1903. operator-=(const months& __m) noexcept
  1904. {
  1905. *this = *this - __m;
  1906. return *this;
  1907. }
  1908. constexpr year_month_day&
  1909. operator+=(const years& __y) noexcept
  1910. {
  1911. *this = *this + __y;
  1912. return *this;
  1913. }
  1914. constexpr year_month_day&
  1915. operator-=(const years& __y) noexcept
  1916. {
  1917. *this = *this - __y;
  1918. return *this;
  1919. }
  1920. constexpr chrono::year
  1921. year() const noexcept
  1922. { return _M_y; }
  1923. constexpr chrono::month
  1924. month() const noexcept
  1925. { return _M_m; }
  1926. constexpr chrono::day
  1927. day() const noexcept
  1928. { return _M_d; }
  1929. constexpr
  1930. operator sys_days() const noexcept
  1931. { return sys_days{_M_days_since_epoch()}; }
  1932. explicit constexpr
  1933. operator local_days() const noexcept
  1934. { return local_days{sys_days{*this}.time_since_epoch()}; }
  1935. constexpr bool ok() const noexcept;
  1936. friend constexpr bool
  1937. operator==(const year_month_day& __x, const year_month_day& __y) noexcept
  1938. {
  1939. return __x.year() == __y.year()
  1940. && __x.month() == __y.month()
  1941. && __x.day() == __y.day();
  1942. }
  1943. friend constexpr strong_ordering
  1944. operator<=>(const year_month_day& __x, const year_month_day& __y) noexcept
  1945. = default;
  1946. template<typename = __detail::__months_years_conversion_disambiguator>
  1947. friend constexpr year_month_day
  1948. operator+(const year_month_day& __ymd, const months& __dm) noexcept
  1949. { return (__ymd.year() / __ymd.month() + __dm) / __ymd.day(); }
  1950. template<typename = __detail::__months_years_conversion_disambiguator>
  1951. friend constexpr year_month_day
  1952. operator+(const months& __dm, const year_month_day& __ymd) noexcept
  1953. { return __ymd + __dm; }
  1954. friend constexpr year_month_day
  1955. operator+(const year_month_day& __ymd, const years& __dy) noexcept
  1956. { return (__ymd.year() + __dy) / __ymd.month() / __ymd.day(); }
  1957. friend constexpr year_month_day
  1958. operator+(const years& __dy, const year_month_day& __ymd) noexcept
  1959. { return __ymd + __dy; }
  1960. template<typename = __detail::__months_years_conversion_disambiguator>
  1961. friend constexpr year_month_day
  1962. operator-(const year_month_day& __ymd, const months& __dm) noexcept
  1963. { return __ymd + -__dm; }
  1964. friend constexpr year_month_day
  1965. operator-(const year_month_day& __ymd, const years& __dy) noexcept
  1966. { return __ymd + -__dy; }
  1967. friend constexpr year_month_day
  1968. operator/(const year_month& __ym, const chrono::day& __d) noexcept
  1969. { return {__ym.year(), __ym.month(), __d}; }
  1970. friend constexpr year_month_day
  1971. operator/(const year_month& __ym, int __d) noexcept
  1972. { return __ym / chrono::day{unsigned(__d)}; }
  1973. friend constexpr year_month_day
  1974. operator/(const chrono::year& __y, const month_day& __md) noexcept
  1975. { return __y / __md.month() / __md.day(); }
  1976. friend constexpr year_month_day
  1977. operator/(int __y, const month_day& __md) noexcept
  1978. { return chrono::year{__y} / __md; }
  1979. friend constexpr year_month_day
  1980. operator/(const month_day& __md, const chrono::year& __y) noexcept
  1981. { return __y / __md; }
  1982. friend constexpr year_month_day
  1983. operator/(const month_day& __md, int __y) noexcept
  1984. { return chrono::year(__y) / __md; }
  1985. // TODO: Implement operator<<, from_stream.
  1986. };
  1987. // Construct from days since 1970/01/01.
  1988. // Proposition 6.3 of Neri and Schneider,
  1989. // "Euclidean Affine Functions and Applications to Calendar Algorithms".
  1990. // https://arxiv.org/abs/2102.06959
  1991. constexpr year_month_day
  1992. year_month_day::_S_from_days(const days& __dp) noexcept
  1993. {
  1994. constexpr auto __z2 = static_cast<uint32_t>(-1468000);
  1995. constexpr auto __r2_e3 = static_cast<uint32_t>(536895458);
  1996. const auto __r0 = static_cast<uint32_t>(__dp.count()) + __r2_e3;
  1997. const auto __n1 = 4 * __r0 + 3;
  1998. const auto __q1 = __n1 / 146097;
  1999. const auto __r1 = __n1 % 146097 / 4;
  2000. constexpr auto __p32 = static_cast<uint64_t>(1) << 32;
  2001. const auto __n2 = 4 * __r1 + 3;
  2002. const auto __u2 = static_cast<uint64_t>(2939745) * __n2;
  2003. const auto __q2 = static_cast<uint32_t>(__u2 / __p32);
  2004. const auto __r2 = static_cast<uint32_t>(__u2 % __p32) / 2939745 / 4;
  2005. constexpr auto __p16 = static_cast<uint32_t>(1) << 16;
  2006. const auto __n3 = 2141 * __r2 + 197913;
  2007. const auto __q3 = __n3 / __p16;
  2008. const auto __r3 = __n3 % __p16 / 2141;
  2009. const auto __y0 = 100 * __q1 + __q2;
  2010. const auto __m0 = __q3;
  2011. const auto __d0 = __r3;
  2012. const auto __j = __r2 >= 306;
  2013. const auto __y1 = __y0 + __j;
  2014. const auto __m1 = __j ? __m0 - 12 : __m0;
  2015. const auto __d1 = __d0 + 1;
  2016. return year_month_day{chrono::year{static_cast<int>(__y1 + __z2)},
  2017. chrono::month{__m1}, chrono::day{__d1}};
  2018. }
  2019. // Days since 1970/01/01.
  2020. // Proposition 6.2 of Neri and Schneider,
  2021. // "Euclidean Affine Functions and Applications to Calendar Algorithms".
  2022. // https://arxiv.org/abs/2102.06959
  2023. constexpr days
  2024. year_month_day::_M_days_since_epoch() const noexcept
  2025. {
  2026. auto constexpr __z2 = static_cast<uint32_t>(-1468000);
  2027. auto constexpr __r2_e3 = static_cast<uint32_t>(536895458);
  2028. const auto __y1 = static_cast<uint32_t>(static_cast<int>(_M_y)) - __z2;
  2029. const auto __m1 = static_cast<uint32_t>(static_cast<unsigned>(_M_m));
  2030. const auto __d1 = static_cast<uint32_t>(static_cast<unsigned>(_M_d));
  2031. const auto __j = static_cast<uint32_t>(__m1 < 3);
  2032. const auto __y0 = __y1 - __j;
  2033. const auto __m0 = __j ? __m1 + 12 : __m1;
  2034. const auto __d0 = __d1 - 1;
  2035. const auto __q1 = __y0 / 100;
  2036. const auto __yc = 1461 * __y0 / 4 - __q1 + __q1 / 4;
  2037. const auto __mc = (979 *__m0 - 2919) / 32;
  2038. const auto __dc = __d0;
  2039. return days{static_cast<int32_t>(__yc + __mc + __dc - __r2_e3)};
  2040. }
  2041. // YEAR_MONTH_DAY_LAST
  2042. class year_month_day_last
  2043. {
  2044. private:
  2045. chrono::year _M_y;
  2046. chrono::month_day_last _M_mdl;
  2047. public:
  2048. constexpr
  2049. year_month_day_last(const chrono::year& __y,
  2050. const chrono::month_day_last& __mdl) noexcept
  2051. : _M_y{__y}, _M_mdl{__mdl}
  2052. { }
  2053. template<typename = __detail::__months_years_conversion_disambiguator>
  2054. constexpr year_month_day_last&
  2055. operator+=(const months& __m) noexcept
  2056. {
  2057. *this = *this + __m;
  2058. return *this;
  2059. }
  2060. template<typename = __detail::__months_years_conversion_disambiguator>
  2061. constexpr year_month_day_last&
  2062. operator-=(const months& __m) noexcept
  2063. {
  2064. *this = *this - __m;
  2065. return *this;
  2066. }
  2067. constexpr year_month_day_last&
  2068. operator+=(const years& __y) noexcept
  2069. {
  2070. *this = *this + __y;
  2071. return *this;
  2072. }
  2073. constexpr year_month_day_last&
  2074. operator-=(const years& __y) noexcept
  2075. {
  2076. *this = *this - __y;
  2077. return *this;
  2078. }
  2079. constexpr chrono::year
  2080. year() const noexcept
  2081. { return _M_y; }
  2082. constexpr chrono::month
  2083. month() const noexcept
  2084. { return _M_mdl.month(); }
  2085. constexpr chrono::month_day_last
  2086. month_day_last() const noexcept
  2087. { return _M_mdl; }
  2088. // Return A day representing the last day of this year, month pair.
  2089. constexpr chrono::day
  2090. day() const noexcept
  2091. {
  2092. const auto __m = static_cast<unsigned>(month());
  2093. // Excluding February, the last day of month __m is either 30 or 31 or,
  2094. // in another words, it is 30 + b = 30 | b, where b is in {0, 1}.
  2095. // If __m in {1, 3, 4, 5, 6, 7}, then b is 1 if, and only if __m is odd.
  2096. // Hence, b = __m & 1 = (__m ^ 0) & 1.
  2097. // If __m in {8, 9, 10, 11, 12}, then b is 1 if, and only if __m is even.
  2098. // Hence, b = (__m ^ 1) & 1.
  2099. // Therefore, b = (__m ^ c) & 1, where c = 0, if __m < 8, or c = 1 if
  2100. // __m >= 8, that is, c = __m >> 3.
  2101. // The above mathematically justifies this implementation whose
  2102. // performance does not depend on look-up tables being on the L1 cache.
  2103. return chrono::day{__m != 2 ? ((__m ^ (__m >> 3)) & 1) | 30
  2104. : _M_y.is_leap() ? 29 : 28};
  2105. }
  2106. constexpr
  2107. operator sys_days() const noexcept
  2108. { return sys_days{year() / month() / day()}; }
  2109. explicit constexpr
  2110. operator local_days() const noexcept
  2111. { return local_days{sys_days{*this}.time_since_epoch()}; }
  2112. constexpr bool
  2113. ok() const noexcept
  2114. { return _M_y.ok() && _M_mdl.ok(); }
  2115. friend constexpr bool
  2116. operator==(const year_month_day_last& __x,
  2117. const year_month_day_last& __y) noexcept
  2118. {
  2119. return __x.year() == __y.year()
  2120. && __x.month_day_last() == __y.month_day_last();
  2121. }
  2122. friend constexpr strong_ordering
  2123. operator<=>(const year_month_day_last& __x,
  2124. const year_month_day_last& __y) noexcept
  2125. = default;
  2126. template<typename = __detail::__months_years_conversion_disambiguator>
  2127. friend constexpr year_month_day_last
  2128. operator+(const year_month_day_last& __ymdl,
  2129. const months& __dm) noexcept
  2130. { return (__ymdl.year() / __ymdl.month() + __dm) / last; }
  2131. template<typename = __detail::__months_years_conversion_disambiguator>
  2132. friend constexpr year_month_day_last
  2133. operator+(const months& __dm,
  2134. const year_month_day_last& __ymdl) noexcept
  2135. { return __ymdl + __dm; }
  2136. template<typename = __detail::__months_years_conversion_disambiguator>
  2137. friend constexpr year_month_day_last
  2138. operator-(const year_month_day_last& __ymdl,
  2139. const months& __dm) noexcept
  2140. { return __ymdl + -__dm; }
  2141. friend constexpr year_month_day_last
  2142. operator+(const year_month_day_last& __ymdl,
  2143. const years& __dy) noexcept
  2144. { return {__ymdl.year() + __dy, __ymdl.month_day_last()}; }
  2145. friend constexpr year_month_day_last
  2146. operator+(const years& __dy,
  2147. const year_month_day_last& __ymdl) noexcept
  2148. { return __ymdl + __dy; }
  2149. friend constexpr year_month_day_last
  2150. operator-(const year_month_day_last& __ymdl,
  2151. const years& __dy) noexcept
  2152. { return __ymdl + -__dy; }
  2153. friend constexpr year_month_day_last
  2154. operator/(const year_month& __ym, last_spec) noexcept
  2155. { return {__ym.year(), chrono::month_day_last{__ym.month()}}; }
  2156. friend constexpr year_month_day_last
  2157. operator/(const chrono::year& __y,
  2158. const chrono::month_day_last& __mdl) noexcept
  2159. { return {__y, __mdl}; }
  2160. friend constexpr year_month_day_last
  2161. operator/(int __y, const chrono::month_day_last& __mdl) noexcept
  2162. { return chrono::year(__y) / __mdl; }
  2163. friend constexpr year_month_day_last
  2164. operator/(const chrono::month_day_last& __mdl,
  2165. const chrono::year& __y) noexcept
  2166. { return __y / __mdl; }
  2167. friend constexpr year_month_day_last
  2168. operator/(const chrono::month_day_last& __mdl, int __y) noexcept
  2169. { return chrono::year(__y) / __mdl; }
  2170. // TODO: Implement operator<<.
  2171. };
  2172. // year_month_day ctor from year_month_day_last
  2173. constexpr
  2174. year_month_day::year_month_day(const year_month_day_last& __ymdl) noexcept
  2175. : _M_y{__ymdl.year()}, _M_m{__ymdl.month()}, _M_d{__ymdl.day()}
  2176. { }
  2177. constexpr bool
  2178. year_month_day::ok() const noexcept
  2179. {
  2180. if (!_M_y.ok() || !_M_m.ok())
  2181. return false;
  2182. return chrono::day{1} <= _M_d && _M_d <= (_M_y / _M_m / last).day();
  2183. }
  2184. // YEAR_MONTH_WEEKDAY
  2185. class year_month_weekday
  2186. {
  2187. private:
  2188. chrono::year _M_y;
  2189. chrono::month _M_m;
  2190. chrono::weekday_indexed _M_wdi;
  2191. static constexpr year_month_weekday
  2192. _S_from_sys_days(const sys_days& __dp)
  2193. {
  2194. year_month_day __ymd{__dp};
  2195. chrono::weekday __wd{__dp};
  2196. auto __index = __wd[(unsigned{__ymd.day()} - 1) / 7 + 1];
  2197. return {__ymd.year(), __ymd.month(), __index};
  2198. }
  2199. public:
  2200. year_month_weekday() = default;
  2201. constexpr
  2202. year_month_weekday(const chrono::year& __y, const chrono::month& __m,
  2203. const chrono::weekday_indexed& __wdi) noexcept
  2204. : _M_y{__y}, _M_m{__m}, _M_wdi{__wdi}
  2205. { }
  2206. constexpr
  2207. year_month_weekday(const sys_days& __dp) noexcept
  2208. : year_month_weekday{_S_from_sys_days(__dp)}
  2209. { }
  2210. explicit constexpr
  2211. year_month_weekday(const local_days& __dp) noexcept
  2212. : year_month_weekday{sys_days{__dp.time_since_epoch()}}
  2213. { }
  2214. template<typename = __detail::__months_years_conversion_disambiguator>
  2215. constexpr year_month_weekday&
  2216. operator+=(const months& __m) noexcept
  2217. {
  2218. *this = *this + __m;
  2219. return *this;
  2220. }
  2221. template<typename = __detail::__months_years_conversion_disambiguator>
  2222. constexpr year_month_weekday&
  2223. operator-=(const months& __m) noexcept
  2224. {
  2225. *this = *this - __m;
  2226. return *this;
  2227. }
  2228. constexpr year_month_weekday&
  2229. operator+=(const years& __y) noexcept
  2230. {
  2231. *this = *this + __y;
  2232. return *this;
  2233. }
  2234. constexpr year_month_weekday&
  2235. operator-=(const years& __y) noexcept
  2236. {
  2237. *this = *this - __y;
  2238. return *this;
  2239. }
  2240. constexpr chrono::year
  2241. year() const noexcept
  2242. { return _M_y; }
  2243. constexpr chrono::month
  2244. month() const noexcept
  2245. { return _M_m; }
  2246. constexpr chrono::weekday
  2247. weekday() const noexcept
  2248. { return _M_wdi.weekday(); }
  2249. constexpr unsigned
  2250. index() const noexcept
  2251. { return _M_wdi.index(); }
  2252. constexpr chrono::weekday_indexed
  2253. weekday_indexed() const noexcept
  2254. { return _M_wdi; }
  2255. constexpr
  2256. operator sys_days() const noexcept
  2257. {
  2258. auto __d = sys_days{year() / month() / 1};
  2259. return __d + (weekday() - chrono::weekday(__d)
  2260. + days{(static_cast<int>(index())-1)*7});
  2261. }
  2262. explicit constexpr
  2263. operator local_days() const noexcept
  2264. { return local_days{sys_days{*this}.time_since_epoch()}; }
  2265. constexpr bool
  2266. ok() const noexcept
  2267. {
  2268. if (!_M_y.ok() || !_M_m.ok() || !_M_wdi.ok())
  2269. return false;
  2270. if (_M_wdi.index() <= 4)
  2271. return true;
  2272. days __d = (_M_wdi.weekday()
  2273. - chrono::weekday{sys_days{_M_y / _M_m / 1}}
  2274. + days((_M_wdi.index()-1)*7 + 1));
  2275. __glibcxx_assert(__d.count() >= 1);
  2276. return __d.count() <= unsigned{(_M_y / _M_m / last).day()};
  2277. }
  2278. friend constexpr bool
  2279. operator==(const year_month_weekday& __x,
  2280. const year_month_weekday& __y) noexcept
  2281. {
  2282. return __x.year() == __y.year()
  2283. && __x.month() == __y.month()
  2284. && __x.weekday_indexed() == __y.weekday_indexed();
  2285. }
  2286. template<typename = __detail::__months_years_conversion_disambiguator>
  2287. friend constexpr year_month_weekday
  2288. operator+(const year_month_weekday& __ymwd, const months& __dm) noexcept
  2289. {
  2290. return ((__ymwd.year() / __ymwd.month() + __dm)
  2291. / __ymwd.weekday_indexed());
  2292. }
  2293. template<typename = __detail::__months_years_conversion_disambiguator>
  2294. friend constexpr year_month_weekday
  2295. operator+(const months& __dm, const year_month_weekday& __ymwd) noexcept
  2296. { return __ymwd + __dm; }
  2297. friend constexpr year_month_weekday
  2298. operator+(const year_month_weekday& __ymwd, const years& __dy) noexcept
  2299. { return {__ymwd.year() + __dy, __ymwd.month(), __ymwd.weekday_indexed()}; }
  2300. friend constexpr year_month_weekday
  2301. operator+(const years& __dy, const year_month_weekday& __ymwd) noexcept
  2302. { return __ymwd + __dy; }
  2303. template<typename = __detail::__months_years_conversion_disambiguator>
  2304. friend constexpr year_month_weekday
  2305. operator-(const year_month_weekday& __ymwd, const months& __dm) noexcept
  2306. { return __ymwd + -__dm; }
  2307. friend constexpr year_month_weekday
  2308. operator-(const year_month_weekday& __ymwd, const years& __dy) noexcept
  2309. { return __ymwd + -__dy; }
  2310. friend constexpr year_month_weekday
  2311. operator/(const year_month& __ym,
  2312. const chrono::weekday_indexed& __wdi) noexcept
  2313. { return {__ym.year(), __ym.month(), __wdi}; }
  2314. friend constexpr year_month_weekday
  2315. operator/(const chrono::year& __y, const month_weekday& __mwd) noexcept
  2316. { return {__y, __mwd.month(), __mwd.weekday_indexed()}; }
  2317. friend constexpr year_month_weekday
  2318. operator/(int __y, const month_weekday& __mwd) noexcept
  2319. { return chrono::year(__y) / __mwd; }
  2320. friend constexpr year_month_weekday
  2321. operator/(const month_weekday& __mwd, const chrono::year& __y) noexcept
  2322. { return __y / __mwd; }
  2323. friend constexpr year_month_weekday
  2324. operator/(const month_weekday& __mwd, int __y) noexcept
  2325. { return chrono::year(__y) / __mwd; }
  2326. // TODO: Implement operator<<.
  2327. };
  2328. // YEAR_MONTH_WEEKDAY_LAST
  2329. class year_month_weekday_last
  2330. {
  2331. private:
  2332. chrono::year _M_y;
  2333. chrono::month _M_m;
  2334. chrono::weekday_last _M_wdl;
  2335. public:
  2336. constexpr
  2337. year_month_weekday_last(const chrono::year& __y, const chrono::month& __m,
  2338. const chrono::weekday_last& __wdl) noexcept
  2339. : _M_y{__y}, _M_m{__m}, _M_wdl{__wdl}
  2340. { }
  2341. template<typename = __detail::__months_years_conversion_disambiguator>
  2342. constexpr year_month_weekday_last&
  2343. operator+=(const months& __m) noexcept
  2344. {
  2345. *this = *this + __m;
  2346. return *this;
  2347. }
  2348. template<typename = __detail::__months_years_conversion_disambiguator>
  2349. constexpr year_month_weekday_last&
  2350. operator-=(const months& __m) noexcept
  2351. {
  2352. *this = *this - __m;
  2353. return *this;
  2354. }
  2355. constexpr year_month_weekday_last&
  2356. operator+=(const years& __y) noexcept
  2357. {
  2358. *this = *this + __y;
  2359. return *this;
  2360. }
  2361. constexpr year_month_weekday_last&
  2362. operator-=(const years& __y) noexcept
  2363. {
  2364. *this = *this - __y;
  2365. return *this;
  2366. }
  2367. constexpr chrono::year
  2368. year() const noexcept
  2369. { return _M_y; }
  2370. constexpr chrono::month
  2371. month() const noexcept
  2372. { return _M_m; }
  2373. constexpr chrono::weekday
  2374. weekday() const noexcept
  2375. { return _M_wdl.weekday(); }
  2376. constexpr chrono::weekday_last
  2377. weekday_last() const noexcept
  2378. { return _M_wdl; }
  2379. constexpr
  2380. operator sys_days() const noexcept
  2381. {
  2382. const auto __d = sys_days{_M_y / _M_m / last};
  2383. return sys_days{(__d - (chrono::weekday{__d}
  2384. - _M_wdl.weekday())).time_since_epoch()};
  2385. }
  2386. explicit constexpr
  2387. operator local_days() const noexcept
  2388. { return local_days{sys_days{*this}.time_since_epoch()}; }
  2389. constexpr bool
  2390. ok() const noexcept
  2391. { return _M_y.ok() && _M_m.ok() && _M_wdl.ok(); }
  2392. friend constexpr bool
  2393. operator==(const year_month_weekday_last& __x,
  2394. const year_month_weekday_last& __y) noexcept
  2395. {
  2396. return __x.year() == __y.year()
  2397. && __x.month() == __y.month()
  2398. && __x.weekday_last() == __y.weekday_last();
  2399. }
  2400. template<typename = __detail::__months_years_conversion_disambiguator>
  2401. friend constexpr year_month_weekday_last
  2402. operator+(const year_month_weekday_last& __ymwdl,
  2403. const months& __dm) noexcept
  2404. {
  2405. return ((__ymwdl.year() / __ymwdl.month() + __dm)
  2406. / __ymwdl.weekday_last());
  2407. }
  2408. template<typename = __detail::__months_years_conversion_disambiguator>
  2409. friend constexpr year_month_weekday_last
  2410. operator+(const months& __dm,
  2411. const year_month_weekday_last& __ymwdl) noexcept
  2412. { return __ymwdl + __dm; }
  2413. friend constexpr year_month_weekday_last
  2414. operator+(const year_month_weekday_last& __ymwdl,
  2415. const years& __dy) noexcept
  2416. { return {__ymwdl.year() + __dy, __ymwdl.month(), __ymwdl.weekday_last()}; }
  2417. friend constexpr year_month_weekday_last
  2418. operator+(const years& __dy,
  2419. const year_month_weekday_last& __ymwdl) noexcept
  2420. { return __ymwdl + __dy; }
  2421. template<typename = __detail::__months_years_conversion_disambiguator>
  2422. friend constexpr year_month_weekday_last
  2423. operator-(const year_month_weekday_last& __ymwdl,
  2424. const months& __dm) noexcept
  2425. { return __ymwdl + -__dm; }
  2426. friend constexpr year_month_weekday_last
  2427. operator-(const year_month_weekday_last& __ymwdl,
  2428. const years& __dy) noexcept
  2429. { return __ymwdl + -__dy; }
  2430. friend constexpr year_month_weekday_last
  2431. operator/(const year_month& __ym,
  2432. const chrono::weekday_last& __wdl) noexcept
  2433. { return {__ym.year(), __ym.month(), __wdl}; }
  2434. friend constexpr year_month_weekday_last
  2435. operator/(const chrono::year& __y,
  2436. const chrono::month_weekday_last& __mwdl) noexcept
  2437. { return {__y, __mwdl.month(), __mwdl.weekday_last()}; }
  2438. friend constexpr year_month_weekday_last
  2439. operator/(int __y, const chrono::month_weekday_last& __mwdl) noexcept
  2440. { return chrono::year(__y) / __mwdl; }
  2441. friend constexpr year_month_weekday_last
  2442. operator/(const chrono::month_weekday_last& __mwdl,
  2443. const chrono::year& __y) noexcept
  2444. { return __y / __mwdl; }
  2445. friend constexpr year_month_weekday_last
  2446. operator/(const chrono::month_weekday_last& __mwdl, int __y) noexcept
  2447. { return chrono::year(__y) / __mwdl; }
  2448. // TODO: Implement operator<<.
  2449. };
  2450. // HH_MM_SS
  2451. namespace __detail
  2452. {
  2453. consteval long long
  2454. __pow10(unsigned __n)
  2455. {
  2456. long long __r = 1;
  2457. while (__n-- > 0)
  2458. __r *= 10;
  2459. return __r;
  2460. }
  2461. }
  2462. template<typename _Duration>
  2463. class hh_mm_ss
  2464. {
  2465. private:
  2466. static constexpr int
  2467. _S_fractional_width()
  2468. {
  2469. int __multiplicity_2 = 0;
  2470. int __multiplicity_5 = 0;
  2471. auto __den = _Duration::period::den;
  2472. while ((__den % 2) == 0)
  2473. {
  2474. ++__multiplicity_2;
  2475. __den /= 2;
  2476. }
  2477. while ((__den % 5) == 0)
  2478. {
  2479. ++__multiplicity_5;
  2480. __den /= 5;
  2481. }
  2482. if (__den != 1)
  2483. return 6;
  2484. int __width = (__multiplicity_2 > __multiplicity_5
  2485. ? __multiplicity_2 : __multiplicity_5);
  2486. if (__width > 18)
  2487. __width = 18;
  2488. return __width;
  2489. }
  2490. public:
  2491. static constexpr unsigned fractional_width = {_S_fractional_width()};
  2492. using precision
  2493. = duration<common_type_t<typename _Duration::rep,
  2494. chrono::seconds::rep>,
  2495. ratio<1, __detail::__pow10(fractional_width)>>;
  2496. constexpr
  2497. hh_mm_ss() noexcept
  2498. : hh_mm_ss{_Duration::zero()}
  2499. { }
  2500. constexpr explicit
  2501. hh_mm_ss(_Duration __d) noexcept
  2502. : _M_is_neg (__d < _Duration::zero()),
  2503. _M_h (duration_cast<chrono::hours>(abs(__d))),
  2504. _M_m (duration_cast<chrono::minutes>(abs(__d) - hours())),
  2505. _M_s (duration_cast<chrono::seconds>(abs(__d) - hours() - minutes()))
  2506. {
  2507. if constexpr (treat_as_floating_point_v<typename precision::rep>)
  2508. _M_ss = abs(__d) - hours() - minutes() - seconds();
  2509. else
  2510. _M_ss = duration_cast<precision>(abs(__d) - hours()
  2511. - minutes() - seconds());
  2512. }
  2513. constexpr bool
  2514. is_negative() const noexcept
  2515. { return _M_is_neg; }
  2516. constexpr chrono::hours
  2517. hours() const noexcept
  2518. { return _M_h; }
  2519. constexpr chrono::minutes
  2520. minutes() const noexcept
  2521. { return _M_m; }
  2522. constexpr chrono::seconds
  2523. seconds() const noexcept
  2524. { return _M_s; }
  2525. constexpr precision
  2526. subseconds() const noexcept
  2527. { return _M_ss; }
  2528. constexpr explicit
  2529. operator precision() const noexcept
  2530. { return to_duration(); }
  2531. constexpr precision
  2532. to_duration() const noexcept
  2533. {
  2534. if (_M_is_neg)
  2535. return -(_M_h + _M_m + _M_s + _M_ss);
  2536. else
  2537. return _M_h + _M_m + _M_s + _M_ss;
  2538. }
  2539. // TODO: Implement operator<<.
  2540. private:
  2541. bool _M_is_neg;
  2542. chrono::hours _M_h;
  2543. chrono::minutes _M_m;
  2544. chrono::seconds _M_s;
  2545. precision _M_ss;
  2546. };
  2547. #endif // C++20
  2548. /// @}
  2549. } // namespace chrono
  2550. #if __cplusplus > 201103L
  2551. #define __cpp_lib_chrono_udls 201304
  2552. inline namespace literals
  2553. {
  2554. /** ISO C++ 2014 namespace for suffixes for duration literals.
  2555. *
  2556. * These suffixes can be used to create `chrono::duration` values with
  2557. * tick periods of hours, minutes, seconds, milliseconds, microseconds
  2558. * or nanoseconds. For example, `std::chrono::seconds(5)` can be written
  2559. * as `5s` after making the suffix visible in the current scope.
  2560. * The suffixes can be made visible by a using-directive or
  2561. * using-declaration such as:
  2562. * - `using namespace std::chrono_literals;`
  2563. * - `using namespace std::literals;`
  2564. * - `using namespace std::chrono;`
  2565. * - `using namespace std;`
  2566. * - `using std::chrono_literals::operator""s;`
  2567. *
  2568. * The result of these suffixes on an integer literal is one of the
  2569. * standard typedefs such as `std::chrono::hours`.
  2570. * The result on a floating-point literal is a duration type with the
  2571. * specified tick period and an unspecified floating-point representation,
  2572. * for example `1.5e2ms` might be equivalent to
  2573. * `chrono::duration<long double, chrono::milli>(1.5e2)`.
  2574. *
  2575. * @ingroup chrono
  2576. */
  2577. inline namespace chrono_literals
  2578. {
  2579. #pragma GCC diagnostic push
  2580. #pragma GCC diagnostic ignored "-Wliteral-suffix"
  2581. /// @cond undocumented
  2582. template<typename _Dur, char... _Digits>
  2583. constexpr _Dur __check_overflow()
  2584. {
  2585. using _Val = __parse_int::_Parse_int<_Digits...>;
  2586. constexpr typename _Dur::rep __repval = _Val::value;
  2587. static_assert(__repval >= 0 && __repval == _Val::value,
  2588. "literal value cannot be represented by duration type");
  2589. return _Dur(__repval);
  2590. }
  2591. /// @endcond
  2592. /// Literal suffix for durations representing non-integer hours
  2593. constexpr chrono::duration<long double, ratio<3600,1>>
  2594. operator""h(long double __hours)
  2595. { return chrono::duration<long double, ratio<3600,1>>{__hours}; }
  2596. /// Literal suffix for durations of type `std::chrono::hours`
  2597. template <char... _Digits>
  2598. constexpr chrono::hours
  2599. operator""h()
  2600. { return __check_overflow<chrono::hours, _Digits...>(); }
  2601. /// Literal suffix for durations representing non-integer minutes
  2602. constexpr chrono::duration<long double, ratio<60,1>>
  2603. operator""min(long double __mins)
  2604. { return chrono::duration<long double, ratio<60,1>>{__mins}; }
  2605. /// Literal suffix for durations of type `std::chrono::minutes`
  2606. template <char... _Digits>
  2607. constexpr chrono::minutes
  2608. operator""min()
  2609. { return __check_overflow<chrono::minutes, _Digits...>(); }
  2610. /// Literal suffix for durations representing non-integer seconds
  2611. constexpr chrono::duration<long double>
  2612. operator""s(long double __secs)
  2613. { return chrono::duration<long double>{__secs}; }
  2614. /// Literal suffix for durations of type `std::chrono::seconds`
  2615. template <char... _Digits>
  2616. constexpr chrono::seconds
  2617. operator""s()
  2618. { return __check_overflow<chrono::seconds, _Digits...>(); }
  2619. /// Literal suffix for durations representing non-integer milliseconds
  2620. constexpr chrono::duration<long double, milli>
  2621. operator""ms(long double __msecs)
  2622. { return chrono::duration<long double, milli>{__msecs}; }
  2623. /// Literal suffix for durations of type `std::chrono::milliseconds`
  2624. template <char... _Digits>
  2625. constexpr chrono::milliseconds
  2626. operator""ms()
  2627. { return __check_overflow<chrono::milliseconds, _Digits...>(); }
  2628. /// Literal suffix for durations representing non-integer microseconds
  2629. constexpr chrono::duration<long double, micro>
  2630. operator""us(long double __usecs)
  2631. { return chrono::duration<long double, micro>{__usecs}; }
  2632. /// Literal suffix for durations of type `std::chrono::microseconds`
  2633. template <char... _Digits>
  2634. constexpr chrono::microseconds
  2635. operator""us()
  2636. { return __check_overflow<chrono::microseconds, _Digits...>(); }
  2637. /// Literal suffix for durations representing non-integer nanoseconds
  2638. constexpr chrono::duration<long double, nano>
  2639. operator""ns(long double __nsecs)
  2640. { return chrono::duration<long double, nano>{__nsecs}; }
  2641. /// Literal suffix for durations of type `std::chrono::nanoseconds`
  2642. template <char... _Digits>
  2643. constexpr chrono::nanoseconds
  2644. operator""ns()
  2645. { return __check_overflow<chrono::nanoseconds, _Digits...>(); }
  2646. #if __cplusplus > 201703L
  2647. constexpr chrono::day
  2648. operator""d(unsigned long long __d) noexcept
  2649. { return chrono::day{static_cast<unsigned>(__d)}; }
  2650. constexpr chrono::year
  2651. operator""y(unsigned long long __y) noexcept
  2652. { return chrono::year{static_cast<int>(__y)}; }
  2653. #endif // C++20
  2654. #pragma GCC diagnostic pop
  2655. } // inline namespace chrono_literals
  2656. } // inline namespace literals
  2657. namespace chrono
  2658. {
  2659. using namespace literals::chrono_literals;
  2660. } // namespace chrono
  2661. #if __cplusplus > 201703L
  2662. namespace chrono
  2663. {
  2664. // 12/24 HOURS FUNCTIONS
  2665. constexpr bool
  2666. is_am(const hours& __h) noexcept
  2667. { return 0h <= __h && __h <= 11h; }
  2668. constexpr bool
  2669. is_pm(const hours& __h) noexcept
  2670. { return 12h <= __h && __h <= 23h; }
  2671. constexpr hours
  2672. make12(const hours& __h) noexcept
  2673. {
  2674. if (__h == 0h)
  2675. return 12h;
  2676. else if (__h > 12h)
  2677. return __h - 12h;
  2678. return __h;
  2679. }
  2680. constexpr hours
  2681. make24(const hours& __h, bool __is_pm) noexcept
  2682. {
  2683. if (!__is_pm)
  2684. {
  2685. if (__h == 12h)
  2686. return 0h;
  2687. else
  2688. return __h;
  2689. }
  2690. else
  2691. {
  2692. if (__h == 12h)
  2693. return __h;
  2694. else
  2695. return __h + 12h;
  2696. }
  2697. }
  2698. }
  2699. #endif
  2700. #if __cplusplus >= 201703L
  2701. namespace filesystem
  2702. {
  2703. struct __file_clock
  2704. {
  2705. using duration = chrono::nanoseconds;
  2706. using rep = duration::rep;
  2707. using period = duration::period;
  2708. using time_point = chrono::time_point<__file_clock>;
  2709. static constexpr bool is_steady = false;
  2710. static time_point
  2711. now() noexcept
  2712. { return _S_from_sys(chrono::system_clock::now()); }
  2713. #if __cplusplus > 201703L
  2714. template<typename _Dur>
  2715. static
  2716. chrono::file_time<_Dur>
  2717. from_sys(const chrono::sys_time<_Dur>& __t) noexcept
  2718. { return _S_from_sys(__t); }
  2719. // For internal use only
  2720. template<typename _Dur>
  2721. static
  2722. chrono::sys_time<_Dur>
  2723. to_sys(const chrono::file_time<_Dur>& __t) noexcept
  2724. { return _S_to_sys(__t); }
  2725. #endif // C++20
  2726. private:
  2727. using __sys_clock = chrono::system_clock;
  2728. // This clock's (unspecified) epoch is 2174-01-01 00:00:00 UTC.
  2729. // A signed 64-bit duration with nanosecond resolution gives roughly
  2730. // +/- 292 years, which covers the 1901-2446 date range for ext4.
  2731. static constexpr chrono::seconds _S_epoch_diff{6437664000};
  2732. protected:
  2733. // For internal use only
  2734. template<typename _Dur>
  2735. static
  2736. chrono::time_point<__file_clock, _Dur>
  2737. _S_from_sys(const chrono::time_point<__sys_clock, _Dur>& __t) noexcept
  2738. {
  2739. using __file_time = chrono::time_point<__file_clock, _Dur>;
  2740. return __file_time{__t.time_since_epoch()} - _S_epoch_diff;
  2741. }
  2742. // For internal use only
  2743. template<typename _Dur>
  2744. static
  2745. chrono::time_point<__sys_clock, _Dur>
  2746. _S_to_sys(const chrono::time_point<__file_clock, _Dur>& __t) noexcept
  2747. {
  2748. using __sys_time = chrono::time_point<__sys_clock, _Dur>;
  2749. return __sys_time{__t.time_since_epoch()} + _S_epoch_diff;
  2750. }
  2751. };
  2752. } // namespace filesystem
  2753. #endif // C++17
  2754. #endif // C++14
  2755. _GLIBCXX_END_NAMESPACE_VERSION
  2756. } // namespace std
  2757. #endif // C++11
  2758. #endif //_GLIBCXX_CHRONO