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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#![allow(clippy::doc_markdown, clippy::missing_errors_doc)]
use std::{
process::Command,
str,
sync::RwLock,
};
use once_cell::sync::Lazy;
use thiserror::Error;
use time::{
error::{
ComponentRange,
Format,
},
format_description::{
parse,
FormatItem,
},
Duration,
OffsetDateTime,
UtcOffset,
};
#[derive(Error, Debug)]
pub enum Error {
#[error("Unable to acquire a write lock")]
WriteLock,
#[error("Unable to acquire a read lock")]
ReadLock,
#[error("Unable to construct offset from offset hours/minutes: {0}")]
Time(#[from] ComponentRange),
#[error("Unable to format timestamp: {0}")]
TimeFormat(#[from] Format),
#[error("Invalid offset hours: {0}")]
InvalidOffsetHours(i8),
#[error("Invalid offset minutes: {0}")]
InvalidOffsetMinutes(i8),
#[error("Unable to parse offset string.")]
InvalidOffsetString,
}
static OFFSET: Lazy<RwLock<Option<UtcOffset>>> = Lazy::new(|| RwLock::new(None));
static TIME_FORMAT: Lazy<Vec<FormatItem<'static>>> = Lazy::new(|| {
parse(
"[year]-[month]-[day]T[hour]:[minute]:[second][offset_hour sign:mandatory]:[offset_second]",
)
.unwrap_or_default()
});
static PARSE_FORMAT: Lazy<Vec<FormatItem<'static>>> =
Lazy::new(|| parse("[offset_hour][offset_minute]").unwrap_or_default());
static PARSE_FORMAT_WITH_COLON: Lazy<Vec<FormatItem<'static>>> =
Lazy::new(|| parse("[offset_hour]:[offset_minute]").unwrap_or_default());
pub fn set_global_offset_from_str(input: &str) -> Result<(i8, i8), Error> {
let trimmed = trim_new_lines(input);
if let Ok(o) = UtcOffset::parse(trimmed, &PARSE_FORMAT) {
init_from_utc_offset(o)
} else if let Ok(o) = UtcOffset::parse(trimmed, &PARSE_FORMAT_WITH_COLON) {
init_from_utc_offset(o)
} else {
Err(Error::InvalidOffsetString)
}
}
#[allow(clippy::manual_range_contains)]
pub fn set_global_offset(offset_hours: i8, offset_minutes: i8) -> Result<(i8, i8), Error> {
if offset_hours < -12 || offset_hours > 14 {
Err(Error::InvalidOffsetHours(offset_hours))
} else if !(0..=59).contains(&offset_minutes) {
Err(Error::InvalidOffsetMinutes(offset_minutes))
} else {
let o = UtcOffset::from_hms(offset_hours, offset_minutes, 0)?;
init_from_utc_offset(o)
}
}
pub fn get_local_timestamp_rfc3339() -> Result<String, Error> {
get_local_timestamp_from_offset_rfc3339(get_local_offset())
}
#[allow(clippy::cast_lossless)]
pub fn get_local_timestamp_from_offset_rfc3339(utc_offset: UtcOffset) -> Result<String, Error> {
let datetime_now = OffsetDateTime::now_utc();
if utc_offset != UtcOffset::UTC {
if let Some(t) = datetime_now.checked_add(Duration::hours(utc_offset.whole_hours() as i64))
{
let offset_datetime_now = t.replace_offset(utc_offset);
return Ok(offset_datetime_now.format(&TIME_FORMAT)?);
}
}
Ok(datetime_now.format(&TIME_FORMAT)?)
}
fn init_from_utc_offset(offset: UtcOffset) -> Result<(i8, i8), Error> {
if let Ok(mut l) = OFFSET.write() {
*l = Some(offset);
} else {
log::warn!("UTC Offset failed: {}", offset);
return Err(Error::WriteLock);
}
log::info!("UTC Offset set to: {}", offset);
Ok((offset.whole_hours(), offset.minutes_past_hour()))
}
pub fn get_local_offset() -> UtcOffset {
if let Ok(reader) = OFFSET.read() {
if let Some(o) = *reader {
return o;
}
}
let offset = if let Ok(o) = time::UtcOffset::current_local_offset() {
o
} else if let Some(o) = offset_from_process() {
o
} else {
UtcOffset::UTC
};
if let Err(e) = init_from_utc_offset(offset) {
log::warn!("Unable to initialize offset: {}", e);
}
offset
}
fn process_cmd_output(stdout: &[u8], formatter: &[FormatItem<'static>]) -> Option<UtcOffset> {
match str::from_utf8(stdout) {
Ok(v) => match UtcOffset::parse(trim_new_lines(v), &formatter) {
Ok(o) => return Some(o),
Err(e) => {
log::warn!("Unable to parse output: {}", e);
},
},
Err(e) => {
log::warn!("Unable to convert output: {}", e);
},
}
None
}
fn offset_from_process() -> Option<UtcOffset> {
if cfg!(target_os = "windows") {
if let Ok(output) = Command::new("powershell")
.arg("Get-Date")
.arg("-Format")
.arg("\"K \"")
.output()
{
return process_cmd_output(&output.stdout, &PARSE_FORMAT_WITH_COLON);
}
} else if let Ok(output) = Command::new("date").arg("+%z").output() {
return process_cmd_output(&output.stdout, &PARSE_FORMAT);
}
None
}
fn trim_new_lines(s: &str) -> &str {
s.trim().trim_end_matches("\r\n").trim_matches('\n')
}