3#ifndef _TIME_SHIELD_TIME_ZONE_CONVERSIONS_HPP_INCLUDED
4#define _TIME_SHIELD_TIME_ZONE_CONVERSIONS_HPP_INCLUDED
59 const int OLD_START_SUMMER_HOUR = 2;
60 const int OLD_STOP_SUMMER_HOUR = 3;
61 const int NEW_SUMMER_HOUR = 1;
67 for(
int d = max_days; d >= dt.
day; --d) {
70 if(dt.
hour >= OLD_START_SUMMER_HOUR) {
80 for(
int d = max_days; d >= dt.
day; --d) {
83 if(dt.
hour >= OLD_STOP_SUMMER_HOUR) {
100 for(
int d = max_days; d >= dt.
day; --d) {
103 if(dt.
hour >= (NEW_SUMMER_HOUR + 2)) {
114 for(
int d = max_days; d >= dt.
day; --d) {
117 if(dt.
hour >= (NEW_SUMMER_HOUR + 1)) {
131 const int SWITCH_HOUR = 1;
144 if(dt.
hour >= SWITCH_HOUR) {
157 if(dt.
hour >= SWITCH_HOUR) {
174 const int SWITCH_HOUR = 2;
180 if(dt.
year >= 2007) {
183 int first_sunday_march =
static_cast<int>(
185 start_day = first_sunday_march + 7;
186 end_day =
static_cast<int>(
191 start_day =
static_cast<int>(
196 if(dt.
mon > start_month && dt.
mon < end_month) {
199 if(dt.
mon < start_month || dt.
mon > end_month) {
202 if(dt.
mon == start_month) {
203 if(dt.
day > start_day) {
206 if(dt.
day < start_day) {
209 return dt.
hour >= SWITCH_HOUR;
211 if(dt.
mon == end_month) {
212 if(dt.
day < end_day) {
215 if(dt.
day > end_day) {
218 return dt.
hour < SWITCH_HOUR;
414 return to_utc(local, utc_offset);
423 return to_utc(local, utc_offset);
572 tz_t& second_offset) {
619 return zone ==
WET || zone ==
CET || zone ==
EET;
623 return zone ==
ET || zone ==
CT;
634 return utc_ms >= start_ms && utc_ms < end_ms;
638 return static_cast<int>(
645 const int year =
static_cast<int>(dt.
year);
646 int start_month =
MAR;
659 const tz_t daylight_offset =
667 return utc_ms >= start_utc && utc_ms < end_utc;
675 const tz_t expected =
676 static_cast<tz_t>(standard_offset +
680 return offset == expected;
685 const tz_t expected =
686 static_cast<tz_t>(standard_offset +
690 return offset == expected;
723 tz_t second_offset) {
762 switch(ambiguous_policy) {
785 tz_t& out)
noexcept {
790 if(zone ==
GMT || zone ==
UTC) {
797 out =
static_cast<tz_t>(standard_offset +
806 out =
static_cast<tz_t>(standard_offset +
829 tz_t& out)
noexcept {
846 if(zone ==
GMT || zone ==
UTC) {
851 tz_t first_offset = 0;
852 tz_t second_offset = 0;
886 const bool forward = direction >= 0;
887 ts_ms_t low = forward ? local_ms : local_ms - window;
888 ts_ms_t high = forward ? local_ms + window : local_ms;
896 while(high - low > 1) {
897 const ts_ms_t mid = low + (high - low) / 2;
931 switch(nonexistent_policy) {
1008 nonexistent_policy);
Header for date and time structure and related functions.
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.
Umbrella header for time conversion functions.
Helper functions for unit conversions between seconds, minutes, hours, and milliseconds.
UTC offset arithmetic helpers (UTC <-> local) and TimeZoneStruct offset extraction.