@@ -263,7 +263,7 @@ impl Debug for RuntimeServices {
263
263
}
264
264
}
265
265
266
- /// The current time information
266
+ /// Date and time representation.
267
267
#[ derive( Copy , Clone ) ]
268
268
#[ repr( C ) ]
269
269
pub struct Time {
@@ -280,92 +280,151 @@ pub struct Time {
280
280
_pad2 : u8 ,
281
281
}
282
282
283
+ /// Input parameters for [`Time::new`].
284
+ #[ derive( Copy , Clone ) ]
285
+ pub struct TimeParams {
286
+ /// Year in the range `1900..=9999`.
287
+ pub year : u16 ,
288
+
289
+ /// Month in the range `1..=12`.
290
+ pub month : u8 ,
291
+
292
+ /// Day in the range `1..=31`.
293
+ pub day : u8 ,
294
+
295
+ /// Hour in the range `0.=23`.
296
+ pub hour : u8 ,
297
+
298
+ /// Minute in the range `0..=59`.
299
+ pub minute : u8 ,
300
+
301
+ /// Second in the range `0..=59`.
302
+ pub second : u8 ,
303
+
304
+ /// Fraction of a second represented as nanoseconds in the range
305
+ /// `0..=999_999_999`.
306
+ pub nanosecond : u32 ,
307
+
308
+ /// Offset in minutes from UTC in the range `-1440..=1440`, or
309
+ /// local time if `None`.
310
+ pub time_zone : Option < i16 > ,
311
+
312
+ /// Daylight savings time information.
313
+ pub daylight : Daylight ,
314
+ }
315
+
283
316
bitflags ! {
284
- /// Flags describing the capabilities of a memory range .
317
+ /// A bitmask containing daylight savings time information .
285
318
pub struct Daylight : u8 {
286
- /// Time is affected by daylight savings time
319
+ /// Time is affected by daylight savings time.
287
320
const ADJUST_DAYLIGHT = 0x01 ;
288
- /// Time has been adjusted for daylight savings time
321
+ /// Time has been adjusted for daylight savings time.
289
322
const IN_DAYLIGHT = 0x02 ;
290
323
}
291
324
}
292
325
326
+ /// Error returned by [`Time`] methods if the input is outside the valid range.
327
+ #[ derive( Copy , Clone , Debug , Default , Eq , PartialEq ) ]
328
+ pub struct TimeError ;
329
+
293
330
impl Time {
294
331
/// Unspecified Timezone/local time.
295
332
const UNSPECIFIED_TIMEZONE : i16 = 0x07ff ;
296
333
297
- /// Build an UEFI time struct
298
- #[ allow( clippy:: too_many_arguments) ]
299
- pub fn new (
300
- year : u16 ,
301
- month : u8 ,
302
- day : u8 ,
303
- hour : u8 ,
304
- minute : u8 ,
305
- second : u8 ,
306
- nanosecond : u32 ,
307
- time_zone : i16 ,
308
- daylight : Daylight ,
309
- ) -> Self {
310
- assert ! ( ( 1900 ..=9999 ) . contains( & year) ) ;
311
- assert ! ( ( 1 ..=12 ) . contains( & month) ) ;
312
- assert ! ( ( 1 ..=31 ) . contains( & day) ) ;
313
- assert ! ( hour <= 23 ) ;
314
- assert ! ( minute <= 59 ) ;
315
- assert ! ( second <= 59 ) ;
316
- assert ! ( nanosecond <= 999_999_999 ) ;
317
- assert ! ( ( time_zone >= -1440 && time_zone <= 1440 ) || time_zone == 2047 ) ;
334
+ /// Create a `Time` value. If a field is not in the valid range,
335
+ /// [`TimeError`] is returned.
336
+ pub fn new ( params : TimeParams ) -> core:: result:: Result < Self , TimeError > {
337
+ let time = Self {
338
+ year : params. year ,
339
+ month : params. month ,
340
+ day : params. day ,
341
+ hour : params. hour ,
342
+ minute : params. minute ,
343
+ second : params. second ,
344
+ _pad1 : 0 ,
345
+ nanosecond : params. nanosecond ,
346
+ time_zone : params. time_zone . unwrap_or ( Self :: UNSPECIFIED_TIMEZONE ) ,
347
+ daylight : params. daylight ,
348
+ _pad2 : 0 ,
349
+ } ;
350
+ if time. is_valid ( ) {
351
+ Ok ( time)
352
+ } else {
353
+ Err ( TimeError )
354
+ }
355
+ }
356
+
357
+ /// Create an invalid `Time` with all fields set to zero. This can
358
+ /// be used with [`FileInfo`] to indicate a field should not be
359
+ /// updated when calling [`File::set_info`].
360
+ ///
361
+ /// [`FileInfo`]: uefi::proto::media::file::FileInfo
362
+ /// [`File::set_info`]: uefi::proto::media::file::File::set_info
363
+ pub fn invalid ( ) -> Self {
318
364
Self {
319
- year,
320
- month,
321
- day,
322
- hour,
323
- minute,
324
- second,
365
+ year : 0 ,
366
+ month : 0 ,
367
+ day : 0 ,
368
+ hour : 0 ,
369
+ minute : 0 ,
370
+ second : 0 ,
325
371
_pad1 : 0 ,
326
- nanosecond,
327
- time_zone,
328
- daylight,
372
+ nanosecond : 0 ,
373
+ time_zone : 0 ,
374
+ daylight : Daylight :: empty ( ) ,
329
375
_pad2 : 0 ,
330
376
}
331
377
}
332
378
333
- /// Query the year
379
+ /// True if all fields are within valid ranges, false otherwise.
380
+ pub fn is_valid ( & self ) -> bool {
381
+ ( 1900 ..=9999 ) . contains ( & self . year )
382
+ && ( 1 ..=12 ) . contains ( & self . month )
383
+ && ( 1 ..=31 ) . contains ( & self . day )
384
+ && self . hour <= 23
385
+ && self . minute <= 59
386
+ && self . second <= 59
387
+ && self . nanosecond <= 999_999_999
388
+ && ( ( -1440 ..=1440 ) . contains ( & self . time_zone )
389
+ || self . time_zone == Self :: UNSPECIFIED_TIMEZONE )
390
+ }
391
+
392
+ /// Query the year.
334
393
pub fn year ( & self ) -> u16 {
335
394
self . year
336
395
}
337
396
338
- /// Query the month
397
+ /// Query the month.
339
398
pub fn month ( & self ) -> u8 {
340
399
self . month
341
400
}
342
401
343
- /// Query the day
402
+ /// Query the day.
344
403
pub fn day ( & self ) -> u8 {
345
404
self . day
346
405
}
347
406
348
- /// Query the hour
407
+ /// Query the hour.
349
408
pub fn hour ( & self ) -> u8 {
350
409
self . hour
351
410
}
352
411
353
- /// Query the minute
412
+ /// Query the minute.
354
413
pub fn minute ( & self ) -> u8 {
355
414
self . minute
356
415
}
357
416
358
- /// Query the second
417
+ /// Query the second.
359
418
pub fn second ( & self ) -> u8 {
360
419
self . second
361
420
}
362
421
363
- /// Query the nanosecond
422
+ /// Query the nanosecond.
364
423
pub fn nanosecond ( & self ) -> u32 {
365
424
self . nanosecond
366
425
}
367
426
368
- /// Query the time offset in minutes from UTC, or None if using local time
427
+ /// Query the time offset in minutes from UTC, or None if using local time.
369
428
pub fn time_zone ( & self ) -> Option < i16 > {
370
429
if self . time_zone == 2047 {
371
430
None
@@ -374,7 +433,7 @@ impl Time {
374
433
}
375
434
}
376
435
377
- /// Query the daylight savings time information
436
+ /// Query the daylight savings time information.
378
437
pub fn daylight ( & self ) -> Daylight {
379
438
self . daylight
380
439
}
0 commit comments