4
4
5
5
//! This module contains various types used by `icu_calendar` and `icu::datetime`
6
6
7
+ use calendrical_calculations:: rata_die:: RataDie ;
7
8
use core:: fmt;
8
9
use core:: num:: NonZeroU8 ;
9
10
use tinystr:: TinyAsciiStr ;
@@ -420,23 +421,13 @@ pub enum Weekday {
420
421
Sunday ,
421
422
}
422
423
423
- impl From < usize > for Weekday {
424
- /// Convert from an ISO-8601 weekday number to an [`Weekday`] enum. 0 is automatically converted
425
- /// to 7 (Sunday). If the number is out of range, it is interpreted modulo 7.
426
- ///
427
- /// # Examples
428
- ///
429
- /// ```
430
- /// use icu::calendar::types::Weekday;
431
- ///
432
- /// assert_eq!(Weekday::Sunday, Weekday::from(0));
433
- /// assert_eq!(Weekday::Monday, Weekday::from(1));
434
- /// assert_eq!(Weekday::Sunday, Weekday::from(7));
435
- /// assert_eq!(Weekday::Monday, Weekday::from(8));
436
- /// ```
437
- fn from ( input : usize ) -> Self {
424
+ // RD 0 is a Sunday
425
+ const SUNDAY : RataDie = RataDie :: new ( 0 ) ;
426
+
427
+ impl From < RataDie > for Weekday {
428
+ fn from ( value : RataDie ) -> Self {
438
429
use Weekday :: * ;
439
- match input % 7 {
430
+ match ( value - SUNDAY ) . rem_euclid ( 7 ) {
440
431
0 => Sunday ,
441
432
1 => Monday ,
442
433
2 => Tuesday ,
@@ -450,6 +441,23 @@ impl From<usize> for Weekday {
450
441
}
451
442
452
443
impl Weekday {
444
+ /// Convert from an ISO-8601 weekday number to an [`Weekday`] enum. 0 is automatically converted
445
+ /// to 7 (Sunday). If the number is out of range, it is interpreted modulo 7.
446
+ ///
447
+ /// # Examples
448
+ ///
449
+ /// ```
450
+ /// use icu::calendar::types::Weekday;
451
+ ///
452
+ /// assert_eq!(Weekday::Sunday, Weekday::from_days_since_sunday(0));
453
+ /// assert_eq!(Weekday::Monday, Weekday::from_days_since_sunday(1));
454
+ /// assert_eq!(Weekday::Sunday, Weekday::from_days_since_sunday(7));
455
+ /// assert_eq!(Weekday::Monday, Weekday::from_days_since_sunday(8));
456
+ /// ```
457
+ pub fn from_days_since_sunday ( input : isize ) -> Self {
458
+ ( SUNDAY + input as i64 ) . into ( )
459
+ }
460
+
453
461
/// Returns the day after the current day.
454
462
pub ( crate ) fn next_day ( self ) -> Weekday {
455
463
use Weekday :: * ;
0 commit comments