env_logger/fmt/
humantime.rs1use std::fmt;
2use std::time::SystemTime;
3
4use humantime::{
5 format_rfc3339_micros, format_rfc3339_millis, format_rfc3339_nanos, format_rfc3339_seconds,
6};
7
8use crate::fmt::{Formatter, TimestampPrecision};
9
10impl Formatter {
11 pub fn timestamp(&self) -> Timestamp {
31 Timestamp {
32 time: SystemTime::now(),
33 precision: TimestampPrecision::Seconds,
34 }
35 }
36
37 pub fn timestamp_seconds(&self) -> Timestamp {
40 Timestamp {
41 time: SystemTime::now(),
42 precision: TimestampPrecision::Seconds,
43 }
44 }
45
46 pub fn timestamp_millis(&self) -> Timestamp {
49 Timestamp {
50 time: SystemTime::now(),
51 precision: TimestampPrecision::Millis,
52 }
53 }
54
55 pub fn timestamp_micros(&self) -> Timestamp {
58 Timestamp {
59 time: SystemTime::now(),
60 precision: TimestampPrecision::Micros,
61 }
62 }
63
64 pub fn timestamp_nanos(&self) -> Timestamp {
67 Timestamp {
68 time: SystemTime::now(),
69 precision: TimestampPrecision::Nanos,
70 }
71 }
72}
73
74pub struct Timestamp {
82 time: SystemTime,
83 precision: TimestampPrecision,
84}
85
86impl fmt::Debug for Timestamp {
87 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
88 struct TimestampValue<'a>(&'a Timestamp);
90
91 impl<'a> fmt::Debug for TimestampValue<'a> {
92 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
93 fmt::Display::fmt(&self.0, f)
94 }
95 }
96
97 f.debug_tuple("Timestamp")
98 .field(&TimestampValue(self))
99 .finish()
100 }
101}
102
103impl fmt::Display for Timestamp {
104 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105 let formatter = match self.precision {
106 TimestampPrecision::Seconds => format_rfc3339_seconds,
107 TimestampPrecision::Millis => format_rfc3339_millis,
108 TimestampPrecision::Micros => format_rfc3339_micros,
109 TimestampPrecision::Nanos => format_rfc3339_nanos,
110 };
111
112 formatter(self.time).fmt(f)
113 }
114}