-
Notifications
You must be signed in to change notification settings - Fork 72
refactor: chrono to jiff #634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
873ad85
bbf28ba
885943e
05d757f
231eb82
365282c
eb8f911
76dbecb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,68 +18,26 @@ | |
| //! Time related utils. | ||
|
|
||
| use crate::Error; | ||
| use chrono::format::Fixed; | ||
| use chrono::format::Item; | ||
| use chrono::format::Numeric; | ||
| use chrono::format::Pad; | ||
| use chrono::SecondsFormat; | ||
| use chrono::Utc; | ||
| use std::str::FromStr; | ||
|
|
||
| /// DateTime is the alias for chrono::DateTime<Utc>. | ||
| pub type DateTime = chrono::DateTime<Utc>; | ||
| /// DateTime is the alias for `jiff::Timestamp`. | ||
| pub type DateTime = jiff::Timestamp; | ||
|
|
||
| /// Create datetime of now. | ||
| pub fn now() -> DateTime { | ||
| Utc::now() | ||
| jiff::Timestamp::now() | ||
| } | ||
|
|
||
| /// DATE is a time format like `20220301` | ||
| const DATE: &[Item<'static>] = &[ | ||
| Item::Numeric(Numeric::Year, Pad::Zero), | ||
| Item::Numeric(Numeric::Month, Pad::Zero), | ||
| Item::Numeric(Numeric::Day, Pad::Zero), | ||
| ]; | ||
|
|
||
| /// Format time into date: `20220301` | ||
| pub fn format_date(t: DateTime) -> String { | ||
| t.format_with_items(DATE.iter()).to_string() | ||
| t.strftime("%Y%m%d").to_string() | ||
| } | ||
|
|
||
| /// ISO8601 is a time format like `20220313T072004Z`. | ||
| const ISO8601: &[Item<'static>] = &[ | ||
| Item::Numeric(Numeric::Year, Pad::Zero), | ||
| Item::Numeric(Numeric::Month, Pad::Zero), | ||
| Item::Numeric(Numeric::Day, Pad::Zero), | ||
| Item::Literal("T"), | ||
| Item::Numeric(Numeric::Hour, Pad::Zero), | ||
| Item::Numeric(Numeric::Minute, Pad::Zero), | ||
| Item::Numeric(Numeric::Second, Pad::Zero), | ||
| Item::Literal("Z"), | ||
| ]; | ||
|
|
||
| /// Format time into ISO8601: `20220313T072004Z` | ||
| pub fn format_iso8601(t: DateTime) -> String { | ||
| t.format_with_items(ISO8601.iter()).to_string() | ||
| t.strftime("%Y%m%dT%H%M%SZ").to_string() | ||
| } | ||
|
|
||
| /// HTTP_DATE is a time format like `Sun, 06 Nov 1994 08:49:37 GMT`. | ||
| const HTTP_DATE: &[Item<'static>] = &[ | ||
| Item::Fixed(Fixed::ShortWeekdayName), | ||
| Item::Literal(", "), | ||
| Item::Numeric(Numeric::Day, Pad::Zero), | ||
| Item::Literal(" "), | ||
| Item::Fixed(Fixed::ShortMonthName), | ||
| Item::Literal(" "), | ||
| Item::Numeric(Numeric::Year, Pad::Zero), | ||
| Item::Literal(" "), | ||
| Item::Numeric(Numeric::Hour, Pad::Zero), | ||
| Item::Literal(":"), | ||
| Item::Numeric(Numeric::Minute, Pad::Zero), | ||
| Item::Literal(":"), | ||
| Item::Numeric(Numeric::Second, Pad::Zero), | ||
| Item::Literal(" GMT"), | ||
| ]; | ||
|
|
||
| /// Format time into http date: `Sun, 06 Nov 1994 08:49:37 GMT` | ||
| /// | ||
| /// ## Note | ||
|
|
@@ -89,12 +47,12 @@ const HTTP_DATE: &[Item<'static>] = &[ | |
| /// - Timezone is fixed to GMT. | ||
| /// - Day must be 2 digit. | ||
| pub fn format_http_date(t: DateTime) -> String { | ||
| t.format_with_items(HTTP_DATE.iter()).to_string() | ||
| t.strftime("%a, %d %b %Y %T GMT").to_string() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to use an explicit RFC 9110 printer instead? Note that I can't immediately spot anything obviously wrong with what you have here. Probably the dedicated printer is faster, but I'm not sure if that matters given the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good to know. Updating.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After a closer look, I'd prefer the current consistent usage of Would you elaborate a bit on how switching to the RFC 9110 printer can benefit?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah .. I'd prefer code consistency for understandability unless a break has a strong reason. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly perf. And I think it makes the code clearer to use a dedicated printer. But that's your call. |
||
| } | ||
|
|
||
| /// Format time into RFC3339: `2022-03-13T07:20:04Z` | ||
| pub fn format_rfc3339(t: DateTime) -> String { | ||
| t.to_rfc3339_opts(SecondsFormat::Secs, true) | ||
| t.strftime("%FT%TZ").to_string() | ||
| } | ||
|
|
||
| /// Parse time from RFC3339. | ||
|
|
@@ -105,21 +63,30 @@ pub fn format_rfc3339(t: DateTime) -> String { | |
| /// - `2022-03-01T08:12:34+00:00` | ||
| /// - `2022-03-01T08:12:34.00+00:00` | ||
| pub fn parse_rfc3339(s: &str) -> crate::Result<DateTime> { | ||
|
tisonkun marked this conversation as resolved.
Outdated
|
||
| Ok(chrono::DateTime::parse_from_rfc3339(s) | ||
| .map_err(|err| { | ||
| Error::unexpected(format!("parse '{s}' into rfc3339 failed")).with_source(err) | ||
| })? | ||
| .with_timezone(&Utc)) | ||
| FromStr::from_str(s).map_err(|err| { | ||
|
tisonkun marked this conversation as resolved.
Outdated
|
||
| Error::unexpected(format!("parse '{s}' into rfc3339 failed")).with_source(err) | ||
| }) | ||
| } | ||
|
|
||
| /// Parse time from RFC2822. | ||
| /// | ||
| /// All of them are valid time: | ||
| /// | ||
| /// - `Sat, 13 Jul 2024 15:09:59 -0400` | ||
| /// - `Mon, 15 Aug 2022 16:50:12 GMT` | ||
| pub fn parse_rfc2822(s: &str) -> crate::Result<DateTime> { | ||
| let zoned = jiff::fmt::rfc2822::parse(s).map_err(|err| { | ||
| Error::unexpected(format!("parse '{s}' into rfc2822 failed")).with_source(err) | ||
| })?; | ||
| Ok(zoned.timestamp()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use chrono::TimeZone; | ||
|
|
||
| use super::*; | ||
|
|
||
| fn test_time() -> DateTime { | ||
| Utc.with_ymd_and_hms(2022, 3, 1, 8, 12, 34).unwrap() | ||
| "2022-03-01T08:12:34Z".parse().unwrap() | ||
| } | ||
|
|
||
| #[test] | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.