3#ifndef _TIME_SHIELD_UNIX_TIME_CONVERSIONS_HPP_INCLUDED
4#define _TIME_SHIELD_UNIX_TIME_CONVERSIONS_HPP_INCLUDED
27 template<
class T = year_t>
32 constexpr int64_t BIAS_292277022000 = 9223371890843040000LL;
33 constexpr int64_t BIAS_2000 = 946684800LL;
36 int64_t secs = -((
ts - BIAS_2000) - BIAS_292277022000);
40 y -= n_400_years * 400;
44 y -= n_100_years * 100;
54 y = secs == 0 ? y : y - 1;
67 template<
class T = year_t>
88 template<
class Year,
class Month,
class Day>
93 const int64_t y =
static_cast<int64_t
>(
year) - (
static_cast<int64_t
>(month) <= 2 ? 1 : 0);
94 const int64_t m =
static_cast<int64_t
>(month) <= 2
95 ?
static_cast<int64_t
>(month) + 9
96 :
static_cast<int64_t
>(month) - 3;
97 const int64_t era = (y >= 0 ? y : y - 399) / 400;
98 const int64_t yoe = y - era * 400;
99 const int64_t doy = (153 * m + 2) / 5 +
static_cast<int64_t
>(day) - 1;
100 const int64_t doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
101 return static_cast<dse_t>(era * 146097 + doe - 719468);
121 template<
class Year,
class Month,
class Day>
126 return static_cast<dse_t>(
128 static_cast<int64_t
>(
year),
129 static_cast<int>(month),
130 static_cast<int>(day)));
140 template<
class T = dse_t>
152 template<
class T = dse_t>
165 template<
class T =
int>
167 return static_cast<T
>((stop - start) /
SEC_PER_DAY);
178 template<
class T = ts_t>
191 template<
class T = ts_t>
204 template<
class T = ts_t>
217 template<
class T = ts_ms_t>
230 template<
class T = ts_ms_t>
243 template<
class T = ts_ms_t>
255 template<
class T =
int64_t>
267 template<
class T =
int>
279 template<
class T =
int>
294 template<
class T1 =
int,
class T2 =
int>
299 return static_cast<T1
>(hour) *
static_cast<T1
>(
SEC_PER_HOUR) +
300 static_cast<T1
>(min) *
static_cast<T1
>(
SEC_PER_MIN) +
301 static_cast<T1
>(sec);
311 template<
class T =
int>
323 template<
class T =
int>
Configuration macros for the library.
Header file with time-related constants.
Fast date conversion helpers.
constexpr int64_t SEC_PER_YEAR
Seconds per year (365 days)
constexpr int64_t SEC_PER_100_YEARS
Seconds per 100 years.
constexpr int64_t MAX_YEAR
Maximum representable year.
constexpr int64_t SEC_PER_HOUR
Seconds per hour.
constexpr int64_t UNIX_EPOCH
Start year of UNIX time.
constexpr int64_t MS_PER_DAY
Milliseconds per day.
constexpr int64_t SEC_PER_400_YEARS
Seconds per 400 years.
constexpr int64_t SEC_PER_DAY
Seconds per day.
constexpr int64_t SEC_PER_MIN
Seconds per minute.
constexpr int64_t SEC_PER_4_YEARS
Seconds per 4 years.
constexpr T unix_day(ts_t ts=time_shield::ts()) noexcept
Alias for days_since_epoch function.
TIME_SHIELD_CONSTEXPR ts_t ts(year_t year, int month, int day)
Alias for to_timestamp.
TIME_SHIELD_CONSTEXPR ts_ms_t ts_ms(year_t year, int month, int day)
Alias for to_timestamp_ms.
constexpr T1 ms_to_sec(T2 ts_ms) noexcept
Converts a timestamp from milliseconds to seconds.
TIME_SHIELD_CONSTEXPR T year(ts_t ts=time_shield::ts())
Alias for year_of function.
bool sec_of_day(const std::string &str, T &sec)
Parse time of day string to seconds of day.
int64_t ts_t
Unix timestamp in seconds since 1970‑01‑01T00:00:00Z.
int64_t dse_t
Unix day count since 1970‑01‑01 (days since epoch).
int64_t ts_ms_t
Unix timestamp in milliseconds since epoch.
TIME_SHIELD_CONSTEXPR DaySplit split_unix_day(ts_t p_ts) noexcept
Split UNIX seconds into whole days and seconds-of-day.
TIME_SHIELD_CONSTEXPR int64_t fast_year_from_days_constexpr(int64_t p_days) noexcept
Convert days since Unix epoch to year using a fast constexpr algorithm.
TIME_SHIELD_CONSTEXPR int64_t fast_days_from_date_constexpr(int64_t p_year, int p_month, int p_day) noexcept
Convert date to days since Unix epoch using a fast constexpr algorithm.
TIME_SHIELD_CONSTEXPR T years_since_epoch(ts_t ts) noexcept
Converts a UNIX timestamp to a year.
TIME_SHIELD_CONSTEXPR dse_t date_to_unix_day(Year year, Month month, Day day) noexcept
Convert a calendar date to UNIX day count.
Main namespace for the Time Shield library.
constexpr T sec_of_hour(ts_t ts=time_shield::ts())
Get the second of the hour.
constexpr T sec_of_min(ts_t ts=time_shield::ts())
Get the second of the minute.
constexpr T days_between(ts_t start, ts_t stop) noexcept
Get the number of days between two timestamps.
constexpr T unix_day_to_ts_ms(dse_t unix_day) noexcept
Converts a UNIX day to a timestamp in milliseconds.
TIME_SHIELD_CONSTEXPR T years_since_epoch(ts_t ts) noexcept
Converts a UNIX timestamp to a year.
constexpr T sec_of_day_ms(ts_ms_t ts_ms) noexcept
Get the second of the day from milliseconds timestamp.
constexpr T start_of_next_day_from_unix_day_ms(dse_t unix_day) noexcept
Converts a UNIX day to a timestamp representing the start of the next day in milliseconds.
constexpr T start_of_next_day_from_unix_day(dse_t unix_day) noexcept
Converts a UNIX day to a timestamp representing the start of the next day in seconds.
constexpr T min_since_epoch(ts_t ts=time_shield::ts())
Get UNIX minute.
constexpr T end_of_day_from_unix_day(dse_t unix_day) noexcept
Converts a UNIX day to a timestamp representing the end of the day in seconds.
constexpr T unix_day_to_ts(dse_t unix_day) noexcept
Converts a UNIX day to a timestamp in seconds.
constexpr T end_of_day_from_unix_day_ms(dse_t unix_day) noexcept
Converts a UNIX day to a timestamp representing the end of the day in milliseconds.
constexpr T days_since_epoch_ms(ts_ms_t t_ms=time_shield::ts_ms()) noexcept
Get UNIX day from milliseconds timestamp.
TIME_SHIELD_CONSTEXPR dse_t date_to_unix_day(Year year, Month month, Day day) noexcept
Convert a calendar date to UNIX day count.
constexpr T days_since_epoch(ts_t ts=time_shield::ts()) noexcept
Get UNIX day.
Helper functions for unit conversions between seconds, minutes, hours, and milliseconds.
Header file with time-related utility functions.
Type definitions for time-related units and formats.