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 //  explicit constexpr year_month_day(const local_days& dp) noexcept;
14 //
15 //
16 //  Effects:  Constructs an object of type year_month_day that corresponds
17 //                to the date represented by dp
18 //
19 //  Remarks: Equivalent to constructing with sys_days{dp.time_since_epoch()}.
20 //
21 //  constexpr chrono::year   year() const noexcept;
22 //  constexpr chrono::month month() const noexcept;
23 //  constexpr chrono::day     day() const noexcept;
24 //  constexpr bool             ok() const noexcept;
25 
26 #include <chrono>
27 #include <type_traits>
28 #include <cassert>
29 
30 #include "test_macros.h"
31 
main(int,char **)32 int main(int, char**)
33 {
34     using year           = std::chrono::year;
35     using day            = std::chrono::day;
36     using local_days     = std::chrono::local_days;
37     using days           = std::chrono::days;
38     using year_month_day = std::chrono::year_month_day;
39 
40     ASSERT_NOEXCEPT(year_month_day{std::declval<local_days>()});
41 
42     {
43     constexpr local_days sd{};
44     constexpr year_month_day ymd{sd};
45 
46     static_assert( ymd.ok(),                            "");
47     static_assert( ymd.year()  == year{1970},           "");
48     static_assert( ymd.month() == std::chrono::January, "");
49     static_assert( ymd.day()   == day{1},               "");
50     }
51 
52     {
53     constexpr local_days sd{days{10957+32}};
54     constexpr year_month_day ymd{sd};
55 
56     static_assert( ymd.ok(),                             "");
57     static_assert( ymd.year()  == year{2000},            "");
58     static_assert( ymd.month() == std::chrono::February, "");
59     static_assert( ymd.day()   == day{2},                "");
60     }
61 
62 
63 //  There's one more leap day between 1/1/40 and 1/1/70
64 //  when compared to 1/1/70 -> 1/1/2000
65     {
66     constexpr local_days sd{days{-10957}};
67     constexpr year_month_day ymd{sd};
68 
69     static_assert( ymd.ok(),                            "");
70     static_assert( ymd.year()  == year{1940},           "");
71     static_assert( ymd.month() == std::chrono::January, "");
72     static_assert( ymd.day()   == day{2},               "");
73     }
74 
75     {
76     local_days sd{days{-(10957+34)}};
77     year_month_day ymd{sd};
78 
79     assert( ymd.ok());
80     assert( ymd.year()  == year{1939});
81     assert( ymd.month() == std::chrono::November);
82     assert( ymd.day()   == day{29});
83     }
84 
85   return 0;
86 }
87