Project Alice
Loading...
Searching...
No Matches
date_interface.cpp
Go to the documentation of this file.
1#include "date_interface.hpp"
2
3namespace sys {
4
5// from http://howardhinnant.github.io/date_algorithms.html
6
7constexpr int days_from_civil(int y, unsigned m, unsigned d) noexcept {
8 y -= m <= 2;
9 int const era = (y >= 0 ? y : y - 399) / 400;
10 unsigned const yoe = static_cast<unsigned>(y - era * 400); // [0, 399]
11 unsigned const doy = (153 * (m > 2 ? m - 3 : m + 9) + 2) / 5 + d - 1; // [0, 365]
12 unsigned const doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096]
13 return era * 146097 + static_cast<int>(doe) - 719468;
14}
15
16constexpr year_month_day civil_from_days(int64_t z) noexcept {
17 z += 719468;
18 const int32_t era = int32_t((z >= 0 ? z : z - 146096) / 146097);
19 unsigned const doe = static_cast<unsigned>(z - era * 146097); // [0, 146096]
20 unsigned const yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; // [0, 399]
21 const int32_t y = static_cast<int32_t>(yoe) + era * 400;
22 unsigned const doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365]
23 unsigned const mp = (5 * doy + 2) / 153; // [0, 11]
24 unsigned const d = doy - (153 * mp + 2) / 5 + 1; // [1, 31]
25 unsigned const m = mp < 10 ? mp + 3 : mp - 9; // [1, 12]
26 return year_month_day{int32_t(y + (m <= 2)), uint16_t(m), uint16_t(d)};
27}
28
30 auto difference = days_from_civil(v.year, v.month, v.day) - base.to_days();
31 difference = std::clamp(difference, int64_t(0), int64_t(std::numeric_limits<uint16_t>::max()) - 1);
32 value = date::value_base_t(1 + difference);
33}
34
36 int64_t count = base.to_days() + (value - 1);
37 return civil_from_days(count);
38}
39
41 days = days_from_civil(d.year, d.month, d.day);
42}
43
45 return bool(d) && start.to_days() + (d.to_raw_value() - 1) < end.to_days();
46}
47
49 return days_from_civil(end.year, end.month, end.day) - days_from_civil(start.year, start.month, start.day);
50}
51
52bool is_leap_year(int32_t year) {
53 return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 == 0));
54}
55
56} // namespace sys
constexpr int64_t to_days() const noexcept
absolute_time_point() noexcept=default
year_month_day to_ymd(absolute_time_point base) const noexcept
uint16_t value_base_t
constexpr date() noexcept=default
Definition: constants.hpp:4
int32_t days_difference(year_month_day start, year_month_day end)
bool is_leap_year(int32_t year)
constexpr int days_from_civil(int y, unsigned m, unsigned d) noexcept
bool is_playable_date(date d, absolute_time_point start, absolute_time_point end)
constexpr year_month_day civil_from_days(int64_t z) noexcept