1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2015, 2016 Howard Hinnant
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22
23 // class year_weeknum_weekday
24
25 // class year_weeknum_weekday
26 // {
27 // public:
28 // constexpr year_weeknum_weekday(const iso_week::year& y, const iso_week::weeknum& wn,
29 // const iso_week::weekday& wd) noexcept;
30 // constexpr year_weeknum_weekday(const year_lastweek_weekday& ylwwd) noexcept;
31 // constexpr year_weeknum_weekday(const sys_days& dp) noexcept;
32 //
33 // year_weeknum_weekday& operator+=(const years& y) noexcept;
34 // year_weeknum_weekday& operator-=(const years& y) noexcept;
35 //
36 // constexpr iso_week::year year() const noexcept;
37 // constexpr iso_week::weeknum weeknum() const noexcept;
38 // constexpr iso_week::weekday weekday() const noexcept;
39 //
40 // constexpr operator sys_days() const noexcept;
41 // constexpr bool ok() const noexcept;
42 // };
43 //
44 // constexpr bool operator==(const year_weeknum_weekday& x, const year_weeknum_weekday& y) noexcept;
45 // constexpr bool operator!=(const year_weeknum_weekday& x, const year_weeknum_weekday& y) noexcept;
46 // constexpr bool operator< (const year_weeknum_weekday& x, const year_weeknum_weekday& y) noexcept;
47 // constexpr bool operator> (const year_weeknum_weekday& x, const year_weeknum_weekday& y) noexcept;
48 // constexpr bool operator<=(const year_weeknum_weekday& x, const year_weeknum_weekday& y) noexcept;
49 // constexpr bool operator>=(const year_weeknum_weekday& x, const year_weeknum_weekday& y) noexcept;
50 //
51 // constexpr year_weeknum_weekday operator+(const year_weeknum_weekday& ywnwd, const years& y) noexcept;
52 // constexpr year_weeknum_weekday operator+(const years& y, const year_weeknum_weekday& ywnwd) noexcept;
53 // constexpr year_weeknum_weekday operator-(const year_weeknum_weekday& ywnwd, const years& y) noexcept;
54 //
55 // std::ostream& operator<<(std::ostream& os, const year_weeknum_weekday& ywnwd);
56
57 #include "iso_week.h"
58
59 #include <cassert>
60 #include <sstream>
61 #include <type_traits>
62
63 static_assert( std::is_trivially_destructible<iso_week::year_weeknum_weekday>{}, "");
64 static_assert(!std::is_default_constructible<iso_week::year_weeknum_weekday>{}, "");
65 static_assert( std::is_trivially_copy_constructible<iso_week::year_weeknum_weekday>{}, "");
66 static_assert( std::is_trivially_copy_assignable<iso_week::year_weeknum_weekday>{}, "");
67 static_assert( std::is_trivially_move_constructible<iso_week::year_weeknum_weekday>{}, "");
68 static_assert( std::is_trivially_move_assignable<iso_week::year_weeknum_weekday>{}, "");
69
70 static_assert(std::is_trivially_copyable<iso_week::year_weeknum_weekday>{}, "");
71 static_assert(std::is_standard_layout<iso_week::year_weeknum_weekday>{}, "");
72 static_assert(std::is_literal_type<iso_week::year_weeknum_weekday>{}, "");
73
74 static_assert( std::is_nothrow_constructible<iso_week::year_weeknum_weekday,
75 iso_week::year, iso_week::weeknum,
76 iso_week::weekday>{}, "");
77 static_assert( std::is_nothrow_constructible<iso_week::year_weeknum_weekday,
78 iso_week::year_lastweek_weekday>{}, "");
79 static_assert( std::is_convertible<iso_week::year_lastweek_weekday,
80 iso_week::year_weeknum_weekday>{}, "");
81 static_assert( std::is_nothrow_constructible<iso_week::year_weeknum_weekday,
82 iso_week::sys_days>{}, "");
83 static_assert( std::is_convertible<iso_week::sys_days,
84 iso_week::year_weeknum_weekday>{}, "");
85 static_assert( std::is_nothrow_constructible<iso_week::sys_days,
86 iso_week::year_weeknum_weekday>{}, "");
87 static_assert( std::is_convertible<iso_week::year_weeknum_weekday,
88 iso_week::sys_days>{}, "");
89
90 int
main()91 main()
92 {
93 using namespace iso_week;
94
95 constexpr auto x0 = year_weeknum_weekday{2015_y, 52_w, tue};
96 static_assert(x0.year() == 2015_y, "");
97 static_assert(x0.weeknum() == 52_w, "");
98 static_assert(x0.weekday() == tue, "");
99
100 constexpr auto x1 = year_weeknum_weekday{2015_y/last/tue};
101 static_assert(x1.year() == 2015_y, "");
102 static_assert(x1.weeknum() == 53_w, "");
103 static_assert(x1.weekday() == tue, "");
104
105 constexpr year_weeknum_weekday x2 = sys_days{days{16792}};
106 static_assert(x2.year() == 2015_y, "");
107 static_assert(x2.weeknum() == 52_w, "");
108 static_assert(x2.weekday() == wed, "");
109
110 auto x3 = x2;
111 x3 += years{2};
112 assert(x3.year() == 2017_y);
113 assert(x3.weeknum() == 52_w);
114 assert(x3.weekday() == wed);
115
116 x3 -= years{1};
117 assert(x3.year() == 2016_y);
118 assert(x3.weeknum() == 52_w);
119 assert(x3.weekday() == wed);
120
121 constexpr sys_days dp = 2015_y/52_w/wed;
122 static_assert(dp == sys_days{days{16792}}, "");
123
124 static_assert(x0.ok(), "");
125 static_assert(x1.ok(), "");
126 static_assert(x2.ok(), "");
127 assert(x3.ok());
128 static_assert(!(2016_y/53_w/sun).ok(), "");
129
130 static_assert(2015_y/52_w/mon < 2015_y/52_w/sun, "");
131
132 static_assert(x0 + years{3} == 2018_y/52_w/tue, "");
133 static_assert(years{3} + x0 == 2018_y/52_w/tue, "");
134 static_assert(x0 - years{3} == 2012_y/52_w/tue, "");
135
136 std::ostringstream os;
137 os << x0;
138 assert(os.str() == "2015-W52-Tue");
139 }
140