3#ifndef _TIME_SHIELD_UNIX_TIME_CONVERSIONS_HPP_INCLUDED
4#define _TIME_SHIELD_UNIX_TIME_CONVERSIONS_HPP_INCLUDED
24 template<
class T = year_t>
29 constexpr int64_t BIAS_292277022000 = 9223371890843040000LL;
30 constexpr int64_t BIAS_2000 = 946684800LL;
33 int64_t secs = -((
ts - BIAS_2000) - BIAS_292277022000);
37 y -= n_400_years * 400;
41 y -= n_100_years * 100;
51 y = secs == 0 ? y : y - 1;
67 template<
class Year,
class Month,
class Day>
72 const int64_t y =
static_cast<int64_t
>(
year) - (
static_cast<int64_t
>(month) <= 2 ? 1 : 0);
73 const int64_t m =
static_cast<int64_t
>(month) <= 2
74 ?
static_cast<int64_t
>(month) + 9
75 :
static_cast<int64_t
>(month) - 3;
76 const int64_t era = (y >= 0 ? y : y - 399) / 400;
77 const int64_t yoe = y - era * 400;
78 const int64_t doy = (153 * m + 2) / 5 +
static_cast<int64_t
>(day) - 1;
79 const int64_t doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
80 return static_cast<uday_t>(era * 146097 + doe - 719468);
90 template<
class T = uday_t>
102 template<
class T = uday_t>
115 template<
class T =
int>
128 template<
class T = ts_t>
141 template<
class T = ts_t>
154 template<
class T = ts_t>
167 template<
class T = ts_ms_t>
180 template<
class T = ts_ms_t>
193 template<
class T = ts_ms_t>
205 template<
class T =
int64_t>
217 template<
class T =
int>
229 template<
class T =
int>
244 template<
class T1 =
int,
class T2 =
int>
249 return static_cast<T1
>(hour) *
static_cast<T1
>(
SEC_PER_HOUR) +
250 static_cast<T1
>(min) *
static_cast<T1
>(
SEC_PER_MIN) +
251 static_cast<T1
>(sec);
261 template<
class T =
int>
273 template<
class T =
int>
Configuration macros for the library.
Header file with time-related constants.
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 sec_of_hour(ts_t ts=time_shield::ts())
Get the second of the hour.
constexpr T unix_day(ts_t ts=time_shield::ts()) noexcept
Alias for days_since_epoch function.
constexpr T end_of_day_from_unix_day_ms(uday_t unix_day) noexcept
Converts a UNIX day to a timestamp representing the end of the day in milliseconds.
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 years_since_epoch(ts_t ts) noexcept
Converts a UNIX timestamp to a year.
constexpr T start_of_next_day_from_unix_day(uday_t unix_day) noexcept
Converts a UNIX day to a timestamp representing the start of the next day in seconds.
TIME_SHIELD_CONSTEXPR uday_t date_to_unix_day(Year year, Month month, Day day) noexcept
Convert a calendar date to UNIX day count.
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 T sec_of_day_ms(ts_ms_t ts_ms) noexcept
Get the second of the day from milliseconds timestamp.
constexpr T1 ms_to_sec(T2 ts_ms) noexcept
Converts a timestamp from milliseconds to seconds.
constexpr T min_since_epoch(ts_t ts=time_shield::ts())
Get UNIX minute.
constexpr T end_of_day_from_unix_day(uday_t unix_day) noexcept
Converts a UNIX day to a timestamp representing the end of the day in seconds.
constexpr T days_since_epoch_ms(ts_ms_t t_ms=time_shield::ts_ms()) noexcept
Get UNIX day from milliseconds timestamp.
constexpr T start_of_next_day_from_unix_day_ms(uday_t unix_day) noexcept
Converts a UNIX day to a timestamp representing the start of the next day in milliseconds.
TIME_SHIELD_CONSTEXPR T year(ts_t ts=time_shield::ts())
Alias for year_of function.
constexpr T unix_day_to_ts(uday_t unix_day) noexcept
Converts a UNIX day to a timestamp in seconds.
constexpr T unix_day_to_ts_ms(uday_t unix_day) noexcept
Converts a UNIX day to a timestamp in milliseconds.
constexpr T days_since_epoch(ts_t ts=time_shield::ts()) noexcept
Get UNIX day.
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 ts_ms_t
Unix timestamp in milliseconds since epoch.
int64_t uday_t
Unix day count since 1970‑01‑01 (days since epoch).
Main namespace for the Time Shield library.
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.