diff options
Diffstat (limited to 'src/timeparse.rs')
-rwxr-xr-x | src/timeparse.rs | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/src/timeparse.rs b/src/timeparse.rs index f4eaca5..830253b 100755 --- a/src/timeparse.rs +++ b/src/timeparse.rs @@ -1,6 +1,6 @@ use time::{ error::{Parse, TryFromParsed}, - format_description::FormatItem, + format_description::{well_known, FormatItem}, macros::{format_description, offset, time}, Date, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset, Weekday, }; @@ -17,6 +17,25 @@ const OFFSET: &[FormatItem<'_>] = const CST: UtcOffset = offset!(-06:00); const CDT: UtcOffset = offset!(-05:00); +// Ripped from infica with the month of Sol removed. I just want the 3 letter months +/// Capitalized months (long, short) and lowercase months (long, short). +// it seems useful to have the lowercase here so we don't have to always call +// to_lowercase +const MONTHS: [[&str; 4]; 12] = [ + ["January", "Jan", "january", "jan"], + ["February", "Feb", "february", "feb"], + ["March", "Mar", "march", "mar"], + ["April", "Apr", "april", "apr"], + ["May", "May", "may", "may"], + ["June", "Jun", "june", "jun"], + ["July", "Jul", "july", "jul"], + ["August", "Aug", "august", "aug"], + ["September", "Sep", "september", "sep"], + ["October", "Oct", "october", "oct"], + ["November", "Nov", "november", "nov"], + ["December", "Dec", "december", "dec"], +]; + /// Offset for the united states in Central Time. Accounts for DST /// DST starts the 2nd sunday in March and ends the 1st sunday in November. /// https://www.nist.gov/pml/time-and-frequency-division/popular-links/daylight-saving-time-dst @@ -103,6 +122,21 @@ pub fn parse(raw: &str) -> Result<OffsetDateTime, time::error::Parse> { Ok(OffsetDateTime::new_in_offset(date, time, offset)) } +pub fn format_long(datetime: &OffsetDateTime) -> String { + let year = datetime.year(); + let month = MONTHS[datetime.month() as usize][1]; + let day = datetime.day(); + let weekday = datetime.weekday(); + + format!("{weekday} {day}, {month} {year}") +} + +pub fn iso8601(datetime: &OffsetDateTime) -> String { + datetime + .format(&well_known::Iso8601::DATE_TIME_OFFSET) + .unwrap() +} + #[cfg(test)] mod test { use time::{ |