CppCommon  1.0.4.1
C++ Common Library
Public Member Functions | Static Public Member Functions | Protected Attributes | Friends | List of all members
CppCommon::Timestamp Class Reference

Timestamp. More...

#include <timestamp.h>

Inheritance diagram for CppCommon::Timestamp:
CppCommon::EpochTimestamp CppCommon::LocalTimestamp CppCommon::NanoTimestamp CppCommon::RdtsTimestamp CppCommon::UtcTimestamp

Public Member Functions

 Timestamp () noexcept
 Initialize timestamp with an epoch time. More...
 
 Timestamp (uint64_t timestamp) noexcept
 Initialize timestamp with a given time moment in nanoseconds. More...
 
template<class Clock , class Duration >
 Timestamp (const std::chrono::time_point< Clock, Duration > &time_point) noexcept
 Initialize timestamp with a given std::chrono time point. More...
 
 Timestamp (const Timestamp &) noexcept=default
 
 Timestamp (Timestamp &&) noexcept=default
 
 ~Timestamp () noexcept=default
 
Timestampoperator= (uint64_t timestamp) noexcept
 
Timestampoperator= (const Timestamp &) noexcept=default
 
Timestampoperator= (Timestamp &&) noexcept=default
 
Timestampoperator+= (int64_t offset) noexcept
 
Timestampoperator+= (const Timespan &offset) noexcept
 
Timestampoperator-= (int64_t offset) noexcept
 
Timestampoperator-= (const Timespan &offset) noexcept
 
std::chrono::system_clock::time_point chrono () const noexcept
 Convert timestamp to the std::chrono time point. More...
 
uint64_t days () const noexcept
 Get total days of the current timestamp. More...
 
uint64_t hours () const noexcept
 Get total hours of the current timestamp. More...
 
uint64_t minutes () const noexcept
 Get total minutes of the current timestamp. More...
 
uint64_t seconds () const noexcept
 Get total seconds of the current timestamp. More...
 
uint64_t milliseconds () const noexcept
 Get total milliseconds of the current timestamp. More...
 
uint64_t microseconds () const noexcept
 Get total microseconds of the current timestamp. More...
 
uint64_t nanoseconds () const noexcept
 Get total nanoseconds of the current timestamp. More...
 
uint64_t total () const noexcept
 Get total value of the current timestamp (total nanoseconds) More...
 
void swap (Timestamp &timestamp) noexcept
 Swap two instances. More...
 

Static Public Member Functions

static Timestamp days (int64_t days) noexcept
 Create the timestamp based on the given days value. More...
 
static Timestamp hours (int64_t hours) noexcept
 Create the timestamp based on the given hours value. More...
 
static Timestamp minutes (int64_t minutes) noexcept
 Create the timestamp based on the given minutes value. More...
 
static Timestamp seconds (int64_t seconds) noexcept
 Create the timestamp based on the given seconds value. More...
 
static Timestamp milliseconds (int64_t milliseconds) noexcept
 Create the timestamp based on the given milliseconds value. More...
 
static Timestamp microseconds (int64_t microseconds) noexcept
 Create the timestamp based on the given microseconds value. More...
 
static Timestamp nanoseconds (int64_t nanoseconds) noexcept
 Create the timestamp based on the given nanoseconds value. More...
 
static uint64_t epoch () noexcept
 Get the epoch timestamp. More...
 
static uint64_t utc ()
 Get the UTC timestamp. More...
 
static uint64_t local ()
 Get the local timestamp. More...
 
static uint64_t nano ()
 Get the high resolution timestamp. More...
 
static uint64_t rdts ()
 Get the current value of RDTS (Read Time Stamp Counter) More...
 

Protected Attributes

uint64_t _timestamp
 Timestamp value. More...
 

Friends

Timestamp operator+ (const Timestamp &timestamp, int64_t offset) noexcept
 
Timestamp operator+ (int64_t offset, const Timestamp &timestamp) noexcept
 
Timestamp operator+ (const Timestamp &timestamp, const Timespan &offset) noexcept
 
