Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tracing-subscriber]: add chrono crate implementations of FormatTime #2690

Merged
merged 10 commits into from
Sep 25, 2023
Prev Previous commit
Next Next commit
[tracing-subscriber]: add chrono feature
shayne-fletcher committed Aug 21, 2023
commit f4817a52f42c4a74dcac28ddf1b6705fbc99b21b
3 changes: 2 additions & 1 deletion tracing-subscriber/Cargo.toml
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ fmt = ["registry", "std"]
ansi = ["fmt", "nu-ansi-term"]
registry = ["sharded-slab", "thread_local", "std"]
json = ["tracing-serde", "serde", "serde_json"]

# Enables support for local time when using the `time` crate timestamp
# formatters.
local-time = ["time/local-offset"]
@@ -50,7 +51,6 @@ once_cell = { optional = true, version = "1.13.0" }
tracing-log = { path = "../tracing-log", version = "0.2", optional = true, default-features = false, features = ["log-tracer", "std"] }
nu-ansi-term = { version = "0.46.0", optional = true }
time = { version = "0.3.2", features = ["formatting"], optional = true }
chrono = { version = "0.4.26" }

# only required by the json feature
serde_json = { version = "1.0.82", optional = true }
@@ -59,6 +59,7 @@ tracing-serde = { path = "../tracing-serde", version = "0.2", optional = true }

# opt-in deps
parking_lot = { version = "0.12.1", optional = true }
chrono = { version = "0.4.26", optional = true }

# registry
sharded-slab = { version = "0.1.4", optional = true }
45 changes: 26 additions & 19 deletions tracing-subscriber/src/fmt/time/chrono_crate.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
use crate::fmt::format::Writer;
use crate::fmt::time::FormatTime;

/// Formats [local time]s and [UTC time]s with [formatter] implementations
/// Formats [local time]s and [UTC time]s with `FormatTime` implementations
/// that use the [`chrono` crate].
///
/// [local time]: https://docs.rs/chrono/0.4.26/chrono/offset/struct.Local.html
/// [UTC time]: https://docs.rs/chrono/0.4.26/chrono/offset/struct.Utc.html
/// [`chrono` crate]: https://docs.rs/chrono/0.4.26/chrono/
/// [formatter]: https://docs.rs/time/0.3/time/formatting/trait.Formattable.html
/// [local time]: [`chrono::offset::Local`]
/// [UTC time]: [`chrono::offset::Utc`]
/// [`chrono` crate]: [`chrono`]
/// Tag-type (indicating UTC timezone) enabling static dispatch
/// to `chrono::Local` functions.
#[derive(Debug)]
pub struct LocalTime;
/// Retrieve and print the current local time.
#[cfg(feature = "chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub struct ChronoLocal;

impl FormatTime for LocalTime {
#[cfg(feature = "chrono")]
impl FormatTime for ChronoLocal {
fn format_time(&self, w: &mut Writer<'_>) -> alloc::fmt::Result {
w.write_str(&chrono::Local::now().to_rfc3339())
}
}

/// Tag-type (indicating the "local" timezone) enabling static
/// dispatch to `chrono::Utc` functions.
#[derive(Debug)]
pub struct Utc;
/// Retrieve and print the current UTC time.
#[cfg(feature = "chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
pub struct ChronoUtc;

impl FormatTime for Utc {
#[cfg(feature = "chrono")]
impl FormatTime for ChronoUtc {
fn format_time(&self, w: &mut Writer<'_>) -> alloc::fmt::Result {
w.write_str(&chrono::Utc::now().to_rfc3339())
}
@@ -36,22 +39,26 @@ mod tests {
use crate::fmt::format::Writer;
use crate::fmt::time::FormatTime;

use super::LocalTime;
use super::Utc;
#[cfg(feature = "chrono")]
use super::ChronoLocal;
#[cfg(feature = "chrono")]
use super::ChronoUtc;

#[cfg(feature = "chrono")]
#[test]
fn test_chrono_format_time_utc() {
let mut buf = String::new();
let mut dst: Writer<'_> = Writer::new(&mut buf);
assert!(FormatTime::format_time(&Utc, &mut dst).is_ok());
assert!(FormatTime::format_time(&ChronoUtc, &mut dst).is_ok());
// e.g. `buf` contains "2023-08-18T19:05:08.662499+00:00"
}

#[cfg(feature = "chrono")]
#[test]
fn test_chrono_format_time_local() {
let mut buf = String::new();
let mut dst: Writer<'_> = Writer::new(&mut buf);
assert!(FormatTime::format_time(&LocalTime, &mut dst).is_ok());
assert!(FormatTime::format_time(&ChronoLocal, &mut dst).is_ok());
// e.g. `buf` contains "2023-08-18T14:59:08.662499-04:00".
}
}
3 changes: 2 additions & 1 deletion tracing-subscriber/src/fmt/time/mod.rs
Original file line number Diff line number Diff line change
@@ -15,7 +15,8 @@ pub use time_crate::UtcTime;
#[cfg_attr(docsrs, doc(cfg(all(unsound_local_offset, feature = "local-time"))))]
pub use time_crate::LocalTime;

/// [`Chrono`]-based implementation for time.
/// [`chrono`]-based implementation for time.
#[cfg(feature = "chrono")]
pub mod chrono_crate;

/// A type that can measure and format the current time.