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
102 const int SWITCH_HOUR = 2;
103 int start_day = 0;
104 int end_day = 0;
105 int start_month = 0;
106 int end_month = 0;
107
108 if(dt.year >= 2007) {
109 start_month = MAR;
110 end_month = NOV;
111 int first_sunday_march = static_cast<int>(
113 start_day = first_sunday_march + 7;
114 end_day = static_cast<int>(
116 } else {
117 start_month = APR;
118 end_month = OCT;
119 start_day = static_cast<int>(
121 end_day = last_sunday_month_day(dt.year, OCT);
122 }
123
124 if(dt.mon > start_month && dt.mon < end_month) {
125 return true;
126 }
127 if(dt.mon < start_month || dt.mon > end_month) {
128 return false;
129 }
130 if(dt.mon == start_month) {
131 if(dt.day > start_day) {
132 return true;
133 }
134 if(dt.day < start_day) {
135 return false;
136 }
137 return dt.hour >= SWITCH_HOUR;
138 }
139 if(dt.mon == end_month) {
140 if(dt.day < end_day) {
141 return true;
142 }
143 if(dt.day > end_day) {
144 return false;
145 }
146 return dt.hour < SWITCH_HOUR;
147 }
148 return false;
149 }
150
157 inline ts_t et_to_gmt(ts_t et) {
159 bool is_dst = is_us_eastern_dst_local(dt);
160 return et + SEC_PER_HOUR * (is_dst ? 4 : 5);
161 }
162
169 inline ts_t gmt_to_et(ts_t gmt) {
170 ts_t et_standard = gmt - SEC_PER_HOUR * 5;
171 DateTimeStruct dt_local = to_date_time(et_standard);
172 bool is_dst = is_us_eastern_dst_local(dt_local);
173 return gmt - SEC_PER_HOUR * (is_dst ? 4 : 5);
174 }
175
179 inline ts_t ny_to_gmt(ts_t ny) {
180 return et_to_gmt(ny);
181 }
182
186 inline ts_t gmt_to_ny(ts_t gmt) {
187 return gmt_to_et(gmt);
188 }
189
193 inline ts_t ct_to_gmt(ts_t ct) {
194 return et_to_gmt(ct + SEC_PER_HOUR);
195 }
196
200 inline ts_t gmt_to_ct(ts_t gmt) {
201 return gmt_to_et(gmt) - SEC_PER_HOUR;
202 }
203
207 inline ts_t gmt_to_cet(ts_t gmt) {
209 const int SWITCH_HOUR = 1;
210
211 if(dt.mon > MAR && dt.mon < OCT) {
212 return gmt + SEC_PER_HOUR * 2;
213 } else if(dt.mon == MAR) {
214 int last = last_sunday_month_day(dt.year, MAR);
215 if(dt.day > last) {
216 return gmt + SEC_PER_HOUR * 2;
217 } else if(dt.day < last) {
218 return gmt + SEC_PER_HOUR;
219 } else {
220 if(dt.hour >= SWITCH_HOUR)
221 return gmt + SEC_PER_HOUR * 2;
222 return gmt + SEC_PER_HOUR;
223 }
224 } else if(dt.mon == OCT) {
225 int last = last_sunday_month_day(dt.year, OCT);
226 if(dt.day > last) {
227 return gmt + SEC_PER_HOUR;
228 } else if(dt.day < last) {
229 return gmt + SEC_PER_HOUR * 2;
230 } else {
231 if(dt.hour >= SWITCH_HOUR)
232 return gmt + SEC_PER_HOUR;
233 return gmt + SEC_PER_HOUR * 2;
234 }
235 }
236 return gmt + SEC_PER_HOUR;
237 }
238
242 inline ts_t gmt_to_eet(ts_t gmt) {
243 return gmt_to_cet(gmt) + SEC_PER_HOUR;
244 }
245
247
248} // namespace time_shield
249
250#endif // _TIME_SHIELD_TIME_ZONE_CONVERSIONS_HPP_INCLUDED
Header for date and time structure and related functions.
constexpr int64_t DAYS_PER_WEEK
Days per week.
constexpr int64_t SEC_PER_HOUR
Seconds per hour.
ts_t gmt_to_et(ts_t gmt)
Convert GMT (UTC) to US Eastern Time (New York, EST/EDT).
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 ct_to_gmt(ts_t ct)
Convert US Central Time (America/Chicago, CST/CDT) to GMT (UTC).
bool is_us_eastern_dst_local(const DateTimeStruct &dt)
Check if local US Eastern time uses DST.
ts_t cet_to_gmt(ts_t cet)
Convert Central European Time to Greenwich Mean Time.
ts_t gmt_to_ny(ts_t gmt)
Convert GMT (UTC) to New York Time.
ts_t gmt_to_cet(ts_t gmt)
Convert Greenwich Mean Time to Central European Time.
ts_t ny_to_gmt(ts_t ny)
Convert New York Time to GMT (UTC).
ts_t et_to_gmt(ts_t et)
Convert US Eastern Time (New York, EST/EDT) to GMT (UTC).
ts_t gmt_to_ct(ts_t gmt)
Convert GMT (UTC) to US Central Time (America/Chicago, CST/CDT).
TIME_SHIELD_CONSTEXPR T1 day_of_week_date(T2 year, T3 month, T4 day)
Get the day of the week.
@ OCT
October.
Definition enums.hpp:103
@ NOV
November.
Definition enums.hpp:104
@ MAR
March.
Definition enums.hpp:96
@ APR
April.
Definition enums.hpp:97
@ SUN
Sunday.
Definition enums.hpp:28
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.
T1 to_date_time(T2 ts)
Converts a timestamp to a date-time structure.
TIME_SHIELD_CONSTEXPR T1 num_days_in_month(T2 year, T3 month) noexcept
Get the number of days in a month.
int64_t ts_t
Unix timestamp in seconds since 1970‑01‑01T00:00:00Z.
Definition types.hpp:49
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).
Umbrella header for time conversion functions.