@@ -24,7 +24,8 @@ pub const UNIX_EPOCH: SystemTime = SystemTime::from_uefi(r_efi::efi::Time {
2424 daylight : 0 ,
2525 pad1 : 0 ,
2626 pad2 : 0 ,
27- } ) ;
27+ } )
28+ . unwrap ( ) ;
2829
2930const MAX_UEFI_TIME : SystemTime = SystemTime :: from_uefi ( r_efi:: efi:: Time {
3031 year : 9999 ,
@@ -38,7 +39,8 @@ const MAX_UEFI_TIME: SystemTime = SystemTime::from_uefi(r_efi::efi::Time {
3839 daylight : 0 ,
3940 pad1 : 0 ,
4041 pad2 : 0 ,
41- } ) ;
42+ } )
43+ . unwrap ( ) ;
4244
4345impl Instant {
4446 pub fn now ( ) -> Instant {
@@ -68,8 +70,11 @@ impl Instant {
6870}
6971
7072impl SystemTime {
71- pub ( crate ) const fn from_uefi ( t : r_efi:: efi:: Time ) -> Self {
72- Self ( system_time_internal:: from_uefi ( & t) )
73+ pub ( crate ) const fn from_uefi ( t : r_efi:: efi:: Time ) -> Option < Self > {
74+ match system_time_internal:: from_uefi ( & t) {
75+ Some ( x) => Some ( Self ( x) ) ,
76+ None => None ,
77+ }
7378 }
7479
7580 pub ( crate ) const fn to_uefi (
@@ -96,9 +101,8 @@ impl SystemTime {
96101 }
97102
98103 pub fn now ( ) -> SystemTime {
99- system_time_internal:: now ( )
100- . map ( Self :: from_uefi)
101- . unwrap_or_else ( || panic ! ( "time not implemented on this platform" ) )
104+ Self :: from_uefi ( system_time_internal:: now ( ) )
105+ . expect ( "time incorrectly implemented on this platform" )
102106 }
103107
104108 pub fn sub_time ( & self , other : & SystemTime ) -> Result < Duration , Duration > {
@@ -129,38 +133,41 @@ pub(crate) mod system_time_internal {
129133 const SECS_IN_DAY : u64 = SECS_IN_HOUR * 24 ;
130134 const SYSTEMTIME_TIMEZONE : i64 = -1440 * SECS_IN_MINUTE as i64 ;
131135
132- pub ( crate ) fn now ( ) -> Option < Time > {
133- let runtime_services: NonNull < RuntimeServices > = helpers:: runtime_services ( ) ?;
136+ pub ( crate ) fn now ( ) -> Time {
137+ let runtime_services: NonNull < RuntimeServices > =
138+ helpers:: runtime_services ( ) . expect ( "Runtime services are not available" ) ;
134139 let mut t: MaybeUninit < Time > = MaybeUninit :: uninit ( ) ;
135140 let r = unsafe {
136141 ( ( * runtime_services. as_ptr ( ) ) . get_time ) ( t. as_mut_ptr ( ) , crate :: ptr:: null_mut ( ) )
137142 } ;
138143 if r. is_error ( ) {
139- return None ;
144+ panic ! ( "time not implemented on this platform" ) ;
140145 }
141146
142- Some ( unsafe { t. assume_init ( ) } )
147+ unsafe { t. assume_init ( ) }
143148 }
144149
145150 /// This algorithm is a modified form of the one described in the post
146151 /// https://blog.reverberate.org/2020/05/12/optimizing-date-algorithms.html
147152 ///
148153 /// The changes are to use 1900-01-01-00:00:00 with timezone -1440 as anchor instead of UNIX
149154 /// epoch used in the original algorithm.
150- pub ( crate ) const fn from_uefi ( t : & Time ) -> Duration {
151- assert ! ( t. month <= 12 && t. month != 0 ) ;
152- assert ! ( t. year >= 1900 && t. year <= 9999 ) ;
153- assert ! ( t. day <= 31 && t. day != 0 ) ;
154-
155- assert ! ( t. second < 60 ) ;
156- assert ! ( t. minute < 60 ) ;
157- assert ! ( t. hour < 24 ) ;
158- assert ! ( t. nanosecond < 1_000_000_000 ) ;
159-
160- assert ! (
161- ( t. timezone <= 1440 && t. timezone >= -1440 )
162- || t. timezone == r_efi:: efi:: UNSPECIFIED_TIMEZONE
163- ) ;
155+ pub ( crate ) const fn from_uefi ( t : & Time ) -> Option < Duration > {
156+ if !( t. month <= 12
157+ && t. month != 0
158+ && t. year >= 1900
159+ && t. year <= 9999
160+ && t. day <= 31
161+ && t. day != 0
162+ && t. second < 60
163+ && t. minute <= 60
164+ && t. hour < 24
165+ && t. nanosecond < 1_000_000_000
166+ && ( ( t. timezone <= 1440 && t. timezone >= -1440 )
167+ || t. timezone == r_efi:: efi:: UNSPECIFIED_TIMEZONE ) )
168+ {
169+ return None ;
170+ }
164171
165172 const YEAR_BASE : u32 = 4800 ; /* Before min year, multiple of 400. */
166173
@@ -188,7 +195,7 @@ pub(crate) mod system_time_internal {
188195 // Calculate the offset from 1/1/1900 at timezone -1440 min
189196 let epoch = localtime_epoch. checked_add_signed ( normalized_timezone) . unwrap ( ) ;
190197
191- Duration :: new ( epoch, t. nanosecond )
198+ Some ( Duration :: new ( epoch, t. nanosecond ) )
192199 }
193200
194201 /// This algorithm is a modified version of the one described in the post:
0 commit comments