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