Skip to content

Commit c7d0fb2

Browse files
committed
rust: time: introduce time module
It only contains the bare minimum to implement inode timestamps. Signed-off-by: Wedson Almeida Filho <[email protected]>
1 parent 14513c0 commit c7d0fb2

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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::code::*, error::Result};
8+
9+
/// A [`Timespec`] instance at the Unix epoch.
10+
pub const UNIX_EPOCH: Timespec = Timespec {
11+
t: bindings::timespec64 {
12+
tv_sec: 0,
13+
tv_nsec: 0,
14+
},
15+
};
16+
17+
/// A timestamp.
18+
#[derive(Copy, Clone)]
19+
#[repr(transparent)]
20+
pub struct Timespec {
21+
t: bindings::timespec64,
22+
}
23+
24+
impl Timespec {
25+
/// Creates a new timestamp.
26+
///
27+
/// `sec` is the number of seconds since the Unix epoch. `nsec` is the number of nanoseconds
28+
/// within that second.
29+
pub fn new(sec: u64, nsec: u32) -> Result<Self> {
30+
if nsec >= 1000000000 {
31+
return Err(EDOM);
32+
}
33+
34+
Ok(Self {
35+
t: bindings::timespec64 {
36+
tv_sec: sec.try_into()?,
37+
tv_nsec: nsec.try_into()?,
38+
},
39+
})
40+
}
41+
}
42+
43+
impl From<Timespec> for bindings::timespec64 {
44+
fn from(v: Timespec) -> Self {
45+
v.t
46+
}
47+
}

0 commit comments

Comments
 (0)