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
81 changes: 71 additions & 10 deletions tracing-subscriber/src/fmt/time/chrono_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,63 @@ use crate::fmt::time::FormatTime;
/// [UTC time]: [`chrono::offset::Utc`]
/// [`chrono` crate]: [`chrono`]

/// The RFC 3339 format is used by default but a custom format string
/// can be used. See [`chrono::format::strftime`]for details on
/// the supported syntax.
///
/// [`chrono::format::strftime`]: https://docs.rs/chrono/0.4.9/chrono/format/strftime/index.html
#[cfg(feature = "chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
davidbarsky marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ChronoFmtType {
shayne-fletcher marked this conversation as resolved.
Show resolved Hide resolved
/// Format according to the RFC 3339 convention.
Rfc3339,
/// Format according to a custom format string.
Custom(String),
shayne-fletcher marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(feature = "chrono")]
impl Default for ChronoFmtType {
fn default() -> Self {
ChronoFmtType::Rfc3339
}
}

/// 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;
#[derive(Debug, Clone, Eq, PartialEq, Default)]
pub struct ChronoLocal {
format: ChronoFmtType,
}

#[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())
let t = chrono::Local::now();
match &self.format {
ChronoFmtType::Rfc3339 => w.write_str(&t.to_rfc3339()),
shayne-fletcher marked this conversation as resolved.
Show resolved Hide resolved
ChronoFmtType::Custom(fmt) => w.write_str(&format!("{}", t.format(fmt))),
shayne-fletcher marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

/// 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;
#[derive(Debug, Clone, Eq, PartialEq, Default)]
pub struct ChronoUtc {
format: ChronoFmtType,
}

#[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())
let t = chrono::Utc::now();
match &self.format {
ChronoFmtType::Rfc3339 => w.write_str(&t.to_rfc3339()),
ChronoFmtType::Custom(fmt) => w.write_str(&format!("{}", t.format(fmt))),
}
}
}

Expand All @@ -39,26 +73,53 @@ mod tests {
use crate::fmt::format::Writer;
use crate::fmt::time::FormatTime;

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

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

#[cfg(feature = "chrono")]
shayne-fletcher marked this conversation as resolved.
Show resolved Hide resolved
#[test]
fn test_chrono_format_time_local() {
fn test_chrono_format_time_utc_custom() {
let fmt = ChronoUtc {
shayne-fletcher marked this conversation as resolved.
Show resolved Hide resolved
format: ChronoFmtType::Custom("%a %b %e %T %Y".to_owned()),
};
let mut buf = String::new();
let mut dst: Writer<'_> = Writer::new(&mut buf);
assert!(FormatTime::format_time(&ChronoLocal, &mut dst).is_ok());
assert!(FormatTime::format_time(&fmt, &mut dst).is_ok());
println!("{}", buf);
shayne-fletcher marked this conversation as resolved.
Show resolved Hide resolved
// e.g. `buf` contains "Wed Aug 23 15:53:23 2023"
}

#[cfg(feature = "chrono")]
#[test]
fn test_chrono_format_time_local_default() {
let mut buf = String::new();
let mut dst: Writer<'_> = Writer::new(&mut buf);
assert!(FormatTime::format_time(&ChronoLocal::default(), &mut dst).is_ok());
// e.g. `buf` contains "2023-08-18T14:59:08.662499-04:00".
}

#[cfg(feature = "chrono")]
#[test]
fn test_chrono_format_time_local_custom() {
let fmt = ChronoLocal {
format: ChronoFmtType::Custom("%a %b %e %T %Y".to_owned()),
};
let mut buf = String::new();
let mut dst: Writer<'_> = Writer::new(&mut buf);
assert!(FormatTime::format_time(&fmt, &mut dst).is_ok());
// e.g. `buf` contains "Wed Aug 23 15:55:46 2023".
}
}