TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec::Timestamp Struct Reference

A point in time. More...

#include <tec_timestamp.hpp>

Public Types

Type aliases
using count_t = int64_t
 Underlying integer type used to store nanosecond count.
 
using duration_t = std::chrono::nanoseconds
 Duration unit used by this timestamp (nanoseconds)
 
using system_clock_t = std::chrono::system_clock
 Clock type used as the basis for time_point conversion.
 
using time_point_t = std::chrono::time_point< system_clock_t, duration_t >
 std::chrono time_point type with nanosecond precision
 

Public Member Functions

 Timestamp ()
 Default constructor — creates timestamp at epoch (count = 0)
 
 Timestamp (count_t _count)
 Construct from raw nanosecond count since epoch.
 
 Timestamp (duration_t d)
 Construct from std::chrono::nanoseconds duration.
 
 Timestamp (const Timestamp &)=default
 
 Timestamp (Timestamp &&)=default
 
duration_t dur () const
 Returns the duration since epoch as std::chrono::nanoseconds.
 
std::tm utc_time () const
 Returns broken-down UTC time as std::tm.
 
std::tm local_time () const
 Returns broken-down local time as std::tm.
 
std::string utc_time_str () const
 Returns ISO 8601 string in UTC (with Z suffix)
 
std::string local_time_str () const
 Returns ISO 8601 string in local time (with timezone offset)
 

Static Public Member Functions

static Timestamp now ()
 Returns current time as Timestamp (UTC-based, nanosecond precision)
 

Public Attributes

count_t count
 Nanoseconds since Unix epoch (1970-01-01T00:00:00Z).
 

Detailed Description

A point in time.

Timestamp represents a point in time independent of any time zone or local calendar, encoded as a count of nanoseconds. The count is relative to an epoch at UTC midnight on January 1, 1970.

Example
#include <ctime>
#include <iostream>
int main() {
using namespace std::literals;
std::cout << "Epoch:\n";
std::cout
<< "Count: "<< ts0.count << "\n"
<< "UTC : "<< ts0.utc_time_str() << "\n"
<< "Local: "<< ts0.local_time_str() << "\n"
;
std::cout << "\nNow:\n";
std::cout
<< "Count: "<< ts1.count << "\n"
<< "UTC : "<< ts1.utc_time_str() << "\n"
<< "Local: "<< ts1.local_time_str() << "\n"
;
std::cout << "\nOne day and five minutes ago:\n";
tec::Timestamp ts2{ts1.dur() - 24h - 5min};
std::cout
<< "Count: "<< ts2.count << "\n"
<< "UTC : "<< ts2.utc_time_str() << "\n"
<< "Local: "<< ts2.local_time_str() << "\n"
;
std::cout << "\nOne week forward:\n";
tec::Timestamp ts3{(ts1.dur() + 7 * 24h).count()};
std::cout
<< "Count: "<< ts3.count << "\n"
<< "UTC : "<< ts3.utc_time_str() << "\n"
<< "Local: "<< ts3.local_time_str() << "\n"
;
return 0;
}
A point in time.
Definition tec_timestamp.hpp:53
std::string utc_time_str() const
Returns ISO 8601 string in UTC (with Z suffix)
Definition tec_timestamp.hpp:149
duration_t dur() const
Returns the duration since epoch as std::chrono::nanoseconds.
Definition tec_timestamp.hpp:112
static Timestamp now()
Returns current time as Timestamp (UTC-based, nanosecond precision)
Definition tec_timestamp.hpp:172
std::string local_time_str() const
Returns ISO 8601 string in local time (with timezone offset)
Definition tec_timestamp.hpp:159
count_t count
Nanoseconds since Unix epoch (1970-01-01T00:00:00Z).
Definition tec_timestamp.hpp:80
A point in time since UNIX epoch.
Output
// Epoch:
// Count: 0
// UTC : 1970-01-01T00:00:00Z
// Local: 1970-01-01T03:00:00+0300
// Now:
// Count: 1768992601096852237
// UTC : 2026-01-21T10:50:01Z
// Local: 2026-01-21T13:50:01+0300
// One day and five minutes ago:
// Count: 1768905901096852237
// UTC : 2026-01-20T10:45:01Z
// Local: 2026-01-20T13:45:01+0300
// One week forward:
// Count: 1769597401096852237
// UTC : 2026-01-28T10:50:01Z
// Local: 2026-01-28T13:50:01+0300
See also
https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/timestamp.proto

Member Function Documentation

◆ dur()

duration_t tec::Timestamp::dur ( ) const
inline

Returns the duration since epoch as std::chrono::nanoseconds.

Returns
duration_t nanoseconds since 1970-01-01T00:00:00Z

◆ local_time()

std::tm tec::Timestamp::local_time ( ) const
inline

Returns broken-down local time as std::tm.

Uses thread-safe wrapper around std::localtime

Returns
std::tm structure with local time components

◆ local_time_str()

std::string tec::Timestamp::local_time_str ( ) const
inline

Returns ISO 8601 string in local time (with timezone offset)

Returns
std::string formatted as "YYYY-MM-DDThh:mm:ss±hhmm"

◆ now()

static Timestamp tec::Timestamp::now ( )
inlinestatic

Returns current time as Timestamp (UTC-based, nanosecond precision)

Uses std::chrono::system_clock::now() and converts to nanoseconds. This is the recommended way to obtain "current time" in this system.

Note
It is not monotonic — subject to system clock adjustments.
Returns
Timestamp representing current UTC time

◆ utc_time()

std::tm tec::Timestamp::utc_time ( ) const
inline

Returns broken-down UTC time as std::tm.

Uses thread-safe wrapper around std::gmtime

Returns
std::tm structure with UTC time components

◆ utc_time_str()

std::string tec::Timestamp::utc_time_str ( ) const
inline

Returns ISO 8601 string in UTC (with Z suffix)

Returns
std::string formatted as "YYYY-MM-DDThh:mm:ssZ"

Member Data Documentation

◆ count

count_t tec::Timestamp::count

Nanoseconds since Unix epoch (1970-01-01T00:00:00Z).

Represents the number of nanoseconds elapsed since 1970-01-01 00:00:00 UTC. The valid range is approximately 1970-01-01 to 2262-04-11 (due to 64-bit signed integer limits).

Negative values represent times before the Unix epoch.


The documentation for this struct was generated from the following file: