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 <cassert>
34 #include <type_traits>
35 #include <utility>
36
37 #include "test_macros.h"
38
39 void RunTheExample()
40 {
41 using namespace std::chrono;
42
43 static_assert(year_month_day{local_days{year{2017}/January/0}} == year{2016}/December/31);
44 static_assert(year_month_day{local_days{year{2017}/January/31}} == year{2017}/January/31);
45 static_assert(year_month_day{local_days{year{2017}/January/32}} == year{2017}/February/1);
46 }
47
48 int main(int, char**)
49 {
50 using year = std::chrono::year;
51 using month = std::chrono::month;
52 using day = std::chrono::day;
53 using local_days = std::chrono::local_days;
54 using days = std::chrono::days;
55 using year_month_day = std::chrono::year_month_day;
56
57 ASSERT_NOEXCEPT(local_days(std::declval<year_month_day>()));
58 RunTheExample();
59
60 {
61 constexpr year_month_day ymd{year{1970}, month{1}, day{1}};
62 constexpr local_days sd{ymd};
63
64 static_assert( sd.time_since_epoch() == days{0}, "");
65 static_assert( year_month_day{sd} == ymd, ""); // and back
66 }
67
68 {
69 constexpr year_month_day ymd{year{2000}, month{2}, day{2}};
70 constexpr local_days sd{ymd};
71
72 static_assert( sd.time_since_epoch() == days{10957+32}, "");
73 static_assert( year_month_day{sd} == ymd, ""); // and back
74 }
75
76 // There's one more leap day between 1/1/40 and 1/1/70
77 // when compared to 1/1/70 -> 1/1/2000
78 {
79 constexpr year_month_day ymd{year{1940}, month{1}, day{2}};
80 constexpr local_days sd{ymd};
81
82 static_assert( sd.time_since_epoch() == days{-10957}, "");
83 static_assert( year_month_day{sd} == ymd, ""); // and back
84 }
85
86 {
87 year_month_day ymd{year{1939}, month{11}, day{29}};
88 local_days sd{ymd};
89
90 assert( sd.time_since_epoch() == days{-(10957+34)});
91 assert( year_month_day{sd} == ymd); // and back
92 }
93
94 return 0;
95 }