Skip to content

Commit 0cab95a

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

File tree

3 files changed

+149
-15
lines changed

3 files changed

+149
-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: 38 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,46 @@ 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)
112+
.map_err(|_| fmt::Error)
113+
.and_then(|s| f.write_str(&s))
114+
}
115+
}
116+
117+
#[cfg(test)]
118+
mod tests {
119+
use super::Timestamp;
120+
use crate::TimestampPrecision;
121+
122+
#[test]
123+
fn test_display_timestamp() {
124+
let mut ts = Timestamp {
125+
time: std::time::SystemTime::UNIX_EPOCH,
126+
precision: TimestampPrecision::Nanos,
107127
};
108128

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

0 commit comments

Comments
 (0)