Fix end of month handling

This commit is contained in:
Savanni D'Gerinel 2023-08-12 21:35:14 -04:00
parent c76396a010
commit 456d872b40
1 changed files with 34 additions and 163 deletions

View File

@ -250,8 +250,12 @@ impl From<chrono::NaiveDate> for IFC {
{ {
days = days - 1; days = days - 1;
} }
let month: u8 = (days / 28).try_into().unwrap(); let mut month: u8 = (days / 28).try_into().unwrap();
let day: u8 = (days % 28).try_into().unwrap(); let mut day: u8 = (days % 28).try_into().unwrap();
if day == 0 {
month = month - 1;
day = 28;
}
Self::Day(Day { Self::Day(Day {
year: date.year(), year: date.year(),
month: month + 1, month: month + 1,
@ -268,167 +272,6 @@ impl From<IFC> for chrono::NaiveDate {
} }
} }
/*
impl IFC {
pub fn year_day(year: u32) -> Self {
Self {
year,
ordinal: if is_leap_year(year) { 366 } else { 365 }
}
}
}
*/
/*
impl From<chrono::Date<chrono::Utc>> for IFC {
fn from(d: chrono::Date<chrono::Utc>) -> IFC {
IFC::from(d.naive_utc())
}
}
impl From<chrono::Date<chrono_tz::Tz>> for IFC {
fn from(d: chrono::Date<chrono_tz::Tz>) -> IFC {
IFC::from(d.naive_utc())
}
}
impl From<chrono::NaiveDate> for IFC {
fn from(d: NaiveDate) -> IFC {
//println!("d: {} [{}]", d.format("%Y-%m-%d"), d.ordinal());
IFC {
year: (d.year() + 10000) as u32,
ordinal: d.ordinal0(),
leap_year: is_leap_year(d.year()),
}
}
}
*/
/*
impl Datelike for IFC {
fn year(&self) -> i32 {
// self.year as i32
unimplemented!()
}
fn month(&self) -> u32 {
// self.month0() + 1
unimplemented!()
}
fn month0(&self) -> u32 {
/*
if self.leap_year && self.ordinal == 365 {
12
} else if self.leap_year && self.ordinal == 168 {
5
} else {
self.ordinal / 28
}
*/
unimplemented!()
}
fn day(&self) -> u32 {
// self.day0() + 1
unimplemented!()
}
fn day0(&self) -> u32 {
/*
if self.leap_year {
if self.ordinal == 365 {
28
} else if self.ordinal == 168 {
28
} else if self.ordinal > 168 {
(self.ordinal - 1).rem_euclid(28) as u32
} else {
self.ordinal.rem_euclid(28) as u32
}
} else {
if self.ordinal == 364 {
28
} else {
self.ordinal.rem_euclid(28) as u32
}
}
*/
unimplemented!()
}
fn ordinal(&self) -> u32 {
// self.ordinal + 1
unimplemented!()
}
fn ordinal0(&self) -> u32 {
// self.ordinal
unimplemented!()
}
fn weekday(&self) -> chrono::Weekday {
if self.day0() == 28 {
chrono::Weekday::Sat
} else {
match self.day0().rem_euclid(7) {
0 => chrono::Weekday::Sun,
1 => chrono::Weekday::Mon,
2 => chrono::Weekday::Tue,
3 => chrono::Weekday::Wed,
4 => chrono::Weekday::Thu,
5 => chrono::Weekday::Fri,
6 => chrono::Weekday::Sat,
_ => panic!("rem_euclid should not return anything outside the 0..6 range"),
}
}
}
fn iso_week(&self) -> chrono::IsoWeek {
panic!("iso_week is not implemented because chrono does not expose any constructors for IsoWeek!");
}
fn with_year(&self, year: i32) -> Option<IFC> {
/*
Some(IFC {
year: (year as u32) + 10000,
ordinal: self.ordinal,
leap_year: is_leap_year(year),
})
*/
unimplemented!()
}
fn with_month(&self, month: u32) -> Option<IFC> {
// Some(IFC::ymd(self.year, month as u8, self.day() as u8))
unimplemented!()
}
fn with_month0(&self, month: u32) -> Option<IFC> {
// Some(IFC::ymd(self.year, month as u8 + 1, self.day() as u8))
unimplemented!()
}
fn with_day(&self, day: u32) -> Option<IFC> {
// Some(IFC::ymd(self.year, self.month() as u8, day as u8))
unimplemented!()
}
fn with_day0(&self, day: u32) -> Option<IFC> {
// Some(IFC::ymd(self.year, self.month() as u8, day as u8 + 1))
unimplemented!()
}
fn with_ordinal(&self, ordinal: u32) -> Option<IFC> {
/*
Some(IFC {
year: self.year,
ordinal,
leap_year: self.leap_year,
})
*/
unimplemented!()
}
fn with_ordinal0(&self, ordinal: u32) -> Option<IFC> {
/*
Some(IFC {
year: self.year,
ordinal: ordinal + 1,
leap_year: self.leap_year,
})
*/
unimplemented!()
}
}
*/
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -612,10 +455,34 @@ mod tests {
IFC::from(NaiveDate::from_ymd_opt(12022, 1, 1).unwrap()), IFC::from(NaiveDate::from_ymd_opt(12022, 1, 1).unwrap()),
IFC::ymd(12022, 1, 1).unwrap() IFC::ymd(12022, 1, 1).unwrap()
); );
assert_eq!(
IFC::from(NaiveDate::from_ymd_opt(12022, 1, 2).unwrap()),
IFC::ymd(12022, 1, 2).unwrap()
);
assert_eq!(
IFC::from(NaiveDate::from_ymd_opt(12022, 1, 3).unwrap()),
IFC::ymd(12022, 1, 3).unwrap()
);
assert_eq!(
IFC::from(chrono::NaiveDate::from_ymd_opt(2023, 01, 26).unwrap()),
IFC::ymd(2023, 1, 26).unwrap()
);
assert_eq!(
IFC::from(chrono::NaiveDate::from_ymd_opt(2023, 01, 27).unwrap()),
IFC::ymd(2023, 1, 27).unwrap()
);
assert_eq!(
IFC::from(chrono::NaiveDate::from_ymd_opt(2023, 01, 28).unwrap()),
IFC::ymd(2023, 1, 28).unwrap()
);
assert_eq!( assert_eq!(
IFC::from(NaiveDate::from_ymd_opt(12022, 1, 29).unwrap()), IFC::from(NaiveDate::from_ymd_opt(12022, 1, 29).unwrap()),
IFC::ymd(12022, 2, 1).unwrap() IFC::ymd(12022, 2, 1).unwrap()
); );
assert_eq!(
IFC::from(NaiveDate::from_ymd_opt(12022, 1, 30).unwrap()),
IFC::ymd(12022, 2, 2).unwrap()
);
assert_eq!( assert_eq!(
IFC::from(NaiveDate::from_ymd_opt(12022, 2, 26).unwrap()), IFC::from(NaiveDate::from_ymd_opt(12022, 2, 26).unwrap()),
IFC::ymd(12022, 3, 1).unwrap() IFC::ymd(12022, 3, 1).unwrap()
@ -644,6 +511,10 @@ mod tests {
IFC::from(NaiveDate::from_ymd_opt(12022, 8, 13).unwrap()), IFC::from(NaiveDate::from_ymd_opt(12022, 8, 13).unwrap()),
IFC::ymd(12022, 9, 1).unwrap() IFC::ymd(12022, 9, 1).unwrap()
); );
assert_eq!(
IFC::from(chrono::NaiveDate::from_ymd_opt(2023, 08, 12).unwrap()),
IFC::ymd(2023, 8, 28).unwrap()
);
assert_eq!( assert_eq!(
IFC::from(NaiveDate::from_ymd_opt(12022, 9, 10).unwrap()), IFC::from(NaiveDate::from_ymd_opt(12022, 9, 10).unwrap()),
IFC::ymd(12022, 10, 1).unwrap() IFC::ymd(12022, 10, 1).unwrap()