EtcPal
0.4.1
ETC Platform Abstraction Layer (EtcPal)
|
View other versions:
|
Represents a point in time.
More specifically, stores the number of milliseconds elapsed since an arbitrary point in the past. This number is stored as a 32-bit unsigned integer and thus wraps every 4,294,967,295 milliseconds, or approximately 49.7 days. Because of this, it's recommended to only compare TimePoints on a time scale which is shorter than that by at least an order of magnitude. This reduces the likelihood that wrapping will be an issue.
TimePoints are compared by comparing the signed difference of their respective values with 0. This creates a "half the range" rule of thumb for comparison; TimePoints will compare accurately as long as they were sampled during the same process execution and within 2^31 milliseconds (24.9 days) of each other.
This can be unintuitive to understand since TimePoints are based on unsigned 32-bit values which are compared as if they are signed. Here are some examples:
In order to avoid headaches of the kind that often arise when thinking about two's complement, again, it's recommended to only compare TimePoints on a scale much less than days.
Public Member Functions | |
TimePoint ()=default | |
Construct a TimePoint with a value of 0 by default. More... | |
constexpr | TimePoint (uint32_t ms) |
Construct a TimePoint from a number of milliseconds elapsed since a point in the past. | |
constexpr uint32_t | value () const noexcept |
Get the raw millisecond value from a TimePoint. | |
ETCPAL_CONSTEXPR_14 TimePoint & | operator+= (uint32_t duration) noexcept |
Add a millisecond duration to a TimePoint. | |
ETCPAL_CONSTEXPR_14 TimePoint & | operator-= (uint32_t duration) noexcept |
Subtract a millisecond duration from a TimePoint. | |
Static Public Member Functions | |
static TimePoint | Now () noexcept |
Get a TimePoint representing the current time. | |
|
default |
Construct a TimePoint with a value of 0 by default.
Be careful when default-constructing TimePoints; since TimePoints store the number of milliseconds elapsed since an arbitrary point in the past, a default-constructed TimePoint will compare equal to etcpal::TimePoint::Now() every 49.7 days.