add option to list timezones
This commit is contained in:
parent
c1b2fce366
commit
a01d0db7ce
16
src/cli.rs
16
src/cli.rs
|
|
@ -4,6 +4,7 @@ use chrono::Local;
|
|||
use chrono::NaiveDate;
|
||||
use chrono::NaiveTime;
|
||||
use chrono::TimeZone;
|
||||
use chrono_tz::TZ_VARIANTS;
|
||||
use chrono_tz::Tz;
|
||||
use clap::Parser;
|
||||
use colored::Colorize;
|
||||
|
|
@ -28,7 +29,10 @@ pub struct Args {
|
|||
#[arg(short, long, default_value = "Local")]
|
||||
pub local: String,
|
||||
|
||||
pub time: String,
|
||||
#[arg(long)]
|
||||
pub list: bool,
|
||||
|
||||
pub time: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
|
|
@ -60,13 +64,15 @@ impl Error for CliError {}
|
|||
|
||||
pub fn print_timezones(
|
||||
timezones: &Vec<String>,
|
||||
time: &String,
|
||||
time: Option<&String>,
|
||||
date: Option<&String>,
|
||||
local_timezone: &String,
|
||||
padding_hours: i8,
|
||||
) -> Result<(), CliError> {
|
||||
let todays_date = &get_todays_date();
|
||||
let date = date.unwrap_or(todays_date);
|
||||
let time_now = Local::now().format("%H:%M").to_string();
|
||||
let time = time.unwrap_or(&time_now);
|
||||
|
||||
let parsed_time = if let Ok(hour) = time.parse::<u32>() {
|
||||
NaiveTime::from_hms_opt(hour, 0, 0).ok_or(CliError::CouldNotParseTime)?
|
||||
|
|
@ -194,3 +200,9 @@ fn print_timetable(name: &String, name_padding_len: u32, timetable: &Vec<NaiveTi
|
|||
|
||||
println!("{}:{}\t{}", name, name_padding, formatted.join("\t"));
|
||||
}
|
||||
|
||||
pub fn list_timezones() {
|
||||
for tz in TZ_VARIANTS {
|
||||
println!("{}", tz.name())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,13 +4,17 @@ mod cli;
|
|||
use clap::Parser;
|
||||
use cli::{Args, print_timezones};
|
||||
|
||||
use crate::cli::list_timezones;
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
if args.timezone.len() > 0 {
|
||||
if args.list {
|
||||
list_timezones();
|
||||
} else if args.timezone.len() > 0 {
|
||||
let res = print_timezones(
|
||||
&args.timezone,
|
||||
&args.time,
|
||||
args.time.as_ref(),
|
||||
args.date.as_ref(),
|
||||
&args.local,
|
||||
5,
|
||||
|
|
|
|||
Loading…
Reference in New Issue