Fixposition SDK 0.0.0-heads/main-0-g90a51ff
Collection of c++ libraries and apps for use with Fixposition products
Loading...
Searching...
No Matches
fpsdk::common::time::Time Class Reference

Time. More...

#include <time.hpp>

Collaboration diagram for fpsdk::common::time::Time:

Public Member Functions

Set time

These do not throw and instead return true/false.

Note
These throw std::runtime_error if the values are out of range.
bool SetSecNSec (const uint32_t sec, const uint32_t nsec)
 Set time from seconds and nanoseconds (atomic)
 
bool SetNSec (const uint64_t nsec)
 Set time from nanoseconds (atomic)
 
bool SetSec (const double sec)
 Set time from seconds (atomic)
 
bool SetPosix (const time_t posix)
 Set time from POSIX time (POSIX)
 
bool SetRosTime (const RosTime &rostime)
 Set time from ROS time (POSIX)
 
bool SetWnoTow (const WnoTow &wnotow)
 Set time from GNSS (GPS, Galileo, BeiDou) time (atomic)
 
bool SetGloTime (const GloTime &glotime)
 Set time from GLONASS time (UTC + 3h)
 
bool SetUtcTime (const UtcTime &utctime)
 Set time from UTC time (UTC)
 
bool SetTai (const time_t tai)
 Set time from TAI time (CLOCK_TAI)
 
bool SetClockRealtime ()
 Set time from system clock current (now) system time (CLOCK_REALTIME)
 
Get time
uint64_t GetNSec () const
 Get time as nanoseconds (atomic)
 
double GetSec (const int prec=9) const
 Get time as seconds (atomic)
 
time_t GetPosix () const
 Get time as POSIX time (POSIX)
 
RosTime GetRosTime () const
 Get time as ROS time (POSIX)
 
WnoTow GetWnoTow (const WnoTow::Sys sys=WnoTow::Sys::GPS, const int prec=9) const
 Get time as GNSS (GPS, Galileo, BeiDou) time (atomic)
 
GloTime GetGloTime (const int prec=9) const
 Get time as GLONASS time (UTC + 3h)
 
UtcTime GetUtcTime (const int prec=9) const
 Get time as UTC time (UTC)
 
double GetDayOfYear (const int prec=12) const
 Get day of year.
 
time_t GetTai () const
 Get time as TAI time (CLOCK_TAI)
 
std::chrono::milliseconds GetChronoMilli () const
 Get time as std::chrono::duration (milliseconds since epoch) (POSIX)
 
std::chrono::nanoseconds GetChronoNano () const
 Get time as std::chrono::nanoseconds (nanoseconds since epoch) (POSIX)
 
Misc
bool IsZero () const
 Check if time is zero (invalid)
 
Arithmetic methods

These do not throw and instead return true/false.

bool AddDur (const Duration &dur)
 Add duration to time.
 
bool AddSec (const double sec)
 Add seconds to time.
 
bool AddNSec (const int64_t nsec)
 Add nanoseconds to time.
 
bool SubDur (const Duration &dur)
 Substract duration from time.
 
bool SubNSec (const int64_t nsec)
 Substract nanoseconds from time.
 
bool SubSec (const double sec)
 Substract seconds from time.
 
bool Diff (const Time &other, Duration &diff) const
 Calculate difference between times.
 
Arithmetic operators
Note
These throw std::runtime_error if the values are out of range.
Time operator+ (const Duration &rhs) const
 Sum time and duration.
 
Time operator+ (const int64_t nsec) const
 Sum time and nanoseconds.
 
Time operator+ (const double sec) const
 Sum time and seconds.
 
Timeoperator+= (const Duration &rhs)
 Add duration to time.
 
Timeoperator+= (const int64_t nsec)
 Add nanoseconds to time.
 
Timeoperator+= (const double sec)
 Add seconds to time.
 
Time operator- (const Duration &rhs) const
 Subtract time and duration.
 
Time operator- (const int64_t nsec) const
 Subtract time and nanoseconds.
 
Time operator- (const double sec) const
 Subtract time and seconds.
 
Timeoperator-= (const Duration &rhs)
 Subtract duration from time.
 
Timeoperator-= (const int64_t nsec)
 Subtract nanoseconds from time.
 
Timeoperator-= (const double sec)
 Subtract seconds from time.
 
