base/sys/unix/
time.rs

1// Copyright 2024 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std::time::Duration;
6
7use libc::timespec;
8
9/// Return a timespec filed with the specified Duration `duration`.
10#[allow(clippy::useless_conversion)]
11pub fn duration_to_timespec(duration: Duration) -> timespec {
12    // nsec always fits in i32 because subsec_nanos is defined to be less than one billion.
13    let nsec = duration.subsec_nanos() as i32;
14    timespec {
15        tv_sec: duration.as_secs() as libc::time_t,
16        tv_nsec: nsec.into(),
17    }
18}