add option to list timezones

This commit is contained in:
Ondrej Novak 2026-02-19 21:47:55 +01:00
parent c1b2fce366
commit a01d0db7ce
2 changed files with 20 additions and 4 deletions

View File

@ -4,6 +4,7 @@ use chrono::Local;
use chrono::NaiveDate; use chrono::NaiveDate;
use chrono::NaiveTime; use chrono::NaiveTime;
use chrono::TimeZone; use chrono::TimeZone;
use chrono_tz::TZ_VARIANTS;
use chrono_tz::Tz; use chrono_tz::Tz;
use clap::Parser; use clap::Parser;
use colored::Colorize; use colored::Colorize;
@ -28,7 +29,10 @@ pub struct Args {
#[arg(short, long, default_value = "Local")] #[arg(short, long, default_value = "Local")]
pub local: String, pub local: String,
pub time: String, #[arg(long)]
pub list: bool,
pub time: Option<String>,
} }
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
@ -60,13 +64,15 @@ impl Error for CliError {}
pub fn print_timezones( pub fn print_timezones(
timezones: &Vec<String>, timezones: &Vec<String>,
time: &String, time: Option<&String>,
date: Option<&String>, date: Option<&String>,
local_timezone: &String, local_timezone: &String,
padding_hours: i8, padding_hours: i8,
) -> Result<(), CliError> { ) -> Result<(), CliError> {
let todays_date = &get_todays_date(); let todays_date = &get_todays_date();
let date = date.unwrap_or(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>() { let parsed_time = if let Ok(hour) = time.parse::<u32>() {
NaiveTime::from_hms_opt(hour, 0, 0).ok_or(CliError::CouldNotParseTime)? 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")); println!("{}:{}\t{}", name, name_padding, formatted.join("\t"));
} }
pub fn list_timezones() {
for tz in TZ_VARIANTS {
println!("{}", tz.name())
}
}

View File

@ -4,13 +4,17 @@ mod cli;
use clap::Parser; use clap::Parser;
use cli::{Args, print_timezones}; use cli::{Args, print_timezones};
use crate::cli::list_timezones;
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
if args.timezone.len() > 0 { if args.list {
list_timezones();
} else if args.timezone.len() > 0 {
let res = print_timezones( let res = print_timezones(
&args.timezone, &args.timezone,
&args.time, args.time.as_ref(),
args.date.as_ref(), args.date.as_ref(),
&args.local, &args.local,
5, 5,