MCPcopy Create free account
hub / github.com/RustCV/RustCV / instant_to_scalar

Method instant_to_scalar

rustcv-core/src/time.rs:116–138  ·  view source on GitHub ↗

辅助:将 Instant 转为 f64 (秒), 仅用于计算差值

(&self, t: Instant)

Source from the content-addressed store, hash-verified

114
115 // 辅助:将 Instant 转为 f64 (秒), 仅用于计算差值
116 fn instant_to_scalar(&self, t: Instant) -> f64 {
117 // 这里实际上只需要相对值,不需要绝对 epoch
118 // 我们可以用 t.elapsed() 的反向值,或者既然在一个进程内,
119 // 我们统一参考 lazy_static 的启动时间点会更准。
120 // 为简化代码,这里假设 t 是单调的,转换成纳秒 float。
121 // 在实际生产代码中,应使用 Duration since Boot。
122 // t.elapsed().as_secs_f64() // 这是一个负相关的量,有点 tricky。
123 // 修正:应该保存一个 Process Start Time。
124 // 这里暂略,仅展示逻辑框架。
125 // 0.0
126
127 // 获取启动时间锚点(如果还没初始化,这里会初始化为当前时间)
128 let start_time = PROCESS_START.get_or_init(Instant::now);
129
130 if t >= *start_time {
131 // 正常情况:t 在启动时间之后
132 t.duration_since(*start_time).as_secs_f64()
133 } else {
134 // 边缘情况:t 在启动时间之前(极少见,但为了数学严谨性)
135 // 返回负数
136 -(start_time.duration_since(t).as_secs_f64())
137 }
138 }
139
140 fn instant_to_duration(&self, t: Instant) -> Duration {
141 // 将 Instant 转换为 "System Up Time" (CLOCK_MONOTONIC)

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected