use crate::calendar::CalendarError;
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Year(i16);
impl Year {
pub const CE: Self = Self(1);
pub const EPOCH: Self = Self(1970);
pub const MAX: Self = Self(32766);
pub const MIN: Self = Self(-32767);
#[inline]
pub const fn from_num(num: i16) -> Result<Self, CalendarError> {
if num < Self::MIN.num() || num > Self::MAX.num() {
return Err(CalendarError::InvalidYear { received: num });
}
Ok(Self(num))
}
#[inline]
pub const fn is_leap_year(&self) -> bool {
let value = if self.0 % 100 == 0 { 0b1111 } else { 0b11 };
self.0 & value == 0
}
#[inline]
pub const fn num(&self) -> i16 {
self.0
}
}
impl TryFrom<i16> for Year {
type Error = crate::Error;
#[inline]
fn try_from(from: i16) -> Result<Self, Self::Error> {
Ok(Self::from_num(from)?)
}
}