Skip to main content

rustolio_utils/time/
date.rs

1//
2// SPDX-License-Identifier: MPL-2.0
3//
4// Copyright (c) 2026 Tobias Binnewies. All rights reserved.
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this
8// file, You can obtain one at http://mozilla.org/MPL/2.0/.
9//
10
11use super::{CalendarMonth, Error, Month, Result, Timezone, Weekday};
12
13#[derive(Clone, Copy, PartialEq, Eq)]
14pub struct Date(pub(super) ::time::Date);
15
16impl Date {
17    pub const fn new(day: u8, month: Month, year: i32) -> Result<Self> {
18        let Ok(date) = time::Date::from_calendar_date(year, month, day) else {
19            return Err(Error::OutOfRange);
20        };
21        Ok(Self(date))
22    }
23
24    pub fn now(timezone: Timezone) -> Result<Self> {
25        let dt = super::DateTime::now(timezone)?;
26        Ok(dt.into_parts().0)
27    }
28
29    pub const fn year(&self) -> i32 {
30        self.0.year()
31    }
32
33    pub const fn month(&self) -> Month {
34        self.0.month()
35    }
36
37    pub const fn day(&self) -> u8 {
38        self.0.day()
39    }
40
41    pub const fn weekday(&self) -> Weekday {
42        self.0.weekday()
43    }
44
45    pub const fn iso_week(&self) -> u8 {
46        self.0.iso_week()
47    }
48
49    pub const fn calendar_month(&self) -> CalendarMonth {
50        CalendarMonth::new(self.year(), self.month())
51    }
52}