| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
//! Functions for region-specific weekday information. |
| 6 |
|
| 7 |
pub use crate::preferences::WeekPreferences; |
| 8 |
|
| 9 |
use crate::preferences::FirstDay; |
| 10 |
use crate::{error::RangeError, provider::*, types::Weekday}; |
| 11 |
use icu_provider::prelude::*; |
| 12 |
|
| 13 |
/// Minimum number of days in a month unit required for using this module |
| 14 |
const MIN_UNIT_DAYS: u16 = 14; |
| 15 |
|
| 16 |
/// Information about the first day of the week and the weekend. |
| 17 |
/// |
| 18 |
/// # Examples |
| 19 |
/// |
| 20 |
/// ``` |
| 21 |
/// use icu::calendar::types::Weekday; |
| 22 |
/// use icu::calendar::week::WeekInformation; |
| 23 |
/// use icu::locale::{Locale, locale}; |
| 24 |
/// |
| 25 |
/// // For en-US |
| 26 |
/// let info = WeekInformation::try_new(locale!("en-US").into()).unwrap(); |
| 27 |
/// assert_eq!(info.first_weekday, Weekday::Sunday); |
| 28 |
/// |
| 29 |
/// // For de-DE |
| 30 |
/// let info = WeekInformation::try_new(locale!("de-DE").into()).unwrap(); |
| 31 |
/// assert_eq!(info.first_weekday, Weekday::Monday); |
| 32 |
/// |
| 33 |
/// // For en-US with a Germany region preference override |
| 34 |
/// let info = |
| 35 |
/// WeekInformation::try_new(locale!("en-US-u-rg-dezzzz").into()).unwrap(); |
| 36 |
/// assert_eq!(info.first_weekday, Weekday::Monday); |
| 37 |
/// |
| 38 |
/// // As above but also with a First Weekday override |
| 39 |
/// let info = WeekInformation::try_new( |
| 40 |
/// Locale::try_from_str("en-US-u-rg-dezzzz-fw-wed") |
| 41 |
/// .unwrap() |
| 42 |
/// .into(), |
| 43 |
/// ) |
| 44 |
/// .unwrap(); |
| 45 |
/// assert_eq!(info.first_weekday, Weekday::Wednesday); |
| 46 |
/// ``` |
| 47 |
#[derive(Clone, Copy, Debug)] |
| 48 |
#[non_exhaustive] |
| 49 |
pub struct WeekInformation { |
| 50 |
/// The first day of a week. |
| 51 |
pub first_weekday: Weekday, |
| 52 |
/// The set of weekend days |
| 53 |
pub weekend: WeekdaySet, |
| 54 |
} |
| 55 |
|
| 56 |
impl WeekInformation { |
| 57 |
icu_provider::gen_buffer_data_constructors!( |
| 58 |
(prefs: WeekPreferences) -> error: DataError, |
| 59 |
/// Creates a new [`WeekCalculator`] from compiled data. |
| 60 |
); |
| 61 |
|
| 62 |
#[doc = icu_provider::gen_buffer_unstable_docs!(UNSTABLE, Self::try_new)] |
| 63 |
pub fn try_new_unstable<P>(provider: &P, prefs: WeekPreferences) -> Result<Self, DataError> |
| 64 |
where |
| 65 |
P: DataProvider<CalendarWeekV1> + ?Sized, |
| 66 |
{ |
| 67 |
let locale = CalendarWeekV1::make_locale(prefs.locale_preferences); |
| 68 |
provider |
| 69 |
.load(DataRequest { |
| 70 |
id: DataIdentifierBorrowed::for_locale(&locale), |
| 71 |
..Default::default() |
| 72 |
}) |
| 73 |
.map(|response| WeekInformation { |
| 74 |
first_weekday: match prefs.first_weekday { |
| 75 |
Some(FirstDay::Mon) => Weekday::Monday, |
| 76 |
Some(FirstDay::Tue) => Weekday::Tuesday, |
| 77 |
Some(FirstDay::Wed) => Weekday::Wednesday, |
| 78 |
Some(FirstDay::Thu) => Weekday::Thursday, |
| 79 |
Some(FirstDay::Fri) => Weekday::Friday, |
| 80 |
Some(FirstDay::Sat) => Weekday::Saturday, |
| 81 |
Some(FirstDay::Sun) => Weekday::Sunday, |
| 82 |
_ => response.payload.get().first_weekday, |
| 83 |
}, |
| 84 |
weekend: response.payload.get().weekend, |
| 85 |
}) |
| 86 |
} |
| 87 |
|
| 88 |
/// Weekdays that are part of the 'weekend', for calendar purposes. |
| 89 |
/// Days may not be contiguous, and order is based off the first weekday. |
| 90 |
pub fn weekend(self) -> WeekdaySetIterator { |
| 91 |
WeekdaySetIterator::new(self.first_weekday, self.weekend) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
#[derive(Clone, Copy, Debug)] |
| 96 |
pub(crate) struct WeekCalculator { |
| 97 |
first_weekday: Weekday, |
| 98 |
min_week_days: u8, |
| 99 |
} |
| 100 |
|
| 101 |
impl WeekCalculator { |
| 102 |
pub(crate) const ISO: Self = Self { |
| 103 |
first_weekday: Weekday::Monday, |
| 104 |
min_week_days: 4, |
| 105 |
}; |
| 106 |
|
| 107 |
/// Returns the zero based index of `weekday` vs this calendar's start of week. |
| 108 |
fn weekday_index(self, weekday: Weekday) -> i8 { |
| 109 |
(7 + (weekday as i8) - (self.first_weekday as i8)) % 7 |
| 110 |
} |
| 111 |
|
| 112 |
/// Computes & returns the week of given month/year according to `calendar`. |
| 113 |
/// |
| 114 |
/// # Arguments |
| 115 |
/// - calendar: Calendar information used to compute the week number. |
| 116 |
/// - `num_days_in_previous_unit`: The number of days in the preceding month/year. |
| 117 |
/// - `num_days_in_unit`: The number of days in the month/year. |
| 118 |
/// - day: 1-based day of month/year. |
| 119 |
/// - `week_day`: The weekday of `day`.. |
| 120 |
/// |
| 121 |
/// # Error |
| 122 |
/// If `num_days_in_unit/num_days_in_previous_unit` < `MIN_UNIT_DAYS` |
| 123 |
pub(crate) fn week_of( |
| 124 |
self, |
| 125 |
num_days_in_previous_unit: u16, |
| 126 |
num_days_in_unit: u16, |
| 127 |
day: u16, |
| 128 |
weekday: Weekday, |
| 129 |
) -> Result<WeekOf, RangeError> { |
| 130 |
let current = UnitInfo::new( |
| 131 |
// The first day of this month/year is (day - 1) days from `day`. |
| 132 |
add_to_weekday(weekday, 1 - i32::from(day)), |
| 133 |
num_days_in_unit, |
| 134 |
)?; |
| 135 |
|
| 136 |
match current.relative_week(self, day) { |
| 137 |
RelativeWeek::LastWeekOfPreviousUnit => { |
| 138 |
let previous = UnitInfo::new( |
| 139 |
add_to_weekday(current.first_day, -i32::from(num_days_in_previous_unit)), |
| 140 |
num_days_in_previous_unit, |
| 141 |
)?; |
| 142 |
|
| 143 |
Ok(WeekOf { |
| 144 |
week: previous.num_weeks(self), |
| 145 |
unit: RelativeUnit::Previous, |
| 146 |
}) |
| 147 |
} |
| 148 |
RelativeWeek::WeekOfCurrentUnit(w) => Ok(WeekOf { |
| 149 |
week: w, |
| 150 |
unit: RelativeUnit::Current, |
| 151 |
}), |
| 152 |
RelativeWeek::FirstWeekOfNextUnit => Ok(WeekOf { |
| 153 |
week: 1, |
| 154 |
unit: RelativeUnit::Next, |
| 155 |
}), |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/// Returns the weekday that's `num_days` after `weekday`. |
| 161 |
fn add_to_weekday(weekday: Weekday, num_days: i32) -> Weekday { |
| 162 |
let new_weekday = (7 + (weekday as i32) + (num_days % 7)) % 7; |
| 163 |
Weekday::from_days_since_sunday(new_weekday as isize) |
| 164 |
} |
| 165 |
|
| 166 |
/// Which year or month that a calendar assigns a week to relative to the year/month |
| 167 |
/// the week is in. |
| 168 |
#[derive(Clone, Copy, Debug, PartialEq)] |
| 169 |
#[expect(clippy::enum_variant_names)] |
| 170 |
enum RelativeWeek { |
| 171 |
/// A week that is assigned to the last week of the previous year/month. e.g. 2021-01-01 is week 54 of 2020 per the ISO calendar. |
| 172 |
LastWeekOfPreviousUnit, |
| 173 |
/// A week that's assigned to the current year/month. The offset is 1-based. e.g. 2021-01-11 is week 2 of 2021 per the ISO calendar so would be WeekOfCurrentUnit(2). |
| 174 |
WeekOfCurrentUnit(u8), |
| 175 |
/// A week that is assigned to the first week of the next year/month. e.g. 2019-12-31 is week 1 of 2020 per the ISO calendar. |
| 176 |
FirstWeekOfNextUnit, |
| 177 |
} |
| 178 |
|
| 179 |
/// Information about a year or month. |
| 180 |
#[derive(Clone, Copy)] |
| 181 |
struct UnitInfo { |
| 182 |
/// The weekday of this year/month's first day. |
| 183 |
first_day: Weekday, |
| 184 |
/// The number of days in this year/month. |
| 185 |
duration_days: u16, |
| 186 |
} |
| 187 |
|
| 188 |
impl UnitInfo { |
| 189 |
/// Creates a [`UnitInfo`] for a given year or month. |
| 190 |
fn new(first_day: Weekday, duration_days: u16) -> Result<UnitInfo, RangeError> { |
| 191 |
if duration_days < MIN_UNIT_DAYS { |
| 192 |
return Err(RangeError { |
| 193 |
field: "num_days_in_unit", |
| 194 |
value: duration_days as i32, |
| 195 |
min: MIN_UNIT_DAYS as i32, |
| 196 |
max: i32::MAX, |
| 197 |
}); |
| 198 |
} |
| 199 |
Ok(UnitInfo { |
| 200 |
first_day, |
| 201 |
duration_days, |
| 202 |
}) |
| 203 |
} |
| 204 |
|
| 205 |
/// Returns the start of this unit's first week. |
| 206 |
/// |
| 207 |
/// The returned value can be negative if this unit's first week started during the previous |
| 208 |
/// unit. |
| 209 |
fn first_week_offset(self, calendar: WeekCalculator) -> i8 { |
| 210 |
let first_day_index = calendar.weekday_index(self.first_day); |
| 211 |
if 7 - first_day_index >= calendar.min_week_days as i8 { |
| 212 |
-first_day_index |
| 213 |
} else { |
| 214 |
7 - first_day_index |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
/// Returns the number of weeks in this unit according to `calendar`. |
| 219 |
fn num_weeks(self, calendar: WeekCalculator) -> u8 { |
| 220 |
let first_week_offset = self.first_week_offset(calendar); |
| 221 |
let num_days_including_first_week = |
| 222 |
(self.duration_days as i32) - (first_week_offset as i32); |
| 223 |
debug_assert!( |
| 224 |
num_days_including_first_week >= 0, |
| 225 |
"Unit is shorter than a week." |
| 226 |
); |
| 227 |
((num_days_including_first_week + 7 - (calendar.min_week_days as i32)) / 7) as u8 |
| 228 |
} |
| 229 |
|
| 230 |
/// Returns the week number for the given day in this unit. |
| 231 |
fn relative_week(self, calendar: WeekCalculator, day: u16) -> RelativeWeek { |
| 232 |
let days_since_first_week = |
| 233 |
i32::from(day) - i32::from(self.first_week_offset(calendar)) - 1; |
| 234 |
if days_since_first_week < 0 { |
| 235 |
return RelativeWeek::LastWeekOfPreviousUnit; |
| 236 |
} |
| 237 |
|
| 238 |
let week_number = (1 + days_since_first_week / 7) as u8; |
| 239 |
if week_number > self.num_weeks(calendar) { |
| 240 |
return RelativeWeek::FirstWeekOfNextUnit; |
| 241 |
} |
| 242 |
RelativeWeek::WeekOfCurrentUnit(week_number) |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
/// The year or month that a calendar assigns a week to relative to the year/month that it is in. |
| 247 |
#[derive(Debug, PartialEq)] |
| 248 |
#[allow(clippy::exhaustive_enums)] // this type is stable |
| 249 |
pub(crate) enum RelativeUnit { |
| 250 |
/// A week that is assigned to previous year/month. e.g. 2021-01-01 is week 54 of 2020 per the ISO calendar. |
| 251 |
Previous, |
| 252 |
/// A week that's assigned to the current year/month. e.g. 2021-01-11 is week 2 of 2021 per the ISO calendar. |
| 253 |
Current, |
| 254 |
/// A week that is assigned to the next year/month. e.g. 2019-12-31 is week 1 of 2020 per the ISO calendar. |
| 255 |
Next, |
| 256 |
} |
| 257 |
|
| 258 |
/// The week number assigned to a given week according to a calendar. |
| 259 |
#[derive(Debug, PartialEq)] |
| 260 |
#[allow(clippy::exhaustive_structs)] // this type is stable |
| 261 |
pub(crate) struct WeekOf { |
| 262 |
/// Week of month/year. 1 based. |
| 263 |
pub week: u8, |
| 264 |
/// The month/year that this week is in, relative to the month/year of the input date. |
| 265 |
pub unit: RelativeUnit, |
| 266 |
} |
| 267 |
|
| 268 |
/// [Iterator] that yields weekdays that are part of the weekend. |
| 269 |
#[derive(Clone, Copy, Debug, PartialEq)] |
| 270 |
pub struct WeekdaySetIterator { |
| 271 |
/// Determines the order in which we should start reading values from `weekend`. |
| 272 |
first_weekday: Weekday, |
| 273 |
/// Day being evaluated. |
| 274 |
current_day: Weekday, |
| 275 |
/// Bitset to read weekdays from. |
| 276 |
weekend: WeekdaySet, |
| 277 |
} |
| 278 |
|
| 279 |
impl WeekdaySetIterator { |
| 280 |
/// Creates the Iterator. Sets `current_day` to the day after `first_weekday`. |
| 281 |
pub(crate) fn new(first_weekday: Weekday, weekend: WeekdaySet) -> Self { |
| 282 |
WeekdaySetIterator { |
| 283 |
first_weekday, |
| 284 |
current_day: first_weekday, |
| 285 |
weekend, |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
impl Iterator for WeekdaySetIterator { |
| 291 |
type Item = Weekday; |
| 292 |
|
| 293 |
fn next(&mut self) -> Option<Self::Item> { |
| 294 |
// Check each bit until we find one that is ON or until we are back to the start of the week. |
| 295 |
while self.current_day.next_day() != self.first_weekday { |
| 296 |
if self.weekend.contains(self.current_day) { |
| 297 |
let result = self.current_day; |
| 298 |
self.current_day = self.current_day.next_day(); |
| 299 |
return Some(result); |
| 300 |
} else { |
| 301 |
self.current_day = self.current_day.next_day(); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
if self.weekend.contains(self.current_day) { |
| 306 |
// Clear weekend, we've seen all bits. |
| 307 |
// Breaks the loop next time `next()` is called |
| 308 |
self.weekend = WeekdaySet::new(&[]); |
| 309 |
return Some(self.current_day); |
| 310 |
} |
| 311 |
|
| 312 |
None |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
#[cfg(test)] |
| 317 |
mod tests { |
| 318 |
use super::*; |
| 319 |
use crate::{Date, RangeError, types::DateDuration, types::Weekday}; |
| 320 |
|
| 321 |
static ISO_CALENDAR: WeekCalculator = WeekCalculator { |
| 322 |
first_weekday: Weekday::Monday, |
| 323 |
min_week_days: 4, |
| 324 |
}; |
| 325 |
|
| 326 |
static AE_CALENDAR: WeekCalculator = WeekCalculator { |
| 327 |
first_weekday: Weekday::Saturday, |
| 328 |
min_week_days: 4, |
| 329 |
}; |
| 330 |
|
| 331 |
static US_CALENDAR: WeekCalculator = WeekCalculator { |
| 332 |
first_weekday: Weekday::Sunday, |
| 333 |
min_week_days: 1, |
| 334 |
}; |
| 335 |
|
| 336 |
#[test] |
| 337 |
fn test_weekday_index() { |
| 338 |
assert_eq!(ISO_CALENDAR.weekday_index(Weekday::Monday), 0); |
| 339 |
assert_eq!(ISO_CALENDAR.weekday_index(Weekday::Sunday), 6); |
| 340 |
|
| 341 |
assert_eq!(AE_CALENDAR.weekday_index(Weekday::Saturday), 0); |
| 342 |
assert_eq!(AE_CALENDAR.weekday_index(Weekday::Friday), 6); |
| 343 |
} |
| 344 |
|
| 345 |
#[test] |
| 346 |
fn test_first_week_offset() { |
| 347 |
let first_week_offset = |
| 348 |
|calendar, day| UnitInfo::new(day, 30).unwrap().first_week_offset(calendar); |
| 349 |
assert_eq!(first_week_offset(ISO_CALENDAR, Weekday::Monday), 0); |
| 350 |
assert_eq!(first_week_offset(ISO_CALENDAR, Weekday::Tuesday), -1); |
| 351 |
assert_eq!(first_week_offset(ISO_CALENDAR, Weekday::Wednesday), -2); |
| 352 |
assert_eq!(first_week_offset(ISO_CALENDAR, Weekday::Thursday), -3); |
| 353 |
assert_eq!(first_week_offset(ISO_CALENDAR, Weekday::Friday), 3); |
| 354 |
assert_eq!(first_week_offset(ISO_CALENDAR, Weekday::Saturday), 2); |
| 355 |
assert_eq!(first_week_offset(ISO_CALENDAR, Weekday::Sunday), 1); |
| 356 |
|
| 357 |
assert_eq!(first_week_offset(AE_CALENDAR, Weekday::Saturday), 0); |
| 358 |
assert_eq!(first_week_offset(AE_CALENDAR, Weekday::Sunday), -1); |
| 359 |
assert_eq!(first_week_offset(AE_CALENDAR, Weekday::Monday), -2); |
| 360 |
assert_eq!(first_week_offset(AE_CALENDAR, Weekday::Tuesday), -3); |
| 361 |
assert_eq!(first_week_offset(AE_CALENDAR, Weekday::Wednesday), 3); |
| 362 |
assert_eq!(first_week_offset(AE_CALENDAR, Weekday::Thursday), 2); |
| 363 |
assert_eq!(first_week_offset(AE_CALENDAR, Weekday::Friday), 1); |
| 364 |
|
| 365 |
assert_eq!(first_week_offset(US_CALENDAR, Weekday::Sunday), 0); |
| 366 |
assert_eq!(first_week_offset(US_CALENDAR, Weekday::Monday), -1); |
| 367 |
assert_eq!(first_week_offset(US_CALENDAR, Weekday::Tuesday), -2); |
| 368 |
assert_eq!(first_week_offset(US_CALENDAR, Weekday::Wednesday), -3); |
| 369 |
assert_eq!(first_week_offset(US_CALENDAR, Weekday::Thursday), -4); |
| 370 |
assert_eq!(first_week_offset(US_CALENDAR, Weekday::Friday), -5); |
| 371 |
assert_eq!(first_week_offset(US_CALENDAR, Weekday::Saturday), -6); |
| 372 |
} |
| 373 |
|
| 374 |
#[test] |
| 375 |
fn test_num_weeks() { |
| 376 |
// 4 days in first & last week. |
| 377 |
assert_eq!( |
| 378 |
UnitInfo::new(Weekday::Thursday, 4 + 2 * 7 + 4) |
| 379 |
.unwrap() |
| 380 |
.num_weeks(ISO_CALENDAR), |
| 381 |
4 |
| 382 |
); |
| 383 |
// 3 days in first week, 4 in last week. |
| 384 |
assert_eq!( |
| 385 |
UnitInfo::new(Weekday::Friday, 3 + 2 * 7 + 4) |
| 386 |
.unwrap() |
| 387 |
.num_weeks(ISO_CALENDAR), |
| 388 |
3 |
| 389 |
); |
| 390 |
// 3 days in first & last week. |
| 391 |
assert_eq!( |
| 392 |
UnitInfo::new(Weekday::Friday, 3 + 2 * 7 + 3) |
| 393 |
.unwrap() |
| 394 |
.num_weeks(ISO_CALENDAR), |
| 395 |
2 |
| 396 |
); |
| 397 |
|
| 398 |
// 1 day in first & last week. |
| 399 |
assert_eq!( |
| 400 |
UnitInfo::new(Weekday::Saturday, 1 + 2 * 7 + 1) |
| 401 |
.unwrap() |
| 402 |
.num_weeks(US_CALENDAR), |
| 403 |
4 |
| 404 |
); |
| 405 |
} |
| 406 |
|
| 407 |
/// Uses enumeration & bucketing to assign each day of a month or year `unit` to a week. |
| 408 |
/// |
| 409 |
/// This alternative implementation serves as an exhaustive safety check |
| 410 |
/// of `relative_week()` (in addition to the manual test points used |
| 411 |
/// for testing `week_of()`). |
| 412 |
fn classify_days_of_unit(calendar: WeekCalculator, unit: UnitInfo) -> Vec<RelativeWeek> { |
| 413 |
let mut weeks: Vec<Vec<Weekday>> = Vec::new(); |
| 414 |
for day_index in 0..unit.duration_days { |
| 415 |
let day = add_to_weekday(unit.first_day, i32::from(day_index)); |
| 416 |
if day == calendar.first_weekday || weeks.is_empty() { |
| 417 |
weeks.push(Vec::new()); |
| 418 |
} |
| 419 |
weeks.last_mut().unwrap().push(day); |
| 420 |
} |
| 421 |
|
| 422 |
let mut day_week_of_units = Vec::new(); |
| 423 |
let mut weeks_in_unit = 0; |
| 424 |
for (index, week) in weeks.iter().enumerate() { |
| 425 |
let week_of_unit = if week.len() < usize::from(calendar.min_week_days) { |
| 426 |
match index { |
| 427 |
0 => RelativeWeek::LastWeekOfPreviousUnit, |
| 428 |
x if x == weeks.len() - 1 => RelativeWeek::FirstWeekOfNextUnit, |
| 429 |
_ => panic!(), |
| 430 |
} |
| 431 |
} else { |
| 432 |
weeks_in_unit += 1; |
| 433 |
RelativeWeek::WeekOfCurrentUnit(weeks_in_unit) |
| 434 |
}; |
| 435 |
|
| 436 |
day_week_of_units.append(&mut [week_of_unit].repeat(week.len())); |
| 437 |
} |
| 438 |
day_week_of_units |
| 439 |
} |
| 440 |
|
| 441 |
#[test] |
| 442 |
fn test_relative_week_of_month() { |
| 443 |
for min_week_days in 1..7 { |
| 444 |
for start_of_week in 1..7 { |
| 445 |
let calendar = WeekCalculator { |
| 446 |
first_weekday: Weekday::from_days_since_sunday(start_of_week), |
| 447 |
min_week_days, |
| 448 |
}; |
| 449 |
for unit_duration in MIN_UNIT_DAYS..400 { |
| 450 |
for start_of_unit in 1..7 { |
| 451 |
let unit = UnitInfo::new( |
| 452 |
Weekday::from_days_since_sunday(start_of_unit), |
| 453 |
unit_duration, |
| 454 |
) |
| 455 |
.unwrap(); |
| 456 |
let expected = classify_days_of_unit(calendar, unit); |
| 457 |
for (index, expected_week_of) in expected.iter().enumerate() { |
| 458 |
let day = index + 1; |
| 459 |
assert_eq!( |
| 460 |
unit.relative_week(calendar, day as u16), |
| 461 |
*expected_week_of, |
| 462 |
"For the {day}/{unit_duration} starting on Weekday \ |
| 463 |
{start_of_unit} using start_of_week {start_of_week} \ |
| 464 |
& min_week_days {min_week_days}" |
| 465 |
); |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
fn week_of_month_from_iso_date( |
| 474 |
calendar: WeekCalculator, |
| 475 |
yyyymmdd: u32, |
| 476 |
) -> Result<WeekOf, RangeError> { |
| 477 |
let year = (yyyymmdd / 10000) as i32; |
| 478 |
let month = ((yyyymmdd / 100) % 100) as u8; |
| 479 |
let day = (yyyymmdd % 100) as u8; |
| 480 |
|
| 481 |
let date = Date::try_new_iso(year, month, day)?; |
| 482 |
let previous_month = date |
| 483 |
.try_added_with_options(DateDuration::for_months(-1), Default::default()) |
| 484 |
.unwrap(); |
| 485 |
|
| 486 |
calendar.week_of( |
| 487 |
u16::from(previous_month.days_in_month()), |
| 488 |
u16::from(date.days_in_month()), |
| 489 |
u16::from(day), |
| 490 |
date.weekday(), |
| 491 |
) |
| 492 |
} |
| 493 |
|
| 494 |
#[test] |
| 495 |
fn test_week_of_month_using_dates() { |
| 496 |
assert_eq!( |
| 497 |
week_of_month_from_iso_date(ISO_CALENDAR, 20210418).unwrap(), |
| 498 |
WeekOf { |
| 499 |
week: 3, |
| 500 |
unit: RelativeUnit::Current, |
| 501 |
} |
| 502 |
); |
| 503 |
assert_eq!( |
| 504 |
week_of_month_from_iso_date(ISO_CALENDAR, 20210419).unwrap(), |
| 505 |
WeekOf { |
| 506 |
week: 4, |
| 507 |
unit: RelativeUnit::Current, |
| 508 |
} |
| 509 |
); |
| 510 |
|
| 511 |
// First day of year is a Thursday. |
| 512 |
assert_eq!( |
| 513 |
week_of_month_from_iso_date(ISO_CALENDAR, 20180101).unwrap(), |
| 514 |
WeekOf { |
| 515 |
week: 1, |
| 516 |
unit: RelativeUnit::Current, |
| 517 |
} |
| 518 |
); |
| 519 |
// First day of year is a Friday. |
| 520 |
assert_eq!( |
| 521 |
week_of_month_from_iso_date(ISO_CALENDAR, 20210101).unwrap(), |
| 522 |
WeekOf { |
| 523 |
week: 5, |
| 524 |
unit: RelativeUnit::Previous, |
| 525 |
} |
| 526 |
); |
| 527 |
|
| 528 |
// The month ends on a Wednesday. |
| 529 |
assert_eq!( |
| 530 |
week_of_month_from_iso_date(ISO_CALENDAR, 20200930).unwrap(), |
| 531 |
WeekOf { |
| 532 |
week: 1, |
| 533 |
unit: RelativeUnit::Next, |
| 534 |
} |
| 535 |
); |
| 536 |
// The month ends on a Thursday. |
| 537 |
assert_eq!( |
| 538 |
week_of_month_from_iso_date(ISO_CALENDAR, 20201231).unwrap(), |
| 539 |
WeekOf { |
| 540 |
week: 5, |
| 541 |
unit: RelativeUnit::Current, |
| 542 |
} |
| 543 |
); |
| 544 |
|
| 545 |
// US calendar always assigns the week to the current month. 2020-12-31 is a Thursday. |
| 546 |
assert_eq!( |
| 547 |
week_of_month_from_iso_date(US_CALENDAR, 20201231).unwrap(), |
| 548 |
WeekOf { |
| 549 |
week: 5, |
| 550 |
unit: RelativeUnit::Current, |
| 551 |
} |
| 552 |
); |
| 553 |
assert_eq!( |
| 554 |
week_of_month_from_iso_date(US_CALENDAR, 20210101).unwrap(), |
| 555 |
WeekOf { |
| 556 |
week: 1, |
| 557 |
unit: RelativeUnit::Current, |
| 558 |
} |
| 559 |
); |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
#[test] |
| 564 |
fn test_first_day() { |
| 565 |
use icu_locale_core::locale; |
| 566 |
|
| 567 |
assert_eq!( |
| 568 |
WeekInformation::try_new(locale!("und-US").into()) |
| 569 |
.unwrap() |
| 570 |
.first_weekday, |
| 571 |
Weekday::Sunday, |
| 572 |
); |
| 573 |
|
| 574 |
assert_eq!( |
| 575 |
WeekInformation::try_new(locale!("und-FR").into()) |
| 576 |
.unwrap() |
| 577 |
.first_weekday, |
| 578 |
Weekday::Monday, |
| 579 |
); |
| 580 |
|
| 581 |
assert_eq!( |
| 582 |
WeekInformation::try_new(locale!("und-FR-u-fw-tue").into()) |
| 583 |
.unwrap() |
| 584 |
.first_weekday, |
| 585 |
Weekday::Tuesday, |
| 586 |
); |
| 587 |
} |
| 588 |
|
| 589 |
#[test] |
| 590 |
fn test_weekend() { |
| 591 |
use icu_locale_core::locale; |
| 592 |
|
| 593 |
assert_eq!( |
| 594 |
WeekInformation::try_new(locale!("und").into()) |
| 595 |
.unwrap() |
| 596 |
.weekend() |
| 597 |
.collect::<Vec<_>>(), |
| 598 |
vec![Weekday::Saturday, Weekday::Sunday], |
| 599 |
); |
| 600 |
|
| 601 |
assert_eq!( |
| 602 |
WeekInformation::try_new(locale!("und-FR").into()) |
| 603 |
.unwrap() |
| 604 |
.weekend() |
| 605 |
.collect::<Vec<_>>(), |
| 606 |
vec![Weekday::Saturday, Weekday::Sunday], |
| 607 |
); |
| 608 |
|
| 609 |
assert_eq!( |
| 610 |
WeekInformation::try_new(locale!("und-IQ").into()) |
| 611 |
.unwrap() |
| 612 |
.weekend() |
| 613 |
.collect::<Vec<_>>(), |
| 614 |
vec![Weekday::Saturday, Weekday::Friday], |
| 615 |
); |
| 616 |
|
| 617 |
assert_eq!( |
| 618 |
WeekInformation::try_new(locale!("und-IR").into()) |
| 619 |
.unwrap() |
| 620 |
.weekend() |
| 621 |
.collect::<Vec<_>>(), |
| 622 |
vec![Weekday::Friday], |
| 623 |
); |
| 624 |
} |
| 625 |
|
| 626 |
#[test] |
| 627 |
fn test_weekdays_iter() { |
| 628 |
use Weekday::*; |
| 629 |
|
| 630 |
// Weekend ends one day before week starts |
| 631 |
let default_weekend = WeekdaySetIterator::new(Monday, WeekdaySet::new(&[Saturday, Sunday])); |
| 632 |
assert_eq!(vec![Saturday, Sunday], default_weekend.collect::<Vec<_>>()); |
| 633 |
|
| 634 |
// Non-contiguous weekend |
| 635 |
let fri_sun_weekend = WeekdaySetIterator::new(Monday, WeekdaySet::new(&[Friday, Sunday])); |
| 636 |
assert_eq!(vec![Friday, Sunday], fri_sun_weekend.collect::<Vec<_>>()); |
| 637 |
|
| 638 |
let multiple_contiguous_days = WeekdaySetIterator::new( |
| 639 |
Monday, |
| 640 |
WeekdaySet::new(&[Tuesday, Wednesday, Thursday, Friday]), |
| 641 |
); |
| 642 |
assert_eq!( |
| 643 |
vec![Tuesday, Wednesday, Thursday, Friday], |
| 644 |
multiple_contiguous_days.collect::<Vec<_>>() |
| 645 |
); |
| 646 |
|
| 647 |
// Non-contiguous days and iterator yielding elements based off first_weekday |
| 648 |
let multiple_non_contiguous_days = WeekdaySetIterator::new( |
| 649 |
Wednesday, |
| 650 |
WeekdaySet::new(&[Tuesday, Thursday, Friday, Sunday]), |
| 651 |
); |
| 652 |
assert_eq!( |
| 653 |
vec![Thursday, Friday, Sunday, Tuesday], |
| 654 |
multiple_non_contiguous_days.collect::<Vec<_>>() |
| 655 |
); |
| 656 |
} |
| 657 |
|
| 658 |
#[test] |
| 659 |
fn test_iso_weeks() { |
| 660 |
use crate::Date; |
| 661 |
use crate::types::IsoWeekOfYear; |
| 662 |
|
| 663 |
#[expect(clippy::zero_prefixed_literal)] |
| 664 |
for ((y, m, d), (iso_year, week_number)) in [ |
| 665 |
// 2010 starts on a Thursday, so 2009 has 53 ISO weeks |
| 666 |
((2009, 12, 30), (2009, 53)), |
| 667 |
((2009, 12, 31), (2009, 53)), |
| 668 |
((2010, 01, 01), (2009, 53)), |
| 669 |
((2010, 01, 02), (2009, 53)), |
| 670 |
((2010, 01, 03), (2009, 53)), |
| 671 |
((2010, 01, 04), (2010, 1)), |
| 672 |
((2010, 01, 05), (2010, 1)), |
| 673 |
// 2030 starts on a Monday |
| 674 |
((2029, 12, 29), (2029, 52)), |
| 675 |
((2029, 12, 30), (2029, 52)), |
| 676 |
((2029, 12, 31), (2030, 1)), |
| 677 |
((2030, 01, 01), (2030, 1)), |
| 678 |
((2030, 01, 02), (2030, 1)), |
| 679 |
((2030, 01, 03), (2030, 1)), |
| 680 |
((2030, 01, 04), (2030, 1)), |
| 681 |
] { |
| 682 |
assert_eq!( |
| 683 |
Date::try_new_iso(y, m, d).unwrap().week_of_year(), |
| 684 |
IsoWeekOfYear { |
| 685 |
iso_year, |
| 686 |
week_number |
| 687 |
} |
| 688 |
); |
| 689 |
} |
| 690 |
} |