| 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 <type_traits> |
| 34 |
#include <cassert> |
| 35 |
|
| 36 |
#include "test_macros.h" |
| 37 |
|
| 38 |
void RunTheExample() |
| 39 |
{ |
| 40 |
using namespace std::chrono; |
| 41 |
|
| 42 |
static_assert(year_month_day{sys_days{year{2017}/January/0}} == year{2016}/December/31); |
| 43 |
static_assert(year_month_day{sys_days{year{2017}/January/31}} == year{2017}/January/31); |
| 44 |
static_assert(year_month_day{sys_days{year{2017}/January/32}} == year{2017}/February/1); |
| 45 |
} |
| 46 |
|
| 47 |
int main(int, char**) |
| 48 |
{ |
| 49 |
using year = std::chrono::year; |
| 50 |
using month = std::chrono::month; |
| 51 |
using day = std::chrono::day; |
| 52 |
using sys_days = std::chrono::sys_days; |
| 53 |
using days = std::chrono::days; |
| 54 |
using year_month_day = std::chrono::year_month_day; |
| 55 |
|
| 56 |
ASSERT_NOEXCEPT(sys_days(std::declval<year_month_day>())); |
| 57 |
RunTheExample(); |
| 58 |
|
| 59 |
{ |
| 60 |
constexpr year_month_day ymd{year{1970}, month{1}, day{1}}; |
| 61 |
constexpr sys_days sd{ymd}; |
| 62 |
|
| 63 |
static_assert( sd.time_since_epoch() == days{0}, ""); |
| 64 |
static_assert( year_month_day{sd} == ymd, ""); // and back |
| 65 |
} |
| 66 |
|
| 67 |
{ |
| 68 |
constexpr year_month_day ymd{year{2000}, month{2}, day{2}}; |
| 69 |
constexpr sys_days sd{ymd}; |
| 70 |
|
| 71 |
static_assert( sd.time_since_epoch() == days{10957+32}, ""); |
| 72 |
static_assert( year_month_day{sd} == ymd, ""); // and back |
| 73 |
} |
| 74 |
|
| 75 |
// There's one more leap day between 1/1/40 and 1/1/70 |
| 76 |
// when compared to 1/1/70 -> 1/1/2000 |
| 77 |
{ |
| 78 |
constexpr year_month_day ymd{year{1940}, month{1}, day{2}}; |
| 79 |
constexpr sys_days sd{ymd}; |
| 80 |
|
| 81 |
static_assert( sd.time_since_epoch() == days{-10957}, ""); |
| 82 |
static_assert( year_month_day{sd} == ymd, ""); // and back |
| 83 |
} |
| 84 |
|
| 85 |
{ |
| 86 |
year_month_day ymd{year{1939}, month{11}, day{29}}; |
| 87 |
sys_days sd{ymd}; |
| 88 |
|
| 89 |
assert( sd.time_since_epoch() == days{-(10957+34)}); |
| 90 |
assert( year_month_day{sd} == ymd); // and back |
| 91 |
} |
| 92 |
|
| 93 |
// These two tests check the wording for LWG 3206 |
| 94 |
{ |
| 95 |
constexpr year_month_day ymd{year{1971}, month{1}, day{0}}; // bad day |
| 96 |
static_assert(!ymd.ok(), ""); |
| 97 |
static_assert( ymd.year().ok(), ""); |
| 98 |
static_assert( ymd.month().ok(), ""); |
| 99 |
static_assert(!ymd.day().ok(), ""); |
| 100 |
constexpr sys_days sd{ymd}; |
| 101 |
static_assert(sd.time_since_epoch() == days{364}, ""); |
| 102 |
static_assert(sd == sys_days{ymd.year()/ymd.month()/day{1}} + (ymd.day() - day{1}), ""); |
| 103 |
} |
| 104 |
|
| 105 |
{ |
| 106 |
constexpr year_month_day ymd{year{1970}, month{12}, day{32}}; // bad day |
| 107 |
static_assert(!ymd.ok(), ""); |
| 108 |
static_assert( ymd.year().ok(), ""); |
| 109 |
static_assert( ymd.month().ok(), ""); |
| 110 |
static_assert(!ymd.day().ok(), ""); |
| 111 |
constexpr sys_days sd{ymd}; |
| 112 |
static_assert(sd.time_since_epoch() == days{365}, ""); |
| 113 |
static_assert(sd == sys_days{ymd.year()/ymd.month()/day{1}} + (ymd.day() - day{1}), ""); |
| 114 |
} |
| 115 |
|
| 116 |
return 0; |
| 117 |
} |