Skip to content

Commit da82378

Browse files
committed
implement custom time zone getter
1 parent 42bde2b commit da82378

File tree

5 files changed

+57
-39
lines changed

5 files changed

+57
-39
lines changed

Cargo.lock

Lines changed: 1 addition & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,3 @@ arrayref = "0.3.9"
5656
libc = { version = "0.2.171", default-features = false }
5757
semaphore = "0.4.0"
5858
circular-queue = "0.2.7"
59-
localzone = "0.3.1"

src/server/db.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ impl DatabaseClient {
4646
Ok(Self(pool))
4747
}
4848

49-
pub async fn setup_timezone(&self, tz: String) -> Result<(), Error> {
50-
let supported_tz = self.get_supported_time_zones().await?;
51-
if !supported_tz.contains(&tz) {
49+
pub async fn setup_timezone(&self, tz: &str) -> Result<(), Error> {
50+
if !self.validate_timezone(tz).await? {
5251
error!("Timezone \"{tz}\" is not supported by database, leaving defaults");
5352
return Ok(());
5453
}
@@ -269,4 +268,10 @@ impl DatabaseClient {
269268

270269
Ok(names)
271270
}
271+
272+
async fn validate_timezone<S: PartialEq<String>>(&self, tz: S) -> Result<bool, Error> {
273+
let supported = self.get_supported_time_zones().await?;
274+
275+
Ok(supported.iter().any(|candidate| tz.eq(candidate)))
276+
}
272277
}

src/server/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub mod config;
1414
pub mod db;
1515
pub mod handle;
1616
pub mod rate_limit;
17+
pub mod tz;
1718

1819
pub async fn main(config: Config) {
1920
let config = Arc::new(config);
@@ -32,10 +33,10 @@ pub async fn main(config: Config) {
3233
.database
3334
.timezone
3435
.clone()
35-
.or(localzone::get_local_zone())
36+
.or(tz::get_system_timezone())
3637
{
37-
Some(tz) => match db.setup_timezone(tz).await {
38-
Ok(()) => info!("Timezone updated successfully"),
38+
Some(tz) => match db.setup_timezone(&tz).await {
39+
Ok(()) => info!("Timezone updated to \"{tz}\" successfully"),
3940
Err(why) => {
4041
error!("Failed to set time zone: {why}");
4142
}

src/server/tz.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use log::debug;
2+
use std::{fs, process::Command};
3+
4+
pub fn get_system_timezone() -> Option<String> {
5+
get_timezone_with_timedatectl().or(get_timezone_with_etc_localtime())
6+
}
7+
8+
fn get_timezone_with_timedatectl() -> Option<String> {
9+
debug!("Attempting to get timezone with 'timedatectl'");
10+
get_command_output(Command::new("timedatectl").args(["show", "-P", "Timezone"]))
11+
}
12+
13+
fn get_timezone_with_etc_localtime() -> Option<String> {
14+
debug!("Attempting to get timezone from '/etc/localtime'");
15+
16+
let target = fs::read_link("/etc/localtime").ok()?;
17+
let second = target.file_name()?;
18+
let first = target.parent()?.file_name()?;
19+
20+
Some(format!("{}/{}", first.to_str()?, second.to_str()?))
21+
}
22+
23+
fn get_command_output(cmd: &mut Command) -> Option<String> {
24+
let mut out = String::from_utf8(cmd.output().ok()?.stdout).ok()?;
25+
out.pop();
26+
27+
Some(out)
28+
}
29+
30+
#[cfg(test)]
31+
mod tests {
32+
use super::get_timezone_with_timedatectl;
33+
use crate::server::tz::get_timezone_with_etc_localtime;
34+
35+
#[test]
36+
fn check_timedatectl_method() {
37+
assert!(get_timezone_with_timedatectl().is_some());
38+
}
39+
40+
#[test]
41+
fn check_etc_localtime() {
42+
assert!(get_timezone_with_etc_localtime().is_some())
43+
}
44+
}

0 commit comments

Comments
 (0)