Skip to content

Commit 89c1631

Browse files
Add first weekday preference to week preferences (#6615)
Per UTS-35
1 parent 6fe1953 commit 89c1631

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

components/calendar/src/week.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! Functions for region-specific weekday information.
66
77
use crate::{error::RangeError, provider::*, types::Weekday};
8-
use icu_locale_core::preferences::define_preferences;
8+
use icu_locale_core::preferences::{define_preferences, extensions::unicode::keywords::FirstDay};
99
use icu_provider::prelude::*;
1010

1111
/// Minimum number of days in a month unit required for using this module
@@ -15,7 +15,10 @@ define_preferences!(
1515
/// The preferences for the week information.
1616
[Copy]
1717
WeekPreferences,
18-
{}
18+
{
19+
/// The first day of the week
20+
first_weekday: FirstDay
21+
}
1922
);
2023

2124
/// Information about the first day of the week and the weekend.
@@ -46,7 +49,16 @@ impl WeekInformation {
4649
..Default::default()
4750
})
4851
.map(|response| WeekInformation {
49-
first_weekday: response.payload.get().first_weekday,
52+
first_weekday: match prefs.first_weekday {
53+
Some(FirstDay::Mon) => Weekday::Monday,
54+
Some(FirstDay::Tue) => Weekday::Tuesday,
55+
Some(FirstDay::Wed) => Weekday::Wednesday,
56+
Some(FirstDay::Thu) => Weekday::Thursday,
57+
Some(FirstDay::Fri) => Weekday::Friday,
58+
Some(FirstDay::Sat) => Weekday::Saturday,
59+
Some(FirstDay::Sun) => Weekday::Sunday,
60+
_ => response.payload.get().first_weekday,
61+
},
5062
weekend: response.payload.get().weekend,
5163
})
5264
}
@@ -524,6 +536,32 @@ mod tests {
524536
}
525537
}
526538

539+
#[test]
540+
fn test_first_day() {
541+
use icu_locale_core::locale;
542+
543+
assert_eq!(
544+
WeekInformation::try_new(locale!("und-US").into())
545+
.unwrap()
546+
.first_weekday,
547+
Weekday::Sunday,
548+
);
549+
550+
assert_eq!(
551+
WeekInformation::try_new(locale!("und-FR").into())
552+
.unwrap()
553+
.first_weekday,
554+
Weekday::Monday,
555+
);
556+
557+
assert_eq!(
558+
WeekInformation::try_new(locale!("und-FR-u-fw-tue").into())
559+
.unwrap()
560+
.first_weekday,
561+
Weekday::Tuesday,
562+
);
563+
}
564+
527565
#[test]
528566
fn test_weekend() {
529567
use icu_locale_core::locale;

0 commit comments

Comments
 (0)