Fix deprecation warning from chrono

Also change logic to use the latest time if the local time is ambiguous
(for example due to daylight savings)
This commit is contained in:
Thayne McCombs 2023-10-18 23:53:10 -06:00 committed by David Peter
parent e6aa8e82f6
commit a11f8426d4
2 changed files with 16 additions and 8 deletions

View File

@ -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"]

View File

@ -1,4 +1,4 @@
use chrono::{offset::TimeZone, DateTime, Local, NaiveDate};
use chrono::{DateTime, Local, NaiveDate, NaiveDateTime};
use std::time::SystemTime;
@ -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())
})
}
@ -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();