gpt4 book ai didi

windows - 为什么 time::Duration 的纳秒值在 Windows 上被降低到最接近的 100 的倍数?

转载 作者:可可西里 更新时间:2023-11-01 10:03:45 34 4
gpt4 key购买 nike

在 Windows 10 64 位上运行以下程序时:

use std::time::{Duration, UNIX_EPOCH};

fn main() {
let d = Duration::new(4660, 22136);
let t = UNIX_EPOCH + d;
let d2 = t.duration_since(UNIX_EPOCH).unwrap();
println!("d: {:?}", d);
println!("d2: {:?}", d2);
}

为什么纳秒值会下限到最接近的 100 的倍数?

d: Duration { secs: 4660, nanos: 22136 }
d2: Duration { secs: 4660, nanos: 22100 }

Windows 文件时间以 100 纳秒的间隔表示,但我不明白为什么这会影响此计算。

最佳答案

通过添加 UNIX_EPOCH (a SystemTime ) 到你的 Duration ,您最终得到一个 SystemTime

Windows-specific implementationsys::time::SystemTime基于 Windows FILETIME 结构。

这个结构has 100 nanosecond granularity :

Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).

大概选择 FILETIME 是因为这是 Windows 世界中普遍使用的时间类型,它提供了性能和功能的良好组合。

关于windows - 为什么 time::Duration 的纳秒值在 Windows 上被降低到最接近的 100 的倍数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41914909/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com