api2: time: use /etc/localtime as fallback

in case there is no /etc/timezone

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-01-30 14:55:43 +01:00
parent a7cac14ed9
commit 13f8310cac
1 changed files with 19 additions and 3 deletions

View File

@ -8,10 +8,26 @@ use serde_json::{json, Value};
use chrono::prelude::*; use chrono::prelude::*;
fn read_etc_localtime() -> Result<String, Error> { fn read_etc_localtime() -> Result<String, Error> {
// use /etc/timezone
if let Ok(line) = tools::file_read_firstline("/etc/timezone") {
return Ok(line.trim().to_owned());
}
let line = tools::file_read_firstline("/etc/timezone")?; // otherwise guess from the /etc/localtime symlink
let mut buf: [u8; 64] = unsafe { std::mem::uninitialized() };
Ok(line.trim().to_owned()) let len = unsafe {
libc::readlink("/etc/localtime".as_ptr() as *const _, buf.as_mut_ptr() as *mut _, buf.len())
};
if len <= 0 {
bail!("failed to guess timezone");
}
let len = len as usize;
buf[len] = 0;
let link = std::str::from_utf8(&buf[..len])?;
match link.rfind("/zoneinfo/") {
Some(pos) => Ok(link[(pos + 10)..].to_string()),
None => Ok(link.to_string()),
}
} }
fn get_time( fn get_time(