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"
12#include "time_utils.hpp"
13#include "types.hpp"
14
15namespace time_shield {
16
19
24 template<class T = year_t>
25 constexpr T years_since_epoch(ts_t ts) noexcept {
26 // 9223372029693630000 - значение на момент 292277024400 от 2000 года
27 // Такое значение приводит к неправильному вычислению умножения n_400_years * SEC_PER_400_YEARS
28 // Поэтому пришлось снизить до 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
67 template<class Year, class Month, class Day>
68 TIME_SHIELD_CONSTEXPR inline uday_t date_to_unix_day(
69 Year year,
70 Month month,
71 Day day) noexcept {
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);
81 }
82
90 template<class T = uday_t>
91 constexpr T days_since_epoch(ts_t ts = time_shield::ts()) noexcept {
92 return ts / SEC_PER_DAY;
93 }
94
102 template<class T = uday_t>
103 constexpr T days_since_epoch_ms(ts_ms_t t_ms = time_shield::ts_ms()) noexcept {
105 }
106
115 template<class T = int>
116 constexpr T days_between(ts_t start, ts_t stop) noexcept {
117 return (stop - start) / SEC_PER_DAY;
118 }
119
128 template<class T = ts_t>
129 constexpr T unix_day_to_ts(uday_t unix_day) noexcept {
130 return unix_day * SEC_PER_DAY;
131 }
132
141 template<class T = ts_t>
142 constexpr T unix_day_to_ts_ms(uday_t unix_day) noexcept {
143 return unix_day * MS_PER_DAY;
144 }
145
154 template<class T = ts_t>
156 return unix_day * SEC_PER_DAY + SEC_PER_DAY - 1;
157 }
158
167 template<class T = ts_ms_t>
169 return unix_day * MS_PER_DAY + MS_PER_DAY - 1;
170 }
171
180 template<class T = ts_ms_t>
183 }
184
193 template<class T = ts_ms_t>
197
205 template<class T = int64_t>
207 return ts / SEC_PER_MIN;
208 }
209
217 template<class T = int>
218 constexpr T sec_of_day(ts_t ts = time_shield::ts()) noexcept {
219 return ts % SEC_PER_DAY;
220 }
221
229 template<class T = int>
230 constexpr T sec_of_day_ms(ts_ms_t ts_ms) noexcept {
232 }
233
244 template<class T1 = int, class T2 = int>
245 constexpr T1 sec_of_day(
246 T2 hour,
247 T2 min,
248 T2 sec) noexcept {
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);
252 }
253
261 template<class T = int>
263 return (ts % SEC_PER_MIN);
264 }
265
273 template<class T = int>
275 return (ts % SEC_PER_HOUR);
276 }
277
279
280}; // namespace time_shield
281
282#endif // _TIME_SHIELD_UNIX_TIME_CONVERSIONS_HPP_INCLUDED
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.
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 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.
Definition types.hpp:48
int64_t ts_ms_t
Unix timestamp in milliseconds since epoch.
Definition types.hpp:49
int64_t uday_t
Unix day count since 1970‑01‑01 (days since epoch).
Definition types.hpp:42
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.