8#if defined(ESP_PLATFORM) || defined(__linux__)
9#define CTIME_HDR_DEFINED
44 [[nodiscard]]
constexpr int64_t
inMicros()
const {
return micros_; }
47 [[nodiscard]]
constexpr int64_t
inMillis()
const {
66 return micros_ / 1000LL;
71 return micros_ / 1000000LL;
76 return micros_ / 60000000LL;
81 return micros_ / 3600000000LL;
86 int64_t q = micros_ / 1000LL;
87 int64_t r = micros_ % 1000LL;
89 return micros_ > 0 ? q + 1 : q - 1;
94 int64_t q = micros_ / 1000000LL;
95 int64_t r = micros_ % 1000000LL;
97 return micros_ > 0 ? q + 1 : q - 1;
102 int64_t q = micros_ / 60000000LL;
103 int64_t r = micros_ % 60000000LL;
104 if (r == 0)
return q;
105 return micros_ > 0 ? q + 1 : q - 1;
110 int64_t q = micros_ / 3600000000LL;
111 int64_t r = micros_ % 3600000000LL;
112 if (r == 0)
return q;
113 return micros_ > 0 ? q + 1 : q - 1;
119 int64_t q = micros_ / 1000LL;
120 int64_t r = micros_ % 1000LL;
121 int64_t ar = r < 0 ? -r : r;
122 if (ar * 2 < 1000LL)
return q;
123 return micros_ > 0 ? q + 1 : q - 1;
128 int64_t q = micros_ / 1000000LL;
129 int64_t r = micros_ % 1000000LL;
130 int64_t ar = r < 0 ? -r : r;
131 if (ar * 2 < 1000000LL)
return q;
132 return micros_ > 0 ? q + 1 : q - 1;
137 int64_t q = micros_ / 60000000LL;
138 int64_t r = micros_ % 60000000LL;
139 int64_t ar = r < 0 ? -r : r;
140 if (ar * 2 < 60000000LL)
return q;
141 return micros_ > 0 ? q + 1 : q - 1;
146 int64_t q = micros_ / 3600000000LL;
147 int64_t r = micros_ % 3600000000LL;
148 int64_t ar = r < 0 ? -r : r;
149 if (ar * 2 < 3600000000LL)
return q;
150 return micros_ > 0 ? q + 1 : q - 1;
154 [[nodiscard]]
constexpr float inMillisFloat()
const {
return micros_ / 1000.0; }
158 return micros_ / 1000000.0;
163 return micros_ / 60000000.0;
168 return micros_ / 3600000000.0;
207 constexpr Duration(int64_t micros) : micros_(micros) {}
222 return Micros(millis * 1000);
226 return Millis((
long long)millis);
230 return Millis((
long long)millis);
234 return Millis((
long long)millis);
238 return Millis((
long long)millis);
242 return Millis((
long long)millis);
246 return Millis((
long long)millis);
250 return Millis((
long long)millis);
254 return Duration((
long long)(millis * 1000));
258 return Duration((
long long)(millis * 1000));
265 return Micros(seconds * 1000 * 1000);
269 return Seconds((
long long)seconds);
273 return Seconds((
long long)seconds);
277 return Seconds((
long long)seconds);
281 return Seconds((
long long)seconds);
285 return Seconds((
long long)seconds);
289 return Seconds((
long long)seconds);
293 return Seconds((
long long)seconds);
297 return Duration((int64_t)(seconds * 1000 * 1000));
301 return Duration((int64_t)(seconds * 1000 * 1000));
308 return Duration(minutes * 1000 * 1000 * 60);
312 return Minutes((
long long)minutes);
316 return Minutes((
long long)minutes);
320 return Minutes((
long long)minutes);
324 return Minutes((
long long)minutes);
328 return Minutes((
long long)minutes);
332 return Minutes((
long long)minutes);
336 return Minutes((
long long)minutes);
340 return Duration((int64_t)(minutes * 1000 * 1000 * 60));
344 return Duration((int64_t)(minutes * 1000 * 1000 * 60));
351 return Duration(hours * 1000 * 1000 * 60 * 60);
355 return Hours((
long long)minutes);
359 return Hours((
long long)minutes);
363 return Hours((
long long)minutes);
367 return Hours((
long long)minutes);
371 return Hours((
long long)minutes);
375 return Hours((
long long)minutes);
379 return Hours((
long long)minutes);
383 return Duration((int64_t)(hours * 1000 * 1000 * 60 * 60));
387 return Duration((int64_t)(hours * 1000 * 1000 * 60 * 60));
466 micros_ = other.micros_;
472 micros_ = other.micros_;
477 [[nodiscard]] int64_t
inMicros()
const {
return micros_; }
480 [[nodiscard]] int64_t
inMillis()
const {
return micros_ / 1000LL; }
483 [[nodiscard]] int64_t
inSeconds()
const {
return micros_ / 1000000LL; }
486 [[nodiscard]] int64_t
inMinutes()
const {
return micros_ / 60000000LL; }
489 [[nodiscard]] int64_t
inHours()
const {
return micros_ / 3600000000LL; }
512 Uptime(int64_t micros) : micros_(micros) {}
672#ifdef CTIME_HDR_DEFINED
679 if (gettimeofday(&tv,
nullptr))
return WallTime();
692 : offset_minutes_(
offset.inMinutes()) {}
696 return Minutes(offset_minutes_);
700 int16_t offset_minutes_;
770 [[nodiscard]] int16_t
year()
const {
return year_; }
776 [[nodiscard]] uint8_t
day()
const {
return day_; }
779 [[nodiscard]] uint8_t
hour()
const {
return hour_; }
782 [[nodiscard]] uint8_t
minute()
const {
return minute_; }
785 [[nodiscard]] uint8_t
second()
const {
return second_; }
788 [[nodiscard]] uint32_t
micros()
const {
return micros_; }
794 [[nodiscard]] uint16_t
dayOfYear()
const {
return day_of_year_; }
796#ifdef CTIME_HDR_DEFINED
799 :
DateTime(t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min,
803 struct tm tmStruct() const {
804 return tm{.tm_sec = second_,
808 .tm_mon = month_ - 1,
809 .tm_year = year_ - 1900,
810 .tm_wday = day_of_week_,
811 .tm_yday = day_of_year_,
826 uint16_t day_of_year_;
844#if defined(__linux__)
851inline std::ostream& operator<<(std::ostream& os,
858inline std::ostream& operator<<(std::ostream& os,
const roo_time::Uptime& t) {
870inline std::ostream& operator<<(std::ostream& os,
872 os << std::setfill(
'0') << std::setw(4) << (int)dt.
year() <<
"-";
873 os << std::setfill(
'0') << std::setw(2) << (int)dt.
month() <<
"-";
874 os << std::setfill(
'0') << std::setw(2) << (int)dt.
day() <<
" ";
875 os << std::setfill(
'0') << std::setw(2) << (int)dt.
hour() <<
":";
876 os << std::setfill(
'0') << std::setw(2) << (int)dt.
minute() <<
":";
877 os << std::setfill(
'0') << std::setw(2) << (int)dt.
second() <<
".";
878 os << std::setfill(
'0') << std::setw(6) << (int64_t)dt.
micros();
Represents wall time decomposed into date/time in a specific time zone.
int16_t year() const
Returns four-digit year.
Month month() const
Returns month in [1, 12].
uint8_t second() const
Returns second in [0, 59].
uint16_t dayOfYear() const
Returns day of year in [1, 366].
DayOfWeek dayOfWeek() const
Returns day of week in this time zone.
uint32_t micros() const
Returns microsecond fraction in [0, 999999].
uint8_t minute() const
Returns minute in [0, 59].
uint8_t hour() const
Returns hour in [0, 23].
DateTime()
Constructs DateTime representing current time in UTC.
WallTime wallTime() const
Returns WallTime corresponding to this DateTime.
uint8_t day() const
Returns day of month in valid range.
TimeZone timeZone() const
Returns time zone of this DateTime.
Represents an amount of time (e.g. 5s, 10min).
Duration & operator+=(const Duration &other)
Adds another duration to this one.
constexpr int64_t inMillis() const
Returns duration in milliseconds, rounded toward zero.
constexpr int64_t inHours() const
Returns duration in hours, rounded toward zero.
constexpr int64_t inMinutesRoundedNearest() const
Returns duration in minutes, rounded to nearest (ties away from zero).
constexpr float inMillisFloat() const
Returns duration in milliseconds as floating-point value.
constexpr int64_t inMillisRoundedDown() const
Returns duration in milliseconds, rounded toward zero.
constexpr int64_t inSecondsRoundedDown() const
Returns duration in seconds, rounded toward zero.
constexpr Duration()
Constructs zero duration.
Duration & operator-=(const Duration &other)
Subtracts another duration from this one.
constexpr float inMinutesFloat() const
Returns duration in minutes as floating-point value.
constexpr int64_t inHoursRoundedUp() const
Returns duration in hours, rounded away from zero.
constexpr int64_t inSecondsRoundedNearest() const
Returns duration in seconds, rounded to nearest (ties away from zero).
constexpr int64_t inMinutesRoundedDown() const
Returns duration in minutes, rounded toward zero.
friend constexpr Duration Hours(long long hours)
Constructs a duration from hours.
constexpr int64_t inMillisRoundedNearest() const
Returns duration in milliseconds, rounded to nearest (ties away from zero).
friend constexpr Duration Seconds(long long seconds)
Constructs a duration from seconds.
static Duration FromComponents(const Components &components)
Reconstructs duration from components.
Components toComponents()
Breaks duration into components (days, hours, minutes, ...).
constexpr int64_t inHoursRoundedDown() const
Returns duration in hours, rounded toward zero.
constexpr int64_t inMillisRoundedUp() const
Returns duration in milliseconds, rounded away from zero.
constexpr int64_t inMinutesRoundedUp() const
Returns duration in minutes, rounded away from zero.
friend constexpr Duration Micros(long long micros)
Constructs a duration from microseconds.
constexpr int64_t inMinutes() const
Returns duration in minutes, rounded toward zero.
constexpr float inSecondsFloat() const
Returns duration in seconds as floating-point value.
constexpr int64_t inSeconds() const
Returns duration in seconds, rounded toward zero.
constexpr int64_t inSecondsRoundedUp() const
Returns duration in seconds, rounded away from zero.
constexpr int64_t inMicros() const
Returns duration in microseconds.
static const Duration Max()
Returns the maximum representable duration.
constexpr int64_t inHoursRoundedNearest() const
Returns duration in hours, rounded to nearest (ties away from zero).
constexpr float inHoursFloat() const
Returns duration in hours as floating-point value.
friend constexpr Duration Minutes(long long minutes)
Constructs a duration from minutes.
friend constexpr Duration Millis(long long millis)
Constructs a duration from milliseconds.
TimeZone()
Constructs UTC timezone.
constexpr Duration offset() const
Returns UTC offset of this time zone.
constexpr TimeZone(Duration offset)
Creates time zone with specified UTC offset.
Represents an instant relative to process/boot start time.
Uptime()
Constructs zero uptime value.
Uptime & operator-=(const Duration &i)
Subtracts duration from this uptime.
static const Uptime Now()
Returns current monotonic process uptime.
int64_t inHours() const
Returns uptime in hours.
int64_t inMicros() const
Returns uptime in microseconds.
int64_t inSeconds() const
Returns uptime in seconds.
Uptime(const Uptime &other)
Copy constructor.
static const Uptime Max()
Returns the maximum representable uptime value.
Uptime & operator=(const volatile Uptime &other)
Assignment operator for volatile sources.
int64_t inMinutes() const
Returns uptime in minutes.
friend Uptime operator-(const Uptime &u, const Duration &i)
Returns uptime shifted backwards by duration.
int64_t inMillis() const
Returns uptime in milliseconds.
Uptime & operator=(const Uptime &other)
Assignment operator.
friend Uptime operator+(const Uptime &u, const Duration &i)
Returns uptime shifted by duration.
Uptime(const volatile Uptime &other)
Copy constructor for volatile sources.
Uptime & operator+=(const Duration &i)
Adds duration to this uptime.
static const Uptime Start()
Returns uptime value at process start.
Abstract interface for obtaining current wall time.
virtual ~WallTimeClock()=default
Virtual destructor.
virtual WallTime now() const =0
Returns current wall time.
Represents absolute wall time since Unix epoch.
WallTime()
Constructs epoch wall time.
Duration sinceEpoch() const
Returns elapsed duration since Unix epoch.
WallTime & operator-=(const Duration &i)
Subtracts duration from this wall time.
friend WallTime operator-(const WallTime &, const Duration &)
Returns wall time shifted backwards by duration.
WallTime(Duration since_epoch)
Constructs wall time from offset since Unix epoch.
WallTime & operator+=(const Duration &i)
Adds duration to this wall time.
friend WallTime operator+(const WallTime &, const Duration &)
Returns wall time shifted by duration.
Umbrella header for the roo_time module.
bool operator==(const Duration &a, const Duration &b)
Returns true if both durations are equal.
Duration operator-(const Duration &a, const Duration &b)
Returns the difference between two durations.
bool operator!=(const Duration &a, const Duration &b)
Returns true if durations differ.
bool operator<=(const Duration &a, const Duration &b)
Returns true if a is not longer than b.
constexpr Duration Micros(long long micros)
Constructs a duration from microseconds.
Duration operator*(const Duration &a, int b)
Multiplies duration by an integer factor.
constexpr Duration Seconds(long long seconds)
Constructs a duration from seconds.
void DelayUntil(Uptime deadline)
Delays execution until deadline.
constexpr Duration Minutes(long long minutes)
Constructs a duration from minutes.
constexpr Duration Millis(long long millis)
Constructs a duration from milliseconds.
bool operator>=(const Duration &a, const Duration &b)
Returns true if a is not shorter than b.
bool operator>(const Duration &a, const Duration &b)
Returns true if a is longer than b.
void Delay(Duration duration)
Delays execution for duration.
constexpr Duration Hours(long long hours)
Constructs a duration from hours.
Duration operator+(const Duration &a, const Duration &b)
Returns the sum of two durations.
bool operator<(const Duration &a, const Duration &b)
Returns true if a is shorter than b.
Calendar-like decomposition of a duration value.