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// constexpr operator sys_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 <cassert>
24#include <type_traits>
25#include <utility>
26
27#include "test_macros.h"
28
29int main(int, char**)
30{
31 using year = std::chrono::year;
32 using month = std::chrono::month;
33 using weekday_indexed = std::chrono::weekday_indexed;
34 using sys_days = std::chrono::sys_days;
35 using days = std::chrono::days;
36 using year_month_weekday = std::chrono::year_month_weekday;
37
38 ASSERT_NOEXCEPT(sys_days(std::declval<year_month_weekday>()));
39
40 {
41 constexpr year_month_weekday ymwd{year{1970}, month{1}, weekday_indexed{std::chrono::Thursday, 1}};
42 constexpr sys_days sd{ymwd};
43
44 static_assert( sd.time_since_epoch() == days{0}, "");
45 static_assert( year_month_weekday{sd} == ymwd, ""); // and back
46 }
47
48 {
49 constexpr year_month_weekday ymwd{year{2000}, month{2}, weekday_indexed{std::chrono::Wednesday, 1}};
50 constexpr sys_days sd{ymwd};
51
52 static_assert( sd.time_since_epoch() == days{10957+32}, "");
53 static_assert( year_month_weekday{sd} == ymwd, ""); // and back
54 }
55
56 // There's one more leap day between 1/1/40 and 1/1/70
57 // when compared to 1/1/70 -> 1/1/2000
58 {
59 constexpr year_month_weekday ymwd{year{1940}, month{1},weekday_indexed{std::chrono::Tuesday, 1}};
60 constexpr sys_days sd{ymwd};
61
62 static_assert( sd.time_since_epoch() == days{-10957}, "");
63 static_assert( year_month_weekday{sd} == ymwd, ""); // and back
64 }
65
66 {
67 year_month_weekday ymwd{year{1939}, month{11}, weekday_indexed{std::chrono::Wednesday, 5}};
68 sys_days sd{ymwd};
69
70 assert( sd.time_since_epoch() == days{-(10957+34)});
71 assert( year_month_weekday{sd} == ymwd); // and back
72 }
73
74 {
75 // Index 0 returns 7 weekdays before index 1 and can't be round-tripped.
76 constexpr year_month_weekday ymwd{year{2000}, month{2}, weekday_indexed{std::chrono::Wednesday, 0}};
77 constexpr sys_days sd{ymwd};
78
79 static_assert(sd.time_since_epoch() == days{10957 + 25});
80 static_assert(year_month_weekday{sd} != ymwd); // and back fails
81 }
82
83 return 0;
84}
85

source code of libcxx/test/std/time/time.cal/time.cal.ymwd/time.cal.ymwd.members/op.sys_days.pass.cpp