From a56833fd02b766b0902e53422994fbe9cc2f54c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix?= Date: Thu, 9 Feb 2023 11:09:11 +0100 Subject: [PATCH 1/3] Prevents the timestamp display to error when it's too far into the future. --- node/src/types/timestamp.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/node/src/types/timestamp.rs b/node/src/types/timestamp.rs index 83c1db6c6b..94f9ccdb5d 100644 --- a/node/src/types/timestamp.rs +++ b/node/src/types/timestamp.rs @@ -84,7 +84,8 @@ impl Timestamp { impl Display for Timestamp { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match SystemTime::UNIX_EPOCH.checked_add(Duration::from_millis(self.0)) { - Some(system_time) => write!(f, "{}", humantime::format_rfc3339_millis(system_time)), + Some(system_time) => write!(f, "{}", humantime::format_rfc3339_millis(system_time)) + .or_else(|_| write!(f, "XXXX-XX-XXTXX:XX:XX.XXXZ")), None => write!(f, "invalid Timestamp: {} ms after the Unix epoch", self.0), } } @@ -355,4 +356,9 @@ mod tests { bytesrepr::test_serialization_roundtrip(&timediff); } + + #[test] + fn does_not_crashe_for_big_timestamp_value() { + assert_eq!("XXXX-XX-XXTXX:XX:XX.XXXZ", Timestamp::MAX.to_string()); + } } From 1874307ddbf7b75de5f77c9a12bbcff618263707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix?= Date: Thu, 9 Feb 2023 15:55:41 +0100 Subject: [PATCH 2/3] Fix typo + error message --- node/src/types/timestamp.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/node/src/types/timestamp.rs b/node/src/types/timestamp.rs index 94f9ccdb5d..32de195e3d 100644 --- a/node/src/types/timestamp.rs +++ b/node/src/types/timestamp.rs @@ -85,7 +85,7 @@ impl Display for Timestamp { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match SystemTime::UNIX_EPOCH.checked_add(Duration::from_millis(self.0)) { Some(system_time) => write!(f, "{}", humantime::format_rfc3339_millis(system_time)) - .or_else(|_| write!(f, "XXXX-XX-XXTXX:XX:XX.XXXZ")), + .or_else(|e| write!(f, "Invalid timestamp: {}: {}", e, self.0)), None => write!(f, "invalid Timestamp: {} ms after the Unix epoch", self.0), } } @@ -358,7 +358,7 @@ mod tests { } #[test] - fn does_not_crashe_for_big_timestamp_value() { - assert_eq!("XXXX-XX-XXTXX:XX:XX.XXXZ", Timestamp::MAX.to_string()); + fn does_not_crash_for_big_timestamp_value() { + assert!(Timestamp::MAX.to_string().starts_with("Invalid timestamp:")); } } From 2db3a68a1ab8d2259634c9ec03ab62aeca63c44d Mon Sep 17 00:00:00 2001 From: Marc Brinkmann Date: Thu, 9 Feb 2023 17:33:40 +0100 Subject: [PATCH 3/3] Backport `Timestamp::MAX` from `dev` --- node/src/types/timestamp.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/node/src/types/timestamp.rs b/node/src/types/timestamp.rs index 32de195e3d..69cb3c624a 100644 --- a/node/src/types/timestamp.rs +++ b/node/src/types/timestamp.rs @@ -38,6 +38,9 @@ static TIMESTAMP_EXAMPLE: Lazy = Lazy::new(|| { pub struct Timestamp(u64); impl Timestamp { + /// The maximum value a timestamp can have. + pub const MAX: Timestamp = Timestamp(u64::MAX); + /// Returns the timestamp of the current moment. pub fn now() -> Self { let millis = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_millis() as u64;