Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ae007f9

Browse files
committed
edit docs, add constructors for ChronoLocal and ChronoUtc
1 parent 3e9aa30 commit ae007f9

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

tracing-subscriber/src/fmt/time/chrono_crate.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,38 @@ impl Default for ChronoFmtType {
2828
}
2929
}
3030

31-
/// Retrieve and print the current local time.
31+
/// Formats the current [local time] using a [formatter] from the [`chrono`] crate.
32+
///
33+
/// [local time]: chrono::Local::now()
34+
/// [formatter]: chrono::format
3235
#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
3336
#[derive(Debug, Clone, Eq, PartialEq, Default)]
3437
pub struct ChronoLocal {
3538
format: ChronoFmtType,
3639
}
3740

41+
impl ChronoLocal {
42+
/// Format the time using the [`RFC 3339`] format
43+
/// (a subset of [`ISO 8601`]).
44+
///
45+
/// [`RFC 3339`]: https://tools.ietf.org/html/rfc3339
46+
/// [`ISO 8601`]: https://en.wikipedia.org/wiki/ISO_8601
47+
pub fn rfc_3339() -> Self {
48+
Self {
49+
format: ChronoFmtType::Rfc3339,
50+
}
51+
}
52+
53+
/// Format the time using the given format string.
54+
///
55+
/// See [`chrono::format::strftime`] for details on the supported syntax.
56+
pub fn new(format_string: String) -> Self {
57+
Self {
58+
format: ChronoFmtType::Custom(format_string),
59+
}
60+
}
61+
}
62+
3863
impl FormatTime for ChronoLocal {
3964
fn format_time(&self, w: &mut Writer<'_>) -> alloc::fmt::Result {
4065
let t = chrono::Local::now();
@@ -45,13 +70,38 @@ impl FormatTime for ChronoLocal {
4570
}
4671
}
4772

48-
/// Retrieve and print the current UTC time.
73+
/// Formats the current [UTC time] using a [formatter] from the [`chrono`] crate.
74+
///
75+
/// [UTC time]: chrono::Utc::now()
76+
/// [formatter]: chrono::format
4977
#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
5078
#[derive(Debug, Clone, Eq, PartialEq, Default)]
5179
pub struct ChronoUtc {
5280
format: ChronoFmtType,
5381
}
5482

83+
impl ChronoUtc {
84+
/// Format the time using the [`RFC 3339`] format
85+
/// (a subset of [`ISO 8601`]).
86+
///
87+
/// [`RFC 3339`]: https://tools.ietf.org/html/rfc3339
88+
/// [`ISO 8601`]: https://en.wikipedia.org/wiki/ISO_8601
89+
pub fn rfc_3339() -> Self {
90+
Self {
91+
format: ChronoFmtType::Rfc3339,
92+
}
93+
}
94+
95+
/// Format the time using the given format string.
96+
///
97+
/// See [`chrono::format::strftime`] for details on the supported syntax.
98+
pub fn new(format_string: String) -> Self {
99+
Self {
100+
format: ChronoFmtType::Custom(format_string),
101+
}
102+
}
103+
}
104+
55105
impl FormatTime for ChronoUtc {
56106
fn format_time(&self, w: &mut Writer<'_>) -> alloc::fmt::Result {
57107
let t = chrono::Utc::now();

tracing-subscriber/src/fmt/time/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod datetime;
77

88
#[cfg(feature = "time")]
99
mod time_crate;
10+
1011
#[cfg(feature = "time")]
1112
#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
1213
pub use time_crate::UtcTime;
@@ -15,9 +16,18 @@ pub use time_crate::UtcTime;
1516
#[cfg_attr(docsrs, doc(cfg(all(unsound_local_offset, feature = "local-time"))))]
1617
pub use time_crate::LocalTime;
1718

18-
/// [`chrono`]-based implementation for time.
19+
/// [`chrono`]-based implementation for [`FormatTime`].
20+
#[cfg(feature = "chrono")]
21+
#[cfg_attr(docsrs, doc(feature = "local-time"))]
22+
mod chrono_crate;
23+
24+
#[cfg(feature = "chrono")]
25+
#[cfg_attr(docsrs, doc(feature = "chrono"))]
26+
pub use chrono_crate::ChronoLocal;
27+
1928
#[cfg(feature = "chrono")]
20-
pub mod chrono_crate;
29+
#[cfg_attr(docsrs, doc(feature = "chrono"))]
30+
pub use chrono_crate::ChronoUtc;
2131

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

0 commit comments

Comments
 (0)