From 13f8310cac3389760ba671b24a372e983574f47f Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 30 Jan 2019 14:55:43 +0100 Subject: [PATCH] api2: time: use /etc/localtime as fallback in case there is no /etc/timezone Signed-off-by: Wolfgang Bumiller --- src/api2/node/time.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/api2/node/time.rs b/src/api2/node/time.rs index b25bfe71..0643d6d0 100644 --- a/src/api2/node/time.rs +++ b/src/api2/node/time.rs @@ -8,10 +8,26 @@ use serde_json::{json, Value}; use chrono::prelude::*; fn read_etc_localtime() -> Result { + // 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")?; - - Ok(line.trim().to_owned()) + // otherwise guess from the /etc/localtime symlink + let mut buf: [u8; 64] = unsafe { std::mem::uninitialized() }; + 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(