1 use anyhow
::{bail, format_err, Error}
;
2 use serde_json
::{json, Value}
;
4 use proxmox_sys
::fs
::{file_read_firstline, replace_file, CreateOptions}
;
5 use proxmox_router
::{Router, Permission}
;
6 use proxmox_schema
::api
;
8 use pbs_api_types
::{NODE_SCHEMA, TIME_ZONE_SCHEMA, PRIV_SYS_MODIFY}
;
10 fn read_etc_localtime() -> Result
<String
, Error
> {
12 if let Ok(line
) = file_read_firstline("/etc/timezone") {
13 return Ok(line
.trim().to_owned());
16 // otherwise guess from the /etc/localtime symlink
17 let link
= std
::fs
::read_link("/etc/localtime").
18 map_err(|err
| format_err
!("failed to guess timezone - {}", err
))?
;
20 let link
= link
.to_string_lossy();
21 match link
.rfind("/zoneinfo/") {
22 Some(pos
) => Ok(link
[(pos
+ 10)..].to_string()),
23 None
=> Ok(link
.to_string()),
36 description
: "Returns server time and timezone.",
39 schema
: TIME_ZONE_SCHEMA
,
43 description
: "Seconds since 1970-01-01 00:00:00 UTC.",
44 minimum
: 1_297_163_644,
48 description
: "Seconds since 1970-01-01 00:00:00 UTC. (local time)",
49 minimum
: 1_297_163_644,
54 permission
: &Permission
::Anybody
,
57 /// Read server time and time zone settings.
58 fn get_time(_param
: Value
) -> Result
<Value
, Error
> {
59 let time
= proxmox_time
::epoch_i64();
60 let tm
= proxmox_time
::localtime(time
)?
;
61 let offset
= tm
.tm_gmtoff
;
63 let localtime
= time
+ offset
;
66 "timezone": read_etc_localtime()?
,
68 "localtime": localtime
,
74 reload_timezone
: true,
81 schema
: TIME_ZONE_SCHEMA
,
86 permission
: &Permission
::Privilege(&["system", "time"], PRIV_SYS_MODIFY
, false),
93 ) -> Result
<Value
, Error
> {
94 let path
= std
::path
::PathBuf
::from(format
!("/usr/share/zoneinfo/{}", timezone
));
97 bail
!("No such timezone.");
100 replace_file("/etc/timezone", timezone
.as_bytes(), CreateOptions
::new(), true)?
;
102 let _
= std
::fs
::remove_file("/etc/localtime");
104 use std
::os
::unix
::fs
::symlink
;
105 symlink(path
, "/etc/localtime")?
;
110 pub const ROUTER
: Router
= Router
::new()
111 .get(&API_METHOD_GET_TIME
)
112 .put(&API_METHOD_SET_TIMEZONE
);