Skip to content

Commit

Permalink
[tracing-subscriber]: add chrono feature
Browse files Browse the repository at this point in the history
  • Loading branch information
shayne-fletcher committed Aug 21, 2023
1 parent 09f209b commit dbb35e0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
2 changes: 2 additions & 0 deletions tracing-subscriber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ fmt = ["registry", "std"]
ansi = ["fmt", "nu-ansi-term"]
registry = ["sharded-slab", "thread_local", "std"]
json = ["tracing-serde", "serde", "serde_json"]
chrono = []

# Enables support for local time when using the `time` crate timestamp
# formatters.
local-time = ["time/local-offset"]
Expand Down
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())
}
Expand All @@ -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
Expand Up @@ -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.
Expand Down

0 comments on commit dbb35e0

Please sign in to comment.