Debian Codegraph @

libcxx/test/std/time/time.cal/time.cal.ymd/time.cal.ymd.members/op.local_days.pass.cpp

raw
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // UNSUPPORTED: c++03, c++11, c++14, c++17
9
10 // <chrono>
11 // class year_month_day;
12
13 // constexpr operator local_days() const noexcept;
14 //
15 // Returns: If ok(), returns a local_days holding a count of days from the
16 // local_days epoch to *this (a negative value if *this represents a date
17 // prior to the sys_days epoch). Otherwise, if y_.ok() && m_.ok() is true,
18 // returns a sys_days which is offset from sys_days{y_/m_/last} by the
19 // number of days d_ is offset from sys_days{y_/m_/last}.day(). Otherwise
20 // the value returned is unspecified.
21 //
22 // Remarks: A local_days in the range [days{-12687428}, days{11248737}] which
23 // is converted to a year_month_day shall have the same value when
24 // converted back to a sys_days.
25 //
26 // [Example:
27 // static_assert(year_month_day{local_days{2017y/January/0}} == 2016y/December/31);
28 // static_assert(year_month_day{local_days{2017y/January/31}} == 2017y/January/31);
29 // static_assert(year_month_day{local_days{2017y/January/32}} == 2017y/February/1);
30 // -end example]
31
32 #include <chrono>
33 #include <type_traits>
34 #include <cassert>
35
36 #include "test_macros.h"
37
38 void RunTheExample()
39 {
40 using namespace std::chrono;
41
42 static_assert(year_month_day{local_days{year{2017}/January/0}} == year{2016}/December/31);
43 static_assert(year_month_day{local_days{year{2017}/January/31}} == year{2017}/January/31);
44 static_assert(year_month_day{local_days{year{2017}/January/32}} == year{2017}/February/1);
45 }
46
47 int main(int, char**)
48 {
49 using year = std::chrono::year;
50 using month = std::chrono::month;
51 using day = std::chrono::day;
52 using local_days = std::chrono::local_days;
53 using days = std::chrono::days;
54 using year_month_day = std::chrono::year_month_day;
55
56 ASSERT_NOEXCEPT(local_days(std::declval<year_month_day>()));
57 RunTheExample();
58
59 {
60 constexpr year_month_day ymd{year{1970}, month{1}, day{1}};
61 constexpr local_days sd{ymd};
62
63 static_assert( sd.time_since_epoch() == days{0}, "");
64 static_assert( year_month_day{sd} == ymd, ""); // and back
65 }
66
67 {
68 constexpr year_month_day ymd{year{2000}, month{2}, day{2}};
69 constexpr local_days sd{ymd};
70
71 static_assert( sd.time_since_epoch() == days{10957+32}, "");
72 static_assert( year_month_day{sd} == ymd, ""); // and back
73 }
74
75 // There's one more leap day between 1/1/40 and 1/1/70
76 // when compared to 1/1/70 -> 1/1/2000
77 {
78 constexpr year_month_day ymd{year{1940}, month{1}, day{2}};
79 constexpr local_days sd{ymd};
80
81 static_assert( sd.time_since_epoch() == days{-10957}, "");
82 static_assert( year_month_day{sd} == ymd, ""); // and back
83 }
84
85 {
86 year_month_day ymd{year{1939}, month{11}, day{29}};
87 local_days sd{ymd};
88
89 assert( sd.time_since_epoch() == days{-(10957+34)});
90 assert( year_month_day{sd} == ymd); // and back
91 }
92
93 return 0;
94 }