ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
time_64.h
Go to the documentation of this file.
1#pragma once
3
4#ifndef USE_NATIVE_64BIT_TIME
5
6#include <cstdint>
7#include <limits>
8
10
11namespace esphome {
12
13class Scheduler;
14
19 friend uint64_t millis_64();
20 friend class Scheduler;
21
22#ifdef ESPHOME_THREAD_SINGLE
23 // Storage defined in time_64.cpp — declared here so the inline body can access them.
24 static uint32_t last_millis_;
25 static uint16_t millis_major_;
26
27 static inline uint64_t ESPHOME_ALWAYS_INLINE compute(uint32_t now) {
28 // Half the 32-bit range - used to detect rollovers vs normal time progression
29 static constexpr uint32_t HALF_MAX_UINT32 = std::numeric_limits<uint32_t>::max() / 2;
30
31 // Single-core platforms have no concurrency, so this is a simple implementation
32 // that just tracks 32-bit rollover (every 49.7 days) without any locking or atomics.
33 uint16_t major = millis_major_;
34 uint32_t last = last_millis_;
35
36 // Check for rollover
37 if (now < last && (last - now) > HALF_MAX_UINT32) {
38 millis_major_++;
39 major++;
40 last_millis_ = now;
41 } else if (now > last) {
42 // Only update if time moved forward
43 last_millis_ = now;
44 }
45
46 // Combine major (high 32 bits) and now (low 32 bits) into 64-bit time
47 return now + (static_cast<uint64_t>(major) << 32);
48 }
49#else
50 static uint64_t compute(uint32_t now);
51#endif
52};
53
54} // namespace esphome
55
56#endif // !USE_NATIVE_64BIT_TIME
Extends 32-bit millis() to 64-bit using rollover tracking.
Definition time_64.h:18
friend uint64_t millis_64()
Definition core.cpp:27
friend class Scheduler
Definition time_64.h:20
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
static void uint32_t