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
use chrono::{
format::{DelayedFormat, StrftimeItems},
DateTime, Local, TimeZone, Utc,
};
#[cfg(feature = "syslog_writer")]
use chrono::{Datelike, Timelike};
use std::sync::{Arc, Mutex};
#[derive(Debug, Default)]
pub struct DeferredNow(Option<DateTime<Local>>);
impl<'a> DeferredNow {
#[must_use]
pub fn new() -> Self {
Self(None)
}
pub fn now(&'a mut self) -> &'a DateTime<Local> {
self.0.get_or_insert_with(Local::now)
}
pub fn format<'b>(&'a mut self, fmt: &'b str) -> DelayedFormat<StrftimeItems<'b>> {
if use_utc() {
Utc.from_utc_datetime(&self.now().naive_utc()).format(fmt)
} else {
self.now().format(fmt)
}
}
#[cfg(feature = "syslog_writer")]
pub(crate) fn format_rfc3339(&mut self) -> DelayedFormat<StrftimeItems<'_>> {
self.format("%Y-%m-%dT%H:%M:%S%.3fZ")
}
#[cfg(feature = "syslog_writer")]
pub(crate) fn format_rfc3164(&mut self) -> String {
let now = self.now();
let date = now.date();
let time = now.time();
format!(
"{mmm} {dd:>2} {hh:02}:{mm:02}:{ss:02}",
mmm = match date.month() {
1 => "Jan",
2 => "Feb",
3 => "Mar",
4 => "Apr",
5 => "May",
6 => "Jun",
7 => "Jul",
8 => "Aug",
9 => "Sep",
10 => "Oct",
11 => "Nov",
12 => "Dec",
_ => unreachable!(),
},
dd = date.day(),
hh = time.hour(),
mm = time.minute(),
ss = time.second()
)
}
pub fn force_utc() {
let mut guard = FORCE_UTC.lock().unwrap();
match *guard {
Some(false) => {
panic!("offset is already initialized not to enforce UTC");
}
Some(true) => {
}
None => *guard = Some(true),
}
}
}
lazy_static::lazy_static! {
static ref FORCE_UTC: Arc<Mutex<Option<bool>>> =
Arc::new(Mutex::new(None));
}
fn use_utc() -> bool {
let mut force_utc_guard = FORCE_UTC.lock().unwrap();
if let Some(true) = *force_utc_guard {
true
} else {
if force_utc_guard.is_none() {
*force_utc_guard = Some(false);
}
false
}
}
#[cfg(test)]
mod test {
#[test]
fn test_deferred_now() {
let mut deferred_now = super::DeferredNow::new();
let once = deferred_now.now().to_string();
println!("This should be the current timestamp: {}", once);
std::thread::sleep(std::time::Duration::from_millis(300));
let again = deferred_now.now().to_string();
println!("This must be the same timestamp: {}", again);
assert_eq!(once, again);
}
#[cfg(feature = "syslog_writer")]
#[test]
fn test_format_rfc3164() {
let mut deferred_now = super::DeferredNow::new();
println!("rfc3164: {}", deferred_now.format_rfc3164());
}
#[test]
#[cfg(feature = "syslog_writer")]
fn test_format_rfc3339() {
let s = super::DeferredNow::new().format_rfc3339().to_string();
let bytes = s.into_bytes();
assert_eq!(bytes[4], b'-');
assert_eq!(bytes[7], b'-');
assert_eq!(bytes[10], b'T');
assert_eq!(bytes[13], b':');
assert_eq!(bytes[16], b':');
assert_eq!(bytes[19], b'.');
assert_eq!(bytes[23], b'Z');
}
}