3#ifndef _TIME_SHIELD_ZONED_CLOCK_HPP_INCLUDED
4#define _TIME_SHIELD_ZONED_CLOCK_HPP_INCLUDED
18#if TIME_SHIELD_ENABLE_NTP_CLIENT
63 throw std::invalid_argument(
"Invalid UTC offset");
113 const std::string trimmed =
trim_ascii(zone_spec);
114 if (trimmed.empty()) {
155#if TIME_SHIELD_ENABLE_NTP_CLIENT
322 std::string
format(
const std::string& fmt)
const {
328 std::size_t begin = 0;
329 std::size_t end = value.size();
336 return value.substr(begin, end - begin);
340 return ch ==
' ' || ch ==
'\t' || ch ==
'\n' || ch ==
'\r' || ch ==
'\f' || ch ==
'\v';
352#if TIME_SHIELD_ENABLE_NTP_CLIENT
Value-type wrapper for timestamps with fixed UTC offset.
Represents a moment in time with optional fixed UTC offset.
std::string to_iso8601() const
Format to ISO8601 string with stored offset.
std::string format(const std::string &fmt) const
Format using custom pattern.
static DateTime from_unix_ms(ts_ms_t utc_ms, tz_t offset=0) noexcept
Create instance from UTC milliseconds.
std::string to_iso8601_utc() const
Format to ISO8601 string in UTC.
bool running() const noexcept
Return true when background runner is active.
static NtpTimeServiceT & instance() noexcept
tz_t offset_at_utc_ms(ts_ms_t utc_ms) const noexcept
Return effective UTC offset in seconds for a specific UTC instant.
static bool is_ascii_space(char ch) noexcept
LocalTimeResolution resolve_local_time_ms(ts_ms_t local_ms) const noexcept
Resolve a local timestamp in this clock's zone.
std::string zone_full_name() const
Return human-readable zone label.
ts_us_t local_time_us() const noexcept
Return current local timestamp in microseconds.
std::string to_iso8601() const
Return current local time formatted as ISO8601 with offset.
DateTime from_utc_s(ts_t utc_s) const noexcept
Return a snapshot for a specific UTC instant in seconds.
ts_t local_time_sec() const noexcept
Return current local timestamp in seconds.
bool try_offset_at_utc_ms(ts_ms_t utc_ms, tz_t &out) const noexcept
Try to resolve effective UTC offset for a UTC instant.
bool ntp_active() const noexcept
Return true when the global NTP service is active for this clock.
static std::string trim_ascii(const std::string &value)
bool try_set_offset(tz_t utc_offset) noexcept
Set the stored fixed UTC offset.
ts_ms_t current_utc_ms() const noexcept
DateTime now() const noexcept
Return current time snapshot with resolved fixed offset.
bool use_ntp() const noexcept
Return the preferred UTC source flag.
bool try_set_zone(const std::string &zone_spec) noexcept
Parse and set a named zone or numeric offset from string.
std::string to_iso8601_utc() const
Return current UTC time formatted as ISO8601 with Z.
static bool try_from_offset(tz_t utc_offset, ZonedClock &out) noexcept
Try to build fixed-offset clock without throwing.
DateTime from_utc_ms(ts_ms_t utc_ms) const noexcept
Return a snapshot for a specific UTC instant in milliseconds.
std::string zone_name() const
Return short name of the stored named zone.
tz_t offset_now() const noexcept
Return effective UTC offset in seconds for the current UTC instant.
std::string format(const std::string &fmt) const
Format current local time using the custom formatter grammar.
ts_ms_t to_utc_ms(ts_ms_t local_ms, AmbiguousTimePolicy ambiguous_policy=AmbiguousTimePolicy::error, NonexistentTimePolicy nonexistent_policy=NonexistentTimePolicy::error) const noexcept
Convert a local timestamp in this clock's zone to UTC.
std::string offset_string() const
Return effective numeric UTC offset as +HH:MM or -HH:MM.
ZonedClock(TimeZone zone, bool use_ntp=false) noexcept
Construct clock for a named zone.
static std::string offset_string_for_offset(tz_t utc_offset)
ZonedClock(tz_t utc_offset, bool use_ntp=false)
Construct clock for a fixed UTC offset.
ts_ms_t local_time_ms() const noexcept
Return current local timestamp in milliseconds.
void set_use_ntp(bool use_ntp) noexcept
Set preferred UTC source.
ts_ms_t utc_time_ms() const noexcept
Return current UTC time in milliseconds.
ts_t utc_time_sec() const noexcept
Return current UTC time in seconds.
void set_zone(TimeZone zone) noexcept
Set the stored named zone.
TimeZone zone() const noexcept
Return stored named zone or UNKNOWN for fixed-offset mode.
bool has_named_zone() const noexcept
Return true when the instance stores a named zone.
ZonedClock() noexcept
Construct UTC fixed-offset clock without NTP.
ts_us_t utc_time_us() const noexcept
Return current UTC time in microseconds.
ts_us_t current_utc_us() const noexcept
Configuration macros for the library.
Header file with time-related constants.
Header file with enumerations for weekdays, months, and other time-related categories.
bool init(std::chrono::milliseconds interval=std::chrono::seconds(30), bool measure_immediately=true)
Initialize NTP time service and start background measurements.
int64_t utc_time_us() noexcept
Return current UTC time in microseconds based on offset.
constexpr int64_t ERROR_TIMESTAMP
Error timestamp value.
constexpr int64_t MS_PER_SEC
Milliseconds per second.
constexpr int64_t US_PER_SEC
Microseconds per second.
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 bool is_valid_tz_offset(tz_t off) noexcept
Check if a numeric offset is within supported bounds.
const std::string & to_str(Weekday value, FormatType format=UPPERCASE_NAME)
Converts a Weekday enum value to a string.
const char * to_cstr(Weekday value, FormatType format=UPPERCASE_NAME)
Converts a Weekday enum value to a string.
@ UNKNOWN
Unknown Time Zone.
bool parse_time_zone_name(const char *data, std::size_t length, TimeZone &zone) noexcept
Parse named time zone character buffer into TimeZone enum.
bool parse_time_zone(const char *data, std::size_t length, TimeZoneStruct &tz) noexcept
Parse timezone character buffer into TimeZoneStruct.
TIME_SHIELD_CONSTEXPR tz_t time_zone_struct_to_offset(const TimeZoneStruct &tz) noexcept
Convert a TimeZoneStruct to a numeric UTC offset (seconds).
TimeZoneStruct to_time_zone_struct(tz_t offset)
Converts an integer to a TimeZoneStruct.
TimeZoneStruct create_time_zone_struct(int hour, int min, bool is_positive=true)
Creates a TimeZoneStruct instance.
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.
int64_t ts_us_t
Unix timestamp in microseconds since epoch.
int64_t now_realtime_us()
Get current real time in microseconds using a platform-specific method.
Main namespace for the Time Shield library.
ts_ms_t zone_to_gmt_ms(ts_ms_t local_ms, TimeZone zone)
Convert supported local civil time in milliseconds to GMT (UTC).
@ unsupported
Zone or timestamp cannot be resolved.
@ valid
Local time maps to exactly one UTC timestamp.
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.
AmbiguousTimePolicy
Policy for ambiguous local civil timestamps.
@ error
Return ERROR_TIMESTAMP.
LocalTimeResolution resolve_local_time_ms(ts_ms_t local_ms, TimeZone zone)
Resolve local civil time to zero, one, or two UTC candidates.
NonexistentTimePolicy
Policy for nonexistent local civil timestamps.
@ error
Return ERROR_TIMESTAMP.
Result of explicit local-time resolution.
Structure to represent time zone information.
Header file with functions for parsing dates and times in ISO8601 format and converting them to vario...
Header file with time-related utility functions.
Helpers for converting supported regional time zones and UTC.
Conversions between numeric offsets and TimeZoneStruct.