Skip to content

Commit c5531ab

Browse files
authored
refactor: display unix timestamp for human (#16212)
1 parent eca78df commit c5531ab

File tree

10 files changed

+85
-10
lines changed

10 files changed

+85
-10
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ hashbrown = { version = "0.14.3", default-features = false }
253253
http = "1"
254254
itertools = "0.10.5"
255255
jsonb = "0.4.1"
256+
jwt-simple = "0.11.0"
256257
match-template = "0.0.1"
257258
mysql_async = { version = "0.34", default-features = false, features = ["rustls-tls"] }
258259
object_store_opendal = "0.45"

src/common/base/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ memory-profiling = [
2424
async-backtrace = { workspace = true }
2525
async-trait = { workspace = true }
2626
bytesize = "1.1.0"
27+
chrono = { workspace = true }
2728
ctrlc = { version = "3.2.3", features = ["termination"] }
2829
databend-common-exception = { workspace = true }
2930
enquote = "1.1.0"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2021 Datafuse Labs
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::fmt;
16+
use std::time::Duration;
17+
use std::time::UNIX_EPOCH;
18+
19+
use chrono::DateTime;
20+
use chrono::Utc;
21+
22+
pub struct DisplayUnixTimeStamp {
23+
/// The duration since the UNIX epoch.
24+
duration: Duration,
25+
}
26+
27+
impl fmt::Display for DisplayUnixTimeStamp {
28+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29+
let system_time = UNIX_EPOCH + self.duration;
30+
let datetime: DateTime<Utc> = system_time.into();
31+
32+
write!(f, "{}", datetime.format("%Y-%m-%dT%H:%M:%S%.6fZ%z"))
33+
}
34+
}
35+
36+
pub trait DisplayUnixTimeStampExt {
37+
fn display_unix_timestamp(&self) -> DisplayUnixTimeStamp;
38+
}
39+
40+
impl DisplayUnixTimeStampExt for Duration {
41+
fn display_unix_timestamp(&self) -> DisplayUnixTimeStamp {
42+
DisplayUnixTimeStamp { duration: *self }
43+
}
44+
}
45+
46+
#[cfg(test)]
47+
mod tests {
48+
use std::time::Duration;
49+
50+
use super::*;
51+
52+
#[test]
53+
fn test_display_unix_epoch() {
54+
let epoch = Duration::from_millis(0);
55+
let display = epoch.display_unix_timestamp();
56+
assert_eq!(format!("{}", display), "1970-01-01T00:00:00.000000Z+0000");
57+
58+
let epoch = Duration::from_millis(1723102819023);
59+
let display = epoch.display_unix_timestamp();
60+
assert_eq!(format!("{}", display), "2024-08-08T07:40:19.023000Z+0000");
61+
}
62+
}

src/common/base/src/display/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
// limitations under the License.
1414

1515
pub mod display_option;
16+
pub mod display_unix_epoch;

src/meta/raft-store/src/applier.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
use std::io;
1616
use std::time::Duration;
17-
use std::time::SystemTime;
1817

18+
use databend_common_base::display::display_unix_epoch::DisplayUnixTimeStampExt;
1919
use databend_common_meta_types::protobuf as pb;
2020
use databend_common_meta_types::txn_condition;
2121
use databend_common_meta_types::txn_op;
@@ -529,8 +529,10 @@ impl<'a> Applier<'a> {
529529
0
530530
}
531531
Some(ms) => {
532-
let t = SystemTime::UNIX_EPOCH + Duration::from_millis(ms);
533-
debug!("apply: raft-log time: {:?}", t);
532+
debug!(
533+
"apply: raft-log time: {}",
534+
Duration::from_millis(ms).display_unix_timestamp()
535+
);
534536
ms
535537
}
536538
},

src/meta/raft-store/src/state_machine/sm.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use std::convert::TryInto;
1717
use std::fmt::Debug;
1818
use std::time::Duration;
1919
use std::time::Instant;
20-
use std::time::SystemTime;
2120

21+
use databend_common_base::display::display_unix_epoch::DisplayUnixTimeStampExt;
2222
use databend_common_meta_sled_store::get_sled_db;
2323
use databend_common_meta_sled_store::openraft::MessageSummary;
2424
use databend_common_meta_sled_store::AsKeySpace;
@@ -336,8 +336,10 @@ impl StateMachine {
336336
0
337337
}
338338
Some(x) => {
339-
let t = SystemTime::UNIX_EPOCH + Duration::from_millis(x);
340-
info!("apply: raft-log time: {:?}", t);
339+
info!(
340+
"apply: raft-log time: {}",
341+
Duration::from_millis(x).display_unix_timestamp()
342+
);
341343
x
342344
}
343345
},

src/meta/types/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ test = true
1212

1313
[dependencies]
1414
anyerror = { workspace = true }
15+
databend-common-base = { workspace = true }
1516
databend-common-exception = { workspace = true }
1617
databend-common-meta-stoerr = { workspace = true }
1718
databend-common-tracing = { workspace = true }

src/meta/types/src/proto_display.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
use std::fmt::Display;
1616
use std::fmt::Formatter;
1717
use std::time::Duration;
18-
use std::time::SystemTime;
1918

19+
use databend_common_base::display::display_unix_epoch::DisplayUnixTimeStampExt;
2020
use num_traits::FromPrimitive;
2121

2222
use crate::txn_condition::Target;
@@ -167,8 +167,11 @@ impl Display for TxnPutRequest {
167167
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
168168
write!(f, "Put key={}", self.key)?;
169169
if let Some(expire_at) = self.expire_at {
170-
let t = SystemTime::UNIX_EPOCH + Duration::from_millis(expire_at);
171-
write!(f, " expire_at: {:?}", t)?;
170+
write!(
171+
f,
172+
" expire_at: {}",
173+
Duration::from_millis(expire_at).display_unix_timestamp()
174+
)?;
172175
}
173176
if let Some(ttl_ms) = self.ttl_ms {
174177
write!(f, " ttl: {:?}", Duration::from_millis(ttl_ms))?;

src/query/ee/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ databend-storages-common-table-meta = { workspace = true }
5555
derive-visitor = { workspace = true }
5656
futures = { workspace = true }
5757
futures-util = { workspace = true }
58-
jwt-simple = "0.11.0"
58+
jwt-simple = { workspace = true }
5959
log = { workspace = true }
6060
opendal = { workspace = true }
6161
tempfile = "3.4.0"

0 commit comments

Comments
 (0)