Skip to content

Commit

Permalink
replace chrono with time
Browse files Browse the repository at this point in the history
  • Loading branch information
mitnk committed Jul 17, 2022
1 parent 75b2cfb commit 8c52ff3
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 75 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
48 changes: 9 additions & 39 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
edition = "2021"
build = "src/build.rs"
name = "cicada"
version = "0.9.31"
version = "0.9.32"
authors = ["Hugo Wang <w@mitnk.com>"]

description = "A simple Bash-like Unix shell."
Expand All @@ -23,7 +23,6 @@ name = "cicada"
doc = false

[dependencies]
chrono = "0.4"
errno = "0.2.0"
exec = "0.3.0"
glob = "0.3.0"
Expand All @@ -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"]
21 changes: 17 additions & 4 deletions src/build.rs
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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(_) => { }
}
}
8 changes: 3 additions & 5 deletions src/builtins/history.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -215,10 +215,8 @@ fn list_current_history(sh: &Shell, conn: &Conn,
return (result_stdout, result_stderr);
}
};
let dt = DateTime::<Utc>::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));
}
Expand Down
54 changes: 54 additions & 0 deletions src/ctime.rs
Original file line number Diff line number Diff line change
@@ -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(),
)
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ extern crate linefeed;
extern crate nix;
extern crate regex;
extern crate rusqlite;
extern crate chrono;

#[macro_use]
extern crate lazy_static;
extern crate pest;
#[macro_use]
extern crate pest_derive;

mod ctime;
mod types;

#[macro_use]
Expand Down
7 changes: 3 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]
Expand All @@ -34,6 +32,7 @@ mod builtins;
mod calculator;
mod completers;
mod core;
mod ctime;
mod execute;
mod history;
mod jobc;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
22 changes: 4 additions & 18 deletions src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 8c52ff3

Please sign in to comment.