Skip to content

Commit

Permalink
Better handling of unparsable --timezone values
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwiles committed Sep 18, 2023
1 parent ba38604 commit c9c7dc6
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,32 @@ fn print_spacer(mut output: impl Write, args: &Args, last_spacer: &Instant) -> R
writeln!(output, "{}", "\n".repeat(args.padding - 1))?;
}

let datetime_strings = if args.timezone.is_none() {
let now: DateTime<Local> = Local::now();
(
now.format("%Y-%m-%d").to_string(),
now.format("%H:%M:%S").to_string(),
)
} else {
let timezone_str = args.timezone.clone();
let timezone: Tz = timezone_str.unwrap().parse().expect("Invalid timezone");
let now: DateTime<Tz> = Local::now().with_timezone(&timezone);
(
now.format("%Y-%m-%d").to_string(),
now.format("%H:%M:%S %Z").to_string(),
)
let datetime_strings = match args.timezone.clone() {
None => {
let now: DateTime<Local> = Local::now();
(
now.format("%Y-%m-%d").to_string(),
now.format("%H:%M:%S").to_string(),
)
}
Some(timezone_str) => match timezone_str.parse::<Tz>() {
Ok(timezone) => {
let now: DateTime<Tz> = Local::now().with_timezone(&timezone);
(
now.format("%Y-%m-%d").to_string(),
now.format("%H:%M:%S %Z").to_string(),
)
}
Err(err) => {
eprintln!("Error: {}", err);
debug!("could not parse supplied timezone name, using local time");
let now: DateTime<Local> = Local::now();
(
now.format("%Y-%m-%d").to_string(),
now.format("%H:%M:%S").to_string(),
)
}
},
};

let date_str = datetime_strings.0;
Expand Down

0 comments on commit c9c7dc6

Please sign in to comment.