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 sys_days() const noexcept;
14//
15// Returns: If ok(), returns a sys_days holding a count of days from the
16// sys_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 sys_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{sys_days{2017y/January/0}} == 2016y/December/31);
28// static_assert(year_month_day{sys_days{2017y/January/31}} == 2017y/January/31);
29// static_assert(year_month_day{sys_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
39void 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
48int main(int, char**)
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 // These two tests check the wording for LWG 3206
95 {
96 constexpr year_month_day ymd{year{1971}, month{1}, day{0}}; // bad day
97 static_assert(!ymd.ok(), "");
98 static_assert( ymd.year().ok(), "");
99 static_assert( ymd.month().ok(), "");
100 static_assert(!ymd.day().ok(), "");
101 constexpr sys_days sd{ymd};
102 static_assert(sd.time_since_epoch() == days{364}, "");
103 static_assert(sd == sys_days{ymd.year()/ymd.month()/day{1}} + (ymd.day() - day{1}), "");
104 }
105
106 {
107 constexpr year_month_day ymd{year{1970}, month{12}, day{32}}; // bad day
108 static_assert(!ymd.ok(), "");
109 static_assert( ymd.year().ok(), "");
110 static_assert( ymd.month().ok(), "");
111 static_assert(!ymd.day().ok(), "");
112 constexpr sys_days sd{ymd};
113 static_assert(sd.time_since_epoch() == days{365}, "");
114 static_assert(sd == sys_days{ymd.year()/ymd.month()/day{1}} + (ymd.day() - day{1}), "");
115 }
116
117 return 0;
118}
119

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