diff --git a/Cargo.toml b/Cargo.toml index 1f57358..bceebb5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tzconvert" -version = "1.1.0" +version = "1.2.0" edition = "2018" authors = ["Jonathan Strong "] diff --git a/src/lib.rs b/src/lib.rs index 010fdc4..9a4c2b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ use colored::*; lazy_static::lazy_static! { static ref HOUR_AMPM: regex::Regex = regex::Regex::new(r#"(?P
\d{1,2})\s?(?P(am|pm|AM|PM))"#).unwrap(); static ref HOUR_MINUTE_AMPM: regex::Regex = regex::Regex::new(r#"(?P
\d{1,2}):(?P\d{2})\s?(?P(am|pm|AM|PM))"#).unwrap(); + static ref ZERO_PADDED_OFFSET: regex::Regex = regex::Regex::new(r#"(?P[+-])0(?P
\d)h"#).unwrap(); } fn parse_time(time_input: &str) -> NaiveTime { @@ -84,38 +85,95 @@ pub fn convert(from: &str, to: &str, time: &str, day: Option, date: Opti let dst = src.with_timezone(&totz); - let fromtz_str = fromtz.to_string(); - let totz_str = totz.to_string(); - let n_spaces = std::cmp::max(fromtz_str.len(), totz_str.len()); - let indent = " "; - println!(); - println!("{}{} ... {} ... {}", - indent, - pad_spaces(fromtz_str, n_spaces), - src.format("%a, %b %e"), - src.format("%l:%M%P"), - ); + // let indent = " "; + // println!(); + // println!("{}{} ... {} ... {}", + // indent, + // pad_spaces(fromtz_str, n_spaces), + // src.format("%a, %b %e"), + // src.format("%l:%M%P"), + // ); + + // println!(); + + // let dst_line = format!("{}{} ... {} ... {}", + // indent, + // pad_spaces(totz_str, n_spaces), + // dst.format("%a, %b %e"), + // dst.format("%l:%M%P").to_string(), + // ); + // println!("{}", dst_line.bold()); + println!("tzconvert v{}", structopt::clap::crate_version!()); println!(); + let line = get_output(src, dst); + println!(" {}", line); + println!(); +} - let dst_line = format!("{}{} ... {} ... {}", - indent, - pad_spaces(totz_str, n_spaces), - dst.format("%a, %b %e"), - dst.format("%l:%M%P").to_string(), +fn get_output(src: DateTime, dst: DateTime) -> String { + let mut out = String::with_capacity(128); + + let fromtz_str = src.timezone().to_string(); + let totz_str = dst.timezone().to_string(); + let n_spaces = std::cmp::max(fromtz_str.len(), totz_str.len()); + + //let mut src_str = format!("{tz} | {offset}h | {dt} | {tm}", + let mut src_str = format!("{tz} --+-- {offset}h --+-- {dt} --+-- {tm}", + tz = pad(fromtz_str, n_spaces, " "), + offset = src.offset().fix(), + dt = src.format("%a, %b %e"), + tm = src.format("%l:%M%P"), + ); + src_str = clean(src_str); + out.push_str(&format!("\n {}\n {}\n {}\n", pad("", src_str.len(), " "), src_str, pad("", src_str.len(), " "))); + //out.push_str(" -> "); + + let abs_diff_in_hours = (src.offset().fix().local_minus_utc() - dst.offset().fix().local_minus_utc()).abs() / 60 / 60; + out.push_str(&format!(" .\n .\n . {diff}h\n .\n .\n ", diff = abs_diff_in_hours)); + + //let mut dst_str = format!("{tz} | {offset}h | {dt} | {tm}", + let mut dst_str = format!("{tz} --+-- {offset}h --+-- {dt} --+-- {tm}", + //tz = pad_spaces(dst.timezone(), n_spaces), + tz = pad(totz_str, n_spaces, " "), + offset = dst.offset().fix(), + dt = dst.format("%a, %b %e"), + tm = dst.format("%l:%M%P"), ); + dst_str = clean(dst_str); + out.push_str(&format!(" {}\n {}\n {}\n", + pad("", dst_str.len(), " "), + dst_str.bold(), + pad("", dst_str.len(), " ")) + ); + //out.push_str(&format!(" {}\n {}\n {}\n\n", + // pad("", dst_str.len(), "-"), dst_str.bold(), pad("", dst_str.len(), "-")) + //); + + //out = out.replace( + // " -> ", + // &format!("\n\n .\n . {diff}h\n .\n\n ", diff = abs_diff_in_hours), + //); + out +} - println!("{}", dst_line.bold()); - println!(); +fn clean(mut out: String) -> String { + out = out.replace(":00", ""); + //while out.contains(" ") { + // out = out.replace(" ", " "); + //} + + out = ZERO_PADDED_OFFSET.replace_all(&out, "${sign}${hr}h").to_string(); + out } -fn pad_spaces(s: S, n: usize) -> String { +fn pad(s: S, n: usize, with: &str) -> String { let s = s.to_string(); let mut out = String::with_capacity(n); out.push_str(&s); while out.len() < n { - out.push_str(" "); + out.push_str(with); } out }