4 use crate::api_schema
::*;
5 use crate::api_schema
::router
::*;
6 use serde_json
::{json, Value}
;
8 use chrono
::prelude
::*;
10 fn read_etc_localtime() -> Result
<String
, Error
> {
12 if let Ok(line
) = tools
::file_read_firstline("/etc/timezone") {
13 return Ok(line
.trim().to_owned());
16 // otherwise guess from the /etc/localtime symlink
17 let mut buf
: [u8; 64] = unsafe { std::mem::uninitialized() }
;
19 libc
::readlink("/etc/localtime".as_ptr() as *const _
, buf
.as_mut_ptr() as *mut _
, buf
.len())
22 bail
!("failed to guess timezone");
24 let len
= len
as usize;
26 let link
= std
::str::from_utf8(&buf
[..len
])?
;
27 match link
.rfind("/zoneinfo/") {
28 Some(pos
) => Ok(link
[(pos
+ 10)..].to_string()),
29 None
=> Ok(link
.to_string()),
36 _rpcenv
: &mut RpcEnvironment
,
37 ) -> Result
<Value
, Error
> {
39 let datetime
= Local
::now();
40 let offset
= datetime
.offset();
41 let time
= datetime
.timestamp();
42 let localtime
= time
+ (offset
.fix().local_minus_utc() as i64);
45 "timezone": read_etc_localtime()?
,
47 "localtime": localtime
,
54 _rpcenv
: &mut RpcEnvironment
,
55 ) -> Result
<Value
, Error
> {
57 let timezone
= tools
::required_string_param(¶m
, "timezone")?
;
59 let path
= std
::path
::PathBuf
::from(format
!("/usr/share/zoneinfo/{}", timezone
));
62 bail
!("No such timezone.");
65 tools
::file_set_contents("/etc/timezone", timezone
.as_bytes(), None
)?
;
67 let _
= std
::fs
::remove_file("/etc/localtime");
69 use std
::os
::unix
::fs
::symlink
;
70 symlink(path
, "/etc/localtime")?
;
75 pub fn router() -> Router
{
77 let route
= Router
::new()
81 ObjectSchema
::new("Read server time and time zone settings.")
82 .required("node", crate::api2
::node
::NODE_SCHEMA
.clone())
84 ObjectSchema
::new("Returns server time and timezone.")
85 .required("timezone", StringSchema
::new("Time zone"))
86 .required("time", IntegerSchema
::new("Seconds since 1970-01-01 00:00:00 UTC.")
88 .required("localtime", IntegerSchema
::new("Seconds since 1970-01-01 00:00:00 UTC. (local time)")
95 ObjectSchema
::new("Set time zone.")
96 .required("node", crate::api2
::node
::NODE_SCHEMA
.clone())
97 .required("timezone", StringSchema
::new(
98 "Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names."))
99 ).protected(true).reload_timezone(true)