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 {
29 Timestamp {
30 time: SystemTime::now(),
31 precision: TimestampPrecision::Seconds,
32 }
33 }
34
35 pub fn timestamp_seconds(&self) -> Timestamp {
38 Timestamp {
39 time: SystemTime::now(),
40 precision: TimestampPrecision::Seconds,
41 }
42 }
43
44 pub fn timestamp_millis(&self) -> Timestamp {
47 Timestamp {
48 time: SystemTime::now(),
49 precision: TimestampPrecision::Millis,
50 }
51 }
52
53 pub fn timestamp_micros(&self) -> Timestamp {
56 Timestamp {
57 time: SystemTime::now(),
58 precision: TimestampPrecision::Micros,
59 }
60 }
61
62 pub fn timestamp_nanos(&self) -> Timestamp {
65 Timestamp {
66 time: SystemTime::now(),
67 precision: TimestampPrecision::Nanos,
68 }
69 }
70}
71
72pub struct Timestamp {
79 time: SystemTime,
80 precision: TimestampPrecision,
81}
82
83impl fmt::Debug for Timestamp {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 struct TimestampValue<'a>(&'a Timestamp);
87
88 impl fmt::Debug for TimestampValue<'_> {
89 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90 fmt::Display::fmt(&self.0, f)
91 }
92 }
93
94 f.debug_tuple("Timestamp")
95 .field(&TimestampValue(self))
96 .finish()
97 }
98}
99
100impl fmt::Display for Timestamp {
101 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102 let formatter = match self.precision {
103 TimestampPrecision::Seconds => format_rfc3339_seconds,
104 TimestampPrecision::Millis => format_rfc3339_millis,
105 TimestampPrecision::Micros => format_rfc3339_micros,
106 TimestampPrecision::Nanos => format_rfc3339_nanos,
107 };
108
109 formatter(self.time).fmt(f)
110 }
111}