Skip to content

Commit

Permalink
fix parse '2024-07-26 09:03:48+00'
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Jul 10, 2024
1 parent e3eebd1 commit 3eb1a80
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fastdate"
version = "0.3.28"
version = "0.3.29"
edition = "2021"
description = "Rust fast date carte"
readme = "Readme.md"
Expand Down
18 changes: 13 additions & 5 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ impl DateTime {
Self {
inner: time1::OffsetDateTime::from(s),
}
.set_offset(offset)
.set_offset(offset)
}

/// stand "0000-00-00 00:00:00.000000000"
Expand Down Expand Up @@ -573,6 +573,14 @@ impl DateTime {
have_offset = Some(index);
}
}
if v.len() >= 3 {
let index = v.len() - 3;
let b = &v[index..(index + 1)];
if b == "+" || b == "-" {
have_offset = Some(index);
v.push_str(":00");
}
}
}
if let Some(mut offset) = have_offset {
if offset >= 1 {
Expand Down Expand Up @@ -663,7 +671,7 @@ impl From<Date> for DateTime {
"{:04}-{:02}-{:02} 00:00:00.000000000Z",
arg.year, arg.mon, arg.day
))
.unwrap()
.unwrap()
}
}

Expand All @@ -682,7 +690,7 @@ impl From<Time> for DateTime {
"0000-01-01 {:02}:{:02}:{:02}.{:09}Z",
arg.hour, arg.minute, arg.sec, arg.nano
))
.unwrap()
.unwrap()
}
}

Expand All @@ -692,7 +700,7 @@ impl From<(Date, Time)> for DateTime {
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:09}Z",
arg.0.year, arg.0.mon, arg.0.day, arg.1.hour, arg.1.minute, arg.1.sec, arg.1.nano
))
.unwrap()
.unwrap()
}
}

Expand All @@ -703,7 +711,7 @@ impl From<(Date, Time, i32)> for DateTime {
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:09}Z",
arg.0.year, arg.0.mon, arg.0.day, arg.1.hour, arg.1.minute, arg.1.sec, arg.1.nano
))
.unwrap();
.unwrap();
datetime = datetime.set_offset(arg.2).add_sub_sec(-arg.2 as i64);
datetime
}
Expand Down
6 changes: 6 additions & 0 deletions tests/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,12 @@ fn test_parse_s() {
assert_eq!(&date.to_string()[..19], "2022-12-12T00:00:00");
}

#[test]
fn test_parse_no_00() {
let date = DateTime::from_str("2024-07-26 09:03:48+00").unwrap();
assert_eq!(date.to_string(), "2024-07-26T09:03:48Z");
}

#[test]
fn test_set_offset_sub() {
let mut date = DateTime::from_str("2022-12-12 09:00:00Z").unwrap();
Expand Down

0 comments on commit 3eb1a80

Please sign in to comment.