use std::collections::BTreeSet;
use jiff::civil::{Date, Weekday};
use jiff::tz::TimeZone;
static BERLIN: TimeZone = jiff::tz::get!("Europe/Berlin");
#[must_use]
pub fn berlin() -> TimeZone {
BERLIN.clone()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Closed {
Weekend,
Holiday(&'static str),
Extra,
}
impl core::fmt::Display for Closed {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Weekend => f.write_str("weekend"),
Self::Holiday(name) => f.write_str(name),
Self::Extra => f.write_str("closed day"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Calendar {
literal: bool,
saturday: bool,
extra: BTreeSet<Date>,
}
impl Calendar {
#[must_use]
pub fn wbci() -> Self {
Self::default()
}
#[must_use]
pub fn mon_fri() -> Self {
Self { literal: true, saturday: false, extra: BTreeSet::new() }
}
#[must_use]
pub fn werktage() -> Self {
Self { literal: false, saturday: true, extra: BTreeSet::new() }
}
#[must_use]
pub const fn counts_saturday(&self) -> bool {
self.saturday
}
#[must_use]
pub fn with_closed(mut self, day: Date) -> Self {
self.extra.insert(day);
self
}
#[must_use]
pub fn closed(&self, day: Date) -> Option<Closed> {
match day.weekday() {
Weekday::Sunday => return Some(Closed::Weekend),
Weekday::Saturday if !self.saturday => return Some(Closed::Weekend),
_ => {}
}
if !self.literal
&& let Some(name) = holiday(day)
{
return Some(Closed::Holiday(name));
}
self.extra.contains(&day).then_some(Closed::Extra)
}
#[must_use]
pub fn is_working_day(&self, day: Date) -> bool {
self.closed(day).is_none()
}
#[must_use]
pub fn add_working_days(&self, day: Date, n: u32) -> Date {
self.walk(day, n, 1).0
}
#[must_use]
pub fn sub_working_days(&self, day: Date, n: u32) -> Date {
self.walk(day, n, -1).0
}
#[must_use]
pub fn walk(&self, start: Date, n: u32, step: i8) -> (Date, Vec<Date>, Vec<(Date, Closed)>) {
let mut day = start;
let mut counted = Vec::new();
let mut skipped = Vec::new();
while counted.len() < n as usize {
day = if step > 0 { day.tomorrow() } else { day.yesterday() }.unwrap_or(day);
match self.closed(day) {
None => counted.push(day),
Some(reason) => skipped.push((day, reason)),
}
if day == Date::MAX || day == Date::MIN {
break;
}
}
(day, counted, skipped)
}
}
#[must_use]
pub fn holiday(day: Date) -> Option<&'static str> {
let fixed = match (day.month(), day.day()) {
(1, 1) => Some("Neujahr"),
(5, 1) => Some("Tag der Arbeit"),
(10, 3) => Some("Tag der Deutschen Einheit"),
(12, 25) => Some("1. Weihnachtstag"),
(12, 26) => Some("2. Weihnachtstag"),
_ => None,
};
if fixed.is_some() {
return fixed;
}
let easter = easter_sunday(day.year())?;
let offset = (day - easter).get_days();
match offset {
-2 => Some("Karfreitag"),
1 => Some("Ostermontag"),
39 => Some("Christi Himmelfahrt"),
50 => Some("Pfingstmontag"),
_ => None,
}
}
#[must_use]
#[allow(clippy::many_single_char_names)] pub fn easter_sunday(year: i16) -> Option<Date> {
let y = i32::from(year);
let a = y % 19;
let b = y / 100;
let c = y % 100;
let d = b / 4;
let e = b % 4;
let f = (b + 8) / 25;
let g = (b - f + 1) / 3;
let h = (19 * a + b - d - g + 15) % 30;
let i = c / 4;
let k = c % 4;
let l = (32 + 2 * e + 2 * i - h - k) % 7;
let m = (a + 11 * h + 22 * l) / 451;
let month = (h + l - 7 * m + 114) / 31;
let day = (h + l - 7 * m + 114) % 31 + 1;
Date::new(year, i8::try_from(month).ok()?, i8::try_from(day).ok()?).ok()
}
#[cfg(test)]
mod tests {
use super::*;
use jiff::civil::date;
#[test]
fn easter_dates() {
assert_eq!(easter_sunday(2024), Some(date(2024, 3, 31)));
assert_eq!(easter_sunday(2026), Some(date(2026, 4, 5)));
assert_eq!(easter_sunday(2027), Some(date(2027, 3, 28)));
assert_eq!(easter_sunday(2038), Some(date(2038, 4, 25)));
}
#[test]
fn holidays_2026() {
assert_eq!(holiday(date(2026, 4, 3)), Some("Karfreitag"));
assert_eq!(holiday(date(2026, 4, 6)), Some("Ostermontag"));
assert_eq!(holiday(date(2026, 5, 14)), Some("Christi Himmelfahrt"));
assert_eq!(holiday(date(2026, 5, 25)), Some("Pfingstmontag"));
assert_eq!(holiday(date(2026, 10, 3)), Some("Tag der Deutschen Einheit"));
assert_eq!(holiday(date(2026, 11, 20)), None);
}
#[test]
fn counting_skips_weekends_and_holidays() {
let cal = Calendar::wbci();
assert_eq!(cal.add_working_days(date(2026, 12, 23), 2), date(2026, 12, 28));
assert_eq!(Calendar::mon_fri().add_working_days(date(2026, 12, 23), 2), date(2026, 12, 25));
assert_eq!(cal.sub_working_days(date(2026, 4, 7), 1), date(2026, 4, 2));
let closed = Calendar::wbci().with_closed(date(2026, 11, 23));
assert_eq!(closed.add_working_days(date(2026, 11, 20), 1), date(2026, 11, 24));
}
#[test]
fn werktage_count_saturdays_but_not_holidays() {
let cal = Calendar::werktage();
assert!(cal.counts_saturday());
assert!(!Calendar::wbci().counts_saturday());
assert!(cal.is_working_day(date(2026, 11, 21))); assert_eq!(cal.closed(date(2026, 11, 22)), Some(Closed::Weekend)); assert_eq!(cal.closed(date(2026, 12, 26)), Some(Closed::Holiday("2. Weihnachtstag"))); assert_eq!(cal.add_working_days(date(2026, 11, 20), 10), date(2026, 12, 2));
assert_eq!(Calendar::wbci().add_working_days(date(2026, 11, 20), 10), date(2026, 12, 4));
assert_eq!(cal.add_working_days(date(2026, 12, 23), 2), date(2026, 12, 28));
assert_eq!(cal.sub_working_days(date(2026, 11, 23), 1), date(2026, 11, 21));
let closed = Calendar::werktage().with_closed(date(2026, 11, 21));
assert_eq!(closed.closed(date(2026, 11, 21)), Some(Closed::Extra));
assert_eq!(Calendar::wbci().closed(date(2026, 11, 21)), Some(Closed::Weekend));
assert_eq!(Calendar::mon_fri().closed(date(2026, 11, 21)), Some(Closed::Weekend));
}
#[test]
fn berlin_is_compiled_in() {
let ts = date(2026, 7, 1).at(12, 0, 0, 0).to_zoned_datetime(berlin()).unwrap().timestamp();
assert_eq!(berlin().to_offset(ts).seconds(), 7200);
}
}