File: op.sys_days.pass.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (117 lines) | stat: -rw-r--r-- 4,173 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17

// <chrono>
// class year_month_day;

// constexpr operator sys_days() const noexcept;
//
// Returns: If ok(), returns a sys_days holding a count of days from the
//   sys_days epoch to *this (a negative value if *this represents a date
//   prior to the sys_days epoch). Otherwise, if y_.ok() && m_.ok() is true,
//   returns a sys_days which is offset from sys_days{y_/m_/last} by the
//   number of days d_ is offset from sys_days{y_/m_/last}.day(). Otherwise
//   the value returned is unspecified.
//
// Remarks: A sys_days in the range [days{-12687428}, days{11248737}] which
//   is converted to a year_month_day shall have the same value when
//   converted back to a sys_days.
//
// [Example:
//   static_assert(year_month_day{sys_days{2017y/January/0}}  == 2016y/December/31);
//   static_assert(year_month_day{sys_days{2017y/January/31}} == 2017y/January/31);
//   static_assert(year_month_day{sys_days{2017y/January/32}} == 2017y/February/1);
// -end example]

#include <chrono>
#include <type_traits>
#include <cassert>

#include "test_macros.h"

void RunTheExample()
{
    using namespace std::chrono;

    static_assert(year_month_day{sys_days{year{2017}/January/0}}  == year{2016}/December/31);
    static_assert(year_month_day{sys_days{year{2017}/January/31}} == year{2017}/January/31);
    static_assert(year_month_day{sys_days{year{2017}/January/32}} == year{2017}/February/1);
}

int main(int, char**)
{
    using year           = std::chrono::year;
    using month          = std::chrono::month;
    using day            = std::chrono::day;
    using sys_days       = std::chrono::sys_days;
    using days           = std::chrono::days;
    using year_month_day = std::chrono::year_month_day;

    ASSERT_NOEXCEPT(sys_days(std::declval<year_month_day>()));
    RunTheExample();

    {
    constexpr year_month_day ymd{year{1970}, month{1}, day{1}};
    constexpr sys_days sd{ymd};

    static_assert( sd.time_since_epoch() == days{0}, "");
    static_assert( year_month_day{sd} == ymd, ""); // and back
    }

    {
    constexpr year_month_day ymd{year{2000}, month{2}, day{2}};
    constexpr sys_days sd{ymd};

    static_assert( sd.time_since_epoch() == days{10957+32}, "");
    static_assert( year_month_day{sd} == ymd, ""); // and back
    }

    // There's one more leap day between 1/1/40 and 1/1/70
    // when compared to 1/1/70 -> 1/1/2000
    {
    constexpr year_month_day ymd{year{1940}, month{1}, day{2}};
    constexpr sys_days sd{ymd};

    static_assert( sd.time_since_epoch() == days{-10957}, "");
    static_assert( year_month_day{sd} == ymd, ""); // and back
    }

    {
    year_month_day ymd{year{1939}, month{11}, day{29}};
    sys_days sd{ymd};

    assert( sd.time_since_epoch() == days{-(10957+34)});
    assert( year_month_day{sd} == ymd); // and back
    }

    // These two tests check the wording for LWG 3206
    {
    constexpr year_month_day ymd{year{1971}, month{1}, day{0}}; // bad day
    static_assert(!ymd.ok(),         "");
    static_assert( ymd.year().ok(),  "");
    static_assert( ymd.month().ok(), "");
    static_assert(!ymd.day().ok(),   "");
    constexpr sys_days sd{ymd};
    static_assert(sd.time_since_epoch() == days{364}, "");
    static_assert(sd == sys_days{ymd.year()/ymd.month()/day{1}} + (ymd.day() - day{1}), "");
    }

    {
    constexpr year_month_day ymd{year{1970}, month{12}, day{32}}; // bad day
    static_assert(!ymd.ok(),         "");
    static_assert( ymd.year().ok(),  "");
    static_assert( ymd.month().ok(), "");
    static_assert(!ymd.day().ok(),   "");
    constexpr sys_days sd{ymd};
    static_assert(sd.time_since_epoch() == days{365}, "");
    static_assert(sd == sys_days{ymd.year()/ymd.month()/day{1}} + (ymd.day() - day{1}), "");
    }

    return 0;
}