Timestamp operator+ (const Timespan &offset, const Timestamp &timestamp) noexcept
 
Timestamp operator- (const Timestamp &timestamp, int64_t offset) noexcept
 
Timestamp operator- (int64_t offset, const Timestamp &timestamp) noexcept
 
Timestamp operator- (const Timestamp &timestamp, const Timespan &offset) noexcept
 
Timestamp operator- (const Timespan &offset, const Timestamp &timestamp) noexcept
 
Timespan operator- (const Timestamp &timestamp1, const Timestamp &timestamp2) noexcept
 
bool operator== (const Timestamp &timestamp1, uint64_t timestamp2) noexcept
 
bool operator== (uint64_t timestamp1, const Timestamp &timestamp2) noexcept
 
bool operator== (const Timestamp &timestamp1, const Timestamp &timestamp2) noexcept
 
bool operator!= (const Timestamp &timestamp1, uint64_t timestamp2) noexcept
 
bool operator!= (uint64_t timestamp1, const Timestamp &timestamp2) noexcept
 
bool operator!= (const Timestamp &timestamp1, const Timestamp &timestamp2) noexcept
 
bool operator> (const Timestamp &timestamp1, uint64_t timestamp2) noexcept
 
bool operator> (uint64_t timestamp1, const Timestamp &timestamp2) noexcept
 
bool operator> (const Timestamp &timestamp1, const Timestamp &timestamp2) noexcept
 
bool operator< (const Timestamp &timestamp1, uint64_t timestamp2) noexcept
 
bool operator< (uint64_t timestamp1, const Timestamp &timestamp2) noexcept
 
bool operator< (const Timestamp &timestamp1, const Timestamp &timestamp2) noexcept
 
bool operator>= (const Timestamp &timestamp1, uint64_t timestamp2) noexcept
 
bool operator>= (uint64_t timestamp1, const Timestamp &timestamp2) noexcept
 
bool operator>= (const Timestamp &timestamp1, const Timestamp &timestamp2) noexcept
 
bool operator<= (const Timestamp &timestamp1, uint64_t timestamp2) noexcept
 
bool operator<= (uint64_t timestamp1, const Timestamp &timestamp2) noexcept
 
bool operator<= (const Timestamp &timestamp1, const Timestamp &timestamp2) noexcept
 
void swap (Timestamp &timestamp1, Timestamp &timestamp2) noexcept
 

Detailed Description

Timestamp.

Timestamp wraps time moment in nanoseconds and allows to get separate values of days, hours, minutes, seconds, milliseconds, microseconds or nanoseconds. Also it is possible to get difference between two timestamps as a timespan.

Nanosecond timestamp based on 64-bit integer can represent each nanosecond in the time range of ~584.554531 years. Therefore timestamp bounds from 01.01.1970 to 31.12.2553.

Timestamp epoch January 1, 1970 at 00:00:00

Not thread-safe.

Examples
time_time.cpp, and time_timestamp.cpp.

Definition at line 29 of file timestamp.h.

Constructor & Destructor Documentation

◆ Timestamp() [1/5]

CppCommon::Timestamp::Timestamp ( )
inlinenoexcept

Initialize timestamp with an epoch time.

Definition at line 33 of file timestamp.h.

◆ Timestamp() [2/5]

CppCommon::Timestamp::Timestamp ( uint64_t  timestamp)
inlineexplicitnoexcept

Initialize timestamp with a given time moment in nanoseconds.

Parameters
timestamp- Time moment in nanoseconds

Definition at line 38 of file timestamp.h.

◆ Timestamp() [3/5]

template<class Clock , class Duration >
CppCommon::Timestamp::Timestamp ( const std::chrono::time_point< Clock, Duration > &  time_point)
inlineexplicitnoexcept

Initialize timestamp with a given std::chrono time point.

Parameters
time_point- std::chrono time point

Definition at line 44 of file timestamp.h.

◆ Timestamp() [4/5]

CppCommon::Timestamp::Timestamp ( const Timestamp )
defaultnoexcept

