Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For #152 - Remove chrono dependency #159

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 0 additions & 173 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2021"

[dependencies]
subprocess = "0.2"
chrono = "0.4"
clap = { version = "4.5", features = ["derive"] }
log = "0.4"
env_logger = "0.11"
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod procfsapi;
mod ps;
mod slurm;
mod sysinfo;
mod time;
mod users;
mod util;

Expand Down Expand Up @@ -80,7 +81,7 @@ fn main() {
// obtained, not the time when reporting was allowed to run. The latter is subject to greater
// system effects, and using that timestamp increases the risk that the samples' timestamp order
// improperly reflects the true order in which they were obtained. See #100.
let timestamp = util::time_iso8601();
let timestamp = time::now_iso8601();

env_logger::init();

Expand Down
70 changes: 70 additions & 0 deletions src/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use libc;
use std::ffi::CStr;

// Get current time as an ISO time stamp: yyyy-mm-ddThh:mm:ss+hhmm
//
// Use libc to avoid pulling in all of Chrono for this:
// t = time()
// localtime_r(&t, timebuf)
// strftime(strbuf, strbufsize, "%FT%T%z", timebuf)
//
// Panic on errors, there should never be any.

pub fn now_iso8601() -> String {
let mut timebuf = libc::tm {
tm_sec: 0,
tm_min: 0,
tm_hour: 0,
tm_mday: 0,
tm_mon: 0,
tm_year: 0,
tm_wday: 0,
tm_yday: 0,
tm_isdst: 0,
tm_gmtoff: 0,
tm_zone: std::ptr::null(),
};
const SIZE: usize = 32; // We need 25 unless something is greatly off
let mut buffer = vec![0i8; SIZE];
unsafe {
let t = libc::time(std::ptr::null_mut());

if libc::localtime_r(&t, &mut timebuf) == std::ptr::null_mut() {
panic!("localtime_r");
}

// strftime returns 0 if the buffer is too small for the result + NUL.
if libc::strftime(
buffer.as_mut_ptr(),
SIZE,
CStr::from_bytes_with_nul_unchecked(b"%FT%T%z\0").as_ptr(),
&timebuf) == 0 {
panic!("strftime");
}

return CStr::from_ptr(buffer.as_ptr()).to_str().unwrap().to_string();
}
}

#[test]
pub fn test_isotime() {
let t = now_iso8601();
let ts = t.as_str().chars().collect::<Vec<char>>();
let expect = "dddd-dd-ddTdd:dd:dd+dddd";
let mut i = 0;
for c in expect.chars() {
match c {
'd' => {
assert!(ts[i] >= '0' && ts[i] <= '9');
}
'+' => {
assert!(ts[i] == '+' || ts[i] == '-');
}
_ => {
assert!(ts[i] == c);
}
}
i += 1;
}
assert!(i == ts.len());
}
8 changes: 0 additions & 8 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#![allow(unused_imports)]
#![allow(unused_macros)]

use chrono::prelude::Local;

// Populate a HashSet.
#[cfg(test)]
macro_rules! set(
Expand Down Expand Up @@ -37,12 +35,6 @@ pub(crate) use map;
#[cfg(test)]
pub(crate) use set;

// Get current time as an ISO time stamp.
pub fn time_iso8601() -> String {
let local_time = Local::now();
format!("{}", local_time.format("%Y-%m-%dT%H:%M:%S%Z"))
}

// Carve up a line of text into space-separated chunks + the start indices of the chunks.
pub fn chunks(input: &str) -> (Vec<usize>, Vec<&str>) {
let mut start_indices: Vec<usize> = Vec::new();
Expand Down
Loading