Duration operator- (const Time &rhs) const
 Subtract time and time.
 
Logic operators
bool operator== (const Time &rhs) const
 Equal.
 
bool operator!= (const Time &rhs) const
 Not equal.
 
bool operator> (const Time &rhs) const
 Greater than.
 
bool operator< (const Time &rhs) const
 Smaller than.
 
bool operator>= (const Time &rhs) const
 Greater or equal than.
 
bool operator<= (const Time &rhs) const
 Smaller or equal than.
 
Stringification
std::string StrWnoTow (const WnoTow::Sys sys=WnoTow::Sys::GPS, const int prec=3) const
 Stringify as GNSS time (atomic)
 
std::string StrUtcTime (const int prec=3) const
 Stringify as year, month, day, hour, minute and second time (UTC)
 
std::string StrIsoTime (const int prec=0) const
 Stringify as ISO 8601 time (UTC)
 

Public Attributes

uint32_t sec_
 Seconds part of time (atomic seconds since 1970-01-01 00:00:00 UTC)
 
uint32_t nsec_
 Nanoseconds part of time (should be in range 0-999999999)
 

Static Public Attributes

static const Time MIN
 Minimum representable time.
 
static const Time MAX
 Maximum representable time.
 
static const Time ZERO
 Zero (invalid, uninitialised) time.
 

Make Time object

Note
These throw std::runtime_error if the time is out of range
 Time ()
 Constructor, empty (invalid) time.
 
static Time FromSecNSec (const uint32_t sec, const uint32_t nsec)
 Time from seconds and nanoseconds (atomic)
 
static Time FromNSec (const uint64_t nsec)
 Time from nanoseconds (atomic)
 
static Time FromSec (const double sec)
 From seconds (atomic)
 
static Time FromPosix (const std::time_t posix)
 From POSIX time (POSIX)
 
static Time FromRosTime (const RosTime &rostime)
 From ROS time (POSIX)
 
static Time FromWnoTow (const WnoTow &wnotow)
 From GNSS time (atomic)
 
static Time FromGloTime (const GloTime &glotime)
 From GLONASS time (UTC + 3h)
 
static Time FromUtcTime (const UtcTime &utctime)
 From UTC time (UTC)
 
static Time FromTai (const time_t tai)
 From TAI time (CLOCK_TAI)
 
static Time FromClockRealtime ()
 From system clock current (now) system time (CLOCK_REALTIME)
 

Detailed Description

Time.

This class implements conversion from and to different time systems and time artithmetics. Internally it uses an atomic time in seconds and nanoseconds since 1970 as its representation of time. Conversion from and to UTC time or POSIX time are available. No timezones or local time are supported (use the ctime API for that).

Some of the constructors and operators can throw. Non-throwing methods to manipulate the time are provided as well.

This time object can represent the time from T_MIN to T_MAX, which is (the old) 32bit POSIX time. Note that for the different GNSS times you will get negative week numbers (GPS, Galileo, BeiDou) or a negative offset "M4" value (GLONASS) for timestamps before the beginning of respective timescales.

*                       ---1970-------------1980-------1996-1999----2006--------------------2106---//--fuuuuuture----> time
*
* 1970-01-01 00:00:00.0 UTC |<----- std::time_t ---------------------------------------------------//----->|
*                           |<----- ros::Time/rclcpp::Time----------------------------------->|
* 1980-01-06 00:00:00.0 UTC                  |<---- GPS time---------------------------------------//----->
* 1996-01-01 00:00:00.0 UTC(SU)                         |<---- GLONASS time -----------------------//----->
* 1999-08-21 23:59:47.0 UTC                                  |<---- Galileo time ------------------//----->
* 2006-01-01 00:00:00.0 UTC                                          |<---- BeiDou time -----------//----->
*
*                           |<======================= Time object ===========================>|
*                         T_MIN                                                             T_MAX
*              1970-01-01 00:00:0.0 UTC                                   2106-02-06 06:28:16.0 UTC (approximately!)
*
* Some notes:
*
* - "Strict" POSIX time is used for the methods marked with "POSIX". This time has a discontinuity and/or ambiguity
*   at/during leapsecond events. It does not use the "Mills" or NTP style of handling these periods and is therefore,
*   for those timestamps, likely not compatible with the system time (CLOCK_REALTIME) of a typical Linux system. See
*   references below.
* - The from/to UTC calculations are not fully correct for times before 1972.
* - FromClockTai() and SetClockTai() likely will not give the expected result unless your system (Linux kernel) is
*   configured for the correct leapsecond.
* - The precision of all integer getters, setters and operators should be [ns] in all cases
* - The precision of all double getters, setters and operators should be [ns] in most cases
* - The internal representation of time (the sec_ value) is atomic, but it is not CLOCK_TAI (which already "has" 10
*   leapseconds at sec_ = 0).
*
* Some references:
*
* - https://en.wikipedia.org/wiki/Unix_time
* - https://en.wikipedia.org/wiki/Atomic_clock
* - https://en.wikipedia.org/wiki/Coordinated_Universal_Time
* - https://docs.ntpsec.org/latest/leapsmear.html
* - https://www.eecis.udel.edu/~mills/leap.html
* - https://manpages.org/adjtimex/2
*
* 

