Skip to content

Commit

Permalink
[tracing-subscriber]: add chrono crate implementations of FormatTime …
Browse files Browse the repository at this point in the history
…(local time & UTC)
  • Loading branch information
shayne-fletcher committed Aug 18, 2023
1 parent 81ab9d6 commit db86c5a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions tracing-subscriber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ 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 }
Expand Down
57 changes: 57 additions & 0 deletions tracing-subscriber/src/fmt/time/chrono_crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::fmt::format::Writer;
use crate::fmt::time::FormatTime;

/// Formats [local time]s and [UTC time]s with [formatter] 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
/// Tag-type (indicating UTC timezone) enabling static dispatch
/// to `chrono::Local` functions.
#[derive(Debug)]
pub struct LocalTime;

impl FormatTime for LocalTime {
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;

impl FormatTime for Utc {
fn format_time(&self, w: &mut Writer<'_>) -> alloc::fmt::Result {
w.write_str(&chrono::Utc::now().to_rfc3339())
}
}

#[cfg(test)]
mod tests {
use crate::fmt::format::Writer;
use crate::fmt::time::FormatTime;

use super::LocalTime;
use super::Utc;

#[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_err());
// e.g. `buf` contains "2023-08-18T19:05:08.662499+00:00"
}

#[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_err());
// e.g. `buf` contains "2023-08-18T14:59:08.662499-04:00".
}
}
3 changes: 3 additions & 0 deletions tracing-subscriber/src/fmt/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ 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.
pub mod chrono_crate;

/// A type that can measure and format the current time.
///
/// This trait is used by `Format` to include a timestamp with each `Event` when it is logged.
Expand Down

0 comments on commit db86c5a

Please sign in to comment.