1 use anyhow
::{bail, format_err, Error}
;
2 use serde_json
::{json, Value}
;
4 use proxmox
::api
::{api, Router, Permission}
;
5 use proxmox
::tools
::fs
::{file_read_firstline, replace_file, CreateOptions}
;
7 use pbs_api_types
::{NODE_SCHEMA, TIME_ZONE_SCHEMA, PRIV_SYS_MODIFY}
;
9 fn read_etc_localtime() -> Result
<String
, Error
> {
11 if let Ok(line
) = file_read_firstline("/etc/timezone") {
12 return Ok(line
.trim().to_owned());
15 // otherwise guess from the /etc/localtime symlink
16 let link
= std
::fs
::read_link("/etc/localtime").
17 map_err(|err
| format_err
!("failed to guess timezone - {}", err
))?
;
19 let link
= link
.to_string_lossy();
20 match link
.rfind("/zoneinfo/") {
21 Some(pos
) => Ok(link
[(pos
+ 10)..].to_string()),
22 None
=> Ok(link
.to_string()),
35 description
: "Returns server time and timezone.",
38 schema
: TIME_ZONE_SCHEMA
,
42 description
: "Seconds since 1970-01-01 00:00:00 UTC.",
43 minimum
: 1_297_163_644,
47 description
: "Seconds since 1970-01-01 00:00:00 UTC. (local time)",
48 minimum
: 1_297_163_644,
53 permission
: &Permission
::Anybody
,
56 /// Read server time and time zone settings.
57 fn get_time(_param
: Value
) -> Result
<Value
, Error
> {
58 let time
= proxmox
::tools
::time
::epoch_i64();
59 let tm
= proxmox
::tools
::time
::localtime(time
)?
;
60 let offset
= tm
.tm_gmtoff
;
62 let localtime
= time
+ offset
;
65 "timezone": read_etc_localtime()?
,
67 "localtime": localtime
,
73 reload_timezone
: true,
80 schema
: TIME_ZONE_SCHEMA
,
85 permission
: &Permission
::Privilege(&["system", "time"], PRIV_SYS_MODIFY
, false),
92 ) -> Result
<Value
, Error
> {
93 let path
= std
::path
::PathBuf
::from(format
!("/usr/share/zoneinfo/{}", timezone
));
96 bail
!("No such timezone.");
99 replace_file("/etc/timezone", timezone
.as_bytes(), CreateOptions
::new())?
;
101 let _
= std
::fs
::remove_file("/etc/localtime");
103 use std
::os
::unix
::fs
::symlink
;
104 symlink(path
, "/etc/localtime")?
;
109 pub const ROUTER
: Router
= Router
::new()
110 .get(&API_METHOD_GET_TIME
)
111 .put(&API_METHOD_SET_TIMEZONE
);