Time Shield Library 2.0.0
C++ library for working with time
Loading...
Searching...
No Matches
date_conversions.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#pragma once
3#ifndef TIME_SHIELD_HEADER_CONVERSIONS_DATE_CONVERSIONS_HPP_INCLUDED
4#define TIME_SHIELD_HEADER_CONVERSIONS_DATE_CONVERSIONS_HPP_INCLUDED
5
8
11
12namespace time_shield {
13
16
24 template<class T = year_t>
25 TIME_SHIELD_CONSTEXPR inline T year_of(ts_t ts = time_shield::ts()) {
26 return years_since_epoch<T>(ts) + static_cast<T>(UNIX_EPOCH);
27 }
28
36 template<class T = year_t>
37 TIME_SHIELD_CONSTEXPR inline T year_of_ms(ts_ms_t ts_ms = time_shield::ts_ms()) {
39 }
40
44 template<class T1 = int, class T2 = year_t>
45 TIME_SHIELD_CONSTEXPR inline T1 num_days_in_year(T2 year) noexcept {
47 return DAYS_PER_YEAR;
48 }
49
56 template<class T = int>
57 TIME_SHIELD_CONSTEXPR inline T num_days_in_year_ts(ts_t ts = time_shield::ts()) {
59 return DAYS_PER_YEAR;
60 }
61
71 template<class T1 = Weekday, class T2 = year_t, class T3 = int, class T4 = int>
72 TIME_SHIELD_CONSTEXPR inline T1 day_of_week_date(T2 year, T3 month, T4 day) {
73 year_t a = 0;
74 year_t y = 0;
75 year_t m = 0;
76 year_t R = 0;
77 a = (14 - month) / MONTHS_PER_YEAR;
78 y = year - a;
79 m = month + MONTHS_PER_YEAR * a - 2;
80 R = 7000 + ( day + y + (y / 4) - (y / 100) + (y / 400) + (31 * m) / MONTHS_PER_YEAR);
81 return static_cast<T1>(R % DAYS_PER_WEEK);
82 }
83
92 template<class T1 = Weekday, class T2>
93 TIME_SHIELD_CONSTEXPR inline T1 weekday_of_date(const T2& date) {
94 return day_of_week_date(date.year, date.mon, date.day);
95 }
96
100 template<class T1 = Weekday, class T2>
101 TIME_SHIELD_CONSTEXPR inline T1 weekday_from_date(const T2& date) {
102 return weekday_of_date<T1>(date);
103 }
104
106
107}; // namespace time_shield
108
109#endif // TIME_SHIELD_HEADER_CONVERSIONS_DATE_CONVERSIONS_HPP_INCLUDED
constexpr int64_t DAYS_PER_WEEK
Days per week.
const int64_t MONTHS_PER_YEAR
Months per year.
constexpr int64_t DAYS_PER_YEAR
Days per year.
constexpr int64_t DAYS_PER_LEAP_YEAR
Days per leap year.
constexpr int64_t UNIX_EPOCH
Start year of UNIX time.
TIME_SHIELD_CONSTEXPR T1 ms_to_sec(T2 ts_ms) noexcept
Converts a timestamp from milliseconds to seconds.
TIME_SHIELD_CONSTEXPR T1 num_days_in_year(T2 year) noexcept
Get the number of days in a year.
TIME_SHIELD_CONSTEXPR T1 day_of_week_date(T2 year, T3 month, T4 day)
Get the day of the week.
TIME_SHIELD_CONSTEXPR T1 weekday_of_date(const T2 &date)
Get the day of the week from a date structure.
TIME_SHIELD_CONSTEXPR T num_days_in_year_ts(ts_t ts=time_shield::ts())
Get the number of days in the current year.
TIME_SHIELD_CONSTEXPR T year_of_ms(ts_ms_t ts_ms=time_shield::ts_ms())
Get the year from the timestamp in milliseconds.
TIME_SHIELD_CONSTEXPR T year(ts_t ts=time_shield::ts())
Alias for year_of function.
TIME_SHIELD_CONSTEXPR T year_of(ts_t ts=time_shield::ts())
Get the year from the timestamp.
TIME_SHIELD_CONSTEXPR T1 weekday_from_date(const T2 &date)
Alias for weekday_of_date.
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 year_t
Year as an integer (e.g., 2024).
Definition types.hpp:40
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 bool is_leap_year_ts(ts_t ts)
Checks if the given year is a leap year.
TIME_SHIELD_CONSTEXPR bool is_leap_year_date(T year) noexcept
Checks if the given year is a leap year.
Main namespace for the Time Shield library.
TIME_SHIELD_CONSTEXPR T years_since_epoch(ts_t ts) noexcept
Converts a UNIX timestamp to a year.
Conversions related to UNIX-based time units and epochs.