diff --git a/Cargo.lock b/Cargo.lock index 61ae8af..f476aab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,56 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + [[package]] name = "autocfg" version = "1.5.0" @@ -62,6 +112,52 @@ dependencies = [ "phf", ] +[[package]] +name = "clap" +version = "4.5.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6899ea499e3fb9305a65d5ebf6e3d2248c5fab291f300ad0a704fbe142eae31a" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b12c8b680195a62a8364d16b8447b01b6c2c8f9aaf68bee653be34d4245e238" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92793da1a46a5f2a02a6f4c46c6496b28c43638adea8306fcb0caa1634f24e5" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e64b0cc0439b12df2fa678eae89a1c56a529fd067a9115f7827f1fffd22b32" + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + [[package]] name = "core-foundation-sys" version = "0.8.7" @@ -74,6 +170,12 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "iana-time-zone" version = "0.1.65" @@ -98,6 +200,12 @@ dependencies = [ "cc", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + [[package]] name = "js-sys" version = "0.3.85" @@ -110,9 +218,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.180" +version = "0.2.181" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" +checksum = "459427e2af2b9c839b132acb702a1c654d95e10f8c326bfc2ad11310e458b1c5" [[package]] name = "log" @@ -135,6 +243,12 @@ version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + [[package]] name = "phf" version = "0.12.1" @@ -189,6 +303,12 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b2aa850e253778c88a04c3d7323b043aeda9d3e30d5971937c1855769763678e" +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "syn" version = "2.0.114" @@ -206,13 +326,20 @@ version = "0.1.0" dependencies = [ "chrono", "chrono-tz", + "clap", ] [[package]] name = "unicode-ident" -version = "1.0.22" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" +checksum = "537dd038a89878be9b64dd4bd1b260315c1bb94f4d784956b81e27a088d9a09e" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "wasm-bindgen" @@ -317,3 +444,12 @@ checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" dependencies = [ "windows-link", ] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] diff --git a/Cargo.toml b/Cargo.toml index 281ee65..3484cfa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2024" [dependencies] chrono = "0.4.43" chrono-tz = "0.10.4" +clap = { version = "4.5.57", features = ["derive"] } diff --git a/src/calendar.rs b/src/calendar.rs index 9c08fad..b858092 100644 --- a/src/calendar.rs +++ b/src/calendar.rs @@ -4,7 +4,7 @@ use std::error::Error; use std::fmt; #[derive(PartialEq, Debug)] -enum CalendarError { +pub enum CalendarError { UnknownTimezone(String), WrongTimeFormat, WrongDateFormat, @@ -58,11 +58,11 @@ pub fn create_timetable( .or_else(|_| NaiveDate::parse_from_str(target_date, "%d-%m")) .map_err(|_| CalendarError::WrongDateFormat)?; - let datetime = NaiveDateTime::new(parsed_date, parsed_time); - let time_in_timezone = parsed_timezone - .from_local_datetime(&datetime) + let datetime = Local.from_local_datetime(&NaiveDateTime::new(parsed_date, parsed_time)); + let time_in_timezone = datetime .single() - .ok_or(CalendarError::TimezoneConversionFailed(in_timezone.clone()))?; + .ok_or(CalendarError::TimezoneConversionFailed(in_timezone.clone()))? + .with_timezone(&parsed_timezone); Ok(create_calendar_strings( &time_in_timezone.time(), @@ -70,6 +70,10 @@ pub fn create_timetable( )) } +pub fn get_todays_date() -> String { + Local::now().format("%d-%m-%Y").to_string() +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..000a929 --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,28 @@ +use super::calendar::create_timetable; +use clap::Parser; + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +pub struct Args { + #[arg(short, long)] + pub timezone: Vec, + + #[arg(short, long)] + pub date: Option, + + pub time: String, +} + +pub fn print_timezones(timezones: &Vec, time: &String, date: &String) { + let max_len = timezones.iter().map(|t| t.len()).max().unwrap_or(0) as u32; + for timezone in timezones { + let name_padding = (0..(max_len - timezone.len() as u32)) + .map(|_| " ".to_string()) + .collect::>() + .join(""); + match create_timetable(time, date, timezone, 5) { + Ok(timetable) => println!("{}:{}\t{}", timezone, name_padding, timetable.join("\t")), + Err(error) => panic!("Failed: {}", error), + } + } +} diff --git a/src/main.rs b/src/main.rs index 1f504fd..6cf19b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,15 @@ mod calendar; +mod cli; -fn main() {} +use calendar::get_todays_date; +use clap::Parser; +use cli::{Args, print_timezones}; + +fn main() { + let args = Args::parse(); + + if args.timezone.len() > 0 { + let date = args.date.unwrap_or(get_todays_date()); + print_timezones(&args.timezone, &args.time, &date); + } +}