api2/node/time.rs: implement set timezone
This commit is contained in:
parent
cd8b91f78c
commit
e6ffeb918c
|
@ -28,20 +28,54 @@ fn get_time(_param: Value, _info: &ApiMethod) -> Result<Value, Error> {
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern "C" { fn tzset(); }
|
||||||
|
|
||||||
|
// Note:: this needs root rights ??
|
||||||
|
|
||||||
|
fn set_timezone(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
|
||||||
|
|
||||||
|
let timezone = tools::required_string_param(¶m, "timezone")?;
|
||||||
|
|
||||||
|
let path = std::path::PathBuf::from(format!("/usr/share/zoneinfo/{}", timezone));
|
||||||
|
|
||||||
|
if !path.exists() {
|
||||||
|
bail!("No such timezone.");
|
||||||
|
}
|
||||||
|
|
||||||
|
tools::file_set_contents("/etc/timezone", timezone.as_bytes(), None);
|
||||||
|
|
||||||
|
let _ = std::fs::remove_file("/etc/localtime");
|
||||||
|
|
||||||
|
use std::os::unix::fs::symlink;
|
||||||
|
symlink(path, "/etc/localtime")?;
|
||||||
|
|
||||||
|
unsafe { tzset() };
|
||||||
|
|
||||||
|
Ok(Value::Null)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn router() -> Router {
|
pub fn router() -> Router {
|
||||||
|
|
||||||
let route = Router::new()
|
let route = Router::new()
|
||||||
.get(
|
.get(
|
||||||
ApiMethod::new(
|
ApiMethod::new(
|
||||||
get_time, ObjectSchema::new("Read server time and time zone settings."))
|
get_time,
|
||||||
.returns(
|
ObjectSchema::new("Read server time and time zone settings.")
|
||||||
ObjectSchema::new("Returns server time and timezone.")
|
).returns(
|
||||||
.required("timezone", StringSchema::new("Time zone"))
|
ObjectSchema::new("Returns server time and timezone.")
|
||||||
.required("time", IntegerSchema::new("Seconds since 1970-01-01 00:00:00 UTC.")
|
.required("timezone", StringSchema::new("Time zone"))
|
||||||
.minimum(1297163644))
|
.required("time", IntegerSchema::new("Seconds since 1970-01-01 00:00:00 UTC.")
|
||||||
.required("localtime", IntegerSchema::new("Seconds since 1970-01-01 00:00:00 UTC. (local time)")
|
.minimum(1297163644))
|
||||||
.minimum(1297163644))
|
.required("localtime", IntegerSchema::new("Seconds since 1970-01-01 00:00:00 UTC. (local time)")
|
||||||
)
|
.minimum(1297163644))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.put(
|
||||||
|
ApiMethod::new(
|
||||||
|
set_timezone,
|
||||||
|
ObjectSchema::new("Set time zone.")
|
||||||
|
.required("timezone", StringSchema::new("Time zone. The file '/usr/share/zoneinfo/zone.tab' contains the list of valid names."))
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue