From a3aa234b2e8178d92c9cbd80c65b47ef089b9d70 Mon Sep 17 00:00:00 2001 From: Sean McNamara Date: Thu, 1 Dec 2022 07:22:40 -0500 Subject: [PATCH] natural_parser returns a valid UNIX timestamp now --- src/main.rs | 4 ++-- src/time_parser.rs | 24 ++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 011692e..737c063 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,7 +76,7 @@ impl Display for Ended { impl StdError for Ended {} #[tokio::main] -async fn main() -> Result<(), Box> { +async fn main() -> anyhow::Result<(), Box> { let (tx, mut rx) = broadcast::channel(16); tokio::select! { @@ -85,7 +85,7 @@ async fn main() -> Result<(), Box> { } } -async fn _main(tx: Sender<()>) -> Result<(), Box> { +async fn _main(tx: Sender<()>) -> anyhow::Result<(), Box> { env_logger::init(); dotenv()?; diff --git a/src/time_parser.rs b/src/time_parser.rs index 3dedd87..ad04b85 100644 --- a/src/time_parser.rs +++ b/src/time_parser.rs @@ -200,8 +200,28 @@ impl TimeParser { } pub async fn natural_parser(time: &str, _timezone: &str) -> Option { - match to_event(time).get_timestamp() { + println!("natural_parser of {}", time); + let evt = to_event(time); + println!("to_event: {}", evt.to_string()); + let ts = evt.get_start(); + + let ts2 = match ts { None => None, - Some(x) => Some(x.timestamp()) + Some(x) => match x { + DatePerhapsTime::DateTime(y) => match y { + CalendarDateTime::Floating(z) => Some(z.timestamp()), + CalendarDateTime::Utc(z) => Some(z.timestamp()), + CalendarDateTime::WithTimezone { date_time, tzid } => Some(date_time.timestamp()), + }, + DatePerhapsTime::Date(y) => Some(y.and_hms_opt(0,0,0).unwrap().timestamp()), + } + }; + + if ts2.is_none() { + println!("No timestamp for {}", time); } + else { + println!("Timestamp for {} = {}", time, ts2.unwrap()); + } + return ts2; }