From 0463602a791a6303319a6dfd69a153f52cbfc793 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 23 Jan 2019 15:14:14 +0100 Subject: [PATCH] api2/node/time.rs: implement read api --- src/api2/node/time.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/api2/node/time.rs b/src/api2/node/time.rs index 1714cdc1..b5f8a860 100644 --- a/src/api2/node/time.rs +++ b/src/api2/node/time.rs @@ -5,13 +5,34 @@ use crate::api::schema::*; use crate::api::router::*; use serde_json::{json, Value}; +use chrono::prelude::*; + +fn read_etc_localtime() -> Result { + + let file = std::fs::File::open("/etc/timezone")?; + + use std::io::{BufRead, BufReader}; + + let mut reader = BufReader::new(file); + + let mut line = String::new(); + + let _ = reader.read_line(&mut line)?; + + Ok(line.trim().to_owned()) +} fn get_time(_param: Value, _info: &ApiMethod) -> Result { + let datetime = Local::now(); + let offset = datetime.offset(); + let time = datetime.timestamp(); + let localtime = time + (offset.fix().local_minus_utc() as i64); + Ok(json!({ - "timezone": "Europe/Vienna", - "time": 1297163644, - "localtime": 1297163644, + "timezone": read_etc_localtime()?, + "time": time, + "localtime": localtime, })) }