blob: 1062a8b2449ac9ce7e16002c034f6208ffb3518b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
use crate::log::*;
use std::fs;
pub fn set_timezone(timezone: Option<String>) -> Result<(), Box<dyn std::error::Error>> {
let tz = match timezone {
Some(tz) => tz,
None => {
if let Ok(content) = fs::read_to_string("/etc/timezone") {
content.trim().to_string()
} else if let Ok(link_info) = fs::read_link("/etc/localtime") {
let path_str = link_info.to_string_lossy();
if path_str.starts_with("/usr/share/zoneinfo/") {
let tz_part = path_str.strip_prefix("/usr/share/zoneinfo/").unwrap_or("");
tz_part.to_string()
} else {
"UTC".to_string()
}
} else {
"UTC".to_string()
}
}
};
let tz_file_path = format!("/usr/share/zoneinfo/{}", tz);
if !std::path::Path::new(&tz_file_path).exists() {
log_warning(&format!(
"Timezone '{}' not found in zoneinfo, using UTC",
tz
));
let result = set_system_timezone_to_utc();
if let Err(e) = result {
log_critical_error(&format!("Failed to set timezone to UTC: {}", e));
return Err(e);
}
return Ok(());
}
if std::path::Path::new("/etc/localtime").exists() {
std::fs::remove_file("/etc/localtime")?;
}
std::os::unix::fs::symlink(&tz_file_path, "/etc/localtime")?;
log_success(&format!("Timezone set to {}", tz));
unsafe {
std::env::set_var("TZ", &tz);
}
Ok(())
}
fn set_system_timezone_to_utc() -> Result<(), Box<dyn std::error::Error>> {
let utc_tz_path = "/usr/share/zoneinfo/Etc/UTC";
if std::path::Path::new("/etc/localtime").exists() {
std::fs::remove_file("/etc/localtime")?;
}
std::os::unix::fs::symlink(utc_tz_path, "/etc/localtime")?;
log_success("Timezone set to UTC");
unsafe {
std::env::set_var("TZ", "UTC");
}
Ok(())
}
|