Skip to content

Commit d6c8c2a

Browse files
authored
Merge pull request #352 from oherrala/jiff
chore(deps): Switch humantime to jiff
2 parents a93b787 + d8dccfc commit d6c8c2a

File tree

3 files changed

+127
-16
lines changed

3 files changed

+127
-16
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ pre-release-replacements = [
120120
default = ["auto-color", "humantime", "regex"]
121121
color = ["dep:anstream", "dep:anstyle"]
122122
auto-color = ["color", "anstream/auto"]
123-
humantime = ["dep:humantime"]
123+
humantime = ["dep:jiff"]
124124
regex = ["env_filter/regex"]
125125
unstable-kv = ["log/kv"]
126126

127127
[dependencies]
128128
log = { version = "0.4.21", features = ["std"] }
129129
env_filter = { version = "0.1.0", path = "crates/env_filter", default-features = false }
130-
humantime = { version = "2.0.0", optional = true }
130+
jiff = { version = "0.2.3", default-features = false, features = ["std"], optional = true }
131131
anstream = { version = "0.6.11", default-features = false, features = ["wincon"], optional = true }
132132
anstyle = { version = "1.0.6", optional = true }
133133

src/fmt/humantime.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
use std::fmt;
22
use std::time::SystemTime;
33

4-
use humantime::{
5-
format_rfc3339_micros, format_rfc3339_millis, format_rfc3339_nanos, format_rfc3339_seconds,
6-
};
7-
84
use crate::fmt::{Formatter, TimestampPrecision};
95

106
impl Formatter {
@@ -99,13 +95,40 @@ impl fmt::Debug for Timestamp {
9995

10096
impl fmt::Display for Timestamp {
10197
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102-
let formatter = match self.precision {
103-
TimestampPrecision::Seconds => format_rfc3339_seconds,
104-
TimestampPrecision::Millis => format_rfc3339_millis,
105-
TimestampPrecision::Micros => format_rfc3339_micros,
106-
TimestampPrecision::Nanos => format_rfc3339_nanos,
98+
let Ok(ts) = jiff::Timestamp::try_from(self.time) else {
99+
return Err(fmt::Error);
100+
};
101+
102+
match self.precision {
103+
TimestampPrecision::Seconds => write!(f, "{ts:.0}"),
104+
TimestampPrecision::Millis => write!(f, "{ts:.3}"),
105+
TimestampPrecision::Micros => write!(f, "{ts:.6}"),
106+
TimestampPrecision::Nanos => write!(f, "{ts:.9}"),
107+
}
108+
}
109+
}
110+
111+
#[cfg(test)]
112+
mod tests {
113+
use super::Timestamp;
114+
use crate::TimestampPrecision;
115+
116+
#[test]
117+
fn test_display_timestamp() {
118+
let mut ts = Timestamp {
119+
time: std::time::SystemTime::UNIX_EPOCH,
120+
precision: TimestampPrecision::Nanos,
107121
};
108122

109-
formatter(self.time).fmt(f)
123+
assert_eq!("1970-01-01T00:00:00.000000000Z", format!("{ts}"));
124+
125+
ts.precision = TimestampPrecision::Micros;
126+
assert_eq!("1970-01-01T00:00:00.000000Z", format!("{ts}"));
127+
128+
ts.precision = TimestampPrecision::Millis;
129+
assert_eq!("1970-01-01T00:00:00.000Z", format!("{ts}"));
130+
131+
ts.precision = TimestampPrecision::Seconds;
132+
assert_eq!("1970-01-01T00:00:00Z", format!("{ts}"));
110133
}
111134
}

0 commit comments

Comments
 (0)