1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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());
        // 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 {
            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());
    }
}