Time Shield Library
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_UNIX_TIME_CONVERSIONS_HPP_INCLUDED
4#define _TIME_SHIELD_UNIX_TIME_CONVERSIONS_HPP_INCLUDED
5
8
9#include "config.hpp"
10#include "constants.hpp"
11#include "detail/fast_date.hpp"
13#include "time_utils.hpp"
14#include "types.hpp"
15
16namespace time_shield {
17
20
21 namespace legacy {
22
27 template<class T = year_t>
28 TIME_SHIELD_CONSTEXPR T years_since_epoch(ts_t ts) noexcept {
29 // 9223372029693630000 - значение на момент 292277024400 от 2000 года
30 // Такое значение приводит к неправильному вычислению умножения n_400_years * SEC_PER_400_YEARS
31 // Поэтому пришлось снизить до 9223371890843040000
32 constexpr int64_t BIAS_292277022000 = 9223371890843040000LL;
33 constexpr int64_t BIAS_2000 = 946684800LL;
34
35 int64_t y = MAX_YEAR;
36 int64_t secs = -((ts - BIAS_2000) - BIAS_292277022000);
37
38 const int64_t n_400_years = secs / SEC_PER_400_YEARS;
39 secs -= n_400_years * SEC_PER_400_YEARS;
40 y -= n_400_years * 400;
41
42 const int64_t n_100_years = secs / SEC_PER_100_YEARS;
43 secs -= n_100_years * SEC_PER_100_YEARS;
44 y -= n_100_years * 100;
45
46 const int64_t n_4_years = secs / SEC_PER_4_YEARS;
47 secs -= n_4_years * SEC_PER_4_YEARS;
48 y -= n_4_years * 4;
49
50 const int64_t n_1_years = secs / SEC_PER_YEAR;
51 secs -= n_1_years * SEC_PER_YEAR;
52 y -= n_1_years;
53
54 y = secs == 0 ? y : y - 1;
55 return y - UNIX_EPOCH;
56 }
57
58 } // namespace legacy
59
67 template<class T = year_t>
68 TIME_SHIELD_CONSTEXPR T years_since_epoch(ts_t ts) noexcept {
70 const int64_t year = detail::fast_year_from_days_constexpr(split.days);
71 return static_cast<T>(year - UNIX_EPOCH);
72 }
73
74 namespace legacy {
75
88 template<class Year, class Month, class Day>
89 TIME_SHIELD_CONSTEXPR inline dse_t date_to_unix_day(
90 Year year,
91 Month month,
92 Day day) noexcept {
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);
102 }
103
104 } // namespace legacy
105
121 template<class Year, class Month, class Day>
122 TIME_SHIELD_CONSTEXPR inline dse_t date_to_unix_day(
123 Year year,
124 Month month,
125 Day day) noexcept {
126 return static_cast<dse_t>(
128 static_cast<int64_t>(year),
129 static_cast<int>(month),
130 static_cast<int>(day)));
131 }
132
140 template<class T = dse_t>
141 constexpr T days_since_epoch(ts_t ts = time_shield::ts()) noexcept {
142 return ts / SEC_PER_DAY;
143 }
144
152 template<class T = dse_t>
153 constexpr T days_since_epoch_ms(ts_ms_t t_ms = time_shield::ts_ms()) noexcept {
155 }
156
165 template<class T = int>
166 constexpr T days_between(ts_t start, ts_t stop) noexcept {
167 return static_cast<T>((stop - start) / SEC_PER_DAY);
168 }
169
178 template<class T = ts_t>
179 constexpr T unix_day_to_ts(dse_t unix_day) noexcept {
180 return unix_day * SEC_PER_DAY;
181 }
182
191 template<class T = ts_t>
192 constexpr T unix_day_to_ts_ms(dse_t unix_day) noexcept {
193 return unix_day * MS_PER_DAY;
194 }
195
204 template<class T = ts_t>
206 return unix_day * SEC_PER_DAY + SEC_PER_DAY - 1;
207 }
208
217 template<class T = ts_ms_t>
219 return unix_day * MS_PER_DAY + MS_PER_DAY - 1;
220 }
221
230 template<class T = ts_ms_t>
233 }
234
243 template<class T = ts_ms_t>
245 return unix_day * MS_PER_DAY + MS_PER_DAY;
246 }
247
255 template<class T = int64_t>
257 return ts / SEC_PER_MIN;
258 }
259
267 template<class T = int>
268 constexpr T sec_of_day(ts_t ts = time_shield::ts()) noexcept {
269 return static_cast<T>(ts % SEC_PER_DAY);
270 }
271
279 template<class T = int>
280 constexpr T sec_of_day_ms(ts_ms_t ts_ms) noexcept {
282 }
283
294 template<class T1 = int, class T2 = int>
295 constexpr T1 sec_of_day(
296 T2 hour,
297 T2 min,
298 T2 sec) noexcept {
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);
302 }
303
311 template<class T = int>
313 return static_cast<T>(ts % SEC_PER_MIN);
314 }
315
323 template<class T = int>
325 return static_cast<T>(ts % SEC_PER_HOUR);
326 }
327
329
330}; // namespace time_shield
331
332#endif // _TIME_SHIELD_UNIX_TIME_CONVERSIONS_HPP_INCLUDED
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.
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.
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.
Definition types.hpp:49
int64_t dse_t
Unix day count since 1970‑01‑01 (days since epoch).
Definition types.hpp:42
int64_t ts_ms_t
Unix timestamp in milliseconds since epoch.
Definition types.hpp:50
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.
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.