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/import | |
| parent | Use `case` for Linux distro choice in `install.sh` (#1200) (diff) | |
| download | atuin-f90c01f702f6a98b041f766b6a1d857bc1b9afef.zip | |
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/import')
| -rw-r--r-- | atuin-client/src/import/bash.rs | 13 | ||||
| -rw-r--r-- | atuin-client/src/import/fish.rs | 10 | ||||
| -rw-r--r-- | atuin-client/src/import/nu.rs | 5 | ||||
| -rw-r--r-- | atuin-client/src/import/nu_histdb.rs | 10 | ||||
| -rw-r--r-- | atuin-client/src/import/resh.rs | 14 | ||||
| -rw-r--r-- | atuin-client/src/import/zsh.rs | 37 | ||||
| -rw-r--r-- | atuin-client/src/import/zsh_histdb.rs | 6 |
7 files changed, 53 insertions, 42 deletions
diff --git a/atuin-client/src/import/bash.rs b/atuin-client/src/import/bash.rs index 25ede053..fe080a55 100644 --- a/atuin-client/src/import/bash.rs +++ b/atuin-client/src/import/bash.rs @@ -1,10 +1,10 @@ use std::{fs::File, io::Read, path::PathBuf, str}; use async_trait::async_trait; -use chrono::{DateTime, Duration, NaiveDateTime, Utc}; use directories::UserDirs; use eyre::{eyre, Result}; use itertools::Itertools; +use time::{Duration, OffsetDateTime}; use super::{get_histpath, unix_byte_lines, Importer, Loader}; use crate::history::History; @@ -55,7 +55,7 @@ impl Importer for Bash { _ => None, }) // if no known timestamps, use now as base - .unwrap_or((lines.len(), Utc::now())); + .unwrap_or((lines.len(), OffsetDateTime::now_utc())); // if no timestamp is recorded, then use this increment to set an arbitrary timestamp // to preserve ordering @@ -99,7 +99,7 @@ enum LineType<'a> { Empty, /// A timestamp line start with a '#', followed immediately by an integer /// that represents seconds since UNIX epoch. - Timestamp(DateTime<Utc>), + Timestamp(OffsetDateTime), /// Anything else. Command(&'a str), } @@ -119,10 +119,9 @@ impl<'a> From<&'a [u8]> for LineType<'a> { } } -fn try_parse_line_as_timestamp(line: &str) -> Option<DateTime<Utc>> { +fn try_parse_line_as_timestamp(line: &str) -> Option<OffsetDateTime> { let seconds = line.strip_prefix('#')?.parse().ok()?; - let time = NaiveDateTime::from_timestamp(seconds, 0); - Some(DateTime::from_utc(time, Utc)) + OffsetDateTime::from_unix_timestamp(seconds).ok() } #[cfg(test)] @@ -183,7 +182,7 @@ cd ../ ["git reset", "git clean -dxf", "cd ../"], ); assert_equal( - loader.buf.iter().map(|h| h.timestamp.timestamp()), + loader.buf.iter().map(|h| h.timestamp.unix_timestamp()), [1672918999, 1672919006, 1672919020], ) } diff --git a/atuin-client/src/import/fish.rs b/atuin-client/src/import/fish.rs index 90ecabc3..82c59013 100644 --- a/atuin-client/src/import/fish.rs +++ b/atuin-client/src/import/fish.rs @@ -4,9 +4,9 @@ use std::{fs::File, io::Read, path::PathBuf}; use async_trait::async_trait; -use chrono::{prelude::*, Utc}; use directories::BaseDirs; use eyre::{eyre, Result}; +use time::OffsetDateTime; use super::{unix_byte_lines, Importer, Loader}; use crate::history::History; @@ -59,8 +59,8 @@ impl Importer for Fish { } async fn load(self, loader: &mut impl Loader) -> Result<()> { - let now = Utc::now(); - let mut time: Option<DateTime<Utc>> = None; + let now = OffsetDateTime::now_utc(); + let mut time: Option<OffsetDateTime> = None; let mut cmd: Option<String> = None; for b in unix_byte_lines(&self.bytes) { @@ -89,7 +89,7 @@ impl Importer for Fish { } else if let Some(t) = s.strip_prefix(" when: ") { // if t is not an int, just ignore this line if let Ok(t) = t.parse::<i64>() { - time = Some(Utc.timestamp(t, 0)); + time = Some(OffsetDateTime::from_unix_timestamp(t)?); } } else { // ... ignore paths lines @@ -164,7 +164,7 @@ ERROR ($timestamp:expr, $command:expr) => { let h = history.next().expect("missing entry in history"); assert_eq!(h.command.as_str(), $command); - assert_eq!(h.timestamp.timestamp(), $timestamp); + assert_eq!(h.timestamp.unix_timestamp(), $timestamp); }; } diff --git a/atuin-client/src/import/nu.rs b/atuin-client/src/import/nu.rs index 46600325..1131fac5 100644 --- a/atuin-client/src/import/nu.rs +++ b/atuin-client/src/import/nu.rs @@ -6,6 +6,7 @@ use std::{fs::File, io::Read, path::PathBuf}; use async_trait::async_trait; use directories::BaseDirs; use eyre::{eyre, Result}; +use time::OffsetDateTime; use super::{unix_byte_lines, Importer, Loader}; use crate::history::History; @@ -44,7 +45,7 @@ impl Importer for Nu { } async fn load(self, h: &mut impl Loader) -> Result<()> { - let now = chrono::Utc::now(); + let now = OffsetDateTime::now_utc(); let mut counter = 0; for b in unix_byte_lines(&self.bytes) { @@ -55,7 +56,7 @@ impl Importer for Nu { let cmd: String = s.replace("<\\n>", "\n"); - let offset = chrono::Duration::nanoseconds(counter); + let offset = time::Duration::nanoseconds(counter); counter += 1; let entry = History::import().timestamp(now - offset).command(cmd); diff --git a/atuin-client/src/import/nu_histdb.rs b/atuin-client/src/import/nu_histdb.rs index 34568d80..f0e8e95c 100644 --- a/atuin-client/src/import/nu_histdb.rs +++ b/atuin-client/src/import/nu_histdb.rs @@ -4,10 +4,10 @@ use std::path::PathBuf; use async_trait::async_trait; -use chrono::{prelude::*, Utc}; use directories::BaseDirs; use eyre::{eyre, Result}; use sqlx::{sqlite::SqlitePool, Pool}; +use time::{Duration, OffsetDateTime}; use super::Importer; use crate::history::History; @@ -31,10 +31,10 @@ impl From<HistDbEntry> for History { let ts_secs = histdb_item.start_timestamp / 1000; let ts_ns = (histdb_item.start_timestamp % 1000) * 1_000_000; let imported = History::import() - .timestamp(DateTime::from_utc( - NaiveDateTime::from_timestamp(ts_secs, ts_ns as u32), - Utc, - )) + .timestamp( + OffsetDateTime::from_unix_timestamp(ts_secs).unwrap() + + Duration::nanoseconds(ts_ns), + ) .command(String::from_utf8(histdb_item.command_line).unwrap()) .cwd(String::from_utf8(histdb_item.cwd).unwrap()) .exit(histdb_item.exit_status) diff --git a/atuin-client/src/import/resh.rs b/atuin-client/src/import/resh.rs index 3c5b799e..5475db51 100644 --- a/atuin-client/src/import/resh.rs +++ b/atuin-client/src/import/resh.rs @@ -1,12 +1,12 @@ use std::{fs::File, io::Read, path::PathBuf}; use async_trait::async_trait; -use chrono::{TimeZone, Utc}; use directories::UserDirs; use eyre::{eyre, Result}; use serde::Deserialize; use atuin_common::utils::uuid_v7; +use time::OffsetDateTime; use super::{get_histpath, unix_byte_lines, Importer, Loader}; use crate::history::History; @@ -110,16 +110,18 @@ impl Importer for Resh { #[allow(clippy::cast_sign_loss)] let timestamp = { let secs = entry.realtime_before.floor() as i64; - let nanosecs = (entry.realtime_before.fract() * 1_000_000_000_f64).round() as u32; - Utc.timestamp(secs, nanosecs) + let nanosecs = (entry.realtime_before.fract() * 1_000_000_000_f64).round() as i64; + OffsetDateTime::from_unix_timestamp(secs)? + time::Duration::nanoseconds(nanosecs) }; #[allow(clippy::cast_possible_truncation)] #[allow(clippy::cast_sign_loss)] let duration = { let secs = entry.realtime_after.floor() as i64; - let nanosecs = (entry.realtime_after.fract() * 1_000_000_000_f64).round() as u32; - let difference = Utc.timestamp(secs, nanosecs) - timestamp; - difference.num_nanoseconds().unwrap_or(0) + let nanosecs = (entry.realtime_after.fract() * 1_000_000_000_f64).round() as i64; + let base = OffsetDateTime::from_unix_timestamp(secs)? + + time::Duration::nanoseconds(nanosecs); + let difference = base - timestamp; + difference.whole_nanoseconds() as i64 }; let imported = History::import() diff --git a/atuin-client/src/import/zsh.rs b/atuin-client/src/import/zsh.rs index e98819e2..632caff9 100644 --- a/atuin-client/src/import/zsh.rs +++ b/atuin-client/src/import/zsh.rs @@ -4,9 +4,9 @@ use std::{fs::File, io::Read, path::PathBuf}; use async_trait::async_trait; -use chrono::{prelude::*, Utc}; use directories::UserDirs; use eyre::{eyre, Result}; +use time::OffsetDateTime; use super::{get_histpath, unix_byte_lines, Importer, Loader}; use crate::history::History; @@ -58,7 +58,7 @@ impl Importer for Zsh { } async fn load(self, h: &mut impl Loader) -> Result<()> { - let now = chrono::Utc::now(); + let now = OffsetDateTime::now_utc(); let mut line = String::new(); let mut counter = 0; @@ -79,7 +79,7 @@ impl Importer for Zsh { counter += 1; h.push(parse_extended(command, counter)).await?; } else { - let offset = chrono::Duration::seconds(counter); + let offset = time::Duration::seconds(counter); counter += 1; let imported = History::import() @@ -102,11 +102,10 @@ fn parse_extended(line: &str, counter: i64) -> History { let time = time .parse::<i64>() - .unwrap_or_else(|_| chrono::Utc::now().timestamp()); - - let offset = chrono::Duration::milliseconds(counter); - let time = Utc.timestamp(time, 0); - let time = time + offset; + .ok() + .and_then(|t| OffsetDateTime::from_unix_timestamp(t).ok()) + .unwrap_or_else(OffsetDateTime::now_utc) + + time::Duration::milliseconds(counter); // use nanos, because why the hell not? we won't display them. let duration = duration.parse::<i64>().map_or(-1, |t| t * 1_000_000_000); @@ -121,8 +120,6 @@ fn parse_extended(line: &str, counter: i64) -> History { #[cfg(test)] mod test { - use chrono::prelude::*; - use chrono::Utc; use itertools::assert_equal; use crate::import::tests::TestLoader; @@ -135,25 +132,37 @@ mod test { assert_eq!(parsed.command, "cargo install atuin"); assert_eq!(parsed.duration, 0); - assert_eq!(parsed.timestamp, Utc.timestamp(1_613_322_469, 0)); + assert_eq!( + parsed.timestamp, + OffsetDateTime::from_unix_timestamp(1_613_322_469).unwrap() + ); let parsed = parse_extended("1613322469:10;cargo install atuin;cargo update", 0); assert_eq!(parsed.command, "cargo install atuin;cargo update"); assert_eq!(parsed.duration, 10_000_000_000); - assert_eq!(parsed.timestamp, Utc.timestamp(1_613_322_469, 0)); + assert_eq!( + parsed.timestamp, + OffsetDateTime::from_unix_timestamp(1_613_322_469).unwrap() + ); let parsed = parse_extended("1613322469:10;cargo :b̷i̶t̴r̵o̴t̴ ̵i̷s̴ ̷r̶e̵a̸l̷", 0); assert_eq!(parsed.command, "cargo :b̷i̶t̴r̵o̴t̴ ̵i̷s̴ ̷r̶e̵a̸l̷"); assert_eq!(parsed.duration, 10_000_000_000); - assert_eq!(parsed.timestamp, Utc.timestamp(1_613_322_469, 0)); + assert_eq!( + parsed.timestamp, + OffsetDateTime::from_unix_timestamp(1_613_322_469).unwrap() + ); let parsed = parse_extended("1613322469:10;cargo install \\n atuin\n", 0); assert_eq!(parsed.command, "cargo install \\n atuin"); assert_eq!(parsed.duration, 10_000_000_000); - assert_eq!(parsed.timestamp, Utc.timestamp(1_613_322_469, 0)); + assert_eq!( + parsed.timestamp, + OffsetDateTime::from_unix_timestamp(1_613_322_469).unwrap() + ); } #[tokio::test] diff --git a/atuin-client/src/import/zsh_histdb.rs b/atuin-client/src/import/zsh_histdb.rs index 78a7176b..37c70814 100644 --- a/atuin-client/src/import/zsh_histdb.rs +++ b/atuin-client/src/import/zsh_histdb.rs @@ -35,10 +35,10 @@ use std::path::{Path, PathBuf}; use async_trait::async_trait; -use chrono::{prelude::*, Utc}; use directories::UserDirs; use eyre::{eyre, Result}; use sqlx::{sqlite::SqlitePool, Pool}; +use time::PrimitiveDateTime; use super::Importer; use crate::history::History; @@ -52,7 +52,7 @@ pub struct HistDbEntryCount { #[derive(sqlx::FromRow, Debug)] pub struct HistDbEntry { pub id: i64, - pub start_time: NaiveDateTime, + pub start_time: PrimitiveDateTime, pub host: Vec<u8>, pub dir: Vec<u8>, pub argv: Vec<u8>, @@ -62,7 +62,7 @@ pub struct HistDbEntry { impl From<HistDbEntry> for History { fn from(histdb_item: HistDbEntry) -> Self { let imported = History::import() - .timestamp(DateTime::from_utc(histdb_item.start_time, Utc)) + .timestamp(histdb_item.start_time.assume_utc()) .command( String::from_utf8(histdb_item.argv) .unwrap_or_else(|_e| String::from("")) |
