Skip to content

Commit f4ea59d

Browse files
committed
fix: correctly parse raw dates with negative timezone offsets
1 parent 9f95f7f commit f4ea59d

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

git-date/src/parse.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,13 @@ pub(crate) mod function {
7272
if offset.len() != 5 {
7373
return None;
7474
}
75-
let sign = if &offset[..1] == "-" { Sign::Plus } else { Sign::Minus };
75+
let sign = if &offset[..1] == "-" { Sign::Minus } else { Sign::Plus };
7676
let hours: i32 = offset[1..3].parse().ok()?;
7777
let minutes: i32 = offset[3..5].parse().ok()?;
78-
let offset_in_seconds = hours * 3600 + minutes * 60;
78+
let mut offset_in_seconds = hours * 3600 + minutes * 60;
79+
if sign == Sign::Minus {
80+
offset_in_seconds *= -1;
81+
};
7982
let time = Time {
8083
seconds_since_unix_epoch,
8184
offset_in_seconds,

git-date/tests/time/parse.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,29 @@ fn rfc2822() {
4040
);
4141
}
4242

43+
#[test]
44+
fn raw() {
45+
assert_eq!(
46+
git_date::parse("1660874655 +0800", None).expect("parsed raw string"),
47+
Time {
48+
seconds_since_unix_epoch: 1660874655,
49+
offset_in_seconds: 28800,
50+
sign: Sign::Plus,
51+
},
52+
"could not parse with raw format"
53+
);
54+
55+
assert_eq!(
56+
git_date::parse("1660874655 -0800", None).expect("parsed raw string"),
57+
Time {
58+
seconds_since_unix_epoch: 1660874655,
59+
offset_in_seconds: -28800,
60+
sign: Sign::Minus,
61+
},
62+
"could not parse with raw format"
63+
);
64+
}
65+
4366
#[test]
4467
fn invalid_dates_can_be_produced_without_current_time() {
4568
assert!(matches!(

0 commit comments

Comments
 (0)