env_logger/fmt/humantime/
extern_impl.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
10pub(in crate::fmt) mod glob {
11 pub use super::*;
12}
13
14impl Formatter {
15 pub fn timestamp(&self) -> Timestamp {
35 Timestamp {
36 time: SystemTime::now(),
37 precision: TimestampPrecision::Seconds,
38 }
39 }
40
41 pub fn timestamp_seconds(&self) -> Timestamp {
44 Timestamp {
45 time: SystemTime::now(),
46 precision: TimestampPrecision::Seconds,
47 }
48 }
49
50 pub fn timestamp_millis(&self) -> Timestamp {
53 Timestamp {
54 time: SystemTime::now(),
55 precision: TimestampPrecision::Millis,
56 }
57 }
58
59 pub fn timestamp_micros(&self) -> Timestamp {
62 Timestamp {
63 time: SystemTime::now(),
64 precision: TimestampPrecision::Micros,
65 }
66 }
67
68 pub fn timestamp_nanos(&self) -> Timestamp {
71 Timestamp {
72 time: SystemTime::now(),
73 precision: TimestampPrecision::Nanos,
74 }
75 }
76}
77
78pub struct Timestamp {
86 time: SystemTime,
87 precision: TimestampPrecision,
88}
89
90impl fmt::Debug for Timestamp {
91 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92 struct TimestampValue<'a>(&'a Timestamp);
94
95 impl<'a> fmt::Debug for TimestampValue<'a> {
96 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
97 fmt::Display::fmt(&self.0, f)
98 }
99 }
100
101 f.debug_tuple("Timestamp")
102 .field(&TimestampValue(&self))
103 .finish()
104 }
105}
106
107impl fmt::Display for Timestamp {
108 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
109 let formatter = match self.precision {
110 TimestampPrecision::Seconds => format_rfc3339_seconds,
111 TimestampPrecision::Millis => format_rfc3339_millis,
112 TimestampPrecision::Micros => format_rfc3339_micros,
113 TimestampPrecision::Nanos => format_rfc3339_nanos,
114 };
115
116 formatter(self.time).fmt(f)
117 }
118}