Time Shield Library 2.0.0
C++ library for working with time
Loading...
Searching...
No Matches
unix_time_conversions.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#pragma once
3#ifndef TIME_SHIELD_HEADER_CONVERSIONS_UNIX_TIME_CONVERSIONS_HPP_INCLUDED
4#define TIME_SHIELD_HEADER_CONVERSIONS_UNIX_TIME_CONVERSIONS_HPP_INCLUDED
5
8
10#include "detail/fast_date.hpp"
12
13namespace time_shield {
14
17
18 namespace legacy {
19
24 template<class T = year_t>
25 TIME_SHIELD_CONSTEXPR T years_since_epoch(ts_t ts) noexcept {
26 // 9223372029693630000 reaches year 292277024400 from the 2000 epoch.
27 // This value overflows the n_400_years * SEC_PER_400_YEARS calculation.
28 // The supported bound is reduced to 9223371890843040000.
29 constexpr int64_t BIAS_292277022000 = 9223371890843040000LL;
30 constexpr int64_t BIAS_2000 = 946684800LL;
31
32 int64_t y = MAX_YEAR;
33 int64_t secs = -((ts - BIAS_2000) - BIAS_292277022000);
34
35 const int64_t n_400_years = secs / SEC_PER_400_YEARS;
36 secs -= n_400_years * SEC_PER_400_YEARS;
37 y -= n_400_years * 400;
38
39 const int64_t n_100_years = secs / SEC_PER_100_YEARS;
40 secs -= n_100_years * SEC_PER_100_YEARS;
41 y -= n_100_years * 100;
42
43 const int64_t n_4_years = secs / SEC_PER_4_YEARS;
44 secs -= n_4_years * SEC_PER_4_YEARS;
45 y -= n_4_years * 4;
46
47 const int64_t n_1_years = secs / SEC_PER_YEAR;
48 secs -= n_1_years * SEC_PER_YEAR;
49 y -= n_1_years;
50
51 y = secs == 0 ? y : y - 1;
52 return y - UNIX_EPOCH;
53 }
54
55 } // namespace legacy
56
64 template<class T = year_t>
65 TIME_SHIELD_CONSTEXPR T years_since_epoch(ts_t ts) noexcept {
67 const int64_t year = detail::fast_year_from_days_constexpr(split.days);
68 return static_cast<T>(year - UNIX_EPOCH);
69 }
70
71 namespace legacy {
72
85 template<class Year, class Month, class Day>
86 TIME_SHIELD_CONSTEXPR inline dse_t date_to_unix_day(
87 Year year,
88 Month month,
89 Day day) noexcept {
90 const int64_t y = static_cast<int64_t>(year) - (static_cast<int64_t>(month) <= 2 ? 1 : 0);
91 const int64_t m = static_cast<int64_t>(month) <= 2
92 ? static_cast<int64_t>(month) + 9
93 : static_cast<int64_t>(month) - 3;
94 const int64_t era = (y >= 0 ? y : y - 399) / 400;
95 const int64_t yoe = y - era * 400;
96 const int64_t doy = (153 * m + 2) / 5 + static_cast<int64_t>(day) - 1;
97 const int64_t doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
98 return static_cast<dse_t>(era * 146097 + doe - 719468);
99 }
100
101 } // namespace legacy
102
118 template<class Year, class Month, class Day>
119 TIME_SHIELD_CONSTEXPR inline dse_t date_to_unix_day(
120 Year year,
121 Month month,
122 Day day) noexcept {
123 return static_cast<dse_t>(
125 static_cast<int64_t>(year),
126 static_cast<int>(month),
127 static_cast<int>(day)));
128 }
129
137 template<class T = dse_t>
138 TIME_SHIELD_CONSTEXPR T days_since_epoch(ts_t ts = time_shield::ts()) noexcept {
139 return ts / SEC_PER_DAY;
140 }
141
149 template<class T = dse_t>
150 TIME_SHIELD_CONSTEXPR T days_since_epoch_ms(ts_ms_t t_ms = time_shield::ts_ms()) noexcept {
152 }
153
162 template<class T = int>
163 TIME_SHIELD_CONSTEXPR T days_between(ts_t start, ts_t stop) noexcept {
164 return static_cast<T>((stop - start) / SEC_PER_DAY);
165 }
166
175 template<class T = ts_t>
176 TIME_SHIELD_CONSTEXPR T unix_day_to_ts(dse_t unix_day) noexcept {
177 return unix_day * SEC_PER_DAY;
178 }
179
188 template<class T = ts_ms_t>
189 TIME_SHIELD_CONSTEXPR T unix_day_to_ts_ms(dse_t unix_day) noexcept {
190 return unix_day * MS_PER_DAY;
191 }
192
201 template<class T = ts_t>
202 TIME_SHIELD_CONSTEXPR T end_of_day_from_unix_day(dse_t unix_day) noexcept {
203 return unix_day * SEC_PER_DAY + SEC_PER_DAY - 1;
204 }
205
214 template<class T = ts_ms_t>
215 TIME_SHIELD_CONSTEXPR T end_of_day_from_unix_day_ms(dse_t unix_day) noexcept {
216 return unix_day * MS_PER_DAY + MS_PER_DAY - 1;
217 }
218
227 template<class T = ts_t>
228 TIME_SHIELD_CONSTEXPR T start_of_next_day_from_unix_day(dse_t unix_day) noexcept {
230 }
231
240 template<class T = ts_ms_t>
241 TIME_SHIELD_CONSTEXPR T start_of_next_day_from_unix_day_ms(dse_t unix_day) noexcept {
242 return unix_day * MS_PER_DAY + MS_PER_DAY;
243 }
244
252 template<class T = int64_t>
253 TIME_SHIELD_CONSTEXPR T min_since_epoch(ts_t ts = time_shield::ts()) {
254 return ts / SEC_PER_MIN;
255 }
256
264 template<class T = int>
265 TIME_SHIELD_CONSTEXPR T sec_of_day(ts_t ts = time_shield::ts()) noexcept {
266 return static_cast<T>(ts % SEC_PER_DAY);
267 }
268
276 template<class T = int>
277 TIME_SHIELD_CONSTEXPR T sec_of_day_ms(ts_ms_t ts_ms) noexcept {
279 }
280
291 template<class T1 = int, class T2 = int>
292 constexpr T1 sec_of_day(
293 T2 hour,
294 T2 min,
295 T2 sec) noexcept {
296 return static_cast<T1>(hour) * static_cast<T1>(SEC_PER_HOUR) +
297 static_cast<T1>(min) * static_cast<T1>(SEC_PER_MIN) +
298 static_cast<T1>(sec);
299 }
300
308 template<class T = int>
309 TIME_SHIELD_CONSTEXPR T sec_of_min(ts_t ts = time_shield::ts()) {
310 return static_cast<T>(ts % SEC_PER_MIN);
311 }
312
320 template<class T = int>
321 TIME_SHIELD_CONSTEXPR T sec_of_hour(ts_t ts = time_shield::ts()) {
322 return static_cast<T>(ts % SEC_PER_HOUR);
323 }
324
326
327}; // namespace time_shield
328
329#endif // TIME_SHIELD_HEADER_CONVERSIONS_UNIX_TIME_CONVERSIONS_HPP_INCLUDED
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.
Definition constants.hpp:97
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.
TIME_SHIELD_CONSTEXPR T1 ms_to_sec(T2 ts_ms) noexcept
Converts a timestamp from milliseconds to seconds.
TIME_SHIELD_CONSTEXPR T unix_day(ts_t ts=time_shield::ts()) noexcept
Alias for days_since_epoch function.
TIME_SHIELD_CONSTEXPR T year(ts_t ts=time_shield::ts())
Alias for year_of function.
int64_t ts_t
Unix timestamp in seconds since 1970‑01‑01T00:00:00Z.
Definition types.hpp:48
int64_t dse_t
Unix day count since 1970‑01‑01 (days since epoch).
Definition types.hpp:41
int64_t ts_ms_t
Unix timestamp in milliseconds since epoch.
Definition types.hpp:49
ts_t ts() noexcept
Get the current UTC timestamp in seconds.
ts_ms_t ts_ms() noexcept
Get the current UTC timestamp in milliseconds.
TIME_SHIELD_CONSTEXPR DaySplit split_unix_day(ts_t p_ts) noexcept
Split UNIX seconds into whole days and seconds-of-day.
Definition fast_date.hpp:39
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.
Definition fast_date.hpp:59
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.
TIME_SHIELD_CONSTEXPR T sec_of_day_ms(ts_ms_t ts_ms) noexcept
Get the second of the day from milliseconds timestamp.
TIME_SHIELD_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 sec_of_hour(ts_t ts=time_shield::ts())
Get the second of the hour.
TIME_SHIELD_CONSTEXPR T years_since_epoch(ts_t ts) noexcept
Converts a UNIX timestamp to a year.
TIME_SHIELD_CONSTEXPR T unix_day_to_ts(dse_t unix_day) noexcept
Converts a UNIX day to a timestamp in seconds.
TIME_SHIELD_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 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.
TIME_SHIELD_CONSTEXPR T days_since_epoch(ts_t ts=time_shield::ts()) noexcept
Get UNIX day.
TIME_SHIELD_CONSTEXPR T min_since_epoch(ts_t ts=time_shield::ts())
Get UNIX minute.
TIME_SHIELD_CONSTEXPR T sec_of_day(ts_t ts=time_shield::ts()) noexcept
Get the second of the day.
TIME_SHIELD_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.
TIME_SHIELD_CONSTEXPR dse_t date_to_unix_day(Year year, Month month, Day day) noexcept
Convert a calendar date to UNIX day count.
TIME_SHIELD_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.
TIME_SHIELD_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.
TIME_SHIELD_CONSTEXPR T sec_of_min(ts_t ts=time_shield::ts())
Get the second of the minute.
TIME_SHIELD_CONSTEXPR T days_between(ts_t start, ts_t stop) noexcept
Get the number of days between two timestamps.
Helper functions for unit conversions between seconds, minutes, hours, and milliseconds.