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::Tz; fn main() { // Regexes //let sep = Regex::new(r":").unwrap(); // read file argument or stdin let input = env::args().nth(1); let reader: Box = match input { None => Box::new(BufReader::new(io::stdin())), Some(filename) => Box::new(BufReader::new(fs::File::open(filename).unwrap())) }; // set vacuous values for old and new timestamps, so we can detect the first and second lines // of input let mut old: DateTime = DateTime::from(DateTime::::MIN_UTC); let mut new: DateTime = DateTime::from(DateTime::::MIN_UTC); for line in reader.lines() { let l: String = line.unwrap(); let (tag, timestamp) = l.split_once(':').unwrap(); // Timezone mapping let ts: Vec<&str> = timestamp.split_whitespace().collect(); let timezone = ts[4]; let tz: Tz = match timezone { "EDT" => "America/New_York".parse().unwrap(), "EST" => "America/New_York".parse().unwrap(), "CDT" => "America/Chicago".parse().unwrap(), "CST" => "America/Chicago".parse().unwrap(), "MDT" => "America/Denver".parse().unwrap(), "MST" => "America/Denver".parse().unwrap(), "PDT" => "America/Los_Angeles".parse().unwrap(), "PST" => "America/Los_Angeles".parse().unwrap(), "UTC" => "UTC".parse().unwrap(), "GMT" => "GMT".parse().unwrap(), other => panic!("Timezone '{}' unknown!", other) }; let ndt = NaiveDateTime::parse_from_str(×tamp, "%a %b %e %H:%M:%S %Z %Y").unwrap(); let dt = tz.from_local_datetime(&ndt).unwrap(); let to: DateTime = DateTime::parse_from_rfc3339(&dt.to_rfc3339()).unwrap(); if old == DateTime::::MIN_UTC { // detect first line old = to; continue; } else if new == DateTime::::MIN_UTC { // detect second line new = to; } else { // subsequent lines old = new; new = to; }; 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", tag, weeks, days, hours, minutes, seconds); }; }