]>
git.proxmox.com Git - proxmox-backup.git/blob - src/api2/node/time.rs
4 use crate::api
::schema
::*;
5 use crate::api
::router
::*;
6 use serde_json
::{json, Value}
;
8 use chrono
::prelude
::*;
10 fn read_etc_localtime() -> Result
<String
, Error
> {
12 let line
= tools
::file_read_firstline("/etc/timezone")?
;
14 Ok(line
.trim().to_owned())
20 _rpcenv
: &mut RpcEnvironment
,
21 ) -> Result
<Value
, Error
> {
23 let datetime
= Local
::now();
24 let offset
= datetime
.offset();
25 let time
= datetime
.timestamp();
26 let localtime
= time
+ (offset
.fix().local_minus_utc() as i64);
29 "timezone": read_etc_localtime()?
,
31 "localtime": localtime
,
35 extern "C" { fn tzset(); }
37 // Note:: this needs root rights ??
42 _rpcenv
: &mut RpcEnvironment
,
43 ) -> Result
<Value
, Error
> {
45 let timezone
= tools
::required_string_param(¶m
, "timezone")?
;
47 let path
= std
::path
::PathBuf
::from(format
!("/usr/share/zoneinfo/{}", timezone
));
50 bail
!("No such timezone.");
53 tools
::file_set_contents("/etc/timezone", timezone
.as_bytes(), None
)?
;
55 let _
= std
::fs
::remove_file("/etc/localtime");
57 use std
::os
::unix
::fs
::symlink
;
58 symlink(path
, "/etc/localtime")?
;
65 pub fn router() -> Router
{
67 let route
= Router
::new()
71 ObjectSchema
::new("Read server time and time zone settings.")
73 ObjectSchema
::new("Returns server time and timezone.")
74 .required("timezone", StringSchema
::new("Time zone"))
75 .required("time", IntegerSchema
::new("Seconds since 1970-01-01 00:00:00 UTC.")
77 .required("localtime", IntegerSchema
::new("Seconds since 1970-01-01 00:00:00 UTC. (local time)")
84 ObjectSchema
::new("Set time zone.")
85 .required("timezone", StringSchema
::new("Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names."))