1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// vim:fileencoding=utf-8:noet
//! Port of `powerline/lib/monotonic.py`.
//!
//! The upstream file is a 100-line tower of Python 2/3 + Windows / macOS
//! / Linux compatibility shims selecting the best available monotonic
//! clock source. The whole file boils down to one logical operation:
//! "give me a monotonic timestamp in seconds (float)."
//!
//! Rust's `std::time::Instant` is the canonical monotonic clock — it
//! is portable across Unix, macOS, and Windows, uses the OS's best
//! monotonic source automatically (`CLOCK_MONOTONIC` on Linux,
//! `mach_absolute_time` on macOS, `QueryPerformanceCounter` on
//! Windows), and requires zero conditional-compilation.
//!
//! Behaviour notes:
//!
//! - Upstream's Python 3.3+ `time.monotonic()` path corresponds to
//! Rust's `Instant::now()`. Both are monotonic and don't go
//! backwards.
//! - Upstream's `CLOCK_MONOTONIC_RAW` preference (when available)
//! gives a NTP-unaffected counter. Rust does not expose `_RAW`
//! directly on Linux (`Instant` uses `CLOCK_MONOTONIC`); the
//! difference is negligible (~ms drift over hours of NTP slewing)
//! for the segment-render use case powerline-status needs this for.
use OnceLock;
use Instant;
/// Storage for the program-start instant. `monotonic()` returns the
/// number of seconds elapsed since this instant — matching the units
/// of every Python branch in `monotonic.py` (seconds as f64).
static EPOCH: = new;
/// Port of module-level binding `monotonic` from `powerline/lib/monotonic.py:14`,
/// `:17`, `:28`, `:64`, `:93`, or `:100` (one of seven `monotonic`
/// definitions depending on platform).
///
/// Returns the number of seconds since the first call to `monotonic()`
/// in this process — a monotonic, non-decreasing wall-clock-independent
/// timestamp suitable for measuring elapsed durations.
///
/// All Python branches return `float` (seconds, sub-millisecond
/// resolution); the Rust port returns `f64` for the same shape.
/// Port of the darwin-branch helper `mach_timebase_info()` from
/// `powerline/lib/monotonic.py:56-59`.
///
/// Python wraps the macOS `mach_timebase_info` syscall to obtain the
/// (numer, denom) rational that converts `mach_absolute_time()` ticks
/// to nanoseconds: `ns = ticks * numer / denom`.
///
/// The Rust port calls `libc::mach_timebase_info` directly. Used as a
/// support helper for the darwin-specific `monotonic()` body at
/// py:64-65; the canonical `monotonic()` above uses
/// `std::time::Instant` which internally calls the same syscall on
/// macOS, so this helper is exposed for parity rather than to drive
/// the runtime clock.
/// Non-darwin fallback for `mach_timebase_info()` from
/// `powerline/lib/monotonic.py:56`. Python only defines this on darwin
/// (inside the `elif sys.platform == 'darwin':` branch at py:31); the
/// Rust port surfaces a stub on other platforms that mirrors Python's
/// "function not defined here" behaviour — calling it on Linux is a
/// programming error and the panic is the equivalent of Python's
/// `NameError`.