Definition at line 564 of file time.hpp.

Member Function Documentation

◆ FromSecNSec()

static Time fpsdk::common::time::Time::FromSecNSec ( const uint32_t sec,
const uint32_t nsec )
static

Time from seconds and nanoseconds (atomic)

Parameters
[in]secTime value seconds
[in]nsecTime value nanoseconds
Returns
the Time object

◆ FromNSec()

static Time fpsdk::common::time::Time::FromNSec ( const uint64_t nsec)
static

Time from nanoseconds (atomic)

Parameters
[in]nsecTime value nanoseconds (> 0)
Returns
the Time object

◆ FromSec()

static Time fpsdk::common::time::Time::FromSec ( const double sec)
static

From seconds (atomic)

Parameters
[in]secTime value seconds (> 0.0)
Returns
the Time object

◆ FromPosix()

static Time fpsdk::common::time::Time::FromPosix ( const std::time_t posix)
static

From POSIX time (POSIX)

Note
See comments in the class description regarding POSIX time!
Parameters
[in]posixTime value seconds (> 0)
Returns
the Time object

◆ FromRosTime()

static Time fpsdk::common::time::Time::FromRosTime ( const RosTime & rostime)
static

From ROS time (POSIX)

Note
See comments in the class description regarding POSIX time!
Parameters
[in]rostimeTime value
Returns
the Time object

◆ FromWnoTow()

static Time fpsdk::common::time::Time::FromWnoTow ( const WnoTow & wnotow)
static

From GNSS time (atomic)

Parameters
[in]wnotowGNSS time
Returns
the Time object

◆ FromGloTime()

static Time fpsdk::common::time::Time::FromGloTime ( const GloTime & glotime)
static

From GLONASS time (UTC + 3h)

Parameters
[in]glotimeGLONASS time
Returns
the Time object

◆ FromUtcTime()

static Time fpsdk::common::time::Time::FromUtcTime ( const UtcTime & utctime)
static

From UTC time (UTC)

Parameters
[in]utctimeUTC time
Returns
the Time object

◆ FromTai()

static Time fpsdk::common::time::Time::FromTai ( const time_t tai)
static

From TAI time (CLOCK_TAI)

Returns
true if successful, false otherwise (bad time)

◆ FromClockRealtime()

static Time fpsdk::common::time::Time::FromClockRealtime ( )
static

From system clock current (now) system time (CLOCK_REALTIME)

Returns
the Time object

◆ SetSecNSec()

bool fpsdk::common::time::Time::SetSecNSec ( const uint32_t sec,
const uint32_t nsec )

Set time from seconds and nanoseconds (atomic)

Parameters
[in]secTime value seconds
[in]nsecTime value nanoseconds
Returns
true if successful, false otherwise (bad time)

◆ SetNSec()

bool fpsdk::common::time::Time::SetNSec ( const uint64_t nsec)

Set time from nanoseconds (atomic)

Parameters
[in]nsecTime value nanoseconds (> 0)
Returns
true if successful, false otherwise (bad time)

◆ SetSec()

bool fpsdk::common::time::Time::SetSec ( const double sec)

Set time from seconds (atomic)

Parameters
[in]secTime value seconds (> 0.0)
Returns
true if successful, false otherwise (bad time)

◆ SetPosix()

