use chrono::{Datelike, NaiveDate};
#[must_use]
#[inline]
pub fn is_weekend(date: NaiveDate) -> bool {
matches!(date.weekday(), chrono::Weekday::Sat | chrono::Weekday::Sun)
}
#[must_use]
#[inline]
pub fn is_weekday(date: NaiveDate) -> bool {
!is_weekend(date)
}
#[must_use]
#[inline]
pub fn day_of_year(date: NaiveDate) -> u32 {
date.ordinal()
}
#[must_use]
#[inline]
pub fn days_in_month(date: NaiveDate) -> u32 {
let (year, month) = if date.month() == 12 {
(date.year() + 1, 1)
} else {
(date.year(), date.month() + 1)
};
NaiveDate::from_ymd_opt(year, month, 1)
.expect("valid next-month date")
.pred_opt()
.expect("valid predecessor")
.day()
}
#[must_use]
#[inline]
pub fn week_of_year(date: NaiveDate) -> u32 {
date.iso_week().week()
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::NaiveDate;
#[test]
fn test_is_weekend_saturday() {
let date = NaiveDate::from_ymd_opt(2026, 2, 21).unwrap(); assert!(is_weekend(date));
assert!(!is_weekday(date));
}
#[test]
fn test_is_weekend_sunday() {
let date = NaiveDate::from_ymd_opt(2026, 2, 22).unwrap(); assert!(is_weekend(date));
assert!(!is_weekday(date));
}
#[test]
fn test_is_weekday_monday() {
let date = NaiveDate::from_ymd_opt(2026, 2, 23).unwrap(); assert!(is_weekday(date));
assert!(!is_weekend(date));
}
#[test]
fn test_is_weekday_friday() {
let date = NaiveDate::from_ymd_opt(2026, 2, 27).unwrap(); assert!(is_weekday(date));
assert!(!is_weekend(date));
}
#[test]
fn test_is_weekend_covers_all_days_of_week() {
let monday = NaiveDate::from_ymd_opt(2026, 2, 23).unwrap();
let expected = [false, false, false, false, false, true, true]; for (i, &expected_weekend) in expected.iter().enumerate() {
let date = monday + chrono::Duration::days(i as i64);
assert_eq!(
is_weekend(date),
expected_weekend,
"failed for {date} (offset {i})"
);
}
}
#[test]
fn test_day_of_year_jan_1() {
let date = NaiveDate::from_ymd_opt(2026, 1, 1).unwrap();
assert_eq!(day_of_year(date), 1);
}
#[test]
fn test_day_of_year_dec_31_non_leap() {
let date = NaiveDate::from_ymd_opt(2026, 12, 31).unwrap();
assert_eq!(day_of_year(date), 365);
}
#[test]
fn test_day_of_year_dec_31_leap() {
let date = NaiveDate::from_ymd_opt(2028, 12, 31).unwrap();
assert_eq!(day_of_year(date), 366);
}
#[test]
fn test_day_of_year_leap_day() {
let date = NaiveDate::from_ymd_opt(2028, 2, 29).unwrap();
assert_eq!(day_of_year(date), 60);
}
#[test]
fn test_day_of_year_mar_1_after_leap() {
let date = NaiveDate::from_ymd_opt(2028, 3, 1).unwrap();
assert_eq!(day_of_year(date), 61);
}
#[test]
fn test_day_of_year_mar_1_non_leap() {
let date = NaiveDate::from_ymd_opt(2026, 3, 1).unwrap();
assert_eq!(day_of_year(date), 60);
}
#[test]
fn test_days_in_month_january() {
assert_eq!(
days_in_month(NaiveDate::from_ymd_opt(2026, 1, 1).unwrap()),
31
);
}
#[test]
fn test_days_in_month_february_non_leap() {
assert_eq!(
days_in_month(NaiveDate::from_ymd_opt(2026, 2, 1).unwrap()),
28
);
}
#[test]
fn test_days_in_month_february_leap_divisible_by_4() {
assert_eq!(
days_in_month(NaiveDate::from_ymd_opt(2028, 2, 1).unwrap()),
29
);
}
#[test]
fn test_days_in_month_february_century_non_leap() {
assert_eq!(
days_in_month(NaiveDate::from_ymd_opt(2100, 2, 1).unwrap()),
28
);
}
#[test]
fn test_days_in_month_february_400_year_leap() {
assert_eq!(
days_in_month(NaiveDate::from_ymd_opt(2000, 2, 1).unwrap()),
29
);
}
#[test]
fn test_days_in_month_april() {
assert_eq!(
days_in_month(NaiveDate::from_ymd_opt(2026, 4, 1).unwrap()),
30
);
}
#[test]
fn test_days_in_month_december() {
assert_eq!(
days_in_month(NaiveDate::from_ymd_opt(2026, 12, 15).unwrap()),
31
);
}
#[test]
fn test_days_in_month_result_same_regardless_of_day_input() {
let d1 = NaiveDate::from_ymd_opt(2026, 3, 1).unwrap();
let d2 = NaiveDate::from_ymd_opt(2026, 3, 15).unwrap();
let d3 = NaiveDate::from_ymd_opt(2026, 3, 31).unwrap();
assert_eq!(days_in_month(d1), 31);
assert_eq!(days_in_month(d2), 31);
assert_eq!(days_in_month(d3), 31);
}
#[test]
fn test_week_of_year_jan_1_2026() {
let date = NaiveDate::from_ymd_opt(2026, 1, 1).unwrap();
assert_eq!(week_of_year(date), 1);
}
#[test]
fn test_week_of_year_jan_1_2016() {
let date = NaiveDate::from_ymd_opt(2016, 1, 1).unwrap();
assert_eq!(week_of_year(date), 53);
}
#[test]
fn test_week_of_year_feb_22_2026() {
let date = NaiveDate::from_ymd_opt(2026, 2, 22).unwrap();
assert_eq!(week_of_year(date), 8);
}
#[test]
fn test_week_of_year_dec_28() {
let date = NaiveDate::from_ymd_opt(2026, 12, 28).unwrap();
assert_eq!(week_of_year(date), 53);
}
#[test]
fn test_week_of_year_dec_31_2026() {
let date = NaiveDate::from_ymd_opt(2026, 12, 31).unwrap();
assert_eq!(week_of_year(date), 53);
}
}