Time Representation
fort::Duration
-
class Duration
The amount of time ellapsed between two Time.
A Duration represents the amount of time between two Time.
This class aims to replicate a go syntax. For example to represent one hour, 10 minute, one may write:
{c++} using namespace fort; Duration d = 1 * Duration::Hour + 10 * Duration::Minute;
Public Functions
-
inline Duration(int64_t ns)
constructor by an ammount of nanosecond
- Parameters:
ns – the number of nanosecond
-
inline Duration()
Default constructor with a zero duration.
-
template<typename T, typename U>
inline Duration(const std::chrono::duration<T, U> &duration) constructor from std::chrono::duration
- Template Parameters:
T – first <std::chrono::duration> template
U – second <std::chrono::duration> template
- Parameters:
duration – the <std::chrono::duration> to convert
-
double Hours() const
Gets the duration in hours.
- Returns:
the duration in hours
-
double Minutes() const
Gets the duration in minutes.
- Returns:
the duration in minutes
-
double Seconds() const
Gets the duration in seconds.
- Returns:
the duration in seconds
-
double Milliseconds() const
Gets the duration in milliseconds.
- Returns:
the duration in milliseconds
-
double Microseconds() const
Gets the duration in microseconds.
- Returns:
the duration in microseconds
-
inline int64_t Nanoseconds() const
Gets the duration in nanoseconds.
- Returns:
the duration in nanoseconds
-
inline Duration operator+(const Duration &other) const
The addition operator.
Adds two Duration. Silently overflow following int64_t arithmetic.
- Parameters:
other – the other Duration to add
- Returns:
a new duration
this + other
-
inline Duration operator*(const fort::Duration &other) const
Multiplication operator.
Multiplies two Duration.
- Parameters:
other – the other Duration to multiply.
- Returns:
a new duration
this * other
-
inline Duration operator-(const fort::Duration &other) const
Substraction operator.
Substracts two Duration.
- Parameters:
other – the other Duration to substract
- Returns:
a new duration
this - other
-
inline bool operator<(const Duration &other) const
Less than operator.
Compares two Duration.
- Parameters:
other – the Duration to compare
- Returns:
this < other
-
inline bool operator<=(const Duration &other) const
Less or equal operator.
Compares two Duration.
- Parameters:
other – the Duration to compare
- Returns:
this <= other
-
inline bool operator>(const Duration &other) const
Greater than operator.
Compares two Duration.
- Parameters:
other – the Duration to compare
- Returns:
this > other
Public Static Functions
-
static Duration Parse(const std::string &d)
Parses a string to a Duration.
Parses a std::string to a Duration. string must be of the form
[amount][unit]
where[amount]
is a value that may contain a decimal point, and[unit]
could be any ofh
,m
,s
,ms
,us
,µs
andns
. This pattern may be repeated. For example4m32s
is a valid input.- Parameters:
d – the string to Parse in the form
"2h"
or"1m"
- Throws:
std::runtime_error – if d is an invalid string
- Returns:
the Duration represented by the string.
Public Static Attributes
-
inline Duration(int64_t ns)
fort::Time
-
class Time
A point in time.
Time represents a point in time in UTC timezone
why not std::chrono::time_point ?
Why re-implementing one of this object and not using a
struct timeval
or astd::chrono
? We are dealing with long living experiment on heterogenous systems. Under this circunstances, we would like also to measure precise time difference. For this purpose we could use the framegrabber monolotic clock, which is timestamping every frame we acquired.Well the issue is that we cannot solely rely on this clock, as we may have several computers each with their own monolithic clock. Or even with a single computer, every time we starts the tracking we must assume a new monotonic clock.
We could use the wall clock but this clock may be resetted any time, and we would end up with issue where a time difference between two consecutive frames could be negative. This happen during leap seconds.
Inspired from golang time.Time we propose an Object that store both a Wall time, and a Monotonic timestamp. But here we could have different monotonic timestamp issued by different monotonic clocks. We try, whenever its possible use monotonic value for time differences and comparisons. It could only be the case if both object have a MonotonicValue() issued from the same clock. Otherwise the Wall clock value will be used with the issue regarding the jitter or Wall clock reset.
Differentiaing Monotonic clock is done through MonoclockID() values. The 0 value is reserved for the SYSTEM_MONOTONIC_CLOCK and which is used by Now(). When reading saved monotonic Timestamp from the filesystem (as it is the case when reading data from different
TrackingDataDirectory
), care must be taken to assign different MonoclockID() for each of those reading. This class does not enforce any mechanism. The only entry point to define the MonoclockID() is through the utility function FromTimestampAndMonotonic().Every time are considered UTC.
Public Types
-
typedef uint32_t MonoclockID
ID for a Monotonic Clock.
Public Functions
-
timeval ToTimeval() const
Converts to a
struct timeval
- Returns:
struct timeval
representing the Time.
-
google::protobuf::Timestamp ToTimestamp() const
Converts to a protobuf Timestamp message.
- Returns:
the protobuf Timestamp representing the Time.
-
void ToTimestamp(google::protobuf::Timestamp *timestamp) const
In-place conversion to a protobuf Timestamp.
- Parameters:
timestamp – the timestamp to modify to represent the Time
-
explicit Time()
Default constructor to the system’s epoch.
The resulting Time will represent the epoch.
-
Time Round(const Duration &d) const
-
Rounds the Time to the half-rounded up Duration d. Currently only multiple of Duration::Second and power of 10 of Duration::Nanosecond which are smaller than a second are supported.
-
Duration Reminder(const Duration &d) const
Gets the Reminder of the division by a Duration.
Finds the Duration which remains if this Time would be divided by d. Only multiple of Duration::Second or power of 10 of Duration::Nanosecond smaller than a second are supported.
-
bool IsSinceEver() const
Reports if this Time is -∞
- Returns:
true if this Time is SinceEver()
-
bool IsInfinite() const
Reports if this Time is either Forever or SinceEver.
- Returns:
true if this Time is Forever() or SinceEver().
-
bool HasMono() const
Reports the presence of a monotonic time value.
Reports the presence of a monotonic time value. Only Time issued by Now() or FromTimestampAndMonotonic() contains a monotonic time value.
- Returns:
true
ifthis
contains a monotonic clock value.
-
MonoclockID MonoID() const
Gets the referred MonoclockID.
- Throws:
std::exception – if this Time has no monotonic clock value.
- Returns:
the MonoclockID designating the monotonic clock the monotonic time value refers to.
-
uint64_t MonotonicValue() const
Gets the monotonic value.
- Throws:
std::exception – if this Time has no monotonic clock value.
- Returns:
the monotonic clock value.
-
std::string Format() const
Formats the Time as a RFC 3339 string.
- Returns:
a string representing this Time. Either using RFC 3339 or +/-∞ if this Time::IsInfinite()
-
std::string DebugString() const
Builds a debug string.
This method is useful for internal debugging. Prefer the standard c++ formatting operator on std::ostream or Format().
- Returns:
a debug string with the complete time internal state.
-
inline bool operator==(const Time &other) const
Equal comparison operator.
- Parameters:
other – the other Time to compare to
- Returns:
true
ifthis == other
-
inline bool operator<(const Time &other) const
Less than operator.
- Parameters:
other – the other Time to compare to
- Returns:
true
ifthis < other
-
inline bool operator<=(const Time &other) const
Less or equal operator.
- Parameters:
other – the other Time to compare to
- Returns:
true
ifthis <= other
Public Static Functions
-
static Time Forever()
Returns the positive infinite Time.
Returns the positive infinite Time. For any other Time value, Before() will return
true
. Any computation with this value will Overflow. Calling this method is the only to construct such Time, as operation on other regular Time will Overflow before.- Returns:
a positive infinite Time
-
static Time SinceEver()
Returns the negative infinite Time.
Returns the negative infinite Time. For any other Time value, After() will return
true
. Any computation with this value will Overflow. Calling this method is the only to construct such Time, as operation on other regular Time will Overflow before.- Returns:
a negative infinite Time
-
static Time Now()
Gets the current Time.
Gets the current Time. This time will both have a wall and a monotonic clock reading associated with the SYSTEM_MONOTONIC_CLOCK. Therefore such idioms:
{c++} auto start = fort::Time::Now(); SomeFunction(); auto ellapsed = fort::Time::Now().Sub(start);
Will always return a positive Duration, even if the wall clock has been reset between the two calls to Now()
- Returns:
the current <myrmidon::Time>
-
static Time FromTimeT(time_t t)
Creates a Time from
time_t
The Time will not have any monotonic clock value.
- Parameters:
t – the time_t value
- Returns:
the converted Time
-
static Time FromTimeval(const timeval &t)
Creates a Time from
struct timeval
The Time will not have any monotonic clock value.
- Parameters:
t – the
struct timeval
- Returns:
the converted Time
-
static Time FromTimestamp(const google::protobuf::Timestamp ×tamp)
Creates a Time from a protobuf Timestamp.
The Time will not have any monotonic clock value.
- Parameters:
timestamp – the
google.protobuf.Timestamp
message- Returns:
the converted Time
-
static Time FromUnix(int64_t seconds, int32_t nanoseconds)
Creates a Time from Unix EPOCH.
- Parameters:
seconds – number of seconds since 1970-01-01T00:00:00.000Z
nanoseconds – reminder of seconds since 1970-01-01T00:00:00.000Z
- Returns:
the corresponding Time
-
static Time FromTimestampAndMonotonic(const google::protobuf::Timestamp ×tamp, uint64_t nsecs, MonoclockID monoID)
Creates a Time from a protobuf Timestamp and an external Monotonic clock.
Creates a Time from a protobuf Timestamp and an external monotonic clock. The two values should correspond to the same physical time. It is an helper function to create accurate Time from data saved in
fort.hermes.FrameReadout
protobuf messages that saves both a Wall time value and a framegrabber timestamp for each frame. It is the caller responsability to manage monoID values for not mixing timestamp issued from different clocks. Nothing prevent you to use SYSTEM_MONOTONIC_CLOCK for the monoID value but the behavior manipulating resulting times is undefined.- Parameters:
timestamp – the
google.protobuf.Timestamp
messagensecs – the external monotonic value in nanoseconds
monoID – the external monoID
- Returns:
the converted Time with associated monotonic data
-
static Time Parse(const std::string &input)
Parses from RFC 3339 date string format.
Parses from RFC 3339 date string format, i.e. string of the form
1972-01-01T10:00:20.021-05:00
. It is merely a wrapper from google::protobuf::time_util functions.- Parameters:
input – the string to parse
- Throws:
std::exception – if input is not a RFC 3339 valid string
- Returns:
the converted <myrmidon::Time>
Public Static Attributes
-
static const MonoclockID SYSTEM_MONOTONIC_CLOCK = 0
The current system monotonic clock.
The MonoclockID reserved for the current system ( aka
CLOCK_MONOTONIC
).
-
typedef uint32_t MonoclockID