Skip to content

Commit e05fa90

Browse files
committed
rust: time: introduce time module
It only contains the bare minimum to implement inode timestamps. Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com>
1 parent b8f0ad7 commit e05fa90

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub mod std_vendor;
4646
pub mod str;
4747
pub mod sync;
4848
pub mod task;
49+
pub mod time;
4950
pub mod types;
5051

5152
#[doc(hidden)]

rust/kernel/time.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Time representation in the kernel.
4+
//!
5+
//! C headers: [`include/linux/time64.h`](../../include/linux/time64.h)
6+
7+
use crate::{bindings, error::Result};
8+
9+
/// A [`Time`] instance at the Unix epoch.
10+
pub const UNIX_EPOCH: Time = Time {
11+
t: bindings::timespec64 {
12+
tv_sec: 0,
13+
tv_nsec: 0,
14+
},
15+
};
16+
17+
/// A timestamp.
18+
#[derive(Copy, Clone)]
19+
pub struct Time {
20+
t: bindings::timespec64,
21+
}
22+
23+
impl Time {
24+
/// Creates a new timestamp.
25+
///
26+
/// `sec` is the number of seconds since the Unix epoch. `nsec` is the number of nanoseconds
27+
/// within that second.
28+
pub fn new(sec: u64, nsec: u64) -> Result<Self> {
29+
Ok(Self {
30+
t: bindings::timespec64 {
31+
tv_sec: sec.try_into()?,
32+
tv_nsec: nsec.try_into()?,
33+
},
34+
})
35+
}
36+
}
37+
38+
impl From<Time> for bindings::timespec64 {
39+
fn from(v: Time) -> Self {
40+
v.t
41+
}
42+
}

0 commit comments

Comments
 (0)