From 8c52ff388755c1c2bf8d37d7e6208770dbc27ad2 Mon Sep 17 00:00:00 2001 From: Hugo Wang Date: Sun, 17 Jul 2022 18:12:02 +0800 Subject: [PATCH] replace chrono with time --- CHANGELOG.md | 5 ++++ Cargo.lock | 48 +++++++----------------------------- Cargo.toml | 12 ++++++--- src/build.rs | 21 +++++++++++++--- src/builtins/history.rs | 8 +++--- src/ctime.rs | 54 +++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 +- src/main.rs | 7 +++--- src/tools.rs | 22 +++-------------- 9 files changed, 104 insertions(+), 75 deletions(-) create mode 100644 src/ctime.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bf27b8..0a52978 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # cicada Release Notes +## 0.9.32 - 2022.07.17 + +- Replaced dep chrono with time. + - due to https://github.com/rustsec/advisory-db/pull/1082 + ## 0.9.31 - 2022.07.17 - fixed an escape char `\` handling issue. diff --git a/Cargo.lock b/Cargo.lock index 2b3e0bf..00220f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -114,24 +114,10 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "chrono" -version = "0.4.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" -dependencies = [ - "libc", - "num-integer", - "num-traits", - "time", - "winapi", -] - [[package]] name = "cicada" -version = "0.9.31" +version = "0.9.32" dependencies = [ - "chrono", "clap 3.2.12", "errno", "exec", @@ -145,6 +131,7 @@ dependencies = [ "regex", "rusqlite", "structopt", + "time", "uuid", "yaml-rust", ] @@ -486,22 +473,12 @@ dependencies = [ ] [[package]] -name = "num-integer" -version = "0.1.45" +name = "num_threads" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" dependencies = [ - "autocfg", - "num-traits", -] - -[[package]] -name = "num-traits" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" -dependencies = [ - "autocfg", + "libc", ] [[package]] @@ -913,13 +890,12 @@ dependencies = [ [[package]] name = "time" -version = "0.1.44" +version = "0.3.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +checksum = "72c91f41dcb2f096c05f0873d667dceec1087ce5bcf984ec8ffb19acddbb3217" dependencies = [ "libc", - "wasi 0.10.0+wasi-snapshot-preview1", - "winapi", + "num_threads", ] [[package]] @@ -1004,12 +980,6 @@ version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" -[[package]] -name = "wasi" -version = "0.10.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 11b1937..e23c4d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ edition = "2021" build = "src/build.rs" name = "cicada" -version = "0.9.31" +version = "0.9.32" authors = ["Hugo Wang "] description = "A simple Bash-like Unix shell." @@ -23,7 +23,6 @@ name = "cicada" doc = false [dependencies] -chrono = "0.4" errno = "0.2.0" exec = "0.3.0" glob = "0.3.0" @@ -47,5 +46,10 @@ features = ["std"] version = "0.27.0" features = ["bundled"] -[build-dependencies] -chrono = "0.4" +[dependencies.time] +version = "0.3" +features = ["local-offset"] + +[build-dependencies.time] +version = "0.3" +features = ["local-offset"] diff --git a/src/build.rs b/src/build.rs index df4a35d..22a6963 100644 --- a/src/build.rs +++ b/src/build.rs @@ -1,6 +1,6 @@ -extern crate chrono; +extern crate time; use std::process::Command; -use chrono::prelude::Local; +use time::OffsetDateTime; fn main() { match Command::new("git") @@ -52,6 +52,19 @@ fn main() { } } - let tm = Local::now(); - println!("cargo:rustc-env=BUILD_DATE={}", tm.to_rfc2822()); + match OffsetDateTime::now_local() { + Ok(dt) => { + let dt_str = format!("{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:03}", + dt.year(), + dt.month() as u8, + dt.day(), + dt.hour(), + dt.minute(), + dt.second(), + dt.millisecond(), + ); + println!("cargo:rustc-env=BUILD_DATE={}", dt_str); + } + Err(_) => { } + } } diff --git a/src/builtins/history.rs b/src/builtins/history.rs index 589f90e..fa5dde5 100644 --- a/src/builtins/history.rs +++ b/src/builtins/history.rs @@ -1,11 +1,11 @@ use std::path::Path; -use chrono::{DateTime, NaiveDateTime, Local, Utc}; use rusqlite::Connection as Conn; use structopt::StructOpt; use crate::builtins::utils::print_stderr_with_capture; use crate::builtins::utils::print_stdout_with_capture; +use crate::ctime; use crate::history; use crate::parsers; use crate::shell::Shell; @@ -215,10 +215,8 @@ fn list_current_history(sh: &Shell, conn: &Conn, return (result_stdout, result_stderr); } }; - let dt = DateTime::::from_utc( - NaiveDateTime::from_timestamp(tsb as i64, 0), Utc - ).with_timezone(&Local); - lines.push(format!("{}: {}: {}", row_id, dt.format("%Y-%m-%d %H:%M:%S"), inp)); + let dt = ctime::DateTime::from_timestamp(tsb); + lines.push(format!("{}: {}: {}", row_id, dt, inp)); } else { lines.push(format!("{}: {}", row_id, inp)); } diff --git a/src/ctime.rs b/src/ctime.rs new file mode 100644 index 0000000..939e857 --- /dev/null +++ b/src/ctime.rs @@ -0,0 +1,54 @@ +use std::fmt; +use time::OffsetDateTime; + +#[derive(Debug, PartialEq, Eq)] +pub struct DateTime { + odt: OffsetDateTime, +} + +impl DateTime { + pub fn now() -> Self { + let odt: OffsetDateTime; + match OffsetDateTime::now_local() { + Ok(dt) => { + odt = dt; + } + Err(_) => { + odt = OffsetDateTime::now_utc(); + } + } + DateTime { odt } + } + + pub fn from_timestamp(ts: f64) -> Self { + let dummy_now = Self::now(); + let offset_seconds = dummy_now.odt.offset().whole_minutes() * 60; + let ts_nano = (ts + offset_seconds as f64) * 1000000000.0; + let odt: OffsetDateTime; + match OffsetDateTime::from_unix_timestamp_nanos(ts_nano as i128) { + Ok(x) => odt = x, + Err(_) => { + odt = OffsetDateTime::now_utc(); + } + } + DateTime { odt } + } + + pub fn unix_timestamp(&self) -> f64 { + self.odt.unix_timestamp_nanos() as f64 / 1000000000.0 + } +} + +impl fmt::Display for DateTime { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:03}", + self.odt.year(), + self.odt.month() as u8, + self.odt.day(), + self.odt.hour(), + self.odt.minute(), + self.odt.second(), + self.odt.millisecond(), + ) + } +} diff --git a/src/lib.rs b/src/lib.rs index cc0ee0b..72ecd7d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,7 +55,6 @@ extern crate linefeed; extern crate nix; extern crate regex; extern crate rusqlite; -extern crate chrono; #[macro_use] extern crate lazy_static; @@ -63,6 +62,7 @@ extern crate pest; #[macro_use] extern crate pest_derive; +mod ctime; mod types; #[macro_use] diff --git a/src/main.rs b/src/main.rs index 3f6fd69..150f9cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,6 @@ extern crate linefeed; extern crate nix; extern crate regex; extern crate rusqlite; -extern crate chrono; extern crate yaml_rust; extern crate clap; @@ -23,7 +22,6 @@ use std::env; use std::io::Write; use std::sync::Arc; -use chrono::prelude::Local; use linefeed::{Interface, ReadResult}; #[macro_use] @@ -34,6 +32,7 @@ mod builtins; mod calculator; mod completers; mod core; +mod ctime; mod execute; mod history; mod jobc; @@ -148,7 +147,7 @@ fn main() { } sh.cmd = line.clone(); - let tsb = Local::now().timestamp_nanos() as f64 / 1000000000.0; + let tsb = ctime::DateTime::now().unix_timestamp(); let mut line = line.clone(); // since `!!` expansion is only meaningful in an interactive @@ -160,7 +159,7 @@ fn main() { if let Some(last) = cr_list.last() { status = last.status; } - let tse = Local::now().timestamp_nanos() as f64 / 1000000000.0; + let tse = ctime::DateTime::now().unix_timestamp(); if !sh.cmd.starts_with(' ') && line != sh.previous_cmd { history::add(&sh, &mut rl, &line, status, tsb, tse); diff --git a/src/tools.rs b/src/tools.rs index cd76055..7ee86ce 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -6,10 +6,10 @@ use std::io::Write; use std::os::unix::io::IntoRawFd; use std::path::{Path, PathBuf}; -use chrono::prelude::{Local, Datelike, Timelike}; use libc; use regex::Regex; +use crate::ctime; use crate::execute; use crate::libs::re::re_contains; use crate::parsers; @@ -50,23 +50,9 @@ pub fn clog(s: &str) { } } let pid = unsafe { libc::getpid() }; - let now = Local::now(); - let s = format!( - "[{:04}-{:02}-{:02} {:02}:{:02}:{:02}][{}] {}", - now.year(), - now.month(), - now.day(), - now.hour(), - now.minute(), - now.second(), - pid, - s, - ); - let s = if s.ends_with('\n') { - s - } else { - format!("{}\n", s) - }; + let now = ctime::DateTime::now(); + let s = format!("[{}][{}] {}", now, pid, s); + let s = if s.ends_with('\n') { s } else { format!("{}\n", s) }; match cfile.write_all(s.as_bytes()) { Ok(_) => {} Err(e) => {