Skip to content

Commit

Permalink
Fix deprecation warning from chrono
Browse files Browse the repository at this point in the history
Also change logic to use the latest time if the local time is ambiguous
(for example due to daylight savings)
  • Loading branch information
tmccombs committed Oct 19, 2023
1 parent b8e7cbd commit 2d51f3f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ version = "4.4.6"
features = ["suggestions", "color", "wrap_help", "cargo", "derive"]

[dependencies.chrono]
version = "0.4.28"
version = "0.4.31"
default-features = false
features = ["std", "clock"]

Expand Down
22 changes: 15 additions & 7 deletions src/filter/time.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use chrono::{offset::TimeZone, DateTime, Local, NaiveDate};
use chrono::{DateTime, Local, NaiveDate, NaiveDateTime};

use std::time::SystemTime;

Expand All @@ -20,11 +20,17 @@ impl TimeFilter {
.ok()
.or_else(|| {
NaiveDate::parse_from_str(s, "%F")
.ok()
.and_then(|nd| nd.and_hms_opt(0, 0, 0))
.and_then(|ndt| Local.from_local_datetime(&ndt).single())
.ok()?
.and_hms_opt(0, 0, 0)?
.and_local_timezone(Local)
.single()
})
.or_else(|| {
NaiveDateTime::parse_from_str(s, "%F %T")
.ok()?
.and_local_timezone(Local)
.latest()
})
.or_else(|| Local.datetime_from_str(s, "%F %T").ok())
.map(|dt| dt.into())
})
}
Expand Down Expand Up @@ -52,8 +58,10 @@ mod tests {

#[test]
fn is_time_filter_applicable() {
let ref_time = Local
.datetime_from_str("2010-10-10 10:10:10", "%F %T")
let ref_time = NaiveDateTime::parse_from_str("2010-10-10 10:10:10", "%F %T")
.unwrap()
.and_local_timezone(Local)
.latest()
.unwrap()
.into();

Expand Down

0 comments on commit 2d51f3f

Please sign in to comment.