◆ Timestamp() [5/5]

CppCommon::Timestamp::Timestamp ( Timestamp &&  )
defaultnoexcept

◆ ~Timestamp()

CppCommon::Timestamp::~Timestamp ( )
defaultnoexcept

Member Function Documentation

◆ chrono()

std::chrono::system_clock::time_point CppCommon::Timestamp::chrono ( ) const
inlinenoexcept

Convert timestamp to the std::chrono time point.

Definition at line 130 of file timestamp.h.

◆ days() [1/2]

uint64_t CppCommon::Timestamp::days ( ) const
inlinenoexcept

Get total days of the current timestamp.

Examples
time_timestamp.cpp.

Definition at line 134 of file timestamp.h.

◆ days() [2/2]

static Timestamp CppCommon::Timestamp::days ( int64_t  days)
inlinestaticnoexcept

Create the timestamp based on the given days value.

Definition at line 159 of file timestamp.h.

◆ epoch()

static uint64_t CppCommon::Timestamp::epoch ( )
inlinestaticnoexcept

Get the epoch timestamp.

Thread-safe.

Returns
Epoch timestamp

Definition at line 186 of file timestamp.h.

◆ hours() [1/2]

uint64_t CppCommon::Timestamp::hours ( ) const
inlinenoexcept

Get total hours of the current timestamp.

Examples
time_timestamp.cpp.

Definition at line 137 of file timestamp.h.

◆ hours() [2/2]

static Timestamp CppCommon::Timestamp::hours ( int64_t  hours)
inlinestaticnoexcept

Create the timestamp based on the given hours value.

Definition at line 162 of file timestamp.h.

◆ local()

uint64_t CppCommon::Timestamp::local ( )
static

Get the local timestamp.

Thread-safe.

Returns
Local timestamp
Examples
time_timestamp.cpp.

Definition at line 123 of file timestamp.cpp.

◆ microseconds() [1/2]

uint64_t CppCommon::Timestamp::microseconds ( ) const
inlinenoexcept

Get total microseconds of the current timestamp.

Definition at line 149 of file timestamp.h.

◆ microseconds() [2/2]

static Timestamp CppCommon::Timestamp::microseconds ( int64_t  microseconds)
inlinestaticnoexcept

Create the timestamp based on the given microseconds value.

Definition at line 174 of file timestamp.h.

◆ milliseconds() [1/2]

uint64_t CppCommon::Timestamp::milliseconds ( ) const
inlinenoexcept

Get total milliseconds of the current timestamp.

Examples
time_timestamp.cpp.

Definition at line 146 of file timestamp.h.

◆ milliseconds() [2/2]

static Timestamp CppCommon::Timestamp::milliseconds ( int64_t  milliseconds)
inlinestaticnoexcept

Create the timestamp based on the given milliseconds value.

Definition at line 171 of file timestamp.h.

◆ minutes() [1/2]

uint64_t CppCommon::Timestamp::minutes ( ) const
inlinenoexcept

Get total minutes of the current timestamp.

Examples
time_timestamp.cpp.

Definition at line 140 of file timestamp.h.

◆ minutes() [2/2]

static Timestamp CppCommon::Timestamp::minutes ( int64_t  minutes)
inlinestaticnoexcept

Create the timestamp based on the given minutes value.

Definition at line 165 of file timestamp.h.

◆ nano()

uint64_t CppCommon::Timestamp::nano ( )
static

Get the high resolution timestamp.

Thread-safe.

Returns
High resolution timestamp
Examples
time_timestamp.cpp.

Definition at line 153 of file timestamp.cpp.

◆ nanoseconds() [1/2]

uint64_t CppCommon::Timestamp::nanoseconds ( ) const
inlinenoexcept

Get total nanoseconds of the current timestamp.

Definition at line 152 of file timestamp.h.

◆ nanoseconds() [2/2]

static Timestamp CppCommon::Timestamp::nanoseconds ( int64_t  nanoseconds)
inlinestaticnoexcept

