Skip to content

Commit 1b3d072

Browse files
committed
chore(deps): Switch humantime to jiff
Humantime seems to be unmaintained and jiff provides same functionality.
1 parent a93b787 commit 1b3d072

File tree

3 files changed

+147
-15
lines changed

3 files changed

+147
-15
lines changed

Cargo.lock

Lines changed: 109 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", 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: 36 additions & 9 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,44 @@ impl fmt::Debug for Timestamp {
9995

10096
impl fmt::Display for Timestamp {
10197
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98+
use jiff::fmt::strtime;
99+
100+
let Ok(ts) = jiff::Timestamp::try_from(self.time) else {
101+
return Err(fmt::Error);
102+
};
103+
102104
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,
105+
TimestampPrecision::Seconds => |t| strtime::format("%FT%TZ", t),
106+
TimestampPrecision::Millis => |t| strtime::format("%FT%T%.3fZ", t),
107+
TimestampPrecision::Micros => |t| strtime::format("%FT%T%.6fZ", t),
108+
TimestampPrecision::Nanos => |t| strtime::format("%FT%T%.9fZ", t),
109+
};
110+
111+
formatter(ts).map_err(|_| fmt::Error).and_then(|s| f.write_str(&s))
112+
}
113+
}
114+
115+
#[cfg(test)]
116+
mod tests {
117+
use crate::TimestampPrecision;
118+
use super::Timestamp;
119+
120+
#[test]
121+
fn test_display_timestamp() {
122+
let mut ts = Timestamp {
123+
time: std::time::SystemTime::UNIX_EPOCH,
124+
precision: TimestampPrecision::Nanos,
107125
};
108126

109-
formatter(self.time).fmt(f)
127+
assert_eq!("1970-01-01T00:00:00.000000000Z", format!("{ts}"));
128+
129+
ts.precision = TimestampPrecision::Micros;
130+
assert_eq!("1970-01-01T00:00:00.000000Z", format!("{ts}"));
131+
132+
ts.precision = TimestampPrecision::Millis;
133+
assert_eq!("1970-01-01T00:00:00.000Z", format!("{ts}"));
134+
135+
ts.precision = TimestampPrecision::Seconds;
136+
assert_eq!("1970-01-01T00:00:00Z", format!("{ts}"));
110137
}
111138
}

0 commit comments

Comments
 (0)