Provided timezone mapping and included origins of this program

This commit is contained in:
Trey Blancher
2022-11-11 19:05:16 -05:00
parent 0fe7608e87
commit f6ccac9d2a
2 changed files with 82 additions and 12 deletions

View File

@ -3,7 +3,7 @@ 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;
use chrono_tz::Tz;
fn main() {
// Regexes
@ -16,24 +16,42 @@ fn main() {
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<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 (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(&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;
let dt = tz.from_local_datetime(&ndt).unwrap();
let to: DateTime<FixedOffset> = DateTime::parse_from_rfc3339(&dt.to_rfc3339()).unwrap();
if old == DateTime::<Utc>::MIN_UTC { // detect first line
old = to;
continue;
} else if new == DateTime::<Utc>::MIN_UTC {
new = ts;
} else {
} else if new == DateTime::<Utc>::MIN_UTC { // detect second line
new = to;
} else { // subsequent lines
old = new;
new = ts;
new = to;
};
let dur: Duration = new - old;
@ -46,6 +64,6 @@ fn main() {
- days * 24 * 60 * 60 - hours * 60 * 60 - minutes * 60;
println!("{}: {} weeks, {} days, {} hours, {} minutes, {} seconds",
item, weeks, days, hours, minutes, seconds);
tag, weeks, days, hours, minutes, seconds);
};
}