use crate::arithmetic;
use crate::date::*;
use std::convert::TryInto;
use std::str::FromStr;
use tinystr::tinystr8;
#[derive(Debug, Default)]
pub struct MockDateTime {
pub year: i32,
pub month: u32,
pub day: u32,
pub hour: IsoHour,
pub minute: IsoMinute,
pub second: IsoSecond,
}
impl MockDateTime {
pub const fn new(
year: i32,
month: u32,
day: u32,
hour: IsoHour,
minute: IsoMinute,
second: IsoSecond,
) -> Self {
Self {
year,
month,
day,
hour,
minute,
second,
}
}
pub fn try_new(
year: usize,
month: usize,
day: usize,
hour: usize,
minute: usize,
second: usize,
) -> Result<Self, DateTimeError> {
Ok(Self {
year: year.try_into().map_err(|_| DateTimeError::Overflow {
field: "Year",
max: i32::MAX as usize,
})?,
month: month as u32,
day: day as u32,
hour: hour.try_into()?,
minute: minute.try_into()?,
second: second.try_into()?,
})
}
}
impl FromStr for MockDateTime {
type Err = DateTimeError;
fn from_str(input: &str) -> Result<Self, Self::Err> {
let year: i32 = input[0..4].parse()?;
let month: u32 = input[5..7].parse()?;
let day: u32 = input[8..10].parse()?;
let hour: IsoHour = input[11..13].parse()?;
let minute: IsoMinute = input[14..16].parse()?;
let second: IsoSecond = input[17..19].parse()?;
Ok(Self {
year,
month: month - 1,
day: day - 1,
hour,
minute,
second,
})
}
}
impl DateInput for MockDateTime {
fn year(&self) -> Option<Year> {
Some(arithmetic::iso_year_to_gregorian(self.year))
}
fn month(&self) -> Option<Month> {
Some(Month {
number: self.month + 1,
code: MonthCode(tinystr8!("TODO")),
})
}
fn day_of_month(&self) -> Option<DayOfMonth> {
Some(DayOfMonth(self.day + 1))
}
fn iso_weekday(&self) -> Option<IsoWeekday> {
Some(arithmetic::iso_date_to_weekday(
self.year,
self.month as usize,
self.day as usize,
))
}
fn day_of_year_info(&self) -> Option<DayOfYearInfo> {
unimplemented!()
}
}
impl IsoTimeInput for MockDateTime {
fn hour(&self) -> Option<IsoHour> {
Some(self.hour)
}
fn minute(&self) -> Option<IsoMinute> {
Some(self.minute)
}
fn second(&self) -> Option<IsoSecond> {
Some(self.second)
}
fn fraction(&self) -> Option<FractionalSecond> {
None
}
}