Debian Codegraph @

libcxx/test/std/time/time.cal/time.cal.ymwd/time.cal.ymwd.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_weekday;
12
13 // explicit constexpr operator local_days() const noexcept;
14 //
15 // Returns: If y_.ok() && m_.ok() && wdi_.weekday().ok(), returns a
16 // sys_days that represents the date (index() - 1) * 7 days after the first
17 // weekday() of year()/month(). If index() is 0 the returned sys_days
18 // represents the date 7 days prior to the first weekday() of
19 // year()/month(). Otherwise the returned value is unspecified.
20 //
21
22 #include <chrono>
23 #include <type_traits>
24 #include <cassert>
25
26 #include "test_macros.h"
27
28 int main(int, char**)
29 {
30 using year = std::chrono::year;
31 using month = std::chrono::month;
32 using weekday_indexed = std::chrono::weekday_indexed;
33 using local_days = std::chrono::local_days;
34 using days = std::chrono::days;
35 using year_month_weekday = std::chrono::year_month_weekday;
36
37 ASSERT_NOEXCEPT(local_days(std::declval<year_month_weekday>()));
38
39 {
40 constexpr year_month_weekday ymwd{year{1970}, month{1}, weekday_indexed{std::chrono::Thursday, 1}};
41 constexpr local_days sd{ymwd};
42
43 static_assert( sd.time_since_epoch() == days{0}, "");
44 static_assert( year_month_weekday{sd} == ymwd, ""); // and back
45 }
46
47 {
48 constexpr year_month_weekday ymwd{year{2000}, month{2}, weekday_indexed{std::chrono::Wednesday, 1}};
49 constexpr local_days sd{ymwd};
50
51 static_assert( sd.time_since_epoch() == days{10957+32}, "");
52 static_assert( year_month_weekday{sd} == ymwd, ""); // and back
53 }
54
55 // There's one more leap day between 1/1/40 and 1/1/70
56 // when compared to 1/1/70 -> 1/1/2000
57 {
58 constexpr year_month_weekday ymwd{year{1940}, month{1},weekday_indexed{std::chrono::Tuesday, 1}};
59 constexpr local_days sd{ymwd};
60
61 static_assert( sd.time_since_epoch() == days{-10957}, "");
62 static_assert( year_month_weekday{sd} == ymwd, ""); // and back
63 }
64
65 {
66 year_month_weekday ymwd{year{1939}, month{11}, weekday_indexed{std::chrono::Wednesday, 5}};
67 local_days sd{ymwd};
68
69 assert( sd.time_since_epoch() == days{-(10957+34)});
70 assert( year_month_weekday{sd} == ymwd); // and back
71 }
72
73 return 0;
74 }