diff options
author | Conrad Ludgate <conradludgate@gmail.com> | 2023-09-11 09:26:05 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-11 09:26:05 +0100 |
commit | f90c01f702f6a98b041f766b6a1d857bc1b9afef (patch) | |
tree | 04a4755bd632abdcf398d0ce903163ed60a5a212 /atuin-client/src/settings.rs | |
parent | 2342a3392349c0ae9d742e9da9833590e6567d08 (diff) |
replace chrono with time (#806)
* replace chrono with time
* Fix test chrono usage
---------
Co-authored-by: Ellie Huxtable <ellie@elliehuxtable.com>
Diffstat (limited to 'atuin-client/src/settings.rs')
-rw-r--r-- | atuin-client/src/settings.rs | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/atuin-client/src/settings.rs b/atuin-client/src/settings.rs index c68be0d5f..ebc1a372d 100644 --- a/atuin-client/src/settings.rs +++ b/atuin-client/src/settings.rs @@ -1,11 +1,11 @@ use std::{ + convert::TryFrom, io::prelude::*, path::{Path, PathBuf}, str::FromStr, }; use atuin_common::record::HostId; -use chrono::{prelude::*, Utc}; use clap::ValueEnum; use config::{ builder::DefaultState, Config, ConfigBuilder, Environment, File as ConfigFile, FileFormat, @@ -16,6 +16,7 @@ use parse_duration::parse; use regex::RegexSet; use semver::Version; use serde::Deserialize; +use time::{format_description::well_known::Rfc3339, OffsetDateTime}; use uuid::Uuid; pub const HISTORY_PAGE_SIZE: i64 = 100; @@ -207,21 +208,20 @@ impl Settings { } fn save_current_time(filename: &str) -> Result<()> { - Settings::save_to_data_dir(filename, Utc::now().to_rfc3339().as_str())?; + Settings::save_to_data_dir( + filename, + OffsetDateTime::now_utc().format(&Rfc3339)?.as_str(), + )?; Ok(()) } - fn load_time_from_file(filename: &str) -> Result<chrono::DateTime<Utc>> { + fn load_time_from_file(filename: &str) -> Result<OffsetDateTime> { let value = Settings::read_from_data_dir(filename); match value { - Some(v) => { - let time = chrono::DateTime::parse_from_rfc3339(v.as_str())?; - - Ok(time.with_timezone(&Utc)) - } - None => Ok(Utc.ymd(1970, 1, 1).and_hms(0, 0, 0)), + Some(v) => Ok(OffsetDateTime::parse(v.as_str(), &Rfc3339)?), + None => Ok(OffsetDateTime::UNIX_EPOCH), } } @@ -233,11 +233,11 @@ impl Settings { Settings::save_current_time(LAST_VERSION_CHECK_FILENAME) } - pub fn last_sync() -> Result<chrono::DateTime<Utc>> { + pub fn last_sync() -> Result<OffsetDateTime> { Settings::load_time_from_file(LAST_SYNC_FILENAME) } - pub fn last_version_check() -> Result<chrono::DateTime<Utc>> { + pub fn last_version_check() -> Result<OffsetDateTime> { Settings::load_time_from_file(LAST_VERSION_CHECK_FILENAME) } @@ -265,8 +265,8 @@ impl Settings { match parse(self.sync_frequency.as_str()) { Ok(d) => { - let d = chrono::Duration::from_std(d).unwrap(); - Ok(Utc::now() - Settings::last_sync()? >= d) + let d = time::Duration::try_from(d).unwrap(); + Ok(OffsetDateTime::now_utc() - Settings::last_sync()? >= d) } Err(e) => Err(eyre!("failed to check sync: {}", e)), } @@ -274,10 +274,10 @@ impl Settings { fn needs_update_check(&self) -> Result<bool> { let last_check = Settings::last_version_check()?; - let diff = Utc::now() - last_check; + let diff = OffsetDateTime::now_utc() - last_check; // Check a max of once per hour - Ok(diff.num_hours() >= 1) + Ok(diff.whole_hours() >= 1) } async fn latest_version(&self) -> Result<Version> { |