Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_timestamp.hpp
1#pragma once
2
3#include <variant>
4
5#include "structure/design_pattern/spk_cached_data.hpp"
6#include "structure/system/time/spk_duration.hpp"
7#include "type/spk_time_unit.hpp"
8
9namespace spk
10{
16 {
17 private:
18 TimeUnit _unit = TimeUnit::Nanosecond;
19 std::variant<double, long long> _value;
20
21 mutable CachedData<double> _secCache;
22 mutable CachedData<long long> _msCache;
23 mutable CachedData<long long> _nsCache;
24
25 double _generateSeconds() const;
26 long long _generateMilliseconds() const;
27 long long _generateNanoseconds() const;
28
29 void _resetCaches() const;
30 void _rebindCaches();
31
32 Timestamp _fromNanoseconds(long long p_nanoseconds) const;
33
34 public:
38 Timestamp();
44 Timestamp(long double p_value, TimeUnit p_unit);
49 Timestamp(const Duration &p_duration);
54 Timestamp(const Timestamp &p_other);
59 Timestamp(Timestamp &&p_other) noexcept;
60
66 Timestamp &operator=(const Timestamp &p_other);
72 Timestamp &operator=(Timestamp &&p_other) noexcept;
73
78 double seconds() const;
83 long long milliseconds() const;
88 long long nanoseconds() const;
89
95 Duration operator-(const Timestamp &p_other) const;
101 Timestamp operator+(const Duration &p_other) const;
107 Timestamp operator-(const Duration &p_other) const;
108
114 Timestamp &operator+=(const Duration &p_other);
120 Timestamp &operator-=(const Duration &p_other);
121
127 bool operator==(const Timestamp &p_other) const;
133 bool operator!=(const Timestamp &p_other) const;
139 bool operator<(const Timestamp &p_other) const;
145 bool operator>(const Timestamp &p_other) const;
151 bool operator<=(const Timestamp &p_other) const;
157 bool operator>=(const Timestamp &p_other) const;
158 };
159}
Lazily generates and caches a value with optional custom destructor.
Definition spk_cached_data.hpp:26
Strongly-typed time span with cached conversions between ns/ms/s.
Definition spk_duration.hpp:15
Timestamp & operator=(const Timestamp &p_other)
Copies timestamp value and caches from another instance.
Definition spk_timestamp.cpp:140
long long nanoseconds() const
Returns the timestamp expressed in nanoseconds.
Definition spk_timestamp.cpp:176
Timestamp & operator-=(const Duration &p_other)
Subtracts a duration from this timestamp in place.
Definition spk_timestamp.cpp:207
bool operator<(const Timestamp &p_other) const
Strictly-less comparison using canonical nanoseconds.
Definition spk_timestamp.cpp:226
bool operator>=(const Timestamp &p_other) const
Greater-or-equal comparison using canonical nanoseconds.
Definition spk_timestamp.cpp:241
bool operator!=(const Timestamp &p_other) const
Inequality comparison using canonical nanoseconds.
Definition spk_timestamp.cpp:221
Timestamp & operator+=(const Duration &p_other)
Adds a duration to this timestamp in place.
Definition spk_timestamp.cpp:198
Timestamp()
Captures current time in nanoseconds.
Definition spk_timestamp.cpp:70
long long milliseconds() const
Returns the timestamp expressed in milliseconds.
Definition spk_timestamp.cpp:171
bool operator==(const Timestamp &p_other) const
Equality comparison using canonical nanoseconds.
Definition spk_timestamp.cpp:216
bool operator>(const Timestamp &p_other) const
Strictly-greater comparison using canonical nanoseconds.
Definition spk_timestamp.cpp:231
Duration operator-(const Timestamp &p_other) const
Computes the duration separating two timestamps.
Definition spk_timestamp.cpp:181
bool operator<=(const Timestamp &p_other) const
Less-or-equal comparison using canonical nanoseconds.
Definition spk_timestamp.cpp:236
Timestamp operator+(const Duration &p_other) const
Shifts the timestamp forward by a duration.
Definition spk_timestamp.cpp:186
double seconds() const
Returns the timestamp expressed in seconds.
Definition spk_timestamp.cpp:166