1
1
use crate :: time:: Duration ;
2
2
3
+ const SECS_IN_MINUTE : u64 = 60 ;
4
+
3
5
#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Debug , Hash ) ]
4
6
pub struct Instant ( Duration ) ;
5
7
@@ -70,13 +72,32 @@ impl SystemTime {
70
72
Self ( system_time_internal:: from_uefi ( & t) )
71
73
}
72
74
73
- #[ expect( dead_code) ]
74
- pub ( crate ) const fn to_uefi ( self , timezone : i16 , daylight : u8 ) -> Option < r_efi:: efi:: Time > {
75
- system_time_internal:: to_uefi ( & self . 0 , timezone, daylight)
75
+ pub ( crate ) const fn to_uefi (
76
+ self ,
77
+ timezone : i16 ,
78
+ daylight : u8 ,
79
+ ) -> Result < r_efi:: efi:: Time , i16 > {
80
+ // system_time_internal::to_uefi requires a valid timezone. In case of unspecified timezone,
81
+ // we just pass 0 since it is assumed that no timezone related adjustments are required.
82
+ if timezone == r_efi:: efi:: UNSPECIFIED_TIMEZONE {
83
+ system_time_internal:: to_uefi ( & self . 0 , 0 , daylight)
84
+ } else {
85
+ system_time_internal:: to_uefi ( & self . 0 , timezone, daylight)
86
+ }
87
+ }
88
+
89
+ /// Create UEFI Time with the closest timezone (minute offset) that still allows the time to be
90
+ /// represented.
91
+ pub ( crate ) fn to_uefi_loose ( self , timezone : i16 , daylight : u8 ) -> r_efi:: efi:: Time {
92
+ match self . to_uefi ( timezone, daylight) {
93
+ Ok ( x) => x,
94
+ Err ( tz) => self . to_uefi ( tz, daylight) . unwrap ( ) ,
95
+ }
76
96
}
77
97
78
98
pub fn now ( ) -> SystemTime {
79
99
system_time_internal:: now ( )
100
+ . map ( Self :: from_uefi)
80
101
. unwrap_or_else ( || panic ! ( "time not implemented on this platform" ) )
81
102
}
82
103
@@ -117,12 +138,11 @@ pub(crate) mod system_time_internal {
117
138
use crate :: mem:: MaybeUninit ;
118
139
use crate :: ptr:: NonNull ;
119
140
120
- const SECS_IN_MINUTE : u64 = 60 ;
121
141
const SECS_IN_HOUR : u64 = SECS_IN_MINUTE * 60 ;
122
142
const SECS_IN_DAY : u64 = SECS_IN_HOUR * 24 ;
123
- const TIMEZONE_DELTA : u64 = 1440 * SECS_IN_MINUTE ;
143
+ const SYSTEMTIME_TIMEZONE : u64 = 1440 * SECS_IN_MINUTE ;
124
144
125
- pub fn now ( ) -> Option < SystemTime > {
145
+ pub ( crate ) fn now ( ) -> Option < Time > {
126
146
let runtime_services: NonNull < RuntimeServices > = helpers:: runtime_services ( ) ?;
127
147
let mut t: MaybeUninit < Time > = MaybeUninit :: uninit ( ) ;
128
148
let r = unsafe {
@@ -132,9 +152,7 @@ pub(crate) mod system_time_internal {
132
152
return None ;
133
153
}
134
154
135
- let t = unsafe { t. assume_init ( ) } ;
136
-
137
- Some ( SystemTime :: from_uefi ( t) )
155
+ Some ( unsafe { t. assume_init ( ) } )
138
156
}
139
157
140
158
/// This algorithm is a modified form of the one described in the post
@@ -175,7 +193,7 @@ pub(crate) mod system_time_internal {
175
193
+ ( t. hour as u64 ) * SECS_IN_HOUR ;
176
194
177
195
// Calculate the offset from 1/1/1900 at timezone -1440 min
178
- let adjusted_localtime_epoc: u64 = localtime_epoch + TIMEZONE_DELTA ;
196
+ let adjusted_localtime_epoc: u64 = localtime_epoch + SYSTEMTIME_TIMEZONE ;
179
197
180
198
let epoch: u64 = if t. timezone == r_efi:: efi:: UNSPECIFIED_TIMEZONE {
181
199
adjusted_localtime_epoc
@@ -193,16 +211,24 @@ pub(crate) mod system_time_internal {
193
211
///
194
212
/// The changes are to use 1900-01-01-00:00:00 with timezone -1440 as anchor instead of UNIX
195
213
/// epoch used in the original algorithm.
196
- pub ( crate ) const fn to_uefi ( dur : & Duration , timezone : i16 , daylight : u8 ) -> Option < Time > {
214
+ pub ( crate ) const fn to_uefi ( dur : & Duration , timezone : i16 , daylight : u8 ) -> Result < Time , i16 > {
215
+ const MIN_IN_HOUR : u64 = 60 ;
216
+ const MIN_IN_DAY : u64 = MIN_IN_HOUR * 24 ;
217
+
197
218
// Check timzone validity
198
219
assert ! ( timezone <= 1440 && timezone >= -1440 ) ;
199
220
200
221
// FIXME(#126043): use checked_sub_signed once stabilized
222
+ // This cannot fail for valid SystemTime due to SYSTEMTIME_TIMEZONE
201
223
let secs =
202
224
dur. as_secs ( ) . checked_add_signed ( ( -timezone as i64 ) * SECS_IN_MINUTE as i64 ) . unwrap ( ) ;
203
225
204
226
// Convert to seconds since 1900-01-01-00:00:00 in timezone.
205
- let Some ( secs) = secs. checked_sub ( TIMEZONE_DELTA ) else { return None } ;
227
+ let Some ( secs) = secs. checked_sub ( SYSTEMTIME_TIMEZONE ) else {
228
+ let new_tz =
229
+ ( secs / SECS_IN_MINUTE - if secs % SECS_IN_MINUTE == 0 { 0 } else { 1 } ) as i16 ;
230
+ return Err ( new_tz) ;
231
+ } ;
206
232
207
233
let days = secs / SECS_IN_DAY ;
208
234
let remaining_secs = secs % SECS_IN_DAY ;
@@ -225,9 +251,10 @@ pub(crate) mod system_time_internal {
225
251
let minute = ( ( remaining_secs % SECS_IN_HOUR ) / SECS_IN_MINUTE ) as u8 ;
226
252
let second = ( remaining_secs % SECS_IN_MINUTE ) as u8 ;
227
253
228
- // Check Bounds
229
- if y >= 1900 && y <= 9999 {
230
- Some ( Time {
254
+ // At this point, invalid time will be greater than MAX representable time. It cannot be less
255
+ // than minimum time since we already take care of that case above.
256
+ if y <= 9999 {
257
+ Ok ( Time {
231
258
year : y as u16 ,
232
259
month : m as u8 ,
233
260
day : d as u8 ,
@@ -241,7 +268,17 @@ pub(crate) mod system_time_internal {
241
268
pad2 : 0 ,
242
269
} )
243
270
} else {
244
- None
271
+ assert ! ( y == 10000 ) ;
272
+ assert ! ( m == 1 ) ;
273
+
274
+ let delta = ( ( d - 1 ) as u64 * MIN_IN_DAY
275
+ + hour as u64 * MIN_IN_HOUR
276
+ + minute as u64
277
+ + if second == 0 { 0 } else { 1 } ) as i16 ;
278
+ let new_tz = timezone + delta;
279
+
280
+ assert ! ( new_tz <= 1440 && new_tz >= -1440 ) ;
281
+ Err ( new_tz)
245
282
}
246
283
}
247
284
}
0 commit comments