Fix some logic errors (requires testing)

This commit is contained in:
Nathan Fisher 2024-02-08 19:06:49 -05:00
parent 9563b1da0d
commit b3ea308997
1 changed files with 9 additions and 9 deletions

View File

@ -65,36 +65,36 @@ impl DateTime {
/// Converts a Unix timestamp to a `DateTime` struct /// Converts a Unix timestamp to a `DateTime` struct
pub fn from_timestamp(ts: i64) -> Self { pub fn from_timestamp(ts: i64) -> Self {
if ts < 0 { if ts < 0 {
let mut seconds: i64 = 0; let mut seconds: i64 = ts;
let mut year = Year::new(-1); let mut year = Year::new(1969);
while seconds > -year.seconds() { while seconds < -year.seconds() {
seconds -= year.seconds(); seconds += year.seconds();
year = year.previous(); year = year.previous();
} }
let mut month = Some(Month::December); let mut month = Some(Month::December);
while month.is_some() { while month.is_some() {
let m = month.unwrap(); let m = month.unwrap();
if seconds > m.seconds(year) { if seconds < -m.seconds(year) {
break; break;
} }
seconds -= m.seconds(year); seconds += m.seconds(year);
month = m.previous(); month = m.previous();
} }
let month = month.unwrap(); let month = month.unwrap();
let mut day = month.days(year); let mut day = month.days(year);
while day > 0 && seconds < -SECONDS_PER_DAY { while day > 0 && seconds < -SECONDS_PER_DAY {
seconds -= SECONDS_PER_HOUR; seconds += SECONDS_PER_DAY;
day -= 1; day -= 1;
} }
let mut hour: i8 = 23; let mut hour: i8 = 23;
while hour >= 0 && seconds < -SECONDS_PER_HOUR { while hour >= 0 && seconds < -SECONDS_PER_HOUR {
seconds -= SECONDS_PER_HOUR; seconds += SECONDS_PER_HOUR;
hour -= 1; hour -= 1;
} }
let hour = hour.try_into().unwrap(); let hour = hour.try_into().unwrap();
let mut minute: i8 = 60; let mut minute: i8 = 60;
while minute >= 0 && seconds < -60 { while minute >= 0 && seconds < -60 {
seconds -= 60; seconds += 60;
minute -= 1; minute -= 1;
} }
let minute = minute.try_into().unwrap(); let minute = minute.try_into().unwrap();