1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
//---------------------------------------------------------------------------------------------------- Use
use derive_more::{
Add,
Display,
From,
Into,
Deref,
Not,
Mul,
Sum,
DerefMut,
AddAssign,
MulAssign,
};
use crate::day::Day;
use crate::month::Month;
use crate::weekday::Weekday;
use crate::days_in_year::DaysInYear;
use crate::days_in_month::DaysInMonth;
use crate::macros::impl_traits;
//---------------------------------------------------------------------------------------------------- Year
/// Any year from `-32,768` to `32,767`
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[derive(Copy,Clone,Debug,Default,PartialEq,PartialOrd,Eq,Ord,Hash)]
#[derive(
Add,
Display,
From,
Deref,
Not,
Mul,
Sum,
DerefMut,
AddAssign,
MulAssign,
)]
pub struct Year(pub i16);
impl_traits!{ Year => i16 |
u8,u16,u32,u64,u128,usize |
i8,i16,i32,i64,i128,isize
}
//---------------------------------------------------------------------------------------------------- Impl
impl Year {
/// ```rust
/// # use nichi::*;
/// assert_eq!(Year::MAX, Year(i16::MAX));
/// ```
pub const MAX: Year = Year(i16::MAX);
/// ```rust
/// # use nichi::*;
/// assert_eq!(Year::MIN, Year(i16::MIN));
/// ```
pub const MIN: Year = Year(i16::MIN);
#[inline]
/// ```rust
/// # use nichi::*;
/// assert!(!Year(2019).is_leap());
/// assert!(Year(2020).is_leap());
/// assert!(!Year(2023).is_leap());
/// assert!(Year(2024).is_leap());
/// ```
pub const fn is_leap(self) -> bool {
(self.0 % 4 == 0 && self.0 % 100 != 0) || self.0 % 400 == 0
}
#[inline]
/// ```rust
/// # use nichi::*;
/// // Non-leap year.
/// assert_eq!(Year(2019).days_in_year(), 365);
/// // Leap year.
/// assert_eq!(Year(2020).days_in_year(), 366);
/// ```
pub const fn days_in_year(self) -> DaysInYear {
if self.is_leap() {
DaysInYear::ThreeSixSix
} else {
DaysInYear::ThreeSixFive
}
}
#[inline]
/// ```rust
/// # use nichi::*;
/// assert_eq!(
/// Year(2019).days_in_month(Month::January),
/// DaysInMonth::ThirtyOne,
/// );
///
/// assert_eq!(
/// Year(2019).days_in_month(Month::April),
/// DaysInMonth::Thirty,
/// );
///
/// assert_eq!(
/// Year(2019).days_in_month(Month::February),
/// DaysInMonth::TwentyEight,
/// );
///
/// // Leap Year
/// assert_eq!(
/// Year(2020).days_in_month(Month::February),
/// DaysInMonth::TwentyNine,
/// );
/// ```
pub const fn days_in_month(self, month: Month) -> DaysInMonth {
use Month as M;
match month {
M::January|M::March|M::May|M::July|
M::August|M::October |M::December => DaysInMonth::ThirtyOne,
M::April|M::June|M::September|M::November => DaysInMonth::Thirty,
M::February => if self.is_leap() {
DaysInMonth::TwentyNine
} else {
DaysInMonth::TwentyEight
}
}
}
#[inline]
/// ```rust
/// # use nichi::*;
/// assert_eq!(Year(2000).inner(), 2000);
/// ```
pub const fn inner(self) -> i16 {
self.0
}
#[inline]
/// ```rust
/// # use nichi::*;
/// assert_eq!(Year::from_str("-32768").unwrap(), Year(-32768));
/// assert_eq!(Year::from_str("0").unwrap(), Year(0));
/// assert_eq!(Year::from_str("2000").unwrap(), Year(2000));
/// assert_eq!(Year::from_str("32767").unwrap(), Year(32767));
/// ```
pub fn from_str(s: &str) -> Option<Self> {
match s.parse::<i16>() {
Ok(u) => Some(Year(u)),
_ => None,
}
}
}
//---------------------------------------------------------------------------------------------------- Floats
macro_rules! impl_f {
($from:ty) => {
impl From<$from> for Year {
#[inline]
fn from(f: $from) -> Self {
if !f.is_finite() {
return Self(0);
}
if f > i16::MAX as $from {
return Self::MAX;
} else if f < i16::MIN as $from {
return Self::MIN;
}
Self(f as i16)
}
}
impl From<&$from> for Year {
#[inline]
fn from(f: &$from) -> Self {
Self::from(*f)
}
}
}
}
impl_f!(f32);
impl_f!(f64);
//---------------------------------------------------------------------------------------------------- uint
macro_rules! impl_u {
($from:ty) => {
impl From<$from> for Year {
#[inline]
fn from(year: $from) -> Self {
if year > i16::MAX as $from {
return Self::MAX;
}
Self(year as i16)
}
}
impl From<&$from> for Year {
#[inline]
fn from(year: &$from) -> Self {
Self::from(*year)
}
}
}
}
impl_u!(u8);
impl_u!(u32);
impl_u!(u64);
impl_u!(u128);
impl_u!(usize);
//---------------------------------------------------------------------------------------------------- Int
macro_rules! impl_i {
($from:ty) => {
impl From<$from> for Year {
#[inline]
fn from(year: $from) -> Self {
if year > i16::MAX as $from {
return Self::MAX;
}
Self(year as i16)
}
}
impl From<&$from> for Year {
#[inline]
fn from(year: &$from) -> Self {
Self::from(*year)
}
}
}
}
impl_i!(i8);
impl_i!(i32);
impl_i!(i64);
impl_i!(i128);
impl_i!(isize);