Create the timestamp based on the given nanoseconds value.

Definition at line 177 of file timestamp.h.

◆ operator+=() [1/2]

Timestamp& CppCommon::Timestamp::operator+= ( const Timespan offset)
inlinenoexcept

Definition at line 57 of file timestamp.h.

◆ operator+=() [2/2]

Timestamp& CppCommon::Timestamp::operator+= ( int64_t  offset)
inlinenoexcept

Definition at line 55 of file timestamp.h.

◆ operator-=() [1/2]

Timestamp& CppCommon::Timestamp::operator-= ( const Timespan offset)
inlinenoexcept

Definition at line 62 of file timestamp.h.

◆ operator-=() [2/2]

Timestamp& CppCommon::Timestamp::operator-= ( int64_t  offset)
inlinenoexcept

Definition at line 60 of file timestamp.h.

◆ operator=() [1/3]

Timestamp& CppCommon::Timestamp::operator= ( const Timestamp )
defaultnoexcept

◆ operator=() [2/3]

Timestamp& CppCommon::Timestamp::operator= ( Timestamp &&  )
defaultnoexcept

◆ operator=() [3/3]

Timestamp& CppCommon::Timestamp::operator= ( uint64_t  timestamp)
inlinenoexcept

Definition at line 49 of file timestamp.h.

◆ rdts()

uint64_t CppCommon::Timestamp::rdts ( )
static

Get the current value of RDTS (Read Time Stamp Counter)

Counts the number of CPU cycles since reset. The Time Stamp Counter (TSC) is a 64-bit register present on all x86 processors since the Pentium.

Thread-safe.

https://en.wikipedia.org/wiki/Time_Stamp_Counter

Returns
Number of CPU cycles since the last system reset
Examples
time_timestamp.cpp.

Definition at line 205 of file timestamp.cpp.

◆ seconds() [1/2]

uint64_t CppCommon::Timestamp::seconds ( ) const
inlinenoexcept

Get total seconds of the current timestamp.

Examples
algorithms_token_bucket.cpp, and time_timestamp.cpp.

Definition at line 143 of file timestamp.h.

◆ seconds() [2/2]

static Timestamp CppCommon::Timestamp::seconds ( int64_t  seconds)
inlinestaticnoexcept

Create the timestamp based on the given seconds value.

Definition at line 168 of file timestamp.h.

◆ swap()

void CppCommon::Timestamp::swap ( Timestamp timestamp)
inlinenoexcept

Swap two instances.

Definition at line 11 of file timestamp.inl.

◆ total()

uint64_t CppCommon::Timestamp::total ( ) const
inlinenoexcept

Get total value of the current timestamp (total nanoseconds)

Definition at line 156 of file timestamp.h.

◆ utc()

uint64_t CppCommon::Timestamp::utc ( )
static

Get the UTC timestamp.

Thread-safe.

Returns
UTC timestamp
Examples
time_timestamp.cpp.

Definition at line 105 of file timestamp.cpp.

Friends And Related Function Documentation

◆ operator!= [1/3]

bool operator!= ( const Timestamp timestamp1,
const Timestamp timestamp2 
)
friend

Definition at line 98 of file timestamp.h.

◆ operator!= [2/3]

bool operator!= ( const Timestamp timestamp1,
uint64_t  timestamp2 
)
friend

Definition at line 94 of file timestamp.h.

◆ operator!= [3/3]

bool operator!= ( uint64_t  timestamp1,
const Timestamp timestamp2 
)
friend

Definition at line 96 of file timestamp.h.

◆ operator+ [1/4]

Timestamp operator+ ( const Timespan offset,
const Timestamp timestamp 
)
friend

Definition at line 71 of file timestamp.h.

◆ operator+ [2/4]

Timestamp operator+ ( const Timestamp timestamp,
const Timespan offset 
)
friend

Definition at line 69 of file timestamp.h.

◆ operator+ [3/4]

Timestamp operator+ ( const Timestamp timestamp,
int64_t  offset 
)
friend

