Skip to content

Commit 356f9a9

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

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@ fn rfc2822() {
4040
);
4141
}
4242

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

0 commit comments

Comments
 (0)