1
0
mirror of https://github.com/bspeice/dtparse synced 2025-04-03 04:21:31 -04:00

fix: modify check timezone index

This commit is contained in:
taichong 2024-07-16 23:53:41 +08:00
parent 7cfd18211d
commit fbf77b442d
2 changed files with 18 additions and 5 deletions

View File

@ -838,13 +838,14 @@ impl Parser {
}
} else if res.hour.is_some() && (l[i] == "+" || l[i] == "-") {
let signal = if l[i] == "+" { 1 } else { -1 };
let len_li = l[i].len();
// check next index's length
let timezone_len = l[i + 1].len();
let mut hour_offset: Option<i32> = None;
let mut min_offset: Option<i32> = None;
// TODO: check that l[i + 1] is integer?
if len_li == 4 {
if timezone_len == 4 {
// -0300
hour_offset = Some(l[i + 1][..2].parse::<i32>()?);
min_offset = Some(l[i + 1][2..4].parse::<i32>()?);
@ -858,7 +859,7 @@ impl Parser {
Some(l[i + 3].parse::<i32>()?)
};
i += 2;
} else if len_li <= 2 {
} else if timezone_len <= 2 {
// -[0]3
let range_len = min(l[i + 1].len(), 2);
hour_offset = Some(l[i + 1][..range_len].parse::<i32>()?);

View File

@ -111,6 +111,18 @@ fn github_36() {
#[test]
fn github_46() {
let parse_result = parse("2000-01-01 12:00:00+00:");
assert!(parse_result.is_err());
assert_eq!(
parse("2000-01-01 12:00:00+00:"),
Err(ParseError::TimezoneUnsupported)
);
let parse_result = parse("2000-01-01 12:00:00+0811");
match parse_result {
Ok((dt, offset)) => {
assert_eq!(format!("{:?}", dt), "2000-01-01T12:00:00".to_string());
assert_eq!(format!("{:?}", offset), "Some(+08:11)".to_string());
}
Err(_) => {
panic!();
}
}
}