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#pragma once
2#ifndef _TIME_SHIELD_TIME_ZONE_CONVERSIONS_HPP_INCLUDED
3#define _TIME_SHIELD_TIME_ZONE_CONVERSIONS_HPP_INCLUDED
4
7
9
10namespace time_shield {
11
16 inline const ts_t cet_to_gmt(ts_t cet) {
18 int max_days = num_days_in_month(dt.year, dt.mon);
19 const int OLD_START_SUMMER_HOUR = 2;
20 const int OLD_STOP_SUMMER_HOUR = 3;
21 const int NEW_SUMMER_HOUR = 1;
22
23 if(dt.year < 2002) {
24 if(dt.mon > MAR && dt.mon < OCT)
25 return cet - SEC_PER_HOUR * 2;
26 else if(dt.mon == MAR) {
27 for(int d = max_days; d >= dt.day; --d) {
28 if(day_of_week_date(dt.year, MAR, d) == SUN) {
29 if(d == dt.day) {
30 if(dt.hour >= OLD_START_SUMMER_HOUR)
31 return cet - SEC_PER_HOUR * 2;
32 return cet - SEC_PER_HOUR;
33 }
34 return cet - SEC_PER_HOUR;
35 }
36 }
37 return cet - SEC_PER_HOUR * 2;
38 } else if(dt.mon == OCT) {
39 for(int d = max_days; d >= dt.day; --d) {
40 if(day_of_week_date(dt.year, OCT, d) == SUN) {
41 if(d == dt.day) {
42 if(dt.hour >= OLD_STOP_SUMMER_HOUR)
43 return cet - SEC_PER_HOUR;
44 return cet - SEC_PER_HOUR;
45 }
46 return cet - SEC_PER_HOUR * 2;
47 }
48 }
49 return cet - SEC_PER_HOUR;
50 }
51 return cet - SEC_PER_HOUR;
52 } else {
53 if(dt.mon > MAR && dt.mon < OCT)
54 return cet - SEC_PER_HOUR * 2;
55 else if(dt.mon == MAR) {
56 for(int d = max_days; d >= dt.day; --d) {
57 if(day_of_week_date(dt.year, MAR, d) == SUN) {
58 if(d == dt.day) {
59 if(dt.hour >= (NEW_SUMMER_HOUR + 2))
60 return cet - SEC_PER_HOUR * 2;
61 return cet - SEC_PER_HOUR;
62 }
63 return cet - SEC_PER_HOUR;
64 }
65 }
66 return cet - SEC_PER_HOUR * 2;
67 } else if(dt.mon == OCT) {
68 for(int d = max_days; d >= dt.day; --d) {
69 if(day_of_week_date(dt.year, OCT, d) == SUN) {
70 if(d == dt.day) {
71 if(dt.hour >= (NEW_SUMMER_HOUR + 1))
72 return cet - SEC_PER_HOUR;
73 return cet - SEC_PER_HOUR * 2;
74 }
75 return cet - SEC_PER_HOUR * 2;
76 }
77 }
78 return cet - SEC_PER_HOUR;
79 }
80 return cet - SEC_PER_HOUR;
81 }
82 return cet - SEC_PER_HOUR;
83 }
84
89 inline const ts_t eet_to_gmt(ts_t eet) {
90 return cet_to_gmt(eet - SEC_PER_HOUR);
91 }
92
93} // namespace time_shield
94
95#endif // _TIME_SHIELD_TIME_ZONE_CONVERSIONS_HPP_INCLUDED
constexpr int64_t SEC_PER_HOUR
Seconds per hour.
Definition constants.hpp:69
constexpr const T1 num_days_in_month(T2 year, T3 month) noexcept
Get the number of days in a month.
const ts_t eet_to_gmt(ts_t eet)
Convert Eastern European Time to Greenwich Mean Time.
const ts_t cet_to_gmt(ts_t cet)
Convert Central European Time to Greenwich Mean Time.
constexpr const T1 day_of_week_date(T2 year, T3 month, T3 day)
Get the day of the week.
@ OCT
October.
Definition enums.hpp:114
@ MAR
March.
Definition enums.hpp:107
@ SUN
Sunday.
Definition enums.hpp:42
T1 to_date_time(T2 ts)
Converts a timestamp to a date-time structure.
int64_t ts_t
Type for representing timestamps in seconds.
Definition types.hpp:34
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.