Time Shield Library
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_DATE_CONVERSIONS_HPP_INCLUDED
4#define _TIME_SHIELD_DATE_CONVERSIONS_HPP_INCLUDED
5
8
9#include "config.hpp"
10#include "constants.hpp"
11#include "types.hpp"
13#include "validation.hpp"
14
15namespace time_shield {
16
19
27 template<class T = year_t>
28 TIME_SHIELD_CONSTEXPR inline T year_of(ts_t ts = time_shield::ts()) {
29 return years_since_epoch<T>(ts) + static_cast<T>(UNIX_EPOCH);
30 }
31
39 template<class T = year_t>
40 TIME_SHIELD_CONSTEXPR inline T year_of_ms(ts_ms_t ts_ms = time_shield::ts_ms()) {
42 }
43
47 template<class T1 = int, class T2 = year_t>
48 TIME_SHIELD_CONSTEXPR inline T1 num_days_in_year(T2 year) noexcept {
50 return DAYS_PER_YEAR;
51 }
52
59 template<class T = int>
60 TIME_SHIELD_CONSTEXPR inline T num_days_in_year_ts(ts_t ts = time_shield::ts()) {
62 return DAYS_PER_YEAR;
63 }
64
74 template<class T1 = Weekday, class T2 = year_t, class T3 = int, class T4 = int>
75 TIME_SHIELD_CONSTEXPR inline T1 day_of_week_date(T2 year, T3 month, T4 day) {
76 year_t a, y, m, R;
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_DATE_CONVERSIONS_HPP_INCLUDED
Configuration macros for the library.
Header file with time-related constants.
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.
constexpr T years_since_epoch(ts_t ts) noexcept
Converts a UNIX timestamp to a year.
TIME_SHIELD_CONSTEXPR ts_t ts(year_t year, int month, int day)
Alias for to_timestamp.
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 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 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:41
constexpr bool is_leap_year_date(T year) noexcept
Checks if the given year is a leap year.
TIME_SHIELD_CONSTEXPR bool is_leap_year_ts(ts_t ts)
Checks if the given year is a leap year.
Main namespace for the Time Shield library.
Type definitions for time-related units and formats.
Conversions related to UNIX-based time units and epochs.
Header file with time-related validation functions.