ORE Studio 0.0.4
Loading...
Searching...
No Matches
Static Public Member Functions | List of all members
time_utils Class Referencefinal

Cross-platform time utilities. More...

#include <time_utils.hpp>

Collaboration diagram for time_utils:
Collaboration graph

Static Public Member Functions

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.
 

Detailed Description

Cross-platform time utilities.

Member Function Documentation

◆ 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
timeThe time_t value to convert.
resultPointer to tm struct to store the result.
Returns
Pointer to result on success, nullptr on failure.
Here is the caller graph for this function:

◆ 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
timeThe time_t value to convert.
resultPointer to tm struct to store the result.
Returns
Pointer to result on success, nullptr on failure.
Here is the caller graph for this function:

◆ 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
tmPointer to tm struct to convert (interpreted as UTC).
Returns
The corresponding time_t value.
Here is the caller graph for this function:

◆ to_time_point_utc()

std::chrono::system_clock::time_point to_time_point_utc ( std::tm  tm)
static

Converts a UTC tm struct directly to a system_clock::time_point.

Interprets the tm fields as UTC. Use this when the source data is known to be in UTC (e.g. a value already normalised to UTC before storing in tm).

Parameters
tmThe tm struct to convert (interpreted as UTC, passed by value because timegm may normalise the fields).
Returns
The corresponding system_clock::time_point.
Examples
/home/runner/work/OreStudio/OreStudio/projects/ores.database/include/ores.database/repository/mapper_helpers.hpp.
Here is the caller graph for this function:

◆ 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
tmThe 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
sThe date string to parse.
Returns
The corresponding year_month_day.
Exceptions
std::invalid_argumentif 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
dateThe 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
dateThe 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
ftThe file time to convert.
Returns
The equivalent system_clock::time_point.