diff --git a/src/timeago.rs b/src/timeago.rs index f0d7f35..a9a31f7 100644 --- a/src/timeago.rs +++ b/src/timeago.rs @@ -18,7 +18,7 @@ use std::ops::Mul; use serde::{Deserialize, Serialize}; -use time::{Date, Duration, OffsetDateTime}; +use time::{Date, Duration, Month, OffsetDateTime}; use crate::{ param::Language, @@ -261,7 +261,8 @@ pub fn parse_textual_date(lang: Language, textual_date: &str) -> Option util::month_from_n(m as u8) + (Some(y), Some(m), Some(d)) => Month::try_from(m as u8) + .ok() .and_then(|m| Date::from_calendar_date(y.into(), m, d as u8).ok()) .map(ParsedDate::Absolute), _ => None, diff --git a/src/util/date.rs b/src/util/date.rs index 742a9c6..25af51c 100644 --- a/src/util/date.rs +++ b/src/util/date.rs @@ -1,23 +1,5 @@ use time::{Date, Month, OffsetDateTime}; -pub const fn month_from_n(n: u8) -> Option { - match n { - 1 => Some(Month::January), - 2 => Some(Month::February), - 3 => Some(Month::March), - 4 => Some(Month::April), - 5 => Some(Month::May), - 6 => Some(Month::June), - 7 => Some(Month::July), - 8 => Some(Month::August), - 9 => Some(Month::September), - 10 => Some(Month::October), - 11 => Some(Month::November), - 12 => Some(Month::December), - _ => None, - } -} - /// Shift a date by the given number of months. /// Ambiguous month-ends are shifted backwards as necessary. pub fn shift_months(date: Date, months: i32) -> Date { @@ -30,7 +12,7 @@ pub fn shift_months(date: Date, months: i32) -> Date { month += 12; } - let month = month_from_n(month as u8).unwrap(); + let month = Month::try_from(month as u8).unwrap(); let month_days = time::util::days_in_year_month(year, month); day = day.min(month_days); diff --git a/src/util/mod.rs b/src/util/mod.rs index 13c470c..3615aa5 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -3,7 +3,7 @@ mod protobuf; pub mod dictionary; -pub use date::{month_from_n, now_sec, shift_months, shift_years}; +pub use date::{now_sec, shift_months, shift_years}; pub use protobuf::{string_from_pb, ProtoBuilder}; use std::{