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

View File

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