• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
10 
11 // <chrono>
12 // class year_month_day;
13 
14 // constexpr operator sys_days() const noexcept;
15 //
16 // Returns: If ok(), returns a sys_days holding a count of days from the
17 //   sys_days epoch to *this (a negative value if *this represents a date
18 //   prior to the sys_days epoch). Otherwise, if y_.ok() && m_.ok() is true,
19 //   returns a sys_days which is offset from sys_days{y_/m_/last} by the
20 //   number of days d_ is offset from sys_days{y_/m_/last}.day(). Otherwise
21 //   the value returned is unspecified.
22 //
23 // Remarks: A sys_days in the range [days{-12687428}, days{11248737}] which
24 //   is converted to a year_month_day shall have the same value when
25 //   converted back to a sys_days.
26 //
27 // [Example:
28 //   static_assert(year_month_day{sys_days{2017y/January/0}}  == 2016y/December/31);
29 //   static_assert(year_month_day{sys_days{2017y/January/31}} == 2017y/January/31);
30 //   static_assert(year_month_day{sys_days{2017y/January/32}} == 2017y/February/1);
31 // —end example]
32 
33 #include <chrono>
34 #include <type_traits>
35 #include <cassert>
36 
37 #include "test_macros.h"
38 
RunTheExample()39 void RunTheExample()
40 {
41     using namespace std::chrono;
42 
43     static_assert(year_month_day{sys_days{year{2017}/January/0}}  == year{2016}/December/31);
44     static_assert(year_month_day{sys_days{year{2017}/January/31}} == year{2017}/January/31);
45     static_assert(year_month_day{sys_days{year{2017}/January/32}} == year{2017}/February/1);
46 }
47 
main()48 int main()
49 {
50     using year           = std::chrono::year;
51     using month          = std::chrono::month;
52     using day            = std::chrono::day;
53     using sys_days       = std::chrono::sys_days;
54     using days           = std::chrono::days;
55     using year_month_day = std::chrono::year_month_day;
56 
57     ASSERT_NOEXCEPT(sys_days(std::declval<year_month_day>()));
58     RunTheExample();
59 
60     {
61     constexpr year_month_day ymd{year{1970}, month{1}, day{1}};
62     constexpr sys_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 sys_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 sys_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     sys_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 }
95