UTILLib 2.0
Common C++ library with utilities.
Loading...
Searching...
No Matches
util::time Namespace Reference

The time namespace is used for time and date functions. More...

Typedefs

using SystemClock = std::chrono::system_clock
 Convenience type definition for std::chrono::system_clock;.
using TimeStamp = std::chrono::time_point<SystemClock>
 Convenience type definition for std::chrono::time_point<std::chrono::system_time>;.

Functions

std::string GetLocalDateTime (TimeStamp timestamp=SystemClock::now())
 Converts a timestamp (UTC) to a local date and time string in the YYYY-MM-DD hh:mm:ss format.
std::string GetLocalTimestampWithMs (TimeStamp timestamp=SystemClock::now())
 Converts a timestamp (UTC) to a local dat and time string in the YYYY-MM-DD hh:mm:ss.ms format.
std::string GetLocalTimestampWithUs (TimeStamp timestamp=SystemClock::now())
 Converts a UTC timestamp to a local date and time string in the YYYY-MM-DD hh:mm:ss.us.
std::string NsToLocalIsoTime (uint64_t ns_since_1970)
 Converts a nanosecond since 1970 timestamp to a local ISO date and time string.
std::string NsToIsoTime (uint64_t ns_since_1970, int format=0)
 Converts a nanosecond since 1970 timestamp to a UTC ISO date and time string.
uint64_t IsoTimeToNs (const std::string &iso_time, bool local_time=false)
 Converts an ISO UTC string (YYYY-MM-DD(T)hh:mm:ss to nanosecond since 1970.
uint64_t TimeStampToNs (TimeStamp timestamp=SystemClock::now())
 Converts system clock to ns since midnight 1970.
std::string NsToLocalDate (uint64_t ns_since_1970)
 Formats a date string according to the local date format.
std::string NsToLocalTime (uint64_t ns_since_1970, int format=0)
 Formats a time string according to the local time format.
uint64_t OdsDateToNs (const std::string &ods_date)
 Converts an ODS date and time string to nanoseconds since 1970.
uint64_t FileTimeToNs (std::filesystem::file_time_type time)
 Converts from std::filesystem file_clock to ns since 1970.

Detailed Description

The time namespace is used for time and date functions.

Function Documentation

◆ FileTimeToNs()

uint64_t util::time::FileTimeToNs ( std::filesystem::file_time_type time)

Converts to a file time to nano-seconds since 1970. Note that this function actually hides a problem between MSC and GCC implementation.

Parameters
timeTime point of a file clock
Returns
Nano-seconds since 1970

◆ GetLocalDateTime()

std::string util::time::GetLocalDateTime ( TimeStamp timestamp = SystemClock::now())

The function converts a chrono system clock timestamp to a common ISO type of local timestamp (YYYY-MM-DD hh:mm:ss). This is a common format in log files that should be read by users. Computers should always use UTC time zone.

Parameters
[in]timestampSystem clock timestamp (UTC)
Returns
Returns a date and time string.

◆ GetLocalTimestampWithMs()

std::string util::time::GetLocalTimestampWithMs ( TimeStamp timestamp = SystemClock::now())

The function converts a UTC system clock timestamp to a local date and time string including milliseconds. It uses the ISO related format YYYY-MM-DD hh:mm:ss.us. This format commonly used in log files.

Parameters
[in]timestampSystem clock timestamp (UTC)
Returns
Returns a date and time string with milliseconds.

◆ GetLocalTimestampWithUs()

std::string util::time::GetLocalTimestampWithUs ( TimeStamp timestamp = SystemClock::now())

The function converts a system clock timestamp (UTC) to a date and time string including microseconds. It uses the YYYY-MM-DD hh:mm:ss.us format.

Parameters
[in]timestampSystem clock timestamp (UTC)
Returns
Returns a date and time string with microseconds.

◆ IsoTimeToNs()

uint64_t util::time::IsoTimeToNs ( const std::string & iso_time,
bool local_time = false )

Converts an ISO date and time string to nano-seconds since 1970. Note that the function also handle ODS date strings 'YYYYMMDDhhmmssxxxxxxxxxx strings. The date and string doesn't need to have more than years in the input string.

Parameters
[in]iso_timeTime stamp string YYYY-MM-DDThh:mm:ss.sssssssssZ
[in]local_timeSet to true if the ISO time uses local time.
Returns
Returns nanoseconds since 1970

◆ NsToIsoTime()

std::string util::time::NsToIsoTime ( uint64_t ns_since_1970,
int format = 0 )

The function converts a nanoseconds since 1970 (Unix time) value to a ISO standard YYYY-MM-DDThh:mm:ss.xxxZ. It always converts to a UTC date and time string. The string can be used to stored the timestamp in both user and computer readable format. The user can define the resolution (format). The format is typical used in XML files and in databases that doesn't have date and time formats (SQLite).

Parameters
[in]ns_since_1970Nanosecond since 1970
[in]format0: Include seconds, 1: Include ms, 2: include micro-seconds, 3: Include ns
Returns
Return a date and time string in format YYYY-MM-DDThh:mm:ss.sssssssssZ

◆ NsToLocalDate()

std::string util::time::NsToLocalDate ( uint64_t ns_since_1970)

Formats a date string according to the local date format.

Parameters
ns_since_1970Number of nanoseconds since 1970
Returns
Localized date string

◆ NsToLocalIsoTime()

std::string util::time::NsToLocalIsoTime ( uint64_t ns_since_1970)

Converts a nanoseconds since 1970 timestamp (UTC) value. This is a so-called Unix timestamp. The nanosecond timestamp is typical used when computers deal with timestamps. Note that the milliseconds text may be missing in case there is no milliseconds (.000).

Parameters
[in]ns_since_1970Nanosecond since 1970-01-01.
Returns
Return a date and time string in local format YYYY-MM-DD hh:mm:ss.ms

◆ NsToLocalTime()

std::string util::time::NsToLocalTime ( uint64_t ns_since_1970,
int format = 0 )

Formats a time string with optional ms..ns. The output format is according to the local time format.

Parameters
ns_since_1970Number of nanoseconds since 1970
format0 = Show only seconds, 1 = Show ms, 2 = Show microseconds, 3 = Show ns
Returns
Localized time string

◆ OdsDateToNs()

uint64_t util::time::OdsDateToNs ( const std::string & ods_date)

Converts an ODS date and time string to nanoseconds since 1970. The ODS DT_DATE string have the format 'YYYYMMDDhhmmsslllcccnnn'. Note that the function also handle ISO time formats as 'YYYY-MM-DD hh:mm:ss(.*)'

Parameters
ods_dateODS date string
Returns
Nanoseconds since 1970

◆ TimeStampToNs()

uint64_t util::time::TimeStampToNs ( TimeStamp timestamp = SystemClock::now())
Parameters
timestampSystem clock time stamp
Returns
Nanoseconds since midnight 1970