| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#ifndef CCTZ_CIVIL_TIME_DETAIL_H_ |
| 16 |
#define CCTZ_CIVIL_TIME_DETAIL_H_ |
| 17 |
|
| 18 |
#include <cstdint> |
| 19 |
#include <limits> |
| 20 |
#include <ostream> |
| 21 |
#include <type_traits> |
| 22 |
|
| 23 |
|
| 24 |
#if __cpp_constexpr >= 201304 || (defined(_MSC_VER) && _MSC_VER >= 1910) |
| 25 |
#define CONSTEXPR_D constexpr // data |
| 26 |
#define CONSTEXPR_F constexpr // function |
| 27 |
#define CONSTEXPR_M constexpr // member |
| 28 |
#else |
| 29 |
#define CONSTEXPR_D const |
| 30 |
#define CONSTEXPR_F inline |
| 31 |
#define CONSTEXPR_M |
| 32 |
#endif |
| 33 |
|
| 34 |
namespace cctz { |
| 35 |
|
| 36 |
|
| 37 |
using year_t = std::int_fast64_t; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
using diff_t = std::int_fast64_t; |
| 42 |
|
| 43 |
namespace detail { |
| 44 |
|
| 45 |
|
| 46 |
using month_t = std::int_fast8_t; |
| 47 |
using day_t = std::int_fast8_t; |
| 48 |
using hour_t = std::int_fast8_t; |
| 49 |
using minute_t = std::int_fast8_t; |
| 50 |
using second_t = std::int_fast8_t; |
| 51 |
|
| 52 |
|
| 53 |
struct fields { |
| 54 |
CONSTEXPR_M fields(year_t year, month_t month, day_t day, |
| 55 |
hour_t hour, minute_t minute, second_t second) |
| 56 |
: y(year), m(month), d(day), hh(hour), mm(minute), ss(second) {} |
| 57 |
std::int_least64_t y; |
| 58 |
std::int_least8_t m; |
| 59 |
std::int_least8_t d; |
| 60 |
std::int_least8_t hh; |
| 61 |
std::int_least8_t mm; |
| 62 |
std::int_least8_t ss; |
| 63 |
}; |
| 64 |
|
| 65 |
struct second_tag {}; |
| 66 |
struct minute_tag : second_tag {}; |
| 67 |
struct hour_tag : minute_tag {}; |
| 68 |
struct day_tag : hour_tag {}; |
| 69 |
struct month_tag : day_tag {}; |
| 70 |
struct year_tag : month_tag {}; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
namespace impl { |
| 77 |
|
| 78 |
CONSTEXPR_F bool is_leap_year(year_t y) noexcept { |
| 79 |
return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0); |
| 80 |
} |
| 81 |
CONSTEXPR_F int year_index(year_t y, month_t m) noexcept { |
| 82 |
const int yi = static_cast<int>((y + (m > 2)) % 400); |
| 83 |
return yi < 0 ? yi + 400 : yi; |
| 84 |
} |
| 85 |
CONSTEXPR_F int days_per_century(int yi) noexcept { |
| 86 |
return 36524 + (yi == 0 || yi > 300); |
| 87 |
} |
| 88 |
CONSTEXPR_F int days_per_4years(int yi) noexcept { |
| 89 |
return 1460 + (yi == 0 || yi > 300 || (yi - 1) % 100 < 96); |
| 90 |
} |
| 91 |
CONSTEXPR_F int days_per_year(year_t y, month_t m) noexcept { |
| 92 |
return is_leap_year(y + (m > 2)) ? 366 : 365; |
| 93 |
} |
| 94 |
CONSTEXPR_F int days_per_month(year_t y, month_t m) noexcept { |
| 95 |
CONSTEXPR_D int k_days_per_month[1 + 12] = { |
| 96 |
-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
| 97 |
}; |
| 98 |
return k_days_per_month[m] + (m == 2 && is_leap_year(y)); |
| 99 |
} |
| 100 |
|
| 101 |
CONSTEXPR_F fields n_day(year_t y, month_t m, diff_t d, diff_t cd, |
| 102 |
hour_t hh, minute_t mm, second_t ss) noexcept { |
| 103 |
year_t ey = y % 400; |
| 104 |
const year_t oey = ey; |
| 105 |
ey += (cd / 146097) * 400; |
| 106 |
cd %= 146097; |
| 107 |
if (cd < 0) { |
| 108 |
ey -= 400; |
| 109 |
cd += 146097; |
| 110 |
} |
| 111 |
ey += (d / 146097) * 400; |
| 112 |
d = d % 146097 + cd; |
| 113 |
if (d > 0) { |
| 114 |
if (d > 146097) { |
| 115 |
ey += 400; |
| 116 |
d -= 146097; |
| 117 |
} |
| 118 |
} else { |
| 119 |
if (d > -365) { |
| 120 |
|
| 121 |
|
| 122 |
ey -= 1; |
| 123 |
d += days_per_year(ey, m); |
| 124 |
} else { |
| 125 |
ey -= 400; |
| 126 |
d += 146097; |
| 127 |
} |
| 128 |
} |
| 129 |
if (d > 365) { |
| 130 |
int yi = year_index(ey, m); |
| 131 |
for (;;) { |
| 132 |
int n = days_per_century(yi); |
| 133 |
if (d <= n) break; |
| 134 |
d -= n; |
| 135 |
ey += 100; |
| 136 |
yi += 100; |
| 137 |
if (yi >= 400) yi -= 400; |
| 138 |
} |
| 139 |
for (;;) { |
| 140 |
int n = days_per_4years(yi); |
| 141 |
if (d <= n) break; |
| 142 |
d -= n; |
| 143 |
ey += 4; |
| 144 |
yi += 4; |
| 145 |
if (yi >= 400) yi -= 400; |
| 146 |
} |
| 147 |
for (;;) { |
| 148 |
int n = days_per_year(ey, m); |
| 149 |
if (d <= n) break; |
| 150 |
d -= n; |
| 151 |
++ey; |
| 152 |
} |
| 153 |
} |
| 154 |
if (d > 28) { |
| 155 |
for (;;) { |
| 156 |
int n = days_per_month(ey, m); |
| 157 |
if (d <= n) break; |
| 158 |
d -= n; |
| 159 |
if (++m > 12) { |
| 160 |
++ey; |
| 161 |
m = 1; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
return fields(y + (ey - oey), m, static_cast<day_t>(d), hh, mm, ss); |
| 166 |
} |
| 167 |
CONSTEXPR_F fields n_mon(year_t y, diff_t m, diff_t d, diff_t cd, |
| 168 |
hour_t hh, minute_t mm, second_t ss) noexcept { |
| 169 |
if (m != 12) { |
| 170 |
y += m / 12; |
| 171 |
m %= 12; |
| 172 |
if (m <= 0) { |
| 173 |
y -= 1; |
| 174 |
m += 12; |
| 175 |
} |
| 176 |
} |
| 177 |
return n_day(y, static_cast<month_t>(m), d, cd, hh, mm, ss); |
| 178 |
} |
| 179 |
CONSTEXPR_F fields n_hour(year_t y, diff_t m, diff_t d, diff_t cd, |
| 180 |
diff_t hh, minute_t mm, second_t ss) noexcept { |
| 181 |
cd += hh / 24; |
| 182 |
hh %= 24; |
| 183 |
if (hh < 0) { |
| 184 |
cd -= 1; |
| 185 |
hh += 24; |
| 186 |
} |
| 187 |
return n_mon(y, m, d, cd, static_cast<hour_t>(hh), mm, ss); |
| 188 |
} |
| 189 |
CONSTEXPR_F fields n_min(year_t y, diff_t m, diff_t d, diff_t hh, diff_t ch, |
| 190 |
diff_t mm, second_t ss) noexcept { |
| 191 |
ch += mm / 60; |
| 192 |
mm %= 60; |
| 193 |
if (mm < 0) { |
| 194 |
ch -= 1; |
| 195 |
mm += 60; |
| 196 |
} |
| 197 |
return n_hour(y, m, d, hh / 24 + ch / 24, hh % 24 + ch % 24, |
| 198 |
static_cast<minute_t>(mm), ss); |
| 199 |
} |
| 200 |
CONSTEXPR_F fields n_sec(year_t y, diff_t m, diff_t d, diff_t hh, diff_t mm, |
| 201 |
diff_t ss) noexcept { |
| 202 |
|
| 203 |
if (0 <= ss && ss < 60) { |
| 204 |
const second_t nss = static_cast<second_t>(ss); |
| 205 |
if (0 <= mm && mm < 60) { |
| 206 |
const minute_t nmm = static_cast<minute_t>(mm); |
| 207 |
if (0 <= hh && hh < 24) { |
| 208 |
const hour_t nhh = static_cast<hour_t>(hh); |
| 209 |
if (1 <= d && d <= 28 && 1 <= m && m <= 12) { |
| 210 |
const day_t nd = static_cast<day_t>(d); |
| 211 |
const month_t nm = static_cast<month_t>(m); |
| 212 |
return fields(y, nm, nd, nhh, nmm, nss); |
| 213 |
} |
| 214 |
return n_mon(y, m, d, 0, nhh, nmm, nss); |
| 215 |
} |
| 216 |
return n_hour(y, m, d, hh / 24, hh % 24, nmm, nss); |
| 217 |
} |
| 218 |
return n_min(y, m, d, hh, mm / 60, mm % 60, nss); |
| 219 |
} |
| 220 |
diff_t cm = ss / 60; |
| 221 |
ss %= 60; |
| 222 |
if (ss < 0) { |
| 223 |
cm -= 1; |
| 224 |
ss += 60; |
| 225 |
} |
| 226 |
return n_min(y, m, d, hh, mm / 60 + cm / 60, mm % 60 + cm % 60, |
| 227 |
static_cast<second_t>(ss)); |
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
CONSTEXPR_F fields step(second_tag, fields f, diff_t n) noexcept { |
| 236 |
return impl::n_sec(f.y, f.m, f.d, f.hh, f.mm + n / 60, f.ss + n % 60); |
| 237 |
} |
| 238 |
CONSTEXPR_F fields step(minute_tag, fields f, diff_t n) noexcept { |
| 239 |
return impl::n_min(f.y, f.m, f.d, f.hh + n / 60, 0, f.mm + n % 60, f.ss); |
| 240 |
} |
| 241 |
CONSTEXPR_F fields step(hour_tag, fields f, diff_t n) noexcept { |
| 242 |
return impl::n_hour(f.y, f.m, f.d + n / 24, 0, f.hh + n % 24, f.mm, f.ss); |
| 243 |
} |
| 244 |
CONSTEXPR_F fields step(day_tag, fields f, diff_t n) noexcept { |
| 245 |
return impl::n_day(f.y, f.m, f.d, n, f.hh, f.mm, f.ss); |
| 246 |
} |
| 247 |
CONSTEXPR_F fields step(month_tag, fields f, diff_t n) noexcept { |
| 248 |
return impl::n_mon(f.y + n / 12, f.m + n % 12, f.d, 0, f.hh, f.mm, f.ss); |
| 249 |
} |
| 250 |
CONSTEXPR_F fields step(year_tag, fields f, diff_t n) noexcept { |
| 251 |
return fields(f.y + n, f.m, f.d, f.hh, f.mm, f.ss); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
namespace impl { |
| 257 |
|
| 258 |
|
| 259 |
CONSTEXPR_F diff_t scale_add(diff_t v, diff_t f, diff_t a) noexcept { |
| 260 |
return (v < 0) ? ((v + 1) * f + a) - f : ((v - 1) * f + a) + f; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
CONSTEXPR_F diff_t ymd_ord(year_t y, month_t m, day_t d) noexcept { |
| 266 |
const diff_t eyear = (m <= 2) ? y - 1 : y; |
| 267 |
const diff_t era = (eyear >= 0 ? eyear : eyear - 399) / 400; |
| 268 |
const diff_t yoe = eyear - era * 400; |
| 269 |
const diff_t doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1; |
| 270 |
const diff_t doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; |
| 271 |
return era * 146097 + doe - 719468; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
CONSTEXPR_F diff_t day_difference(year_t y1, month_t m1, day_t d1, |
| 280 |
year_t y2, month_t m2, day_t d2) noexcept { |
| 281 |
const diff_t a_c4_off = y1 % 400; |
| 282 |
const diff_t b_c4_off = y2 % 400; |
| 283 |
diff_t c4_diff = (y1 - a_c4_off) - (y2 - b_c4_off); |
| 284 |
diff_t delta = ymd_ord(a_c4_off, m1, d1) - ymd_ord(b_c4_off, m2, d2); |
| 285 |
if (c4_diff > 0 && delta < 0) { |
| 286 |
delta += 2 * 146097; |
| 287 |
c4_diff -= 2 * 400; |
| 288 |
} else if (c4_diff < 0 && delta > 0) { |
| 289 |
delta -= 2 * 146097; |
| 290 |
c4_diff += 2 * 400; |
| 291 |
} |
| 292 |
return (c4_diff / 400 * 146097) + delta; |
| 293 |
} |
| 294 |
|
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
CONSTEXPR_F diff_t difference(year_tag, fields f1, fields f2) noexcept { |
| 299 |
return f1.y - f2.y; |
| 300 |
} |
| 301 |
CONSTEXPR_F diff_t difference(month_tag, fields f1, fields f2) noexcept { |
| 302 |
return impl::scale_add(difference(year_tag{}, f1, f2), 12, (f1.m - f2.m)); |
| 303 |
} |
| 304 |
CONSTEXPR_F diff_t difference(day_tag, fields f1, fields f2) noexcept { |
| 305 |
return impl::day_difference(f1.y, f1.m, f1.d, f2.y, f2.m, f2.d); |
| 306 |
} |
| 307 |
CONSTEXPR_F diff_t difference(hour_tag, fields f1, fields f2) noexcept { |
| 308 |
return impl::scale_add(difference(day_tag{}, f1, f2), 24, (f1.hh - f2.hh)); |
| 309 |
} |
| 310 |
CONSTEXPR_F diff_t difference(minute_tag, fields f1, fields f2) noexcept { |
| 311 |
return impl::scale_add(difference(hour_tag{}, f1, f2), 60, (f1.mm - f2.mm)); |
| 312 |
} |
| 313 |
CONSTEXPR_F diff_t difference(second_tag, fields f1, fields f2) noexcept { |
| 314 |
return impl::scale_add(difference(minute_tag{}, f1, f2), 60, f1.ss - f2.ss); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
CONSTEXPR_F fields align(second_tag, fields f) noexcept { |
| 321 |
return f; |
| 322 |
} |
| 323 |
CONSTEXPR_F fields align(minute_tag, fields f) noexcept { |
| 324 |
return fields{f.y, f.m, f.d, f.hh, f.mm, 0}; |
| 325 |
} |
| 326 |
CONSTEXPR_F fields align(hour_tag, fields f) noexcept { |
| 327 |
return fields{f.y, f.m, f.d, f.hh, 0, 0}; |
| 328 |
} |
| 329 |
CONSTEXPR_F fields align(day_tag, fields f) noexcept { |
| 330 |
return fields{f.y, f.m, f.d, 0, 0, 0}; |
| 331 |
} |
| 332 |
CONSTEXPR_F fields align(month_tag, fields f) noexcept { |
| 333 |
return fields{f.y, f.m, 1, 0, 0, 0}; |
| 334 |
} |
| 335 |
CONSTEXPR_F fields align(year_tag, fields f) noexcept { |
| 336 |
return fields{f.y, 1, 1, 0, 0, 0}; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
template <typename T> |
| 342 |
class civil_time { |
| 343 |
public: |
| 344 |
explicit CONSTEXPR_M civil_time(year_t y, diff_t m = 1, diff_t d = 1, |
| 345 |
diff_t hh = 0, diff_t mm = 0, |
| 346 |
diff_t ss = 0) noexcept |
| 347 |
: civil_time(impl::n_sec(y, m, d, hh, mm, ss)) {} |
| 348 |
|
| 349 |
CONSTEXPR_M civil_time() noexcept : f_{1970, 1, 1, 0, 0, 0} {} |
| 350 |
civil_time(const civil_time&) = default; |
| 351 |
civil_time& operator=(const civil_time&) = default; |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
template <typename U, typename S> |
| 358 |
using preserves_data = |
| 359 |
typename std::enable_if<std::is_base_of<U, S>::value>::type; |
| 360 |
template <typename U> |
| 361 |
CONSTEXPR_M civil_time(const civil_time<U>& ct, |
| 362 |
preserves_data<T, U>* = nullptr) noexcept |
| 363 |
: civil_time(ct.f_) {} |
| 364 |
template <typename U> |
| 365 |
explicit CONSTEXPR_M civil_time(const civil_time<U>& ct, |
| 366 |
preserves_data<U, T>* = nullptr) noexcept |
| 367 |
: civil_time(ct.f_) {} |
| 368 |
|
| 369 |
|
| 370 |
static CONSTEXPR_F civil_time (max)() { |
| 371 |
const auto max_year = (std::numeric_limits<std::int_least64_t>::max)(); |
| 372 |
return civil_time(max_year, 12, 31, 23, 59, 59); |
| 373 |
} |
| 374 |
static CONSTEXPR_F civil_time (min)() { |
| 375 |
const auto min_year = (std::numeric_limits<std::int_least64_t>::min)(); |
| 376 |
return civil_time(min_year, 1, 1, 0, 0, 0); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
CONSTEXPR_M year_t year() const noexcept { return f_.y; } |
| 381 |
CONSTEXPR_M int month() const noexcept { return f_.m; } |
| 382 |
CONSTEXPR_M int day() const noexcept { return f_.d; } |
| 383 |
CONSTEXPR_M int hour() const noexcept { return f_.hh; } |
| 384 |
CONSTEXPR_M int minute() const noexcept { return f_.mm; } |
| 385 |
CONSTEXPR_M int second() const noexcept { return f_.ss; } |
| 386 |
|
| 387 |
|
| 388 |
CONSTEXPR_M civil_time& operator+=(diff_t n) noexcept { |
| 389 |
return *this = *this + n; |
| 390 |
} |
| 391 |
CONSTEXPR_M civil_time& operator-=(diff_t n) noexcept { |
| 392 |
return *this = *this - n; |
| 393 |
} |
| 394 |
CONSTEXPR_M civil_time& operator++() noexcept { |
| 395 |
return *this += 1; |
| 396 |
} |
| 397 |
CONSTEXPR_M civil_time operator++(int) noexcept { |
| 398 |
const civil_time a = *this; |
| 399 |
++*this; |
| 400 |
return a; |
| 401 |
} |
| 402 |
CONSTEXPR_M civil_time& operator--() noexcept { |
| 403 |
return *this -= 1; |
| 404 |
} |
| 405 |
CONSTEXPR_M civil_time operator--(int) noexcept { |
| 406 |
const civil_time a = *this; |
| 407 |
--*this; |
| 408 |
return a; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
friend CONSTEXPR_F civil_time operator+(civil_time a, diff_t n) noexcept { |
| 413 |
return civil_time(step(T{}, a.f_, n)); |
| 414 |
} |
| 415 |
friend CONSTEXPR_F civil_time operator+(diff_t n, civil_time a) noexcept { |
| 416 |
return a + n; |
| 417 |
} |
| 418 |
friend CONSTEXPR_F civil_time operator-(civil_time a, diff_t n) noexcept { |
| 419 |
return n != (std::numeric_limits<diff_t>::min)() |
| 420 |
? civil_time(step(T{}, a.f_, -n)) |
| 421 |
: civil_time(step(T{}, step(T{}, a.f_, -(n + 1)), 1)); |
| 422 |
} |
| 423 |
friend CONSTEXPR_F diff_t operator-(civil_time lhs, civil_time rhs) noexcept { |
| 424 |
return difference(T{}, lhs.f_, rhs.f_); |
| 425 |
} |
| 426 |
|
| 427 |
private: |
| 428 |
|
| 429 |
|
| 430 |
template <typename U> |
| 431 |
friend class civil_time; |
| 432 |
|
| 433 |
|
| 434 |
explicit CONSTEXPR_M civil_time(fields f) noexcept : f_(align(T{}, f)) {} |
| 435 |
|
| 436 |
fields f_; |
| 437 |
}; |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
template <typename T, typename U> |
| 442 |
CONSTEXPR_F diff_t operator-(civil_time<T>, civil_time<U>) = delete; |
| 443 |
|
| 444 |
using civil_year = civil_time<year_tag>; |
| 445 |
using civil_month = civil_time<month_tag>; |
| 446 |
using civil_day = civil_time<day_tag>; |
| 447 |
using civil_hour = civil_time<hour_tag>; |
| 448 |
using civil_minute = civil_time<minute_tag>; |
| 449 |
using civil_second = civil_time<second_tag>; |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
template <typename T1, typename T2> |
| 456 |
CONSTEXPR_F bool operator<(const civil_time<T1>& lhs, |
| 457 |
const civil_time<T2>& rhs) noexcept { |
| 458 |
return (lhs.year() < rhs.year() || |
| 459 |
(lhs.year() == rhs.year() && |
| 460 |
(lhs.month() < rhs.month() || |
| 461 |
(lhs.month() == rhs.month() && |
| 462 |
(lhs.day() < rhs.day() || |
| 463 |
(lhs.day() == rhs.day() && |
| 464 |
(lhs.hour() < rhs.hour() || |
| 465 |
(lhs.hour() == rhs.hour() && |
| 466 |
(lhs.minute() < rhs.minute() || |
| 467 |
(lhs.minute() == rhs.minute() && |
| 468 |
(lhs.second() < rhs.second()))))))))))); |
| 469 |
} |
| 470 |
template <typename T1, typename T2> |
| 471 |
CONSTEXPR_F bool operator<=(const civil_time<T1>& lhs, |
| 472 |
const civil_time<T2>& rhs) noexcept { |
| 473 |
return !(rhs < lhs); |
| 474 |
} |
| 475 |
template <typename T1, typename T2> |
| 476 |
CONSTEXPR_F bool operator>=(const civil_time<T1>& lhs, |
| 477 |
const civil_time<T2>& rhs) noexcept { |
| 478 |
return !(lhs < rhs); |
| 479 |
} |
| 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 rhs < lhs; |
| 484 |
} |
| 485 |
template <typename T1, typename T2> |
| 486 |
CONSTEXPR_F bool operator==(const civil_time<T1>& lhs, |
| 487 |
const civil_time<T2>& rhs) noexcept { |
| 488 |
return lhs.year() == rhs.year() && lhs.month() == rhs.month() && |
| 489 |
lhs.day() == rhs.day() && lhs.hour() == rhs.hour() && |
| 490 |
lhs.minute() == rhs.minute() && lhs.second() == rhs.second(); |
| 491 |
} |
| 492 |
template <typename T1, typename T2> |
| 493 |
CONSTEXPR_F bool operator!=(const civil_time<T1>& lhs, |
| 494 |
const civil_time<T2>& rhs) noexcept { |
| 495 |
return !(lhs == rhs); |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
enum class weekday { |
| 501 |
monday, |
| 502 |
tuesday, |
| 503 |
wednesday, |
| 504 |
thursday, |
| 505 |
friday, |
| 506 |
saturday, |
| 507 |
sunday, |
| 508 |
}; |
| 509 |
|
| 510 |
CONSTEXPR_F weekday get_weekday(const civil_second& cs) noexcept { |
| 511 |
CONSTEXPR_D weekday k_weekday_by_mon_off[13] = { |
| 512 |
weekday::monday, weekday::tuesday, weekday::wednesday, |
| 513 |
weekday::thursday, weekday::friday, weekday::saturday, |
| 514 |
weekday::sunday, weekday::monday, weekday::tuesday, |
| 515 |
weekday::wednesday, weekday::thursday, weekday::friday, |
| 516 |
weekday::saturday, |
| 517 |
}; |
| 518 |
CONSTEXPR_D int k_weekday_offsets[1 + 12] = { |
| 519 |
-1, 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4, |
| 520 |
}; |
| 521 |
year_t wd = 2400 + (cs.year() % 400) - (cs.month() < 3); |
| 522 |
wd += wd / 4 - wd / 100 + wd / 400; |
| 523 |
wd += k_weekday_offsets[cs.month()] + cs.day(); |
| 524 |
return k_weekday_by_mon_off[wd % 7 + 6]; |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
CONSTEXPR_F civil_day next_weekday(civil_day cd, weekday wd) noexcept { |
| 530 |
CONSTEXPR_D weekday k_weekdays_forw[14] = { |
| 531 |
weekday::monday, weekday::tuesday, weekday::wednesday, |
| 532 |
weekday::thursday, weekday::friday, weekday::saturday, |
| 533 |
weekday::sunday, weekday::monday, weekday::tuesday, |
| 534 |
weekday::wednesday, weekday::thursday, weekday::friday, |
| 535 |
weekday::saturday, weekday::sunday, |
| 536 |
}; |
| 537 |
weekday base = get_weekday(cd); |
| 538 |
for (int i = 0;; ++i) { |
| 539 |
if (base == k_weekdays_forw[i]) { |
| 540 |
for (int j = i + 1;; ++j) { |
| 541 |
if (wd == k_weekdays_forw[j]) { |
| 542 |
return cd + (j - i); |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
CONSTEXPR_F civil_day prev_weekday(civil_day cd, weekday wd) noexcept { |
| 550 |
CONSTEXPR_D weekday k_weekdays_back[14] = { |
| 551 |
weekday::sunday, weekday::saturday, weekday::friday, |
| 552 |
weekday::thursday, weekday::wednesday, weekday::tuesday, |
| 553 |
weekday::monday, weekday::sunday, weekday::saturday, |
| 554 |
weekday::friday, weekday::thursday, weekday::wednesday, |
| 555 |
weekday::tuesday, weekday::monday, |
| 556 |
}; |
| 557 |
weekday base = get_weekday(cd); |
| 558 |
for (int i = 0;; ++i) { |
| 559 |
if (base == k_weekdays_back[i]) { |
| 560 |
for (int j = i + 1;; ++j) { |
| 561 |
if (wd == k_weekdays_back[j]) { |
| 562 |
return cd - (j - i); |
| 563 |
} |
| 564 |
} |
| 565 |
} |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
CONSTEXPR_F int get_yearday(const civil_second& cs) noexcept { |
| 570 |
CONSTEXPR_D int k_month_offsets[1 + 12] = { |
| 571 |
-1, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, |
| 572 |
}; |
| 573 |
const int feb29 = (cs.month() > 2 && impl::is_leap_year(cs.year())); |
| 574 |
return k_month_offsets[cs.month()] + feb29 + cs.day(); |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
std::ostream& operator<<(std::ostream& os, const civil_year& y); |
| 580 |
std::ostream& operator<<(std::ostream& os, const civil_month& m); |
| 581 |
std::ostream& operator<<(std::ostream& os, const civil_day& d); |
| 582 |
std::ostream& operator<<(std::ostream& os, const civil_hour& h); |
| 583 |
std::ostream& operator<<(std::ostream& os, const civil_minute& m); |
| 584 |
std::ostream& operator<<(std::ostream& os, const civil_second& s); |
| 585 |
std::ostream& operator<<(std::ostream& os, weekday wd); |
| 586 |
|
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
#undef CONSTEXPR_M |
| 591 |
#undef CONSTEXPR_F |
| 592 |
#undef CONSTEXPR_D |
| 593 |
|
| 594 |
#endif |