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
parent b8e7cbd5e3
commit 05ecb54723
3 changed files with 18 additions and 10 deletions

4
Cargo.lock generated
View File

@ -139,9 +139,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.28"
version = "0.4.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95ed24df0632f708f5f6d8082675bef2596f7084dee3dd55f632290bf35bfe0f"
checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38"
dependencies = [
"android-tzdata",
"iana-time-zone",

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();