File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed
Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -46,6 +46,7 @@ pub mod std_vendor;
4646pub mod str;
4747pub mod sync;
4848pub mod task;
49+ pub mod time;
4950pub mod types;
5051
5152#[ doc( hidden) ]
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments