-
Notifications
You must be signed in to change notification settings - Fork 733
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
davidbarsky
merged 10 commits into
tokio-rs:master
from
shayne-fletcher:chrono-subscriber
Sep 25, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
09f209b
[tracing-subscriber]: add chrono crate implementations of FormatTime …
shayne-fletcher f4817a5
[tracing-subscriber]: add chrono feature
shayne-fletcher f802334
allow for custom datetime formattters w/chrono_crate
shayne-fletcher e2b09eb
Merge branch 'master' into chrono-subscriber
davidbarsky 897cfeb
[tracing-subscriber]: remove redundant cfg
3e9aa30
Merge branch 'master' into chrono-subscriber
davidbarsky ae007f9
edit docs, add constructors for `ChronoLocal` and `ChronoUtc`
davidbarsky e2c7bec
fix invalid doc(cfg(...))
davidbarsky 269695a
implement hawkw's review suggestions
56f3577
fix remaining nits
davidbarsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
use crate::fmt::format::Writer; | ||
use crate::fmt::time::FormatTime; | ||
|
||
use std::sync::Arc; | ||
|
||
/// Formats [local time]s and [UTC time]s with `FormatTime` implementations | ||
/// that use the [`chrono` crate]. | ||
/// | ||
/// [local time]: [`chrono::offset::Local`] | ||
/// [UTC time]: [`chrono::offset::Utc`] | ||
/// [`chrono` crate]: [`chrono`] | ||
|
||
/// Formats the current [local time] using a [formatter] from the [`chrono`] crate. | ||
/// | ||
/// [local time]: chrono::Local::now() | ||
/// [formatter]: chrono::format | ||
#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))] | ||
#[derive(Debug, Clone, Eq, PartialEq, Default)] | ||
pub struct ChronoLocal { | ||
format: Arc<ChronoFmtType>, | ||
} | ||
|
||
impl ChronoLocal { | ||
/// Format the time using the [`RFC 3339`] format | ||
/// (a subset of [`ISO 8601`]). | ||
/// | ||
/// [`RFC 3339`]: https://tools.ietf.org/html/rfc3339 | ||
/// [`ISO 8601`]: https://en.wikipedia.org/wiki/ISO_8601 | ||
pub fn rfc_3339() -> Self { | ||
Self { | ||
format: Arc::new(ChronoFmtType::Rfc3339), | ||
} | ||
} | ||
|
||
/// Format the time using the given format string. | ||
/// | ||
/// See [`chrono::format::strftime`] for details on the supported syntax. | ||
pub fn new(format_string: String) -> Self { | ||
Self { | ||
format: Arc::new(ChronoFmtType::Custom(format_string)), | ||
} | ||
} | ||
} | ||
|
||
impl FormatTime for ChronoLocal { | ||
fn format_time(&self, w: &mut Writer<'_>) -> alloc::fmt::Result { | ||
let t = chrono::Local::now(); | ||
match self.format.as_ref() { | ||
ChronoFmtType::Rfc3339 => { | ||
use chrono::format::{Fixed, Item}; | ||
write!( | ||
w, | ||
"{}", | ||
t.format_with_items(core::iter::once(Item::Fixed(Fixed::RFC3339))) | ||
) | ||
} | ||
ChronoFmtType::Custom(fmt) => { | ||
write!(w, "{}", t.format(fmt)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Formats the current [UTC time] using a [formatter] from the [`chrono`] crate. | ||
/// | ||
/// [UTC time]: chrono::Utc::now() | ||
/// [formatter]: chrono::format | ||
#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))] | ||
#[derive(Debug, Clone, Eq, PartialEq, Default)] | ||
pub struct ChronoUtc { | ||
format: Arc<ChronoFmtType>, | ||
} | ||
|
||
impl ChronoUtc { | ||
/// Format the time using the [`RFC 3339`] format | ||
/// (a subset of [`ISO 8601`]). | ||
/// | ||
/// [`RFC 3339`]: https://tools.ietf.org/html/rfc3339 | ||
/// [`ISO 8601`]: https://en.wikipedia.org/wiki/ISO_8601 | ||
pub fn rfc_3339() -> Self { | ||
Self { | ||
format: Arc::new(ChronoFmtType::Rfc3339), | ||
} | ||
} | ||
|
||
/// Format the time using the given format string. | ||
/// | ||
/// See [`chrono::format::strftime`] for details on the supported syntax. | ||
pub fn new(format_string: String) -> Self { | ||
Self { | ||
format: Arc::new(ChronoFmtType::Custom(format_string)), | ||
} | ||
} | ||
} | ||
|
||
impl FormatTime for ChronoUtc { | ||
fn format_time(&self, w: &mut Writer<'_>) -> alloc::fmt::Result { | ||
let t = chrono::Utc::now(); | ||
match self.format.as_ref() { | ||
ChronoFmtType::Rfc3339 => w.write_str(&t.to_rfc3339()), | ||
ChronoFmtType::Custom(fmt) => w.write_str(&format!("{}", t.format(fmt))), | ||
} | ||
} | ||
} | ||
|
||
/// 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 | ||
#[derive(Debug, Clone, Eq, PartialEq)] | ||
enum ChronoFmtType { | ||
/// Format according to the RFC 3339 convention. | ||
Rfc3339, | ||
/// Format according to a custom format string. | ||
Custom(String), | ||
} | ||
|
||
impl Default for ChronoFmtType { | ||
fn default() -> Self { | ||
ChronoFmtType::Rfc3339 | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::fmt::format::Writer; | ||
use crate::fmt::time::FormatTime; | ||
|
||
use std::sync::Arc; | ||
|
||
use super::ChronoFmtType; | ||
use super::ChronoLocal; | ||
use super::ChronoUtc; | ||
|
||
#[test] | ||
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::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" | ||
assert!(chrono::DateTime::parse_from_str(&buf, "%FT%H:%M:%S%.6f%z").is_ok()); | ||
} | ||
|
||
#[test] | ||
fn test_chrono_format_time_utc_custom() { | ||
let fmt = ChronoUtc { | ||
shayne-fletcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
format: Arc::new(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:53:23 2023" | ||
assert!(chrono::NaiveDateTime::parse_from_str(&buf, "%a %b %e %T %Y").is_ok()); | ||
} | ||
|
||
#[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". | ||
assert!(chrono::DateTime::parse_from_str(&buf, "%FT%H:%M:%S%.6f%z").is_ok()); | ||
} | ||
|
||
#[test] | ||
fn test_chrono_format_time_local_custom() { | ||
let fmt = ChronoLocal { | ||
format: Arc::new(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". | ||
assert!(chrono::NaiveDateTime::parse_from_str(&buf, "%a %b %e %T %Y").is_ok()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this was supposed to be a module-level doc comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...but, i guess having a module-level doc comment isn't super important since the module is private?