tracing_subscriber/fmt/time/
chrono_crate.rs1use crate::fmt::format::Writer;
2use crate::fmt::time::FormatTime;
3
4use alloc::{format, string::String, sync::Arc};
5
6#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
18#[derive(Debug, Clone, Eq, PartialEq, Default)]
19pub struct ChronoLocal {
20 format: Arc<ChronoFmtType>,
21}
22
23impl ChronoLocal {
24 pub fn rfc_3339() -> Self {
30 Self {
31 format: Arc::new(ChronoFmtType::Rfc3339),
32 }
33 }
34
35 pub fn new(format_string: String) -> Self {
39 Self {
40 format: Arc::new(ChronoFmtType::Custom(format_string)),
41 }
42 }
43}
44
45impl FormatTime for ChronoLocal {
46 fn format_time(&self, w: &mut Writer<'_>) -> alloc::fmt::Result {
47 let t = chrono::Local::now();
48 match self.format.as_ref() {
49 ChronoFmtType::Rfc3339 => {
50 use chrono::format::{Fixed, Item};
51 write!(
52 w,
53 "{}",
54 t.format_with_items(core::iter::once(Item::Fixed(Fixed::RFC3339)))
55 )
56 }
57 ChronoFmtType::Custom(fmt) => {
58 write!(w, "{}", t.format(fmt))
59 }
60 }
61 }
62}
63
64#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
69#[derive(Debug, Clone, Eq, PartialEq, Default)]
70pub struct ChronoUtc {
71 format: Arc<ChronoFmtType>,
72}
73
74impl ChronoUtc {
75 pub fn rfc_3339() -> Self {
81 Self {
82 format: Arc::new(ChronoFmtType::Rfc3339),
83 }
84 }
85
86 pub fn new(format_string: String) -> Self {
90 Self {
91 format: Arc::new(ChronoFmtType::Custom(format_string)),
92 }
93 }
94}
95
96impl FormatTime for ChronoUtc {
97 fn format_time(&self, w: &mut Writer<'_>) -> alloc::fmt::Result {
98 let t = chrono::Utc::now();
99 match self.format.as_ref() {
100 ChronoFmtType::Rfc3339 => w.write_str(&t.to_rfc3339()),
101 ChronoFmtType::Custom(fmt) => w.write_str(&format!("{}", t.format(fmt))),
102 }
103 }
104}
105
106#[derive(Debug, Clone, Eq, PartialEq, Default)]
112enum ChronoFmtType {
113 #[default]
115 Rfc3339,
116 Custom(String),
118}
119
120#[cfg(test)]
121mod tests {
122 use crate::fmt::format::Writer;
123 use crate::fmt::time::FormatTime;
124
125 use alloc::{borrow::ToOwned, string::String, sync::Arc};
126
127 use super::ChronoFmtType;
128 use super::ChronoLocal;
129 use super::ChronoUtc;
130
131 #[test]
132 fn test_chrono_format_time_utc_default() {
133 let mut buf = String::new();
134 let mut dst: Writer<'_> = Writer::new(&mut buf);
135 assert!(FormatTime::format_time(&ChronoUtc::default(), &mut dst).is_ok());
136 assert!(chrono::DateTime::parse_from_str(&buf, "%FT%H:%M:%S%.6f%z").is_ok());
138 }
139
140 #[test]
141 fn test_chrono_format_time_utc_custom() {
142 let fmt = ChronoUtc {
143 format: Arc::new(ChronoFmtType::Custom("%a %b %e %T %Y".to_owned())),
144 };
145 let mut buf = String::new();
146 let mut dst: Writer<'_> = Writer::new(&mut buf);
147 assert!(FormatTime::format_time(&fmt, &mut dst).is_ok());
148 assert!(chrono::NaiveDateTime::parse_from_str(&buf, "%a %b %e %T %Y").is_ok());
150 }
151
152 #[test]
153 fn test_chrono_format_time_local_default() {
154 let mut buf = String::new();
155 let mut dst: Writer<'_> = Writer::new(&mut buf);
156 assert!(FormatTime::format_time(&ChronoLocal::default(), &mut dst).is_ok());
157 assert!(chrono::DateTime::parse_from_str(&buf, "%FT%H:%M:%S%.6f%z").is_ok());
159 }
160
161 #[test]
162 fn test_chrono_format_time_local_custom() {
163 let fmt = ChronoLocal {
164 format: Arc::new(ChronoFmtType::Custom("%a %b %e %T %Y".to_owned())),
165 };
166 let mut buf = String::new();
167 let mut dst: Writer<'_> = Writer::new(&mut buf);
168 assert!(FormatTime::format_time(&fmt, &mut dst).is_ok());
169 assert!(chrono::NaiveDateTime::parse_from_str(&buf, "%a %b %e %T %Y").is_ok());
171 }
172}