UTILLib 2.0
Common C++ library with utilities.
Loading...
Searching...
No Matches
timestamp.h File Reference

Contains various conversion between different time formats. More...

#include <chrono>
#include <filesystem>
#include <string>
#include <vector>
Include dependency graph for timestamp.h:

Go to the source code of this file.

Namespaces

namespace  util
 Main namespace for the util library.
namespace  util::time
 The time namespace is used for time and date functions.

Typedefs

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

Functions

std::string util::time::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 util::time::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 util::time::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 util::time::NsToLocalIsoTime (uint64_t ns_since_1970)
 Converts a nanosecond since 1970 timestamp to a local ISO date and time string.
std::string util::time::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 util::time::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 util::time::TimeStampToNs (TimeStamp timestamp=SystemClock::now())
 Converts system clock to ns since midnight 1970.
std::string util::time::NsToLocalDate (uint64_t ns_since_1970)
 Formats a date string according to the local date format.
std::string util::time::NsToLocalTime (uint64_t ns_since_1970, int format=0)
 Formats a time string according to the local time format.
uint64_t util::time::OdsDateToNs (const std::string &ods_date)
 Converts an ODS date and time string to nanoseconds since 1970.
uint64_t util::time::FileTimeToNs (std::filesystem::file_time_type time)
 Converts from std::filesystem file_clock to ns since 1970.

Detailed Description

The central time is an uint64_t UTC nanoseconds since 1970-01-01. This is an extension of the ols time_t that is seconds since 1970-01-01. In general the std::chrono::system_clock is used as reference.

The nanosecond timestamp is used in communications and a general timestamp. This due to the fact that different system used different epoch for system clock. Note most system uses 1970-01-01 as epoch and is called POSIX time or UNIX time.

Note
The std::chrono library have many new functions in C++20. Unfortunately not many compiler have implemented these function so most most of the implementation uses C++17 functionality.