Skip to content

Commit

Permalink
refactor: display unix timestamp for human (#16212)
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer authored Aug 8, 2024
1 parent eca78df commit c5531ab
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ hashbrown = { version = "0.14.3", default-features = false }
http = "1"
itertools = "0.10.5"
jsonb = "0.4.1"
jwt-simple = "0.11.0"
match-template = "0.0.1"
mysql_async = { version = "0.34", default-features = false, features = ["rustls-tls"] }
object_store_opendal = "0.45"
Expand Down
1 change: 1 addition & 0 deletions src/common/base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ memory-profiling = [
async-backtrace = { workspace = true }
async-trait = { workspace = true }
bytesize = "1.1.0"
chrono = { workspace = true }
ctrlc = { version = "3.2.3", features = ["termination"] }
databend-common-exception = { workspace = true }
enquote = "1.1.0"
Expand Down
62 changes: 62 additions & 0 deletions src/common/base/src/display/display_unix_epoch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2021 Datafuse Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fmt;
use std::time::Duration;
use std::time::UNIX_EPOCH;

use chrono::DateTime;
use chrono::Utc;

pub struct DisplayUnixTimeStamp {
/// The duration since the UNIX epoch.
duration: Duration,
}

impl fmt::Display for DisplayUnixTimeStamp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let system_time = UNIX_EPOCH + self.duration;
let datetime: DateTime<Utc> = system_time.into();

write!(f, "{}", datetime.format("%Y-%m-%dT%H:%M:%S%.6fZ%z"))
}
}

pub trait DisplayUnixTimeStampExt {
fn display_unix_timestamp(&self) -> DisplayUnixTimeStamp;
}

impl DisplayUnixTimeStampExt for Duration {
fn display_unix_timestamp(&self) -> DisplayUnixTimeStamp {
DisplayUnixTimeStamp { duration: *self }
}
}

#[cfg(test)]
mod tests {
use std::time::Duration;

use super::*;

#[test]
fn test_display_unix_epoch() {
let epoch = Duration::from_millis(0);
let display = epoch.display_unix_timestamp();
assert_eq!(format!("{}", display), "1970-01-01T00:00:00.000000Z+0000");

let epoch = Duration::from_millis(1723102819023);
let display = epoch.display_unix_timestamp();
assert_eq!(format!("{}", display), "2024-08-08T07:40:19.023000Z+0000");
}
}
1 change: 1 addition & 0 deletions src/common/base/src/display/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
// limitations under the License.

pub mod display_option;
pub mod display_unix_epoch;
8 changes: 5 additions & 3 deletions src/meta/raft-store/src/applier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

use std::io;
use std::time::Duration;
use std::time::SystemTime;

use databend_common_base::display::display_unix_epoch::DisplayUnixTimeStampExt;
use databend_common_meta_types::protobuf as pb;
use databend_common_meta_types::txn_condition;
use databend_common_meta_types::txn_op;
Expand Down Expand Up @@ -529,8 +529,10 @@ impl<'a> Applier<'a> {
0
}
Some(ms) => {
let t = SystemTime::UNIX_EPOCH + Duration::from_millis(ms);
debug!("apply: raft-log time: {:?}", t);
debug!(
"apply: raft-log time: {}",
Duration::from_millis(ms).display_unix_timestamp()
);
ms
}
},
Expand Down
8 changes: 5 additions & 3 deletions src/meta/raft-store/src/state_machine/sm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use std::convert::TryInto;
use std::fmt::Debug;
use std::time::Duration;
use std::time::Instant;
use std::time::SystemTime;

use databend_common_base::display::display_unix_epoch::DisplayUnixTimeStampExt;
use databend_common_meta_sled_store::get_sled_db;
use databend_common_meta_sled_store::openraft::MessageSummary;
use databend_common_meta_sled_store::AsKeySpace;
Expand Down Expand Up @@ -336,8 +336,10 @@ impl StateMachine {
0
}
Some(x) => {
let t = SystemTime::UNIX_EPOCH + Duration::from_millis(x);
info!("apply: raft-log time: {:?}", t);
info!(
"apply: raft-log time: {}",
Duration::from_millis(x).display_unix_timestamp()
);
x
}
},
Expand Down
1 change: 1 addition & 0 deletions src/meta/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ test = true

[dependencies]
anyerror = { workspace = true }
databend-common-base = { workspace = true }
databend-common-exception = { workspace = true }
databend-common-meta-stoerr = { workspace = true }
databend-common-tracing = { workspace = true }
Expand Down
9 changes: 6 additions & 3 deletions src/meta/types/src/proto_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use std::fmt::Display;
use std::fmt::Formatter;
use std::time::Duration;
use std::time::SystemTime;

use databend_common_base::display::display_unix_epoch::DisplayUnixTimeStampExt;
use num_traits::FromPrimitive;

use crate::txn_condition::Target;
Expand Down Expand Up @@ -167,8 +167,11 @@ impl Display for TxnPutRequest {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "Put key={}", self.key)?;
if let Some(expire_at) = self.expire_at {
let t = SystemTime::UNIX_EPOCH + Duration::from_millis(expire_at);
write!(f, " expire_at: {:?}", t)?;
write!(
f,
" expire_at: {}",
Duration::from_millis(expire_at).display_unix_timestamp()
)?;
}
if let Some(ttl_ms) = self.ttl_ms {
write!(f, " ttl: {:?}", Duration::from_millis(ttl_ms))?;
Expand Down
2 changes: 1 addition & 1 deletion src/query/ee/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ databend-storages-common-table-meta = { workspace = true }
derive-visitor = { workspace = true }
futures = { workspace = true }
futures-util = { workspace = true }
jwt-simple = "0.11.0"
jwt-simple = { workspace = true }
log = { workspace = true }
opendal = { workspace = true }
tempfile = "3.4.0"
Expand Down

0 comments on commit c5531ab

Please sign in to comment.