bool fpsdk::common::time::Time::SetPosix ( const time_t posix)

Set time from POSIX time (POSIX)

Note
See comments in the class description regarding POSIX time!
Parameters
[in]posixThe POSIX time
Returns
true if successful, false otherwise (bad time)

◆ SetRosTime()

bool fpsdk::common::time::Time::SetRosTime ( const RosTime & rostime)

Set time from ROS time (POSIX)

Note
See comments in the class description regarding POSIX time!
Parameters
[in]rostimeTime value
Returns
true if successful, false otherwise (bad time)

◆ SetWnoTow()

bool fpsdk::common::time::Time::SetWnoTow ( const WnoTow & wnotow)

Set time from GNSS (GPS, Galileo, BeiDou) time (atomic)

Parameters
[in]wnotowGNSS time
Returns
true if successful, false otherwise (bad time)

◆ SetGloTime()

bool fpsdk::common::time::Time::SetGloTime ( const GloTime & glotime)

Set time from GLONASS time (UTC + 3h)

Parameters
[in]glotimeGLONASS time
Returns
true if successful, false otherwise (bad time)

◆ SetUtcTime()

bool fpsdk::common::time::Time::SetUtcTime ( const UtcTime & utctime)

Set time from UTC time (UTC)

Parameters
[in]utctimeUTC time
Returns
true if successful, false otherwise (bad time)

◆ SetTai()

bool fpsdk::common::time::Time::SetTai ( const time_t tai)

Set time from TAI time (CLOCK_TAI)

Parameters
[in]taiThe TAI time
Returns
true if successful, false otherwise (bad time)

◆ SetClockRealtime()

bool fpsdk::common::time::Time::SetClockRealtime ( )

Set time from system clock current (now) system time (CLOCK_REALTIME)

Returns
true if successful, false otherwise (bad time)

◆ GetNSec()

uint64_t fpsdk::common::time::Time::GetNSec ( ) const

Get time as nanoseconds (atomic)

Returns
the time as nanoseconds

◆ GetSec()

double fpsdk::common::time::Time::GetSec ( const int prec = 9) const

Get time as seconds (atomic)

Parameters
[in]precRound the seconds to this many fractional digits (0-9)
Returns
the time as seconds

◆ GetPosix()

time_t fpsdk::common::time::Time::GetPosix ( ) const

Get time as POSIX time (POSIX)

Note
See comments in the class description regarding POSIX time!
Returns
the POSIX time, truncated (rounded down, sub-seconds ignored)

◆ GetRosTime()

RosTime fpsdk::common::time::Time::GetRosTime ( ) const

Get time as ROS time (POSIX)

Note
See comments in the class description regarding POSIX time!
Returns
the time

◆ GetWnoTow()

WnoTow fpsdk::common::time::Time::GetWnoTow ( const WnoTow::Sys sys = WnoTow::Sys::GPS,
const int prec = 9 ) const

Get time as GNSS (GPS, Galileo, BeiDou) time (atomic)

Note
For times before the respective GNSS epoch the result is not usable (e.g. negative week numbers).
Parameters
[in]sysGNSS
[in]precRound the seconds to this many fractional digits (0-9)
Returns
the GNSS time for the selected GNSS

◆ GetGloTime()

GloTime fpsdk::common::time::Time::GetGloTime ( const int prec = 9) const

Get time as GLONASS time (UTC + 3h)

Parameters
[in]precRound the seconds to this many fractional digits (0-9)
Returns
the GLONASS time

◆ GetUtcTime()

UtcTime fpsdk::common::time::Time::GetUtcTime ( const int prec = 9) const

Get time as UTC time (UTC)

Parameters
[in]precRound the seconds to this many fractional digits (0-9)
Returns
the UTC time

◆ GetDayOfYear()

double fpsdk::common::time::Time::GetDayOfYear ( const int prec = 12) const

Get day of year.

Parameters
[in]precRound to this many fractional digits (0-12)
Returns
the day of the year

◆ GetTai()

time_t fpsdk::common::time::Time::GetTai ( ) const

Get time as TAI time (CLOCK_TAI)

Returns
the TAI time, truncated (rounded down, sub-seconds ignored)

◆ GetChronoMilli()

std::chrono::milliseconds fpsdk::common::time::Time::GetChronoMilli ( ) const

