1 // Copyright 2016 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef ABSL_TIME_INTERNAL_CCTZ_CIVIL_TIME_DETAIL_H_
16 #define ABSL_TIME_INTERNAL_CCTZ_CIVIL_TIME_DETAIL_H_
17
18 #include <cstdint>
19 #include <limits>
20 #include <ostream>
21 #include <type_traits>
22
23 #include "absl/base/config.h"
24
25 // Disable constexpr support unless we are in C++14 mode.
26 #if __cpp_constexpr >= 201304 || (defined(_MSC_VER) && _MSC_VER >= 1910)
27 #define CONSTEXPR_D constexpr // data
28 #define CONSTEXPR_F constexpr // function
29 #define CONSTEXPR_M constexpr // member
30 #else
31 #define CONSTEXPR_D const
32 #define CONSTEXPR_F inline
33 #define CONSTEXPR_M
34 #endif
35
36 namespace absl {
37 ABSL_NAMESPACE_BEGIN
38 namespace time_internal {
39 namespace cctz {
40
41 // Support years that at least span the range of 64-bit time_t values.
42 using year_t = std::int_fast64_t;
43
44 // Type alias that indicates an argument is not normalized (e.g., the
45 // constructor parameters and operands/results of addition/subtraction).
46 using diff_t = std::int_fast64_t;
47
48 namespace detail {
49
50 // Type aliases that indicate normalized argument values.
51 using month_t = std::int_fast8_t; // [1:12]
52 using day_t = std::int_fast8_t; // [1:31]
53 using hour_t = std::int_fast8_t; // [0:23]
54 using minute_t = std::int_fast8_t; // [0:59]
55 using second_t = std::int_fast8_t; // [0:59]
56
57 // Normalized civil-time fields: Y-M-D HH:MM:SS.
58 struct fields {
fieldsfields59 CONSTEXPR_M fields(year_t year, month_t month, day_t day, hour_t hour,
60 minute_t minute, second_t second)
61 : y(year), m(month), d(day), hh(hour), mm(minute), ss(second) {}
62 std::int_least64_t y;
63 std::int_least8_t m;
64 std::int_least8_t d;
65 std::int_least8_t hh;
66 std::int_least8_t mm;
67 std::int_least8_t ss;
68 };
69
70 struct second_tag {};
71 struct minute_tag : second_tag {};
72 struct hour_tag : minute_tag {};
73 struct day_tag : hour_tag {};
74 struct month_tag : day_tag {};
75 struct year_tag : month_tag {};
76
77 ////////////////////////////////////////////////////////////////////////
78
79 // Field normalization (without avoidable overflow).
80
81 namespace impl {
82
is_leap_year(year_t y)83 CONSTEXPR_F bool is_leap_year(year_t y) noexcept {
84 return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
85 }
year_index(year_t y,month_t m)86 CONSTEXPR_F int year_index(year_t y, month_t m) noexcept {
87 return (static_cast<int>((y + (m > 2)) % 400) + 400) % 400;
88 }
days_per_century(year_t y,month_t m)89 CONSTEXPR_F int days_per_century(year_t y, month_t m) noexcept {
90 const int yi = year_index(y, m);
91 return 36524 + (yi == 0 || yi > 300);
92 }
days_per_4years(year_t y,month_t m)93 CONSTEXPR_F int days_per_4years(year_t y, month_t m) noexcept {
94 const int yi = year_index(y, m);
95 return 1460 + (yi == 0 || yi > 300 || (yi - 1) % 100 < 96);
96 }
days_per_year(year_t y,month_t m)97 CONSTEXPR_F int days_per_year(year_t y, month_t m) noexcept {
98 return is_leap_year(y + (m > 2)) ? 366 : 365;
99 }
days_per_month(year_t y,month_t m)100 CONSTEXPR_F int days_per_month(year_t y, month_t m) noexcept {
101 CONSTEXPR_D int k_days_per_month[1 + 12] = {
102 -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 // non leap year
103 };
104 return k_days_per_month[m] + (m == 2 && is_leap_year(y));
105 }
106
n_day(year_t y,month_t m,diff_t d,diff_t cd,hour_t hh,minute_t mm,second_t ss)107 CONSTEXPR_F fields n_day(year_t y, month_t m, diff_t d, diff_t cd, hour_t hh,
108 minute_t mm, second_t ss) noexcept {
109 y += (cd / 146097) * 400;
110 cd %= 146097;
111 if (cd < 0) {
112 y -= 400;
113 cd += 146097;
114 }
115 y += (d / 146097) * 400;
116 d = d % 146097 + cd;
117 if (d > 0) {
118 if (d > 146097) {
119 y += 400;
120 d -= 146097;
121 }
122 } else {
123 if (d > -365) {
124 // We often hit the previous year when stepping a civil time backwards,
125 // so special case it to avoid counting up by 100/4/1-year chunks.
126 y -= 1;
127 d += days_per_year(y, m);
128 } else {
129 y -= 400;
130 d += 146097;
131 }
132 }
133 if (d > 365) {
134 for (int n = days_per_century(y, m); d > n; n = days_per_century(y, m)) {
135 d -= n;
136 y += 100;
137 }
138 for (int n = days_per_4years(y, m); d > n; n = days_per_4years(y, m)) {
139 d -= n;
140 y += 4;
141 }
142 for (int n = days_per_year(y, m); d > n; n = days_per_year(y, m)) {
143 d -= n;
144 ++y;
145 }
146 }
147 if (d > 28) {
148 for (int n = days_per_month(y, m); d > n; n = days_per_month(y, m)) {
149 d -= n;
150 if (++m > 12) {
151 ++y;
152 m = 1;
153 }
154 }
155 }
156 return fields(y, m, static_cast<day_t>(d), hh, mm, ss);
157 }
n_mon(year_t y,diff_t m,diff_t d,diff_t cd,hour_t hh,minute_t mm,second_t ss)158 CONSTEXPR_F fields n_mon(year_t y, diff_t m, diff_t d, diff_t cd, hour_t hh,
159 minute_t mm, second_t ss) noexcept {
160 if (m != 12) {
161 y += m / 12;
162 m %= 12;
163 if (m <= 0) {
164 y -= 1;
165 m += 12;
166 }
167 }
168 return n_day(y, static_cast<month_t>(m), d, cd, hh, mm, ss);
169 }
n_hour(year_t y,diff_t m,diff_t d,diff_t cd,diff_t hh,minute_t mm,second_t ss)170 CONSTEXPR_F fields n_hour(year_t y, diff_t m, diff_t d, diff_t cd, diff_t hh,
171 minute_t mm, second_t ss) noexcept {
172 cd += hh / 24;
173 hh %= 24;
174 if (hh < 0) {
175 cd -= 1;
176 hh += 24;
177 }
178 return n_mon(y, m, d, cd, static_cast<hour_t>(hh), mm, ss);
179 }
n_min(year_t y,diff_t m,diff_t d,diff_t hh,diff_t ch,diff_t mm,second_t ss)180 CONSTEXPR_F fields n_min(year_t y, diff_t m, diff_t d, diff_t hh, diff_t ch,
181 diff_t mm, second_t ss) noexcept {
182 ch += mm / 60;
183 mm %= 60;
184 if (mm < 0) {
185 ch -= 1;
186 mm += 60;
187 }
188 return n_hour(y, m, d, hh / 24 + ch / 24, hh % 24 + ch % 24,
189 static_cast<minute_t>(mm), ss);
190 }
n_sec(year_t y,diff_t m,diff_t d,diff_t hh,diff_t mm,diff_t ss)191 CONSTEXPR_F fields n_sec(year_t y, diff_t m, diff_t d, diff_t hh, diff_t mm,
192 diff_t ss) noexcept {
193 // Optimization for when (non-constexpr) fields are already normalized.
194 if (0 <= ss && ss < 60) {
195 const second_t nss = static_cast<second_t>(ss);
196 if (0 <= mm && mm < 60) {
197 const minute_t nmm = static_cast<minute_t>(mm);
198 if (0 <= hh && hh < 24) {
199 const hour_t nhh = static_cast<hour_t>(hh);
200 if (1 <= d && d <= 28 && 1 <= m && m <= 12) {
201 const day_t nd = static_cast<day_t>(d);
202 const month_t nm = static_cast<month_t>(m);
203 return fields(y, nm, nd, nhh, nmm, nss);
204 }
205 return n_mon(y, m, d, 0, nhh, nmm, nss);
206 }
207 return n_hour(y, m, d, hh / 24, hh % 24, nmm, nss);
208 }
209 return n_min(y, m, d, hh, mm / 60, mm % 60, nss);
210 }
211 diff_t cm = ss / 60;
212 ss %= 60;
213 if (ss < 0) {
214 cm -= 1;
215 ss += 60;
216 }
217 return n_min(y, m, d, hh, mm / 60 + cm / 60, mm % 60 + cm % 60,
218 static_cast<second_t>(ss));
219 }
220
221 } // namespace impl
222
223 ////////////////////////////////////////////////////////////////////////
224
225 // Increments the indicated (normalized) field by "n".
step(second_tag,fields f,diff_t n)226 CONSTEXPR_F fields step(second_tag, fields f, diff_t n) noexcept {
227 return impl::n_sec(f.y, f.m, f.d, f.hh, f.mm + n / 60, f.ss + n % 60);
228 }
step(minute_tag,fields f,diff_t n)229 CONSTEXPR_F fields step(minute_tag, fields f, diff_t n) noexcept {
230 return impl::n_min(f.y, f.m, f.d, f.hh + n / 60, 0, f.mm + n % 60, f.ss);
231 }
step(hour_tag,fields f,diff_t n)232 CONSTEXPR_F fields step(hour_tag, fields f, diff_t n) noexcept {
233 return impl::n_hour(f.y, f.m, f.d + n / 24, 0, f.hh + n % 24, f.mm, f.ss);
234 }
step(day_tag,fields f,diff_t n)235 CONSTEXPR_F fields step(day_tag, fields f, diff_t n) noexcept {
236 return impl::n_day(f.y, f.m, f.d, n, f.hh, f.mm, f.ss);
237 }
step(month_tag,fields f,diff_t n)238 CONSTEXPR_F fields step(month_tag, fields f, diff_t n) noexcept {
239 return impl::n_mon(f.y + n / 12, f.m + n % 12, f.d, 0, f.hh, f.mm, f.ss);
240 }
step(year_tag,fields f,diff_t n)241 CONSTEXPR_F fields step(year_tag, fields f, diff_t n) noexcept {
242 return fields(f.y + n, f.m, f.d, f.hh, f.mm, f.ss);
243 }
244
245 ////////////////////////////////////////////////////////////////////////
246
247 namespace impl {
248
249 // Returns (v * f + a) but avoiding intermediate overflow when possible.
scale_add(diff_t v,diff_t f,diff_t a)250 CONSTEXPR_F diff_t scale_add(diff_t v, diff_t f, diff_t a) noexcept {
251 return (v < 0) ? ((v + 1) * f + a) - f : ((v - 1) * f + a) + f;
252 }
253
254 // Map a (normalized) Y/M/D to the number of days before/after 1970-01-01.
255 // Probably overflows for years outside [-292277022656:292277026595].
ymd_ord(year_t y,month_t m,day_t d)256 CONSTEXPR_F diff_t ymd_ord(year_t y, month_t m, day_t d) noexcept {
257 const diff_t eyear = (m <= 2) ? y - 1 : y;
258 const diff_t era = (eyear >= 0 ? eyear : eyear - 399) / 400;
259 const diff_t yoe = eyear - era * 400;
260 const diff_t doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1;
261 const diff_t doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
262 return era * 146097 + doe - 719468;
263 }
264
265 // Returns the difference in days between two normalized Y-M-D tuples.
266 // ymd_ord() will encounter integer overflow given extreme year values,
267 // yet the difference between two such extreme values may actually be
268 // small, so we take a little care to avoid overflow when possible by
269 // exploiting the 146097-day cycle.
day_difference(year_t y1,month_t m1,day_t d1,year_t y2,month_t m2,day_t d2)270 CONSTEXPR_F diff_t day_difference(year_t y1, month_t m1, day_t d1, year_t y2,
271 month_t m2, day_t d2) noexcept {
272 const diff_t a_c4_off = y1 % 400;
273 const diff_t b_c4_off = y2 % 400;
274 diff_t c4_diff = (y1 - a_c4_off) - (y2 - b_c4_off);
275 diff_t delta = ymd_ord(a_c4_off, m1, d1) - ymd_ord(b_c4_off, m2, d2);
276 if (c4_diff > 0 && delta < 0) {
277 delta += 2 * 146097;
278 c4_diff -= 2 * 400;
279 } else if (c4_diff < 0 && delta > 0) {
280 delta -= 2 * 146097;
281 c4_diff += 2 * 400;
282 }
283 return (c4_diff / 400 * 146097) + delta;
284 }
285
286 } // namespace impl
287
288 // Returns the difference between fields structs using the indicated unit.
difference(year_tag,fields f1,fields f2)289 CONSTEXPR_F diff_t difference(year_tag, fields f1, fields f2) noexcept {
290 return f1.y - f2.y;
291 }
difference(month_tag,fields f1,fields f2)292 CONSTEXPR_F diff_t difference(month_tag, fields f1, fields f2) noexcept {
293 return impl::scale_add(difference(year_tag{}, f1, f2), 12, (f1.m - f2.m));
294 }
difference(day_tag,fields f1,fields f2)295 CONSTEXPR_F diff_t difference(day_tag, fields f1, fields f2) noexcept {
296 return impl::day_difference(f1.y, f1.m, f1.d, f2.y, f2.m, f2.d);
297 }
difference(hour_tag,fields f1,fields f2)298 CONSTEXPR_F diff_t difference(hour_tag, fields f1, fields f2) noexcept {
299 return impl::scale_add(difference(day_tag{}, f1, f2), 24, (f1.hh - f2.hh));
300 }
difference(minute_tag,fields f1,fields f2)301 CONSTEXPR_F diff_t difference(minute_tag, fields f1, fields f2) noexcept {
302 return impl::scale_add(difference(hour_tag{}, f1, f2), 60, (f1.mm - f2.mm));
303 }
difference(second_tag,fields f1,fields f2)304 CONSTEXPR_F diff_t difference(second_tag, fields f1, fields f2) noexcept {
305 return impl::scale_add(difference(minute_tag{}, f1, f2), 60, f1.ss - f2.ss);
306 }
307
308 ////////////////////////////////////////////////////////////////////////
309
310 // Aligns the (normalized) fields struct to the indicated field.
align(second_tag,fields f)311 CONSTEXPR_F fields align(second_tag, fields f) noexcept { return f; }
align(minute_tag,fields f)312 CONSTEXPR_F fields align(minute_tag, fields f) noexcept {
313 return fields{f.y, f.m, f.d, f.hh, f.mm, 0};
314 }
align(hour_tag,fields f)315 CONSTEXPR_F fields align(hour_tag, fields f) noexcept {
316 return fields{f.y, f.m, f.d, f.hh, 0, 0};
317 }
align(day_tag,fields f)318 CONSTEXPR_F fields align(day_tag, fields f) noexcept {
319 return fields{f.y, f.m, f.d, 0, 0, 0};
320 }
align(month_tag,fields f)321 CONSTEXPR_F fields align(month_tag, fields f) noexcept {
322 return fields{f.y, f.m, 1, 0, 0, 0};
323 }
align(year_tag,fields f)324 CONSTEXPR_F fields align(year_tag, fields f) noexcept {
325 return fields{f.y, 1, 1, 0, 0, 0};
326 }
327
328 ////////////////////////////////////////////////////////////////////////
329
330 namespace impl {
331
332 template <typename H>
AbslHashValueImpl(second_tag,H h,fields f)333 H AbslHashValueImpl(second_tag, H h, fields f) {
334 return H::combine(std::move(h), f.y, f.m, f.d, f.hh, f.mm, f.ss);
335 }
336 template <typename H>
AbslHashValueImpl(minute_tag,H h,fields f)337 H AbslHashValueImpl(minute_tag, H h, fields f) {
338 return H::combine(std::move(h), f.y, f.m, f.d, f.hh, f.mm);
339 }
340 template <typename H>
AbslHashValueImpl(hour_tag,H h,fields f)341 H AbslHashValueImpl(hour_tag, H h, fields f) {
342 return H::combine(std::move(h), f.y, f.m, f.d, f.hh);
343 }
344 template <typename H>
AbslHashValueImpl(day_tag,H h,fields f)345 H AbslHashValueImpl(day_tag, H h, fields f) {
346 return H::combine(std::move(h), f.y, f.m, f.d);
347 }
348 template <typename H>
AbslHashValueImpl(month_tag,H h,fields f)349 H AbslHashValueImpl(month_tag, H h, fields f) {
350 return H::combine(std::move(h), f.y, f.m);
351 }
352 template <typename H>
AbslHashValueImpl(year_tag,H h,fields f)353 H AbslHashValueImpl(year_tag, H h, fields f) {
354 return H::combine(std::move(h), f.y);
355 }
356
357 } // namespace impl
358
359 ////////////////////////////////////////////////////////////////////////
360
361 template <typename T>
362 class civil_time {
363 public:
364 explicit CONSTEXPR_M civil_time(year_t y, diff_t m = 1, diff_t d = 1,
365 diff_t hh = 0, diff_t mm = 0,
366 diff_t ss = 0) noexcept
civil_time(impl::n_sec (y,m,d,hh,mm,ss))367 : civil_time(impl::n_sec(y, m, d, hh, mm, ss)) {}
368
civil_time()369 CONSTEXPR_M civil_time() noexcept : f_{1970, 1, 1, 0, 0, 0} {}
370 civil_time(const civil_time&) = default;
371 civil_time& operator=(const civil_time&) = default;
372
373 // Conversion between civil times of different alignment. Conversion to
374 // a more precise alignment is allowed implicitly (e.g., day -> hour),
375 // but conversion where information is discarded must be explicit
376 // (e.g., second -> minute).
377 template <typename U, typename S>
378 using preserves_data =
379 typename std::enable_if<std::is_base_of<U, S>::value>::type;
380 template <typename U>
381 CONSTEXPR_M civil_time(const civil_time<U>& ct,
382 preserves_data<T, U>* = nullptr) noexcept
383 : civil_time(ct.f_) {}
384 template <typename U>
385 explicit CONSTEXPR_M civil_time(const civil_time<U>& ct,
386 preserves_data<U, T>* = nullptr) noexcept
387 : civil_time(ct.f_) {}
388
389 // Factories for the maximum/minimum representable civil_time.
civil_time(max)390 static CONSTEXPR_F civil_time(max)() {
391 const auto max_year = (std::numeric_limits<std::int_least64_t>::max)();
392 return civil_time(max_year, 12, 31, 23, 59, 59);
393 }
civil_time(min)394 static CONSTEXPR_F civil_time(min)() {
395 const auto min_year = (std::numeric_limits<std::int_least64_t>::min)();
396 return civil_time(min_year, 1, 1, 0, 0, 0);
397 }
398
399 // Field accessors. Note: All but year() return an int.
year()400 CONSTEXPR_M year_t year() const noexcept { return f_.y; }
month()401 CONSTEXPR_M int month() const noexcept { return f_.m; }
day()402 CONSTEXPR_M int day() const noexcept { return f_.d; }
hour()403 CONSTEXPR_M int hour() const noexcept { return f_.hh; }
minute()404 CONSTEXPR_M int minute() const noexcept { return f_.mm; }
second()405 CONSTEXPR_M int second() const noexcept { return f_.ss; }
406
407 // Assigning arithmetic.
408 CONSTEXPR_M civil_time& operator+=(diff_t n) noexcept {
409 f_ = step(T{}, f_, n);
410 return *this;
411 }
412 CONSTEXPR_M civil_time& operator-=(diff_t n) noexcept {
413 if (n != (std::numeric_limits<diff_t>::min)()) {
414 f_ = step(T{}, f_, -n);
415 } else {
416 f_ = step(T{}, step(T{}, f_, -(n + 1)), 1);
417 }
418 return *this;
419 }
420 CONSTEXPR_M civil_time& operator++() noexcept { return *this += 1; }
421 CONSTEXPR_M civil_time operator++(int) noexcept {
422 const civil_time a = *this;
423 ++*this;
424 return a;
425 }
426 CONSTEXPR_M civil_time& operator--() noexcept { return *this -= 1; }
427 CONSTEXPR_M civil_time operator--(int) noexcept {
428 const civil_time a = *this;
429 --*this;
430 return a;
431 }
432
433 // Binary arithmetic operators.
434 friend CONSTEXPR_F civil_time operator+(civil_time a, diff_t n) noexcept {
435 return a += n;
436 }
437 friend CONSTEXPR_F civil_time operator+(diff_t n, civil_time a) noexcept {
438 return a += n;
439 }
440 friend CONSTEXPR_F civil_time operator-(civil_time a, diff_t n) noexcept {
441 return a -= n;
442 }
443 friend CONSTEXPR_F diff_t operator-(civil_time lhs, civil_time rhs) noexcept {
444 return difference(T{}, lhs.f_, rhs.f_);
445 }
446
447 template <typename H>
AbslHashValue(H h,civil_time a)448 friend H AbslHashValue(H h, civil_time a) {
449 return impl::AbslHashValueImpl(T{}, std::move(h), a.f_);
450 }
451
452 private:
453 // All instantiations of this template are allowed to call the following
454 // private constructor and access the private fields member.
455 template <typename U>
456 friend class civil_time;
457
458 // The designated constructor that all others eventually call.
civil_time(fields f)459 explicit CONSTEXPR_M civil_time(fields f) noexcept : f_(align(T{}, f)) {}
460
461 fields f_;
462 };
463
464 // Disallows difference between differently aligned types.
465 // auto n = civil_day(...) - civil_hour(...); // would be confusing.
466 template <typename T, typename U>
467 CONSTEXPR_F diff_t operator-(civil_time<T>, civil_time<U>) = delete;
468
469 using civil_year = civil_time<year_tag>;
470 using civil_month = civil_time<month_tag>;
471 using civil_day = civil_time<day_tag>;
472 using civil_hour = civil_time<hour_tag>;
473 using civil_minute = civil_time<minute_tag>;
474 using civil_second = civil_time<second_tag>;
475
476 ////////////////////////////////////////////////////////////////////////
477
478 // Relational operators that work with differently aligned objects.
479 // Always compares all six fields.
480 template <typename T1, typename T2>
481 CONSTEXPR_F bool operator<(const civil_time<T1>& lhs,
482 const civil_time<T2>& rhs) noexcept {
483 return (
484 lhs.year() < rhs.year() ||
485 (lhs.year() == rhs.year() &&
486 (lhs.month() < rhs.month() ||
487 (lhs.month() == rhs.month() &&
488 (lhs.day() < rhs.day() || (lhs.day() == rhs.day() &&
489 (lhs.hour() < rhs.hour() ||
490 (lhs.hour() == rhs.hour() &&
491 (lhs.minute() < rhs.minute() ||
492 (lhs.minute() == rhs.minute() &&
493 (lhs.second() < rhs.second())))))))))));
494 }
495 template <typename T1, typename T2>
496 CONSTEXPR_F bool operator<=(const civil_time<T1>& lhs,
497 const civil_time<T2>& rhs) noexcept {
498 return !(rhs < lhs);
499 }
500 template <typename T1, typename T2>
501 CONSTEXPR_F bool operator>=(const civil_time<T1>& lhs,
502 const civil_time<T2>& rhs) noexcept {
503 return !(lhs < rhs);
504 }
505 template <typename T1, typename T2>
506 CONSTEXPR_F bool operator>(const civil_time<T1>& lhs,
507 const civil_time<T2>& rhs) noexcept {
508 return rhs < lhs;
509 }
510 template <typename T1, typename T2>
511 CONSTEXPR_F bool operator==(const civil_time<T1>& lhs,
512 const civil_time<T2>& rhs) noexcept {
513 return lhs.year() == rhs.year() && lhs.month() == rhs.month() &&
514 lhs.day() == rhs.day() && lhs.hour() == rhs.hour() &&
515 lhs.minute() == rhs.minute() && lhs.second() == rhs.second();
516 }
517 template <typename T1, typename T2>
518 CONSTEXPR_F bool operator!=(const civil_time<T1>& lhs,
519 const civil_time<T2>& rhs) noexcept {
520 return !(lhs == rhs);
521 }
522
523 ////////////////////////////////////////////////////////////////////////
524
525 enum class weekday {
526 monday,
527 tuesday,
528 wednesday,
529 thursday,
530 friday,
531 saturday,
532 sunday,
533 };
534
get_weekday(const civil_second & cs)535 CONSTEXPR_F weekday get_weekday(const civil_second& cs) noexcept {
536 CONSTEXPR_D weekday k_weekday_by_mon_off[13] = {
537 weekday::monday, weekday::tuesday, weekday::wednesday,
538 weekday::thursday, weekday::friday, weekday::saturday,
539 weekday::sunday, weekday::monday, weekday::tuesday,
540 weekday::wednesday, weekday::thursday, weekday::friday,
541 weekday::saturday,
542 };
543 CONSTEXPR_D int k_weekday_offsets[1 + 12] = {
544 -1, 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4,
545 };
546 year_t wd = 2400 + (cs.year() % 400) - (cs.month() < 3);
547 wd += wd / 4 - wd / 100 + wd / 400;
548 wd += k_weekday_offsets[cs.month()] + cs.day();
549 return k_weekday_by_mon_off[wd % 7 + 6];
550 }
551
552 ////////////////////////////////////////////////////////////////////////
553
next_weekday(civil_day cd,weekday wd)554 CONSTEXPR_F civil_day next_weekday(civil_day cd, weekday wd) noexcept {
555 CONSTEXPR_D weekday k_weekdays_forw[14] = {
556 weekday::monday, weekday::tuesday, weekday::wednesday,
557 weekday::thursday, weekday::friday, weekday::saturday,
558 weekday::sunday, weekday::monday, weekday::tuesday,
559 weekday::wednesday, weekday::thursday, weekday::friday,
560 weekday::saturday, weekday::sunday,
561 };
562 weekday base = get_weekday(cd);
563 for (int i = 0;; ++i) {
564 if (base == k_weekdays_forw[i]) {
565 for (int j = i + 1;; ++j) {
566 if (wd == k_weekdays_forw[j]) {
567 return cd + (j - i);
568 }
569 }
570 }
571 }
572 }
573
prev_weekday(civil_day cd,weekday wd)574 CONSTEXPR_F civil_day prev_weekday(civil_day cd, weekday wd) noexcept {
575 CONSTEXPR_D weekday k_weekdays_back[14] = {
576 weekday::sunday, weekday::saturday, weekday::friday,
577 weekday::thursday, weekday::wednesday, weekday::tuesday,
578 weekday::monday, weekday::sunday, weekday::saturday,
579 weekday::friday, weekday::thursday, weekday::wednesday,
580 weekday::tuesday, weekday::monday,
581 };
582 weekday base = get_weekday(cd);
583 for (int i = 0;; ++i) {
584 if (base == k_weekdays_back[i]) {
585 for (int j = i + 1;; ++j) {
586 if (wd == k_weekdays_back[j]) {
587 return cd - (j - i);
588 }
589 }
590 }
591 }
592 }
593
get_yearday(const civil_second & cs)594 CONSTEXPR_F int get_yearday(const civil_second& cs) noexcept {
595 CONSTEXPR_D int k_month_offsets[1 + 12] = {
596 -1, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334,
597 };
598 const int feb29 = (cs.month() > 2 && impl::is_leap_year(cs.year()));
599 return k_month_offsets[cs.month()] + feb29 + cs.day();
600 }
601
602 ////////////////////////////////////////////////////////////////////////
603
604 std::ostream& operator<<(std::ostream& os, const civil_year& y);
605 std::ostream& operator<<(std::ostream& os, const civil_month& m);
606 std::ostream& operator<<(std::ostream& os, const civil_day& d);
607 std::ostream& operator<<(std::ostream& os, const civil_hour& h);
608 std::ostream& operator<<(std::ostream& os, const civil_minute& m);
609 std::ostream& operator<<(std::ostream& os, const civil_second& s);
610 std::ostream& operator<<(std::ostream& os, weekday wd);
611
612 } // namespace detail
613 } // namespace cctz
614 } // namespace time_internal
615 ABSL_NAMESPACE_END
616 } // namespace absl
617
618 #undef CONSTEXPR_M
619 #undef CONSTEXPR_F
620 #undef CONSTEXPR_D
621
622 #endif // ABSL_TIME_INTERNAL_CCTZ_CIVIL_TIME_DETAIL_H_
623