After all, the timezone can be invalid, which makes the conversion (and formatting) fail. The `format_or_raw()` method brings back an infallible version.
Unified · Split
| @@ -36,25 +36,31 @@ impl Time { · 35 unmodified lines | |||
| 36 | /// | 36 | /// |
| 37 | /// Use [`Format::Unix`], [`Format::Raw`] or one of the custom formats | 37 | /// Use [`Format::Unix`], [`Format::Raw`] or one of the custom formats |
| 38 | /// defined in the [`format`](mod@crate::time::format) submodule. | 38 | /// defined in the [`format`](mod@crate::time::format) submodule. |
| 39 | pub fn format(&self, format: impl Into<Format>) -> String { | 39 | /// |
| 40 | /// Note that this can fail if the timezone isn't valid and the format requires a conversion to [`jiff::Zoned`]. | ||
| 41 | pub fn format(&self, format: impl Into<Format>) -> Result<String, jiff::Error> { | ||
| 40 | self.format_inner(format.into()) | 42 | self.format_inner(format.into()) |
| 41 | } | 43 | } |
| 42 | 44 | ||
| 43 | fn format_inner(&self, format: Format) -> String { | 45 | /// Like [`Self::format()`], but on time conversion error, produce the [RAW] format instead to make it |
| 44 | match format { | 46 | /// infallible. |
| 45 | Format::Custom(CustomFormat(format)) => self.to_zoned().strftime(format).to_string(), | 47 | pub fn format_or_raw(&self, format: impl Into<Format>) -> String { |
| 48 | self.format_inner(format.into()).unwrap_or_else(|_| self.to_string()) | ||
| 49 | } | ||
| 50 | |||
| 51 | fn format_inner(&self, format: Format) -> Result<String, jiff::Error> { | ||
| 52 | Ok(match format { | ||
| 53 | Format::Custom(CustomFormat(format)) => self.to_zoned()?.strftime(format).to_string(), | ||
| 46 | Format::Unix => self.seconds.to_string(), | 54 | Format::Unix => self.seconds.to_string(), |
| 47 | Format::Raw => self.to_string(), | 55 | Format::Raw => self.to_string(), |
| 48 | } | 56 | }) |
| 49 | } | 57 | } |
| 50 | } | 58 | } |
| 51 | 59 | ||
| 52 | impl Time { | 60 | impl Time { |
| 53 | /// Produce a `Zoned` time for complex time computations and limitless formatting. | 61 | /// Produce a `Zoned` time for complex time computations and limitless formatting. |
| 54 | pub fn to_zoned(self) -> jiff::Zoned { | 62 | pub fn to_zoned(self) -> Result<jiff::Zoned, jiff::Error> { |
| 55 | let offset = jiff::tz::Offset::from_seconds(self.offset).expect("valid offset"); | 63 | let offset = jiff::tz::Offset::from_seconds(self.offset)?; |
| 56 | jiff::Timestamp::from_second(self.seconds) | 64 | Ok(jiff::Timestamp::from_second(self.seconds)?.to_zoned(offset.to_time_zone())) |
| 57 | .expect("always valid unix time") | ||
| 58 | .to_zoned(offset.to_time_zone()) | ||
| 59 | } | 65 | } |
| 60 | } | 66 | } |
| @@ -66,15 +66,17 @@ fn parse_compare_format() { · 65 unmodified lines | |||
| 66 | "{pattern:?} disagrees with baseline seconds since epoch: {actual:?}" | 66 | "{pattern:?} disagrees with baseline seconds since epoch: {actual:?}" |
| 67 | ); | 67 | ); |
| 68 | if let Some(format_name) = format_name { | 68 | if let Some(format_name) = format_name { |
| 69 | let reformatted = t.format(match format_name.as_str() { | 69 | let reformatted = t |
| 70 | "RFC2822" => Format::Custom(format::RFC2822), | 70 | .format(match format_name.as_str() { |
| 71 | "ISO8601" => Format::Custom(format::ISO8601), | 71 | "RFC2822" => Format::Custom(format::RFC2822), |
| 72 | "ISO8601_STRICT" => Format::Custom(format::ISO8601_STRICT), | 72 | "ISO8601" => Format::Custom(format::ISO8601), |
| 73 | "GITOXIDE" => Format::Custom(format::GITOXIDE), | 73 | "ISO8601_STRICT" => Format::Custom(format::ISO8601_STRICT), |
| 74 | "UNIX" => Format::Unix, | 74 | "GITOXIDE" => Format::Custom(format::GITOXIDE), |
| 75 | "RAW" => Format::Raw, | 75 | "UNIX" => Format::Unix, |
| 76 | unknown => unreachable!("All formats should be well-known and implemented: {unknown:?}"), | 76 | "RAW" => Format::Raw, |
| 77 | }); | 77 | unknown => unreachable!("All formats should be well-known and implemented: {unknown:?}"), |
| 78 | }) | ||
| 79 | .expect("valid input time"); | ||
| 78 | assert_eq!( | 80 | assert_eq!( |
| 79 | reformatted, *pattern, | 81 | reformatted, *pattern, |
| 80 | "{reformatted:?} disagrees with baseline pattern: {pattern:?}" | 82 | "{reformatted:?} disagrees with baseline pattern: {pattern:?}" |
| @@ -4,19 +4,21 @@ use gix_date::{ · 3 unmodified lines | |||
| 4 | }; | 4 | }; |
| 5 | 5 | ||
| 6 | #[test] | 6 | #[test] |
| 7 | fn short() { | 7 | fn short() -> gix_testtools::Result { |
| 8 | assert_eq!(time().format(format::SHORT), "1973-11-30"); | 8 | assert_eq!(time().format(format::SHORT)?, "1973-11-30"); |
| 9 | Ok(()) | ||
| 9 | } | 10 | } |
| 10 | 11 | ||
| 11 | #[test] | 12 | #[test] |
| 12 | fn unix() { | 13 | fn unix() -> gix_testtools::Result { |
| 13 | let expected = "123456789"; | 14 | let expected = "123456789"; |
| 14 | assert_eq!(time().format(Format::Unix), expected); | 15 | assert_eq!(time().format(Format::Unix)?, expected); |
| 15 | assert_eq!(time().format(format::UNIX), expected); | 16 | assert_eq!(time().format(format::UNIX)?, expected); |
| 17 | Ok(()) | ||
| 16 | } | 18 | } |
| 17 | 19 | ||
| 18 | #[test] | 20 | #[test] |
| 19 | fn raw() { | 21 | fn raw() -> gix_testtools::Result { |
| 20 | for (time, expected) in [ | 22 | for (time, expected) in [ |
| 21 | (time(), "123456789 +0230"), | 23 | (time(), "123456789 +0230"), |
| 22 | ( | 24 | ( |
| @@ -27,58 +29,65 @@ fn raw() { · 4 unmodified lines | |||
| 27 | "1112911993 +0100", | 29 | "1112911993 +0100", |
| 28 | ), | 30 | ), |
| 29 | ] { | 31 | ] { |
| 30 | assert_eq!(time.format(Format::Raw), expected); | 32 | assert_eq!(time.format(Format::Raw)?, expected); |
| 31 | assert_eq!(time.format(format::RAW), expected); | 33 | assert_eq!(time.format(format::RAW)?, expected); |
| 32 | } | 34 | } |
| 35 | Ok(()) | ||
| 33 | } | 36 | } |
| 34 | 37 | ||
| 35 | #[test] | 38 | #[test] |
| 36 | fn iso8601() { | 39 | fn iso8601() -> gix_testtools::Result { |
| 37 | assert_eq!(time().format(format::ISO8601), "1973-11-30 00:03:09 +0230"); | 40 | assert_eq!(time().format(format::ISO8601)?, "1973-11-30 00:03:09 +0230"); |
| 41 | Ok(()) | ||
| 38 | } | 42 | } |
| 39 | 43 | ||
| 40 | #[test] | 44 | #[test] |
| 41 | fn iso8601_strict() { | 45 | fn iso8601_strict() -> gix_testtools::Result { |
| 42 | assert_eq!(time().format(format::ISO8601_STRICT), "1973-11-30T00:03:09+02:30"); | 46 | assert_eq!(time().format(format::ISO8601_STRICT)?, "1973-11-30T00:03:09+02:30"); |
| 47 | Ok(()) | ||
| 43 | } | 48 | } |
| 44 | 49 | ||
| 45 | #[test] | 50 | #[test] |
| 46 | fn rfc2822() { | 51 | fn rfc2822() -> gix_testtools::Result { |
| 47 | assert_eq!(time().format(format::RFC2822), "Fri, 30 Nov 1973 00:03:09 +0230"); | 52 | assert_eq!(time().format(format::RFC2822)?, "Fri, 30 Nov 1973 00:03:09 +0230"); |
| 48 | assert_eq!(time_dec1().format(format::RFC2822), "Sat, 01 Dec 1973 00:03:09 +0230"); | 53 | assert_eq!(time_dec1().format(format::RFC2822)?, "Sat, 01 Dec 1973 00:03:09 +0230"); |
| 54 | Ok(()) | ||
| 49 | } | 55 | } |
| 50 | 56 | ||
| 51 | #[test] | 57 | #[test] |
| 52 | fn git_rfc2822() { | 58 | fn git_rfc2822() -> gix_testtools::Result { |
| 53 | assert_eq!(time().format(format::GIT_RFC2822), "Fri, 30 Nov 1973 00:03:09 +0230"); | 59 | assert_eq!(time().format(format::GIT_RFC2822)?, "Fri, 30 Nov 1973 00:03:09 +0230"); |
| 54 | assert_eq!( | 60 | assert_eq!( |
| 55 | time_dec1().format(format::GIT_RFC2822), | 61 | time_dec1().format(format::GIT_RFC2822)?, |
| 56 | "Sat, 1 Dec 1973 00:03:09 +0230" | 62 | "Sat, 1 Dec 1973 00:03:09 +0230" |
| 57 | ); | 63 | ); |
| 64 | Ok(()) | ||
| 58 | } | 65 | } |
| 59 | 66 | ||
| 60 | #[test] | 67 | #[test] |
| 61 | fn default() { | 68 | fn default() -> gix_testtools::Result { |
| 62 | assert_eq!( | 69 | assert_eq!( |
| 63 | time().format(gix_date::time::format::GITOXIDE), | 70 | time().format(gix_date::time::format::GITOXIDE)?, |
| 64 | "Fri Nov 30 1973 00:03:09 +0230" | 71 | "Fri Nov 30 1973 00:03:09 +0230" |
| 65 | ); | 72 | ); |
| 66 | assert_eq!( | 73 | assert_eq!( |
| 67 | time_dec1().format(gix_date::time::format::GITOXIDE), | 74 | time_dec1().format(gix_date::time::format::GITOXIDE)?, |
| 68 | "Sat Dec 01 1973 00:03:09 +0230" | 75 | "Sat Dec 01 1973 00:03:09 +0230" |
| 69 | ); | 76 | ); |
| 77 | Ok(()) | ||
| 70 | } | 78 | } |
| 71 | 79 | ||
| 72 | #[test] | 80 | #[test] |
| 73 | fn git_default() { | 81 | fn git_default() -> gix_testtools::Result { |
| 74 | assert_eq!( | 82 | assert_eq!( |
| 75 | time().format(gix_date::time::format::DEFAULT), | 83 | time().format(gix_date::time::format::DEFAULT)?, |
| 76 | "Fri Nov 30 00:03:09 1973 +0230" | 84 | "Fri Nov 30 00:03:09 1973 +0230" |
| 77 | ); | 85 | ); |
| 78 | assert_eq!( | 86 | assert_eq!( |
| 79 | time_dec1().format(gix_date::time::format::DEFAULT), | 87 | time_dec1().format(gix_date::time::format::DEFAULT)?, |
| 80 | "Sat Dec 1 00:03:09 1973 +0230" | 88 | "Sat Dec 1 00:03:09 1973 +0230" |
| 81 | ); | 89 | ); |
| 90 | Ok(()) | ||
| 82 | } | 91 | } |
| 83 | 92 | ||
| 84 | fn time() -> Time { | 93 | fn time() -> Time { |