From fe9a209cbf154283860b6af8086b7620bf4a6781 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Tue, 24 Jan 2023 11:29:52 -0800 Subject: [PATCH] Display timestamps as UTC (#1348) --- src/blocktime.rs | 41 ++++++++++-------------------------- src/index.rs | 8 +++++-- src/index/updater.rs | 2 +- src/lib.rs | 6 +++--- src/subcommand/server.rs | 4 ++-- src/templates/block.rs | 2 +- src/templates/inscription.rs | 4 ++-- src/templates/sat.rs | 16 +++++++------- static/index.js | 4 ++++ templates/block.html | 2 +- templates/inscription.html | 2 +- templates/sat.html | 2 +- tests/server.rs | 2 +- 13 files changed, 42 insertions(+), 53 deletions(-) diff --git a/src/blocktime.rs b/src/blocktime.rs index 8a346c1fff..117bb37cc8 100644 --- a/src/blocktime.rs +++ b/src/blocktime.rs @@ -2,44 +2,25 @@ use super::*; #[derive(Copy, Clone)] pub(crate) enum Blocktime { - Confirmed(i64), - Expected(i64), + Confirmed(DateTime), + Expected(DateTime), } impl Blocktime { - fn timestamp(self) -> i64 { + pub(crate) fn confirmed(seconds: u32) -> Self { + Self::Confirmed(timestamp(seconds)) + } + + pub(crate) fn timestamp(self) -> DateTime { match self { Self::Confirmed(timestamp) | Self::Expected(timestamp) => timestamp, } } -} -impl Display for Blocktime { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!( - f, - "{}", - NaiveDateTime::from_timestamp_opt(self.timestamp(), 0).unwrap() - )?; - - if let Self::Expected(_) = self { - write!(f, " (expected)")?; + pub(crate) fn suffix(self) -> &'static str { + match self { + Self::Confirmed(_) => "", + Self::Expected(_) => " (expected)", } - - Ok(()) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn display() { - assert_eq!(Blocktime::Confirmed(0).to_string(), "1970-01-01 00:00:00"); - assert_eq!( - Blocktime::Expected(0).to_string(), - "1970-01-01 00:00:00 (expected)" - ); } } diff --git a/src/index.rs b/src/index.rs index d81a496822..13b5eb9b4a 100644 --- a/src/index.rs +++ b/src/index.rs @@ -614,7 +614,7 @@ impl Index { let height = height.n(); match self.get_block_by_height(height)? { - Some(block) => Ok(Blocktime::Confirmed(block.header.time.into())), + Some(block) => Ok(Blocktime::confirmed(block.header.time)), None => { let tx = self.database.begin_read()?; @@ -632,7 +632,11 @@ impl Index { })?; Ok(Blocktime::Expected( - Utc::now().timestamp() + 10 * 60 * i64::try_from(expected_blocks).unwrap(), + Utc::now() + .checked_add_signed(chrono::Duration::seconds( + 10 * 60 * i64::try_from(expected_blocks)?, + )) + .ok_or_else(|| anyhow!("block timestamp out of range"))?, )) } } diff --git a/src/index/updater.rs b/src/index/updater.rs index 79fc06aecd..4c202b2fe3 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -255,7 +255,7 @@ impl Updater { let mut sat_ranges_written = 0; let mut outputs_in_block = 0; - let time = Utc.timestamp_opt(block.header.time.into(), 0).unwrap(); + let time = timestamp(block.header.time); log::info!( "Block {} at {} with {} transactions…", diff --git a/src/lib.rs b/src/lib.rs index 27a09b4f16..2cf877b543 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,7 +43,7 @@ use { }, bitcoincore_rpc::{Client, RpcApi}, chain::Chain, - chrono::{NaiveDateTime, TimeZone, Utc}, + chrono::{DateTime, TimeZone, Utc}, clap::{ArgGroup, Parser}, derive_more::{Display, FromStr}, html_escaper::{Escape, Trusted}, @@ -135,8 +135,8 @@ fn integration_test() -> bool { .unwrap_or(false) } -fn timestamp(seconds: u32) -> NaiveDateTime { - NaiveDateTime::from_timestamp_opt(seconds.into(), 0).unwrap() +fn timestamp(seconds: u32) -> DateTime { + Utc.timestamp_opt(seconds.into(), 0).unwrap() } pub fn main() { diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 53d3d5c734..bac64c6676 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -1584,7 +1584,7 @@ mod tests { TestServer::new().assert_response_regex( "/sat/0", StatusCode::OK, - ".*
time
2009-01-03 18:15:05
.*", + ".*
timestamp
.*", ); } @@ -1593,7 +1593,7 @@ mod tests { TestServer::new().assert_response_regex( "/sat/5000000000", StatusCode::OK, - ".*
time
.* \\(expected\\)
.*", + ".*
timestamp
\\(expected\\)
.*", ); } diff --git a/src/templates/block.rs b/src/templates/block.rs index f86ac7675d..1c8f0d933d 100644 --- a/src/templates/block.rs +++ b/src/templates/block.rs @@ -43,7 +43,7 @@ mod tests {
hash
000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
target
00000000ffff0000000000000000000000000000000000000000000000000000
-
timestamp
1231006505
+
timestamp
size
285
weight
1140
diff --git a/src/templates/inscription.rs b/src/templates/inscription.rs index 0a5df90265..98c9f57209 100644 --- a/src/templates/inscription.rs +++ b/src/templates/inscription.rs @@ -12,7 +12,7 @@ pub(crate) struct InscriptionHtml { pub(crate) previous: Option, pub(crate) sat: Option, pub(crate) satpoint: SatPoint, - pub(crate) timestamp: NaiveDateTime, + pub(crate) timestamp: DateTime, } impl PageContent for InscriptionHtml { @@ -68,7 +68,7 @@ mod tests {
content type
text/plain;charset=utf-8
timestamp
-
1970-01-01 00:00:00
+
genesis height
0
genesis transaction
diff --git a/src/templates/sat.rs b/src/templates/sat.rs index c05688b9b5..fd61d2c8ec 100644 --- a/src/templates/sat.rs +++ b/src/templates/sat.rs @@ -24,7 +24,7 @@ mod tests { SatHtml { sat: Sat(0), satpoint: None, - blocktime: Blocktime::Confirmed(0), + blocktime: Blocktime::confirmed(0), inscription: None, } .to_string(), @@ -41,7 +41,7 @@ mod tests {
block
0
offset
0
rarity
mythic
-
time
1970-01-01 00:00:00
+
timestamp
prev @@ -56,7 +56,7 @@ mod tests { SatHtml { sat: Sat(2099999997689999), satpoint: None, - blocktime: Blocktime::Confirmed(0), + blocktime: Blocktime::confirmed(0), inscription: None, } .to_string(), @@ -73,7 +73,7 @@ mod tests {
block
6929999
offset
0
rarity
uncommon
-
time
1970-01-01 00:00:00
+
timestamp
next @@ -88,7 +88,7 @@ mod tests { SatHtml { sat: Sat(1), satpoint: None, - blocktime: Blocktime::Confirmed(0), + blocktime: Blocktime::confirmed(0), inscription: None, }, r"

Sat 1

.*\n\n", @@ -101,7 +101,7 @@ mod tests { SatHtml { sat: Sat(0), satpoint: None, - blocktime: Blocktime::Confirmed(0), + blocktime: Blocktime::confirmed(0), inscription: Some(inscription_id(1)), }, r"

Sat 0

.*
inscription
.*
.*", @@ -114,7 +114,7 @@ mod tests { SatHtml { sat: Sat::LAST, satpoint: None, - blocktime: Blocktime::Confirmed(0), + blocktime: Blocktime::confirmed(0), inscription: None, }, r"

Sat 2099999997689999

.*\nnext\n", @@ -127,7 +127,7 @@ mod tests { SatHtml { sat: Sat(0), satpoint: Some(satpoint(1, 0)), - blocktime: Blocktime::Confirmed(0), + blocktime: Blocktime::confirmed(0), inscription: None, }, "

Sat 0

.*
location
1{64}:1:0
.*", diff --git a/static/index.js b/static/index.js index d4800e3b59..90b18b519c 100644 --- a/static/index.js +++ b/static/index.js @@ -1,3 +1,7 @@ +for (let time of document.body.getElementsByTagName('time')) { + time.setAttribute('title', new Date(time.textContent)); +} + let next = document.querySelector('a.next'); let previous = document.querySelector('a.previous'); diff --git a/templates/block.html b/templates/block.html index 0ef316974c..23ce1e6fc8 100644 --- a/templates/block.html +++ b/templates/block.html @@ -2,7 +2,7 @@

Block {{ self.height }}

hash
{{self.hash}}
target
{{self.target}}
-
timestamp
{{self.block.header.time}}
+
timestamp
size
{{self.block.size()}}
weight
{{self.block.weight()}}
%% if self.height.0 > 0 { diff --git a/templates/inscription.html b/templates/inscription.html index ea348741f7..397649d8ea 100644 --- a/templates/inscription.html +++ b/templates/inscription.html @@ -38,7 +38,7 @@

Inscription {{ self.number }}

{{ content_type }}
%% }
timestamp
-
{{ self.timestamp }}
+
genesis height
{{ self.genesis_height }}
genesis transaction
diff --git a/templates/sat.html b/templates/sat.html index a30f62510b..7f5908d499 100644 --- a/templates/sat.html +++ b/templates/sat.html @@ -10,7 +10,7 @@

Sat {{ self.sat.n() }}

block
{{ self.sat.height() }}
offset
{{ self.sat.third() }}
rarity
{{ self.sat.rarity() }}
-
time
{{ self.blocktime }}
+
timestamp
{{self.blocktime.suffix()}}
%% if let Some((inscription)) = &self.inscription {
inscription
{{ Iframe::thumbnail(*inscription) }}
%% } diff --git a/tests/server.rs b/tests/server.rs index 010a0a7651..9050845d9e 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -67,7 +67,7 @@ fn inscription_page() {
content type
text/plain;charset=utf-8
timestamp
-
1970-01-01 00:00:02
+
genesis height
2
genesis transaction