spdlog/formatter/pattern_formatter/pattern/
datetime.rs

1use std::{fmt::Write, marker::PhantomData};
2
3use crate::{
4    formatter::pattern_formatter::{Pattern, PatternContext},
5    Error, Record, StringBuf,
6};
7
8/// A pattern that writes the abbreviated weekday name of log records into the
9/// output. Example: `Mon`, `Tue`.
10#[derive(Clone, Default)]
11pub struct AbbrWeekdayName;
12
13impl Pattern for AbbrWeekdayName {
14    fn format(
15        &self,
16        _record: &Record,
17        dest: &mut StringBuf,
18        ctx: &mut PatternContext,
19    ) -> crate::Result<()> {
20        dest.write_str(ctx.time_date().weekday_name().short)
21            .map_err(Error::FormatRecord)
22    }
23}
24
25/// A pattern that writes the weekday name of log records into the output.
26/// Example: `Monday`, `Tuesday`.
27#[derive(Clone, Default)]
28pub struct WeekdayName;
29
30impl Pattern for WeekdayName {
31    fn format(
32        &self,
33        _record: &Record,
34        dest: &mut StringBuf,
35        ctx: &mut PatternContext,
36    ) -> crate::Result<()> {
37        dest.write_str(ctx.time_date().weekday_name().full)
38            .map_err(Error::FormatRecord)
39    }
40}
41
42/// A pattern that writes the abbreviated month name of log records into the
43/// output. Example: `Jan`, `Feb`.
44#[derive(Clone, Default)]
45pub struct AbbrMonthName;
46
47impl Pattern for AbbrMonthName {
48    fn format(
49        &self,
50        _record: &Record,
51        dest: &mut StringBuf,
52        ctx: &mut PatternContext,
53    ) -> crate::Result<()> {
54        dest.write_str(ctx.time_date().month_name().short)
55            .map_err(Error::FormatRecord)
56    }
57}
58
59/// A pattern that writes the month name of log records into the output.
60/// Example: `January`, `February`.
61#[derive(Clone, Default)]
62pub struct MonthName;
63
64impl Pattern for MonthName {
65    fn format(
66        &self,
67        _record: &Record,
68        dest: &mut StringBuf,
69        ctx: &mut PatternContext,
70    ) -> crate::Result<()> {
71        dest.write_str(ctx.time_date().month_name().full)
72            .map_err(Error::FormatRecord)
73    }
74}
75
76/// A pattern that writes the full date time of log records into the output.
77/// Example: `Thu Aug 23 15:35:46 2014`.
78#[derive(Clone, Default)]
79pub struct FullDateTime;
80
81impl Pattern for FullDateTime {
82    fn format(
83        &self,
84        _record: &Record,
85        dest: &mut StringBuf,
86        ctx: &mut PatternContext,
87    ) -> crate::Result<()> {
88        (|| {
89            dest.write_str(ctx.time_date().weekday_name().short)?;
90            dest.write_char(' ')?;
91            dest.write_str(ctx.time_date().month_name().short)?;
92            dest.write_char(' ')?;
93            dest.write_str(ctx.time_date().day_str())?;
94            dest.write_char(' ')?;
95            dest.write_str(ctx.time_date().hour_str())?;
96            dest.write_char(':')?;
97            dest.write_str(ctx.time_date().minute_str())?;
98            dest.write_char(':')?;
99            dest.write_str(ctx.time_date().second_str())?;
100            dest.write_char(' ')?;
101            dest.write_str(ctx.time_date().year_str())
102        })()
103        .map_err(Error::FormatRecord)
104    }
105}
106
107/// A pattern that writes the short year of log records into the output.
108/// Examples: `22`, `20`.
109#[derive(Clone, Default)]
110pub struct ShortYear;
111
112impl Pattern for ShortYear {
113    fn format(
114        &self,
115        _record: &Record,
116        dest: &mut StringBuf,
117        ctx: &mut PatternContext,
118    ) -> crate::Result<()> {
119        dest.write_str(ctx.time_date().year_short_str())
120            .map_err(Error::FormatRecord)
121    }
122}
123
124/// A pattern that writes the year of log records into the output.
125/// Examples: `2022`, `2021`.
126#[derive(Clone, Default)]
127pub struct Year;
128
129impl Pattern for Year {
130    fn format(
131        &self,
132        _record: &Record,
133        dest: &mut StringBuf,
134        ctx: &mut PatternContext,
135    ) -> crate::Result<()> {
136        dest.write_str(ctx.time_date().year_str())
137            .map_err(Error::FormatRecord)
138    }
139}
140
141/// A pattern that writes the date of log records in `YYYY-MM-DD` format (ISO
142/// 8601) into the output. Examples: `2022-04-01`, `2021-12-31`.
143#[derive(Clone, Default)]
144pub struct Date;
145
146impl Pattern for Date {
147    fn format(
148        &self,
149        _record: &Record,
150        dest: &mut StringBuf,
151        ctx: &mut PatternContext,
152    ) -> crate::Result<()> {
153        (|| {
154            dest.write_str(ctx.time_date().year_str())?;
155            dest.write_char('-')?;
156            dest.write_str(ctx.time_date().month_str())?;
157            dest.write_char('-')?;
158            dest.write_str(ctx.time_date().day_str())
159        })()
160        .map_err(Error::FormatRecord)
161    }
162}
163
164/// A pattern that writes the short date of log records in `MM/DD/YY` format
165/// into the output. Examples: `04/01/22`, `12/31/21`.
166#[derive(Clone, Default)]
167pub struct ShortDate;
168
169impl Pattern for ShortDate {
170    fn format(
171        &self,
172        _record: &Record,
173        dest: &mut StringBuf,
174        ctx: &mut PatternContext,
175    ) -> crate::Result<()> {
176        (|| {
177            dest.write_str(ctx.time_date().month_str())?;
178            dest.write_char('/')?;
179            dest.write_str(ctx.time_date().day_str())?;
180            dest.write_char('/')?;
181            dest.write_str(ctx.time_date().year_short_str())
182        })()
183        .map_err(Error::FormatRecord)
184    }
185}
186
187/// A pattern that writes the month of log records into the output.
188/// Examples: `01`, `12`.
189#[derive(Clone, Default)]
190pub struct Month;
191
192impl Pattern for Month {
193    fn format(
194        &self,
195        _record: &Record,
196        dest: &mut StringBuf,
197        ctx: &mut PatternContext,
198    ) -> crate::Result<()> {
199        dest.write_str(ctx.time_date().month_str())
200            .map_err(Error::FormatRecord)
201    }
202}
203
204/// A pattern that writes the day of log records into the output.
205/// Examples: `01`, `12`, `31`, `30`.
206#[derive(Clone, Default)]
207pub struct Day;
208
209impl Pattern for Day {
210    fn format(
211        &self,
212        _record: &Record,
213        dest: &mut StringBuf,
214        ctx: &mut PatternContext,
215    ) -> crate::Result<()> {
216        dest.write_str(ctx.time_date().day_str())
217            .map_err(Error::FormatRecord)
218    }
219}
220
221/// A pattern that writes the hour of log records into the output. Examples:
222/// `01`, `12`, `23`.
223#[derive(Clone, Default)]
224pub struct Hour;
225
226impl Pattern for Hour {
227    fn format(
228        &self,
229        _record: &Record,
230        dest: &mut StringBuf,
231        ctx: &mut PatternContext,
232    ) -> crate::Result<()> {
233        dest.write_str(ctx.time_date().hour_str())
234            .map_err(Error::FormatRecord)
235    }
236}
237
238/// A pattern that writes the hour in 12-hour format of log records into the
239/// output. Examples: `01`, `12`.
240#[derive(Clone, Default)]
241pub struct Hour12;
242
243impl Pattern for Hour12 {
244    fn format(
245        &self,
246        _record: &Record,
247        dest: &mut StringBuf,
248        ctx: &mut PatternContext,
249    ) -> crate::Result<()> {
250        dest.write_str(ctx.time_date().hour12_str())
251            .map_err(Error::FormatRecord)
252    }
253}
254
255/// A pattern that writes the minute of log records into the output. Examples:
256/// `00`, `05`, `59`.
257#[derive(Clone, Default)]
258pub struct Minute;
259
260impl Pattern for Minute {
261    fn format(
262        &self,
263        _record: &Record,
264        dest: &mut StringBuf,
265        ctx: &mut PatternContext,
266    ) -> crate::Result<()> {
267        dest.write_str(ctx.time_date().minute_str())
268            .map_err(Error::FormatRecord)
269    }
270}
271
272/// A pattern that writes the second of log records into the output. Examples:
273/// `00`, `05`, `59`.
274#[derive(Clone, Default)]
275pub struct Second;
276
277impl Pattern for Second {
278    fn format(
279        &self,
280        _record: &Record,
281        dest: &mut StringBuf,
282        ctx: &mut PatternContext,
283    ) -> crate::Result<()> {
284        dest.write_str(ctx.time_date().second_str())
285            .map_err(Error::FormatRecord)
286    }
287}
288
289/// A pattern that writes the millisecond part within a second of the timestamp
290/// of a log record into the output. Example: `231`.
291#[derive(Clone, Default)]
292pub struct Millisecond {
293    /// This field prevents users from creating `Millisecond` objects
294    /// literally.
295    _phantom: PhantomData<()>,
296}
297
298impl Pattern for Millisecond {
299    fn format(
300        &self,
301        _record: &Record,
302        dest: &mut StringBuf,
303        ctx: &mut PatternContext,
304    ) -> crate::Result<()> {
305        write!(dest, "{:03}", ctx.time_date().millisecond()).map_err(Error::FormatRecord)
306    }
307}
308
309/// A pattern that writes the microsecond part within a second of the timestamp
310/// of a log record into the output. Example: `372152`.
311#[derive(Clone, Default)]
312pub struct Microsecond;
313
314impl Pattern for Microsecond {
315    fn format(
316        &self,
317        _record: &Record,
318        dest: &mut StringBuf,
319        ctx: &mut PatternContext,
320    ) -> crate::Result<()> {
321        let nanosecond = ctx.time_date().nanosecond();
322        write!(dest, "{:06}", nanosecond / 1_000).map_err(Error::FormatRecord)
323    }
324}
325
326/// A pattern that writes the nanosecond part within a second of the timestamp
327/// of a log record into the output. Example: `482930154`.
328#[derive(Clone, Default)]
329pub struct Nanosecond;
330
331impl Pattern for Nanosecond {
332    fn format(
333        &self,
334        _record: &Record,
335        dest: &mut StringBuf,
336        ctx: &mut PatternContext,
337    ) -> crate::Result<()> {
338        write!(dest, "{:09}", ctx.time_date().nanosecond()).map_err(Error::FormatRecord)
339    }
340}
341
342/// A pattern that writes "AM" or "PM" into the output according to the
343/// timestamp of a log record. Example: `AM`, `PM`.
344#[derive(Clone, Default)]
345pub struct AmPm;
346
347impl Pattern for AmPm {
348    fn format(
349        &self,
350        _record: &Record,
351        dest: &mut StringBuf,
352        ctx: &mut PatternContext,
353    ) -> crate::Result<()> {
354        dest.write_str(ctx.time_date().am_pm_str())
355            .map_err(Error::FormatRecord)
356    }
357}
358
359/// A pattern that writes the time of log records in 12-hour format into the
360/// output. Examples: `02:55:02 PM`.
361#[derive(Clone, Default)]
362pub struct Time12;
363
364impl Pattern for Time12 {
365    fn format(
366        &self,
367        _record: &Record,
368        dest: &mut StringBuf,
369        ctx: &mut PatternContext,
370    ) -> crate::Result<()> {
371        (|| {
372            dest.write_str(ctx.time_date().hour12_str())?;
373            dest.write_char(':')?;
374            dest.write_str(ctx.time_date().minute_str())?;
375            dest.write_char(':')?;
376            dest.write_str(ctx.time_date().second_str())?;
377            dest.write_str(" ")?;
378            dest.write_str(ctx.time_date().am_pm_str())
379        })()
380        .map_err(Error::FormatRecord)
381    }
382}
383
384/// A pattern that writes the short time of log records into the output.
385/// Examples: `22:28`, `09:53`.
386#[derive(Clone, Default)]
387pub struct ShortTime;
388
389impl Pattern for ShortTime {
390    fn format(
391        &self,
392        _record: &Record,
393        dest: &mut StringBuf,
394        ctx: &mut PatternContext,
395    ) -> crate::Result<()> {
396        (|| {
397            dest.write_str(ctx.time_date().hour_str())?;
398            dest.write_char(':')?;
399            dest.write_str(ctx.time_date().minute_str())
400        })()
401        .map_err(Error::FormatRecord)
402    }
403}
404
405/// A pattern that writes the time of log records into the output. Examples:
406/// `22:28:02`, `09:53:41`.
407#[derive(Clone, Default)]
408pub struct Time;
409
410impl Pattern for Time {
411    fn format(
412        &self,
413        _record: &Record,
414        dest: &mut StringBuf,
415        ctx: &mut PatternContext,
416    ) -> crate::Result<()> {
417        (|| {
418            dest.write_str(ctx.time_date().hour_str())?;
419            dest.write_char(':')?;
420            dest.write_str(ctx.time_date().minute_str())?;
421            dest.write_char(':')?;
422            dest.write_str(ctx.time_date().second_str())
423        })()
424        .map_err(Error::FormatRecord)
425    }
426}
427
428/// A pattern that writes the timezone offset of log records into the output.
429/// Examples: `+08:00`, `+00:00`, `-06:00`.
430#[derive(Clone, Default)]
431pub struct TzOffset;
432
433impl Pattern for TzOffset {
434    fn format(
435        &self,
436        _record: &Record,
437        dest: &mut StringBuf,
438        ctx: &mut PatternContext,
439    ) -> crate::Result<()> {
440        dest.write_str(ctx.time_date().tz_offset_str())
441            .map_err(Error::FormatRecord)
442    }
443}
444
445/// A pattern that writes the unix timestamp of log records into the output.
446/// Examples: `1528834770`.
447#[derive(Clone, Default)]
448pub struct UnixTimestamp;
449
450impl Pattern for UnixTimestamp {
451    fn format(
452        &self,
453        _record: &Record,
454        dest: &mut StringBuf,
455        ctx: &mut PatternContext,
456    ) -> crate::Result<()> {
457        dest.write_str(ctx.time_date().unix_timestamp_str())
458            .map_err(Error::FormatRecord)
459    }
460}