11use core:: fmt;
22use core:: ops:: { Add , AddAssign , Sub , SubAssign } ;
33
4- use super :: { Duration , GCD_1K , GCD_1M , TICK_HZ } ;
4+ use super :: { Duration , GCD_1G , GCD_1K , GCD_1M , TICK_HZ } ;
55
66#[ derive( Debug , Copy , Clone , PartialEq , Eq , PartialOrd , Ord ) ]
77#[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
@@ -29,6 +29,13 @@ impl Instant {
2929 Self { ticks }
3030 }
3131
32+ /// Create an Instant from a nanosecond count since system boot.
33+ pub const fn from_nanos ( nanos : u64 ) -> Self {
34+ Self {
35+ ticks : nanos * ( TICK_HZ / GCD_1G ) / ( 1_000_000_000 / GCD_1G ) ,
36+ }
37+ }
38+
3239 /// Create an Instant from a microsecond count since system boot.
3340 pub const fn from_micros ( micros : u64 ) -> Self {
3441 Self {
@@ -50,6 +57,17 @@ impl Instant {
5057 }
5158 }
5259
60+ /// Try to create an Instant from a nanosecond count since system boot.
61+ /// Fails if the number of nanoseconds is too large.
62+ pub const fn try_from_nanos ( nanos : u64 ) -> Option < Self > {
63+ let Some ( value) = nanos. checked_mul ( TICK_HZ / GCD_1G ) else {
64+ return None ;
65+ } ;
66+ Some ( Self {
67+ ticks : value / ( 1_000_000_000 / GCD_1G ) ,
68+ } )
69+ }
70+
5371 /// Try to create an Instant from a microsecond count since system boot.
5472 /// Fails if the number of microseconds is too large.
5573 pub const fn try_from_micros ( micros : u64 ) -> Option < Self > {
@@ -101,6 +119,11 @@ impl Instant {
101119 self . ticks * ( 1_000_000 / GCD_1M ) / ( TICK_HZ / GCD_1M )
102120 }
103121
122+ /// Nanoseconds since system boot.
123+ pub const fn as_nanos ( & self ) -> u64 {
124+ self . ticks * ( 1_000_000_000 / GCD_1G ) / ( TICK_HZ / GCD_1G )
125+ }
126+
104127 /// Duration between this Instant and another Instant
105128 /// Panics on over/underflow.
106129 pub fn duration_since ( & self , earlier : Instant ) -> Duration {
0 commit comments