Provide a more helpful Debug impl for timestamps
This commit is contained in:
parent
4ffbed0e06
commit
95707b86c3
@ -73,6 +73,7 @@ serde = { workspace = true }
|
|||||||
serde_html_form = { workspace = true }
|
serde_html_form = { workspace = true }
|
||||||
serde_json = { workspace = true, features = ["raw_value"] }
|
serde_json = { workspace = true, features = ["raw_value"] }
|
||||||
thiserror = { workspace = true }
|
thiserror = { workspace = true }
|
||||||
|
time = "0.3.34"
|
||||||
tracing = { workspace = true, features = ["attributes"] }
|
tracing = { workspace = true, features = ["attributes"] }
|
||||||
url = "2.2.2"
|
url = "2.2.2"
|
||||||
uuid = { version = "1.0.0", optional = true, features = ["v4"] }
|
uuid = { version = "1.0.0", optional = true, features = ["v4"] }
|
||||||
|
@ -5,6 +5,7 @@ use std::{
|
|||||||
|
|
||||||
use js_int::{uint, UInt};
|
use js_int::{uint, UInt};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use time::OffsetDateTime;
|
||||||
|
|
||||||
/// A timestamp represented as the number of milliseconds since the unix epoch.
|
/// A timestamp represented as the number of milliseconds since the unix epoch.
|
||||||
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
|
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
|
||||||
@ -48,9 +49,28 @@ impl MilliSecondsSinceUnixEpoch {
|
|||||||
|
|
||||||
impl fmt::Debug for MilliSecondsSinceUnixEpoch {
|
impl fmt::Debug for MilliSecondsSinceUnixEpoch {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
// The default Debug impl would put the inner value on its own line if the formatter's
|
match OffsetDateTime::from_unix_timestamp(i64::from(self.0) / 1000) {
|
||||||
// alternate mode is enabled, which bloats debug strings unnecessarily
|
Ok(date) => {
|
||||||
write!(f, "MilliSecondsSinceUnixEpoch({})", self.0)
|
let date = date + Duration::from_millis(u64::from(self.0) % 1000);
|
||||||
|
|
||||||
|
let (year, month, day) = date.to_calendar_date();
|
||||||
|
let month = month as u8;
|
||||||
|
let (hours, minutes, seconds, milliseconds) = date.to_hms_milli();
|
||||||
|
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{year}-{month:02}-{day:02}T\
|
||||||
|
{hours:02}:{minutes:02}:{seconds:02}.{milliseconds:03}"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
// Probably dead code..
|
||||||
|
Err(_) => {
|
||||||
|
// The default Debug impl would put the inner value on its own
|
||||||
|
// line if the formatter's alternate mode is enabled, which
|
||||||
|
// bloats debug strings unnecessarily
|
||||||
|
write!(f, "MilliSecondsSinceUnixEpoch({})", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,9 +111,26 @@ impl SecondsSinceUnixEpoch {
|
|||||||
|
|
||||||
impl fmt::Debug for SecondsSinceUnixEpoch {
|
impl fmt::Debug for SecondsSinceUnixEpoch {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
// The default Debug impl would put the inner value on its own line if the formatter's
|
match OffsetDateTime::from_unix_timestamp(i64::from(self.0)) {
|
||||||
// alternate mode is enabled, which bloats debug strings unnecessarily
|
Ok(date) => {
|
||||||
write!(f, "SecondsSinceUnixEpoch({})", self.0)
|
let (year, month, day) = date.to_calendar_date();
|
||||||
|
let month = month as u8;
|
||||||
|
let (hours, minutes, seconds) = date.to_hms();
|
||||||
|
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"{year}-{month:02}-{day:02}T\
|
||||||
|
{hours:02}:{minutes:02}:{seconds:02}"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
// Probably dead code..
|
||||||
|
Err(_) => {
|
||||||
|
// The default Debug impl would put the inner value on its own
|
||||||
|
// line if the formatter's alternate mode is enabled, which
|
||||||
|
// bloats debug strings unnecessarily
|
||||||
|
write!(f, "SecondsSinceUnixEpoch({})", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,4 +176,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(serde_json::to_value(request).unwrap(), json!({ "millis": 2000, "secs": 0 }));
|
assert_eq!(serde_json::to_value(request).unwrap(), json!({ "millis": 2000, "secs": 0 }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn debug_s() {
|
||||||
|
let seconds = SecondsSinceUnixEpoch(uint!(0));
|
||||||
|
assert_eq!(format!("{seconds:?}"), "1970-01-01T00:00:00");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn debug_ms() {
|
||||||
|
let seconds = MilliSecondsSinceUnixEpoch(uint!(0));
|
||||||
|
assert_eq!(format!("{seconds:?}"), "1970-01-01T00:00:00.000");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user