Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix negative TzOffset to_string handling #56

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ impl fmt::Display for Time {
if tz_offset == 0 {
write!(f, "Z")?;
} else {
let mins = tz_offset / 60;
let mut min = mins / 60;
let sec = (mins % 60).abs();
// tz offset is given in seconds, so we do convertions from seconds -> mins -> hours
let total_minutes = tz_offset / 60;
let hours = total_minutes / 60;
let minutes = total_minutes % 60;
let mut buf: [u8; 6] = *b"+00:00";
if min < 0 {
if tz_offset < 0 {
buf[0] = b'-';
min = min.abs();
}
crate::display_num_buf(2, 1, min as u32, &mut buf);
crate::display_num_buf(2, 4, sec as u32, &mut buf);
crate::display_num_buf(2, 1, hours.unsigned_abs(), &mut buf);
crate::display_num_buf(2, 4, minutes.unsigned_abs(), &mut buf);
f.write_str(std::str::from_utf8(&buf[..]).unwrap())?;
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,14 @@ fn datetime_tz_negative_2212() {
let dt = DateTime::parse_str("2020-01-01T12:13:14−02:15").unwrap();
assert_eq!(dt.time.tz_offset, Some(-8100));
assert_eq!(dt.to_string(), "2020-01-01T12:13:14-02:15");

let dt = DateTime::parse_str("2020-01-01T12:13:14−00:01").unwrap();
assert_eq!(dt.time.tz_offset, Some(-60));
assert_eq!(dt.to_string(), "2020-01-01T12:13:14-00:01");

let dt = DateTime::parse_str("2020-01-01T12:13:14−01:00").unwrap();
assert_eq!(dt.time.tz_offset, Some(-3600));
assert_eq!(dt.to_string(), "2020-01-01T12:13:14-01:00");
}

#[test]
Expand Down
Loading