3#ifndef TIME_SHIELD_HEADER_TIMEZONE_TIME_ZONE_CONVERSIONS_HPP_INCLUDED
4#define TIME_SHIELD_HEADER_TIMEZONE_TIME_ZONE_CONVERSIONS_HPP_INCLUDED
57 const int OLD_START_SUMMER_HOUR = 2;
58 const int OLD_STOP_SUMMER_HOUR = 3;
59 const int NEW_SUMMER_HOUR = 1;
65 for(
int d = max_days; d >= dt.
day; --d) {
68 if(dt.
hour >= OLD_START_SUMMER_HOUR) {
78 for(
int d = max_days; d >= dt.
day; --d) {
81 if(dt.
hour >= OLD_STOP_SUMMER_HOUR) {
98 for(
int d = max_days; d >= dt.
day; --d) {
101 if(dt.
hour >= (NEW_SUMMER_HOUR + 2)) {
112 for(
int d = max_days; d >= dt.
day; --d) {
115 if(dt.
hour >= (NEW_SUMMER_HOUR + 1)) {
129 const int SWITCH_HOUR = 1;
142 if(dt.
hour >= SWITCH_HOUR) {
155 if(dt.
hour >= SWITCH_HOUR) {
172 const int SWITCH_HOUR = 2;
178 if(dt.
year >= 2007) {
181 int first_sunday_march =
static_cast<int>(
183 start_day = first_sunday_march + 7;
184 end_day =
static_cast<int>(
189 start_day =
static_cast<int>(
194 if(dt.
mon > start_month && dt.
mon < end_month) {
197 if(dt.
mon < start_month || dt.
mon > end_month) {
200 if(dt.
mon == start_month) {
201 if(dt.
day > start_day) {
204 if(dt.
day < start_day) {
207 return dt.
hour >= SWITCH_HOUR;
209 if(dt.
mon == end_month) {
210 if(dt.
day < end_day) {
213 if(dt.
day > end_day) {
216 return dt.
hour < SWITCH_HOUR;
412 return to_utc(local, utc_offset);
421 return to_utc(local, utc_offset);
570 tz_t& second_offset) {
617 return zone ==
WET || zone ==
CET || zone ==
EET;
621 return zone ==
ET || zone ==
CT;
632 return utc_ms >= start_ms && utc_ms < end_ms;
636 return static_cast<int>(
643 const int year =
static_cast<int>(dt.
year);
644 int start_month =
MAR;
657 const tz_t daylight_offset =
665 return utc_ms >= start_utc && utc_ms < end_utc;
673 const tz_t expected =
674 static_cast<tz_t>(standard_offset +
678 return offset == expected;
683 const tz_t expected =
684 static_cast<tz_t>(standard_offset +
688 return offset == expected;
721 tz_t second_offset) {
760 switch(ambiguous_policy) {
783 tz_t& out)
noexcept {
788 if(zone ==
GMT || zone ==
UTC) {
795 out =
static_cast<tz_t>(standard_offset +
804 out =
static_cast<tz_t>(standard_offset +
827 tz_t& out)
noexcept {
844 if(zone ==
GMT || zone ==
UTC) {
849 tz_t first_offset = 0;
850 tz_t second_offset = 0;
884 const bool forward = direction >= 0;
885 ts_ms_t low = forward ? local_ms : local_ms - window;
886 ts_ms_t high = forward ? local_ms + window : local_ms;
894 while(high - low > 1) {
895 const ts_ms_t mid = low + (high - low) / 2;
929 switch(nonexistent_policy) {
1006 nonexistent_policy);
constexpr int64_t ERROR_TIMESTAMP
Error timestamp value.
constexpr int64_t DAYS_PER_WEEK
Days per week.
constexpr int64_t SEC_PER_HOUR
Seconds per hour.
constexpr int64_t MS_PER_DAY
Milliseconds per day.
constexpr int64_t SEC_PER_MIN
Seconds per minute.
TIME_SHIELD_CONSTEXPR ts_t to_utc(ts_t local, tz_t utc_offset) noexcept
Convert local timestamp (seconds) to UTC using UTC offset.
TIME_SHIELD_CONSTEXPR ts_t to_local(ts_t utc, tz_t utc_offset) noexcept
Convert UTC timestamp (seconds) to local time using UTC offset.
TIME_SHIELD_CONSTEXPR ts_ms_t to_local_ms(ts_ms_t utc_ms, tz_t utc_offset) noexcept
Convert UTC timestamp (milliseconds) to local time using UTC offset.
ts_t zone_to_gmt(ts_t local, TimeZone zone)
Convert supported local civil time to GMT (UTC).
TIME_SHIELD_CONSTEXPR ts_ms_t to_utc_ms(ts_ms_t local_ms, tz_t utc_offset) noexcept
Convert local timestamp (milliseconds) to UTC using UTC offset.
TIME_SHIELD_CONSTEXPR T1 ms_to_sec(T2 ts_ms) noexcept
Converts a timestamp from milliseconds to seconds.
TIME_SHIELD_CONSTEXPR T1 day_of_week_date(T2 year, T3 month, T4 day)
Get the day of the week.
TIME_SHIELD_CONSTEXPR T year(ts_t ts=time_shield::ts())
Alias for year_of function.
TIME_SHIELD_CONSTEXPR T1 sec_to_ms(T2 ts) noexcept
Converts a timestamp from seconds to milliseconds.
@ JST
Japan Standard Time.
@ KST
Korea Standard Time.
@ EET
Eastern European Time.
@ CEST
Central European Summer Time.
@ WIT
Eastern Indonesia Time.
@ WEST
Western European Summer Time.
@ WITA
Central Indonesia Time.
@ WET
Western European Time.
@ UNKNOWN
Unknown Time Zone.
@ UTC
Coordinated Universal Time.
@ GMT
Greenwich Mean Time.
@ WIB
Western Indonesia Time.
@ EEST
Eastern European Summer Time.
@ CET
Central European Time.
@ IST
India Standard 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.
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.
TIME_SHIELD_CONSTEXPR ts_ms_t to_timestamp_ms(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0, T2 ms=0)
Converts a date-time structure to a timestamp in milliseconds.
int64_t ts_t
Unix timestamp in seconds since 1970‑01‑01T00:00:00Z.
int32_t tz_t
Time zone offset in minutes from UTC (e.g., +180 = UTC+3).
int64_t ts_ms_t
Unix timestamp in milliseconds since epoch.
void add_local_time_candidate(LocalTimeResolution &result, ts_ms_t local_ms, TimeZone zone, tz_t offset)
bool is_us_dynamic_zone(TimeZone zone)
bool local_time_status_has_utc(LocalTimeStatus status)
bool fixed_zone_offset(TimeZone zone, tz_t &utc_offset)
tz_t dynamic_zone_standard_offset(TimeZone zone)
ts_t gmt_to_cet_impl(ts_t gmt)
LocalTimeResolution resolve_with_dynamic_offsets(ts_ms_t local_ms, TimeZone zone, tz_t first_offset, tz_t second_offset)
LocalTimeResolution make_local_time_resolution(LocalTimeStatus status, ts_ms_t first_utc_ms=ERROR_TIMESTAMP, ts_ms_t second_utc_ms=ERROR_TIMESTAMP)
ts_t cet_to_gmt_impl(ts_t cet)
int first_sunday_month_day(int year, int month)
ts_t gmt_to_european_local(ts_t gmt, int standard_offset_hours)
bool is_us_eastern_dst_local(const DateTimeStruct &dt)
ts_ms_t gmt_to_zone_ms_by_seconds(ts_ms_t gmt_ms, TimeZone zone)
bool european_dst_at_utc_ms(ts_ms_t utc_ms)
bool dynamic_dst_zone_offsets(TimeZone zone, tz_t &first_offset, tz_t &second_offset)
bool is_european_dynamic_zone(TimeZone zone)
ts_ms_t local_time_resolution_to_utc(const LocalTimeResolution &resolution, AmbiguousTimePolicy ambiguous_policy)
ts_ms_t shift_nonexistent_local_time_ms(ts_ms_t local_ms, TimeZone zone, int direction)
ts_t european_local_to_gmt(ts_t local, int standard_offset_hours)
bool dynamic_offset_applies_at_utc_ms(TimeZone zone, ts_ms_t utc_ms, tz_t offset)
ts_ms_t zone_to_gmt_ms_by_seconds(ts_ms_t local_ms, TimeZone zone)
bool us_dst_at_utc_ms(TimeZone zone, ts_ms_t utc_ms)
Main namespace for the Time Shield library.
ts_t gmt_to_et(ts_t gmt)
Convert GMT (UTC) to US Eastern Time (New York, EST/EDT).
ts_t kzt_to_gmt(ts_t kzt)
ts_t eet_to_gmt(ts_t eet)
Convert Eastern European Time to Greenwich Mean Time.
ts_t gmt_to_gst(ts_t gmt)
ts_t gmt_to_hkt(ts_t gmt)
ts_t gmt_to_eet(ts_t gmt)
Convert Greenwich Mean Time to Eastern European Time.
ts_t gst_to_gmt(ts_t gst)
ts_t byt_to_gmt(ts_t byt)
ts_ms_t zone_to_gmt_ms(ts_ms_t local_ms, TimeZone zone)
Convert supported local civil time in milliseconds to GMT (UTC).
ts_t gmt_to_kyiv(ts_t gmt)
Convert GMT to Kyiv civil time using the EET/EEST rules.
ts_t gmt_to_ist(ts_t gmt)
ts_t ct_to_gmt(ts_t ct)
Convert US Central Time (America/Chicago, CST/CDT) to GMT (UTC).
ts_t gmt_to_jst(ts_t gmt)
bool is_us_eastern_dst_local(const DateTimeStruct &dt)
Check if local US Eastern time uses DST.
ts_t ict_to_gmt(ts_t ict)
ts_t gmt_to_wit(ts_t gmt)
ts_t gmt_to_kzt(ts_t gmt)
ts_ms_t zone_to_gmt_ms_strict(ts_ms_t local_ms, TimeZone zone)
Convert only unambiguous existing local time to UTC.
ts_t kyiv_to_gmt(ts_t kyiv)
Convert Kyiv civil time to GMT using the EET/EEST rules.
ts_t wita_to_gmt(ts_t wita)
ts_t cet_to_gmt(ts_t cet)
Convert Central European Time to Greenwich Mean Time.
ts_t convert_time_zone(ts_t local, TimeZone from, TimeZone to)
Convert a timestamp between two supported local civil time zones.
LocalTimeResolution resolve_local_time(ts_t local, TimeZone zone)
Resolve local civil time given in seconds.
ts_t gmt_to_byt(ts_t gmt)
ts_ms_t gmt_to_zone_ms(ts_ms_t gmt_ms, TimeZone zone)
Convert GMT (UTC) in milliseconds to a supported local civil time zone.
ts_t gmt_to_pht(ts_t gmt)
LocalTimeStatus
Classification of a local civil timestamp in a time zone.
@ ambiguous
Local time maps to more than one UTC timestamp.
@ nonexistent
Local time falls into a DST forward gap.
@ unsupported
Zone or timestamp cannot be resolved.
@ valid
Local time maps to exactly one UTC timestamp.
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 gmt_to_sgt(ts_t gmt)
ts_t kst_to_gmt(ts_t kst)
bool zone_offset_at_utc_ms(ts_ms_t utc_ms, TimeZone zone, tz_t &out) noexcept
Resolve the effective UTC offset for a UTC millisecond instant.
ts_t gmt_to_trt(ts_t gmt)
ts_t wit_to_gmt(ts_t wit)
ts_t hkt_to_gmt(ts_t hkt)
ts_t gmt_to_ict(ts_t gmt)
bool zone_offset_at_utc(ts_t utc, TimeZone zone, tz_t &out) noexcept
Resolve the effective UTC offset for a UTC second instant.
ts_t ist_to_gmt(ts_t ist)
ts_t gmt_to_wib(ts_t gmt)
ts_t gmt_to_myt(ts_t gmt)
ts_t wib_to_gmt(ts_t wib)
ts_t trt_to_gmt(ts_t trt)
ts_t gmt_to_zone(ts_t gmt, TimeZone zone)
Convert GMT (UTC) to a supported local civil time zone.
ts_t pht_to_gmt(ts_t pht)
AmbiguousTimePolicy
Policy for ambiguous local civil timestamps.
@ second_occurrence
Use the latest UTC occurrence.
@ first_occurrence
Use the earliest UTC occurrence.
@ error
Return ERROR_TIMESTAMP.
ts_t sgt_to_gmt(ts_t sgt)
ts_t zone_to_gmt_strict(ts_t local, TimeZone zone)
Convert only unambiguous existing local time in seconds to UTC.
ts_t ny_to_gmt(ts_t ny)
Convert New York Time to GMT (UTC).
ts_t gmt_to_wita(ts_t gmt)
ts_t et_to_gmt(ts_t et)
Convert US Eastern Time (New York, EST/EDT) to GMT (UTC).
LocalTimeResolution resolve_local_time_ms(ts_ms_t local_ms, TimeZone zone)
Resolve local civil time to zero, one, or two UTC candidates.
ts_ms_t convert_time_zone_ms(ts_ms_t local_ms, TimeZone from, TimeZone to)
Convert a millisecond timestamp between two supported local civil time zones.
ts_t gmt_to_ct(ts_t gmt)
Convert GMT (UTC) to US Central Time (America/Chicago, CST/CDT).
NonexistentTimePolicy
Policy for nonexistent local civil timestamps.
@ shift_forward
Use the earliest valid local instant after the gap.
@ shift_backward
Use the latest valid local instant before the gap.
@ error
Return ERROR_TIMESTAMP.
ts_t gmt_to_kst(ts_t gmt)
ts_t jst_to_gmt(ts_t jst)
ts_t myt_to_gmt(ts_t myt)
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).
Result of explicit local-time resolution.