Get time as std::chrono::duration (milliseconds since epoch) (POSIX)

Note
See comments in the class description regarding POSIX time!
Returns
the time as std::chrono::milliseconds

◆ GetChronoNano()

std::chrono::nanoseconds fpsdk::common::time::Time::GetChronoNano ( ) const

Get time as std::chrono::nanoseconds (nanoseconds since epoch) (POSIX)

Note
See comments in the class description regarding POSIX time!
Returns
the time as std::chrono::nanoseconds

◆ IsZero()

bool fpsdk::common::time::Time::IsZero ( ) const

Check if time is zero (invalid)

Returns
true if time is exactly zero

◆ AddDur()

bool fpsdk::common::time::Time::AddDur ( const Duration & dur)

Add duration to time.

Parameters
[in]durDuration to add
Returns
true if successful (dur value in range), false otherwise (bad sec value)

◆ AddSec()

bool fpsdk::common::time::Time::AddSec ( const double sec)

Add seconds to time.

Parameters
[in]secSeconds to add
Returns
true if successful (sec value in range), false otherwise (bad sec value)

◆ AddNSec()

bool fpsdk::common::time::Time::AddNSec ( const int64_t nsec)

Add nanoseconds to time.

Parameters
[in]nsecNanoseconds to add
Returns
true if successful (sec value in range), false otherwise (bad sec value)

◆ SubDur()

bool fpsdk::common::time::Time::SubDur ( const Duration & dur)

Substract duration from time.

Parameters
[in]durDuration to substract
Returns
true if successful (dur value in range), false otherwise (bad sec value)

◆ SubNSec()

bool fpsdk::common::time::Time::SubNSec ( const int64_t nsec)

Substract nanoseconds from time.

Parameters
[in]nsecNanoseconds to substract
Returns
true if successful (sec value in range), false otherwise (bad sec value)

◆ SubSec()

bool fpsdk::common::time::Time::SubSec ( const double sec)

Substract seconds from time.

Parameters
[in]secSeconds to substract
Returns
true if successful (sec value in range), false otherwise (bad sec value)

◆ Diff()

bool fpsdk::common::time::Time::Diff ( const Time & other,
Duration & diff ) const

Calculate difference between times.

Parameters
[in]otherThe other time
[out]diffThe difference to the other time (time - other)
Returns
true if the times and the difference are within range, false otherwise

◆ StrWnoTow()

std::string fpsdk::common::time::Time::StrWnoTow ( const WnoTow::Sys sys = WnoTow::Sys::GPS,
const int prec = 3 ) const

Stringify as GNSS time (atomic)

Parameters
[in]sysThe desired GNSS time system
[in]precNumber of fractional digits for the seconds (0-9)
Returns
a string with formatted week-number and time-of-week ("wwww:tttttt.ttt")

◆ StrUtcTime()

std::string fpsdk::common::time::Time::StrUtcTime ( const int prec = 3) const

Stringify as year, month, day, hour, minute and second time (UTC)

Parameters
[in]precNumber of fractional digits for the seconds (0-9)
Returns
a string with formatted UTC time ("yyyy-mm-dd hh:mm:ss.sss")

◆ StrIsoTime()

std::string fpsdk::common::time::Time::StrIsoTime ( const int prec = 0) const

Stringify as ISO 8601 time (UTC)

Parameters
[in]precNumber of fractional digits for the seconds (0-9)
Returns
a string with formatted UTC time ("yyyyddmmThhmmssZ")

Member Data Documentation

◆ MIN

const Time fpsdk::common::time::Time::MIN
static

Minimum representable time.

Definition at line 1070 of file time.hpp.

◆ MAX

const Time fpsdk::common::time::Time::MAX
static

Maximum representable time.

Definition at line 1071 of file time.hpp.

◆ ZERO

const Time fpsdk::common::time::Time::ZERO
static

Zero (invalid, uninitialised) time.

Definition at line 1072 of file time.hpp.

◆ sec_

uint32_t fpsdk::common::time::Time::sec_

Seconds part of time (atomic seconds since 1970-01-01 00:00:00 UTC)

Definition at line 1075 of file time.hpp.

◆ nsec_

uint32_t fpsdk::common::time::Time::nsec_

Nanoseconds part of time (should be in range 0-999999999)

Definition at line 1076 of file time.hpp.


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