File tree Expand file tree Collapse file tree 5 files changed +57
-39
lines changed
Expand file tree Collapse file tree 5 files changed +57
-39
lines changed Original file line number Diff line number Diff line change @@ -56,4 +56,3 @@ arrayref = "0.3.9"
5656libc = { version = " 0.2.171" , default-features = false }
5757semaphore = " 0.4.0"
5858circular-queue = " 0.2.7"
59- localzone = " 0.3.1"
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ pub mod config;
1414pub mod db;
1515pub mod handle;
1616pub mod rate_limit;
17+ pub mod tz;
1718
1819pub 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 }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments