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 authored and sharkdp committed Oct 21, 2023
1 parent e6aa8e8 commit a11f842
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
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)
.latest()
})
.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 a11f842

Please sign in to comment.