Skip to content

Commit

Permalink
[chronotope#654] Get week start and end days
Browse files Browse the repository at this point in the history
  • Loading branch information
sestrella committed Mar 29, 2022
1 parent 4781689 commit 3003497
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion src/naive/isoweek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
//! ISO 8601 week.

use core::fmt;
use std::ops::Range;

use time::Duration;

use crate::{Weekday, NaiveDate};

use super::internals::{DateImpl, Of, YearFlags};

Expand Down Expand Up @@ -104,6 +109,21 @@ impl IsoWeek {
pub fn week0(&self) -> u32 {
((self.ywf >> 4) & 0x3f) as u32 - 1
}

/// TODO: missing docs
pub fn start_day(&self, weekday: Weekday) -> NaiveDate {
NaiveDate::from_isoywd(self.year(), self.week(), weekday)
}

/// TODO: missing docs
pub fn end_day(&self, weekday: Weekday) -> NaiveDate {
self.start_day(weekday) + Duration::days(6)
}

/// TODO: missing docs
pub fn bounds(&self, weekday: Weekday) -> Range<NaiveDate> {
self.start_day(weekday)..self.end_day(weekday)
}
}

/// The `Debug` output of the ISO week `w` is the same as
Expand Down Expand Up @@ -143,7 +163,7 @@ impl fmt::Debug for IsoWeek {
#[cfg(test)]
mod tests {
use crate::naive::{internals, MAX_DATE, MIN_DATE};
use crate::Datelike;
use crate::{Datelike, NaiveDate, Weekday};

#[test]
fn test_iso_week_extremes() {
Expand All @@ -160,4 +180,29 @@ mod tests {
assert_eq!(maxweek.week0(), 0);
assert_eq!(format!("{:?}", maxweek), MAX_DATE.format("%G-W%V").to_string());
}

#[test]
fn test_start_day() {
let week = NaiveDate::from_ymd(2022, 3, 28).iso_week();

assert_eq!(week.start_day(Weekday::Mon), NaiveDate::from_ymd(2022, 3, 28));
assert_eq!(week.start_day(Weekday::Tue), NaiveDate::from_ymd(2022, 3, 29));
}

#[test]
fn test_end_day() {
let week = NaiveDate::from_ymd(2022, 3, 28).iso_week();

assert_eq!(week.end_day(Weekday::Mon), NaiveDate::from_ymd(2022, 4, 3));
assert_eq!(week.end_day(Weekday::Tue), NaiveDate::from_ymd(2022, 4, 4));
}

#[test]
fn test_bounds() {
let date = NaiveDate::from_ymd(2022, 3, 28);
let week = date.iso_week();

assert!(week.bounds(Weekday::Mon).contains(&date));
assert!(week.bounds(Weekday::Tue).contains(&date));
}
}

0 comments on commit 3003497

Please sign in to comment.