Cross-platform time utilities.
More...
#include <time_utils.hpp>
|
| static std::tm * | gmtime_safe (const std::time_t *time, std::tm *result) |
| | Converts time_t to UTC tm struct (cross-platform).
|
| |
| static std::tm * | localtime_safe (const std::time_t *time, std::tm *result) |
| | Converts time_t to local tm struct (cross-platform).
|
| |
| static std::time_t | timegm_safe (std::tm *tm) |
| | Converts a UTC tm struct to time_t (cross-platform).
|
| |
| static std::chrono::system_clock::time_point | to_time_point_utc (std::tm tm) |
| | Converts a UTC tm struct directly to a system_clock::time_point.
|
| |
| static std::chrono::system_clock::time_point | to_time_point_local (std::tm tm) |
| | Converts a local-time tm struct directly to a system_clock::time_point.
|
| |
| static std::chrono::year_month_day | parse_date (std::string_view s) |
| | Parses a calendar date string in YYYYMMDD or YYYY-MM-DD format.
|
| |
| static std::string | format_date_compact (std::chrono::year_month_day date) |
| | Formats a calendar date as YYYYMMDD (e.g. "20160205").
|
| |
| static std::string | format_date_iso (std::chrono::year_month_day date) |
| | Formats a calendar date as YYYY-MM-DD (ISO 8601, e.g. "2016-02-05").
|
| |
| static std::chrono::system_clock::time_point | file_time_to_system_clock (std::filesystem::file_time_type ft) |
| | Converts a filesystem file_time_type to a system_clock::time_point.
|
| |
Cross-platform time utilities.
◆ gmtime_safe()
| std::tm * gmtime_safe |
( |
const std::time_t * |
time, |
|
|
std::tm * |
result |
|
) |
| |
|
static |
Converts time_t to UTC tm struct (cross-platform).
This is a cross-platform wrapper around gmtime_r (POSIX) and gmtime_s (Windows).
- Parameters
-
| time | The time_t value to convert. |
| result | Pointer to tm struct to store the result. |
- Returns
- Pointer to result on success, nullptr on failure.
◆ localtime_safe()
| std::tm * localtime_safe |
( |
const std::time_t * |
time, |
|
|
std::tm * |
result |
|
) |
| |
|
static |
Converts time_t to local tm struct (cross-platform).
This is a cross-platform wrapper around localtime_r (POSIX) and localtime_s (Windows).
- Parameters
-
| time | The time_t value to convert. |
| result | Pointer to tm struct to store the result. |
- Returns
- Pointer to result on success, nullptr on failure.
◆ timegm_safe()
| std::time_t timegm_safe |
( |
std::tm * |
tm | ) |
|
|
static |
Converts a UTC tm struct to time_t (cross-platform).
This is a cross-platform wrapper around timegm (POSIX) and _mkgmtime (Windows). Unlike std::mktime, this interprets the tm struct as UTC rather than local time.
- Parameters
-
| tm | Pointer to tm struct to convert (interpreted as UTC). |
- Returns
- The corresponding time_t value.
◆ to_time_point_utc()
| std::chrono::system_clock::time_point to_time_point_utc |
( |
std::tm |
tm | ) |
|
|
static |
◆ to_time_point_local()
| std::chrono::system_clock::time_point to_time_point_local |
( |
std::tm |
tm | ) |
|
|
static |
Converts a local-time tm struct directly to a system_clock::time_point.
Interprets the tm fields as the process's local timezone (via std::mktime), then converts to UTC. Use this when the source data is in the session/system timezone — e.g. a timestamp returned by libpq whose offset has been stripped by rfl::Timestamp::strptime.
- Parameters
-
| tm | The tm struct to convert (passed by value because mktime may normalise DST and other fields). |
- Returns
- The corresponding system_clock::time_point.
◆ parse_date()
| std::chrono::year_month_day parse_date |
( |
std::string_view |
s | ) |
|
|
static |
Parses a calendar date string in YYYYMMDD or YYYY-MM-DD format.
Uses std::from_chars for allocation-free, locale-independent parsing. Both formats are auto-detected from the string length and separators.
- Parameters
-
| s | The date string to parse. |
- Returns
- The corresponding year_month_day.
- Exceptions
-
| std::invalid_argument | if the format is unrecognised or the resulting date is not a valid calendar date. |
◆ format_date_compact()
| std::string format_date_compact |
( |
std::chrono::year_month_day |
date | ) |
|
|
static |
Formats a calendar date as YYYYMMDD (e.g. "20160205").
Uses manual integer formatting to avoid reliance on C++20 chrono format specifiers, which are not fully implemented on all platforms.
- Parameters
-
| date | The calendar date to format. |
- Returns
- Eight-character string in YYYYMMDD format.
◆ format_date_iso()
| std::string format_date_iso |
( |
std::chrono::year_month_day |
date | ) |
|
|
static |
Formats a calendar date as YYYY-MM-DD (ISO 8601, e.g. "2016-02-05").
Uses manual integer formatting to avoid reliance on C++20 chrono format specifiers, which are not fully implemented on all platforms.
- Parameters
-
| date | The calendar date to format. |
- Returns
- Ten-character string in YYYY-MM-DD format.
◆ file_time_to_system_clock()
| std::chrono::system_clock::time_point file_time_to_system_clock |
( |
std::filesystem::file_time_type |
ft | ) |
|
|
static |
Converts a filesystem file_time_type to a system_clock::time_point.
std::chrono::clock_cast is the C++20 standard way to do this but is not implemented on all platforms (notably Apple libc++). This uses the now()-based portable alternative that works on all supported platforms without making epoch assumptions.
- Parameters
-
| ft | The file time to convert. |
- Returns
- The equivalent system_clock::time_point.