Skip to main content

yash_env/system/
time.rs

1// This file is part of yash, an extended POSIX shell.
2// Copyright (C) 2025 WATANABE Yuki
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17//! Items about time
18
19use super::Result;
20use std::time::Instant;
21
22/// Trait for getting the current time
23pub trait Clock {
24    /// Returns the current time.
25    #[must_use]
26    fn now(&self) -> Instant;
27}
28
29/// Set of consumed CPU time statistics
30///
31/// This structure contains four CPU time values, all in seconds.
32///
33/// This structure is returned by [`Times::times`].
34#[derive(Clone, Copy, Debug, Default, PartialEq)]
35pub struct CpuTimes {
36    /// User CPU time consumed by the current process
37    pub self_user: f64,
38    /// System CPU time consumed by the current process
39    pub self_system: f64,
40    /// User CPU time consumed by the children of the current process
41    pub children_user: f64,
42    /// System CPU time consumed by the children of the current process
43    pub children_system: f64,
44}
45
46/// Trait for getting consumed CPU time statistics
47pub trait Times {
48    /// Returns the consumed CPU time statistics.
49    ///
50    /// This function abstracts the behavior of the
51    /// [`times` system call](https://pubs.opengroup.org/onlinepubs/9799919799/functions/times.html).
52    fn times(&self) -> Result<CpuTimes>;
53}