simplify date and time parsing
This commit is contained in:
parent
a01d0db7ce
commit
2a979cd98f
|
|
@ -46,10 +46,6 @@ pub fn create_timetable<TzLocal: TimeZone, TzTarget: TimeZone>(
|
|||
Ok(create_timetable_vecs(&time_in_timezone.time(), padding_hrs))
|
||||
}
|
||||
|
||||
pub fn get_todays_date() -> String {
|
||||
Local::now().format("%d-%m-%Y").to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
|||
25
src/cli.rs
25
src/cli.rs
|
|
@ -1,4 +1,4 @@
|
|||
use super::calendar::{create_timetable, get_todays_date};
|
||||
use super::calendar::create_timetable;
|
||||
|
||||
use chrono::Local;
|
||||
use chrono::NaiveDate;
|
||||
|
|
@ -69,20 +69,23 @@ pub fn print_timezones(
|
|||
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>() {
|
||||
let parsed_time = if let Some(t) = time {
|
||||
if let Ok(hour) = t.parse::<u32>() {
|
||||
NaiveTime::from_hms_opt(hour, 0, 0).ok_or(CliError::CouldNotParseTime)?
|
||||
} else {
|
||||
NaiveTime::parse_from_str(time, "%H:%M").map_err(|_| CliError::CouldNotParseTime)?
|
||||
NaiveTime::parse_from_str(t, "%H:%M").map_err(|_| CliError::CouldNotParseTime)?
|
||||
}
|
||||
} else {
|
||||
Local::now().naive_local().time()
|
||||
};
|
||||
|
||||
let parsed_date = NaiveDate::parse_from_str(date, "%d-%m-%Y")
|
||||
.or_else(|_| NaiveDate::parse_from_str(date, "%d-%m"))
|
||||
.map_err(|_| CliError::CouldNotParseDate)?;
|
||||
let parsed_date = if let Some(d) = date {
|
||||
NaiveDate::parse_from_str(d, "%d-%m-%Y")
|
||||
.or_else(|_| NaiveDate::parse_from_str(d, "%d-%m"))
|
||||
.map_err(|_| CliError::CouldNotParseDate)?
|
||||
} else {
|
||||
Local::now().naive_local().date()
|
||||
};
|
||||
|
||||
if local_timezone == "Local" {
|
||||
print_calendar(
|
||||
|
|
|
|||
Loading…
Reference in New Issue