Definition at line 65 of file timestamp.h.

◆ operator+ [4/4]

Timestamp operator+ ( int64_t  offset,
const Timestamp timestamp 
)
friend

Definition at line 67 of file timestamp.h.

◆ operator- [1/5]

Timestamp operator- ( const Timespan offset,
const Timestamp timestamp 
)
friend

Definition at line 80 of file timestamp.h.

◆ operator- [2/5]

Timestamp operator- ( const Timestamp timestamp,
const Timespan offset 
)
friend

Definition at line 78 of file timestamp.h.

◆ operator- [3/5]

Timestamp operator- ( const Timestamp timestamp,
int64_t  offset 
)
friend

Definition at line 74 of file timestamp.h.

◆ operator- [4/5]

Timespan operator- ( const Timestamp timestamp1,
const Timestamp timestamp2 
)
friend

Definition at line 83 of file timestamp.h.

◆ operator- [5/5]

Timestamp operator- ( int64_t  offset,
const Timestamp timestamp 
)
friend

Definition at line 76 of file timestamp.h.

◆ operator< [1/3]

bool operator< ( const Timestamp timestamp1,
const Timestamp timestamp2 
)
friend

Definition at line 112 of file timestamp.h.

◆ operator< [2/3]

bool operator< ( const Timestamp timestamp1,
uint64_t  timestamp2 
)
friend

Definition at line 108 of file timestamp.h.

◆ operator< [3/3]

bool operator< ( uint64_t  timestamp1,
const Timestamp timestamp2 
)
friend

Definition at line 110 of file timestamp.h.

◆ operator<= [1/3]

bool operator<= ( const Timestamp timestamp1,
const Timestamp timestamp2 
)
friend

Definition at line 126 of file timestamp.h.

◆ operator<= [2/3]

bool operator<= ( const Timestamp timestamp1,
uint64_t  timestamp2 
)
friend

Definition at line 122 of file timestamp.h.

◆ operator<= [3/3]

bool operator<= ( uint64_t  timestamp1,
const Timestamp timestamp2 
)
friend

Definition at line 124 of file timestamp.h.

◆ operator== [1/3]

bool operator== ( const Timestamp timestamp1,
const Timestamp timestamp2 
)
friend

Definition at line 91 of file timestamp.h.

◆ operator== [2/3]

bool operator== ( const Timestamp timestamp1,
uint64_t  timestamp2 
)
friend

Definition at line 87 of file timestamp.h.

◆ operator== [3/3]

bool operator== ( uint64_t  timestamp1,
const Timestamp timestamp2 
)
friend

Definition at line 89 of file timestamp.h.

◆ operator> [1/3]

bool operator> ( const Timestamp timestamp1,
const Timestamp timestamp2 
)
friend

Definition at line 105 of file timestamp.h.

◆ operator> [2/3]

bool operator> ( const Timestamp timestamp1,
uint64_t  timestamp2 
)
friend

Definition at line 101 of file timestamp.h.

◆ operator> [3/3]

bool operator> ( uint64_t  timestamp1,
const Timestamp timestamp2 
)
friend

Definition at line 103 of file timestamp.h.

◆ operator>= [1/3]

bool operator>= ( const Timestamp timestamp1,
const Timestamp timestamp2 
)
friend

Definition at line 119 of file timestamp.h.

◆ operator>= [2/3]

bool operator>= ( const Timestamp timestamp1,
uint64_t  timestamp2 
)
friend

Definition at line 115 of file timestamp.h.

◆ operator>= [3/3]

bool operator>= ( uint64_t  timestamp1,
const Timestamp timestamp2 
)
friend

Definition at line 117 of file timestamp.h.

◆ swap

void swap ( Timestamp timestamp1,
Timestamp timestamp2 
)
friend

Definition at line 17 of file timestamp.inl.

Member Data Documentation

◆ _timestamp

uint64_t CppCommon::Timestamp::_timestamp
protected

Timestamp value.

Definition at line 231 of file timestamp.h.


The documentation for this class was generated from the following files: