Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_zone_conversions.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#pragma once
3#ifndef _TIME_SHIELD_TIME_ZONE_CONVERSIONS_HPP_INCLUDED
4#define _TIME_SHIELD_TIME_ZONE_CONVERSIONS_HPP_INCLUDED
5
9
10#include "date_time_struct.hpp"
11#include "time_conversions.hpp"
12
13namespace time_shield {
14
17
21 inline ts_t cet_to_gmt(ts_t cet) {
23 int max_days = num_days_in_month(dt.year, dt.mon);
24 const int OLD_START_SUMMER_HOUR = 2;
25 const int OLD_STOP_SUMMER_HOUR = 3;
26 const int NEW_SUMMER_HOUR = 1;
27
28 if(dt.year < 2002) {
29 if(dt.mon > MAR && dt.mon < OCT) {
30 return cet - SEC_PER_HOUR * 2;
31 } else
32 if(dt.mon == MAR) {
33 for(int d = max_days; d >= dt.day; --d) {
34 if(day_of_week_date(dt.year, MAR, d) == SUN) {
35 if(d == dt.day) {
36 if(dt.hour >= OLD_START_SUMMER_HOUR)
37 return cet - SEC_PER_HOUR * 2;
38 return cet - SEC_PER_HOUR;
39 }
40 return cet - SEC_PER_HOUR;
41 }
42 }
43 return cet - SEC_PER_HOUR * 2;
44 } else if(dt.mon == OCT) {
45 for(int d = max_days; d >= dt.day; --d) {
46 if(day_of_week_date(dt.year, OCT, d) == SUN) {
47 if(d == dt.day) {
48 if(dt.hour >= OLD_STOP_SUMMER_HOUR)
49 return cet - SEC_PER_HOUR;
50 return cet - SEC_PER_HOUR;
51 }
52 return cet - SEC_PER_HOUR * 2;
53 }
54 }
55 return cet - SEC_PER_HOUR;
56 }
57 return cet - SEC_PER_HOUR;
58 } else {
59 if(dt.mon > MAR && dt.mon < OCT) {
60 return cet - SEC_PER_HOUR * 2;
61 } else
62 if(dt.mon == MAR) {
63 for(int d = max_days; d >= dt.day; --d) {
64 if(day_of_week_date(dt.year, MAR, d) == SUN) {
65 if(d == dt.day) {
66 if(dt.hour >= (NEW_SUMMER_HOUR + 2))
67 return cet - SEC_PER_HOUR * 2;
68 return cet - SEC_PER_HOUR;
69 }
70 return cet - SEC_PER_HOUR;
71 }
72 }
73 return cet - SEC_PER_HOUR * 2;
74 } else
75 if(dt.mon == OCT) {
76 for(int d = max_days; d >= dt.day; --d) {
77 if(day_of_week_date(dt.year, OCT, d) == SUN) {
78 if(d == dt.day) {
79 if(dt.hour >= (NEW_SUMMER_HOUR + 1))
80 return cet - SEC_PER_HOUR;
81 return cet - SEC_PER_HOUR * 2;
82 }
83 return cet - SEC_PER_HOUR * 2;
84 }
85 }
86 }
87 }
88 return cet - SEC_PER_HOUR;
89 }
90
94 inline ts_t eet_to_gmt(ts_t eet) {
95 return cet_to_gmt(eet - SEC_PER_HOUR);
96 }
97
101 inline ts_t gmt_to_cet(ts_t gmt) {
103 const int SWITCH_HOUR = 1;
104
105 if(dt.mon > MAR && dt.mon < OCT) {
106 return gmt + SEC_PER_HOUR * 2;
107 } else if(dt.mon == MAR) {
108 int last = last_sunday_month_day(dt.year, MAR);
109 if(dt.day > last) {
110 return gmt + SEC_PER_HOUR * 2;
111 } else if(dt.day < last) {
112 return gmt + SEC_PER_HOUR;
113 } else {
114 if(dt.hour >= SWITCH_HOUR)
115 return gmt + SEC_PER_HOUR * 2;
116 return gmt + SEC_PER_HOUR;
117 }
118 } else if(dt.mon == OCT) {
119 int last = last_sunday_month_day(dt.year, OCT);
120 if(dt.day > last) {
121 return gmt + SEC_PER_HOUR;
122 } else if(dt.day < last) {
123 return gmt + SEC_PER_HOUR * 2;
124 } else {
125 if(dt.hour >= SWITCH_HOUR)
126 return gmt + SEC_PER_HOUR;
127 return gmt + SEC_PER_HOUR * 2;
128 }
129 }
130 return gmt + SEC_PER_HOUR;
131 }
132
136 inline ts_t gmt_to_eet(ts_t gmt) {
137 return gmt_to_cet(gmt) + SEC_PER_HOUR;
138 }
139
141
142} // namespace time_shield
143
144#endif // _TIME_SHIELD_TIME_ZONE_CONVERSIONS_HPP_INCLUDED
Header for date and time structure and related functions.
constexpr int64_t SEC_PER_HOUR
Seconds per hour.
ts_t eet_to_gmt(ts_t eet)
Convert Eastern European Time to Greenwich Mean Time.
ts_t gmt_to_eet(ts_t gmt)
Convert Greenwich Mean Time to Eastern European Time.
ts_t cet_to_gmt(ts_t cet)
Convert Central European Time to Greenwich Mean Time.
ts_t gmt_to_cet(ts_t gmt)
Convert Greenwich Mean Time to Central European Time.
TIME_SHIELD_CONSTEXPR T1 last_sunday_month_day(T2 year, T3 month)
Get the day of the last Sunday of the given month and year.
constexpr T1 num_days_in_month(ts_t ts=time_shield::ts()) noexcept
Alias for num_days_in_month_ts function.
T1 to_date_time(T2 ts)
Converts a timestamp to a date-time structure.
constexpr T1 day_of_week_date(T2 year, T3 month, T4 day)
Get the day of the week.
@ OCT
October.
Definition enums.hpp:103
@ MAR
March.
Definition enums.hpp:96
@ SUN
Sunday.
Definition enums.hpp:28
int64_t ts_t
Unix timestamp in seconds since 1970‑01‑01T00:00:00Z.
Definition types.hpp:46
Main namespace for the Time Shield library.
Structure to represent date and time.
int hour
Hour component of time (0-23)
int64_t year
Year component of the date.
int day
Day component of the date (1-31).
int mon
Month component of the date (1-12).
Header file for time conversion functions.