proxmox/src/tools/time.rs: add epoch_to_rfc_3339

This commit is contained in:
Dietmar Maurer 2020-09-13 10:51:27 +02:00
parent fb4e426a0a
commit 3aa8e22b0c

View File

@ -122,3 +122,30 @@ pub fn epoch_to_rfc_3339_utc(epoch: i64) -> Result<String, Error> {
let gmtime = gmtime(epoch)?;
strftime("%FT%TZ", &gmtime)
}
/// Convert Unix epoch into RFC3339 local time with TZ
pub fn epoch_to_rfc_3339(epoch: i64) -> Result<String, Error> {
let localtime = localtime(epoch)?;
// Note: We cannot use strftime %z because of missing collon
let mut offset = localtime.tm_gmtoff;
let prefix = if offset < 0 {
offset = -offset;
'-'
} else {
'+'
};
let mins = offset / 60;
let hours = mins / 60;
let mins = mins % 60;
let mut s = strftime("%FT%T", &localtime)?;
s.push(prefix);
s.push_str(&format!("{:02}:{:02}", hours, mins));
Ok(s)
}