Initial commit of Rust date-tally

This commit is contained in:
Trey Blancher 2022-11-11 12:09:09 -05:00
commit 8e5e4f5e87
3 changed files with 63 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

11
Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "date-tally"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = "0.4"
regex = "1"
chrono-tz = "0.8"

51
src/main.rs Normal file
View File

@ -0,0 +1,51 @@
use std::env;
use std::fs;
use std::io::{self, BufReader, BufRead};
//use regex::Regex;
use chrono::{Duration, DateTime, FixedOffset, NaiveDateTime, TimeZone, Utc};
use chrono_tz::America::New_York;
fn main() {
// Regexes
//let sep = Regex::new(r":").unwrap();
// read file argument or stdin
let input = env::args().nth(1);
let reader: Box<dyn BufRead> = match input {
None => Box::new(BufReader::new(io::stdin())),
Some(filename) => Box::new(BufReader::new(fs::File::open(filename).unwrap()))
};
let mut old: DateTime<FixedOffset> = DateTime::from(DateTime::<Utc>::MIN_UTC);
let mut new: DateTime<FixedOffset> = DateTime::from(DateTime::<Utc>::MIN_UTC);
for line in reader.lines() {
let l: String = line.unwrap();
let (item, timestamp) = l.split_once(':').unwrap();
let ndt = NaiveDateTime::parse_from_str(&timestamp, "%a %b %e %H:%M:%S %Z %Y").unwrap();
let dt = New_York.from_local_datetime(&ndt).unwrap();
let ts: DateTime<FixedOffset> = DateTime::parse_from_rfc3339(&dt.to_rfc3339()).unwrap();
if old == DateTime::<Utc>::MIN_UTC {
old = ts;
continue;
} else if new == DateTime::<Utc>::MIN_UTC {
new = ts;
} else {
old = new;
new = ts;
};
let dur: Duration = new - old;
let weeks = dur.num_weeks();
let days = dur.num_days() - weeks * 7;
let hours = dur.num_hours() - weeks * 7 * 24 - days * 24;
let minutes = dur.num_minutes() - weeks * 7 * 24 * 60 - days * 24 * 60 - hours * 60;
let seconds = dur.num_seconds() - weeks * 7 * 24 * 60 * 60
- days * 24 * 60 * 60 - hours * 60 * 60 - minutes * 60;
println!("{}: {} weeks, {} days, {} hours, {} minutes, {} seconds",
item, weeks, days, hours, minutes, seconds);
};
}