Skip to content

Commit 2b56727

Browse files
authored
chore: check timestamp value before fmt (#11475)
1 parent c771e5c commit 2b56727

File tree

1 file changed

+30
-3
lines changed
  • crates/common/fmt/src

1 file changed

+30
-3
lines changed

crates/common/fmt/src/ui.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -969,9 +969,7 @@ requestsHash {}",
969969
size.pretty(),
970970
state_root.pretty(),
971971
timestamp.pretty(),
972-
chrono::DateTime::from_timestamp(*timestamp as i64, 0)
973-
.expect("block timestamp in range")
974-
.to_rfc2822(),
972+
fmt_timestamp(*timestamp),
975973
withdrawals_root.pretty(),
976974
total_difficulty.pretty(),
977975
blob_gas_used.pretty(),
@@ -980,6 +978,24 @@ requestsHash {}",
980978
)
981979
}
982980

981+
/// Formats the timestamp to string
982+
///
983+
/// Assumes timestamp is seconds, but handles millis if it is too large
984+
fn fmt_timestamp(timestamp: u64) -> String {
985+
// Tue Jan 19 2038 03:14:07 GMT+0000
986+
if timestamp > 2147483647 {
987+
// assume this is in millis, incorrectly set to millis by a node
988+
chrono::DateTime::from_timestamp_millis(timestamp as i64)
989+
.expect("block timestamp in range")
990+
.to_rfc3339()
991+
} else {
992+
// assume this is still in seconds
993+
chrono::DateTime::from_timestamp(timestamp as i64, 0)
994+
.expect("block timestamp in range")
995+
.to_rfc2822()
996+
}
997+
}
998+
983999
#[cfg(test)]
9841000
mod tests {
9851001
use super::*;
@@ -988,6 +1004,17 @@ mod tests {
9881004
use similar_asserts::assert_eq;
9891005
use std::str::FromStr;
9901006

1007+
#[test]
1008+
fn format_date_time() {
1009+
// Fri Aug 29 2025 08:05:38 GMT+0000
1010+
let timestamp = 1756454738u64;
1011+
1012+
let datetime = fmt_timestamp(timestamp);
1013+
assert_eq!(datetime, "Fri, 29 Aug 2025 08:05:38 +0000");
1014+
let datetime = fmt_timestamp(timestamp * 1000);
1015+
assert_eq!(datetime, "2025-08-29T08:05:38+00:00");
1016+
}
1017+
9911018
#[test]
9921019
fn can_format_bytes32() {
9931020
let val = hex::decode("7465737400000000000000000000000000000000000000000000000000000000")

0 commit comments

Comments
 (0)