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))
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
||||||
27
src/cli.rs
27
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::Local;
|
||||||
use chrono::NaiveDate;
|
use chrono::NaiveDate;
|
||||||
|
|
@ -69,20 +69,23 @@ pub fn print_timezones(
|
||||||
local_timezone: &String,
|
local_timezone: &String,
|
||||||
padding_hours: i8,
|
padding_hours: i8,
|
||||||
) -> Result<(), CliError> {
|
) -> Result<(), CliError> {
|
||||||
let todays_date = &get_todays_date();
|
let parsed_time = if let Some(t) = time {
|
||||||
let date = date.unwrap_or(todays_date);
|
if let Ok(hour) = t.parse::<u32>() {
|
||||||
let time_now = Local::now().format("%H:%M").to_string();
|
NaiveTime::from_hms_opt(hour, 0, 0).ok_or(CliError::CouldNotParseTime)?
|
||||||
let time = time.unwrap_or(&time_now);
|
} else {
|
||||||
|
NaiveTime::parse_from_str(t, "%H:%M").map_err(|_| CliError::CouldNotParseTime)?
|
||||||
let parsed_time = if let Ok(hour) = time.parse::<u32>() {
|
}
|
||||||
NaiveTime::from_hms_opt(hour, 0, 0).ok_or(CliError::CouldNotParseTime)?
|
|
||||||
} else {
|
} else {
|
||||||
NaiveTime::parse_from_str(time, "%H:%M").map_err(|_| CliError::CouldNotParseTime)?
|
Local::now().naive_local().time()
|
||||||
};
|
};
|
||||||
|
|
||||||
let parsed_date = NaiveDate::parse_from_str(date, "%d-%m-%Y")
|
let parsed_date = if let Some(d) = date {
|
||||||
.or_else(|_| NaiveDate::parse_from_str(date, "%d-%m"))
|
NaiveDate::parse_from_str(d, "%d-%m-%Y")
|
||||||
.map_err(|_| CliError::CouldNotParseDate)?;
|
.or_else(|_| NaiveDate::parse_from_str(d, "%d-%m"))
|
||||||
|
.map_err(|_| CliError::CouldNotParseDate)?
|
||||||
|
} else {
|
||||||
|
Local::now().naive_local().date()
|
||||||
|
};
|
||||||
|
|
||||||
if local_timezone == "Local" {
|
if local_timezone == "Local" {
|
||||||
print_calendar(
|
print_calendar(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue