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