xref: /aosp_15_r20/external/crosvm/base/src/sys/unix/time.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
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 
5 use std::time::Duration;
6 
7 use libc::timespec;
8 
9 /// Return a timespec filed with the specified Duration `duration`.
10 #[allow(clippy::useless_conversion)]
duration_to_timespec(duration: Duration) -> timespec11 pub 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 }
19