Skip to content

Commit ef7afaf

Browse files
committed
Added handling of TZ=:/etc/localtime
1 parent 36397d3 commit ef7afaf

1 file changed

Lines changed: 32 additions & 18 deletions

File tree

‎timedesc-tzlocal/unix/timedesc_tzlocal.ml‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,36 @@ let local () : string list =
1818
(* Approach copied from Python package tzlocal:
1919
https://github.com/regebro/tzlocal
2020
*)
21+
let etc_localtime = "/etc/localtime" in
22+
let process_etc_localtime_symlink () =
23+
(* systemd distributions use symlinks that include the zone name *)
24+
try
25+
let real_path = Unix.readlink etc_localtime in
26+
let parts = String.split_on_char '/' real_path in
27+
let combinations, _ =
28+
List.fold_left
29+
(fun (acc, parts) _ -> (parts :: acc, List.tl parts))
30+
([], parts) parts
31+
in
32+
List.map (String.concat "/") combinations
33+
with
34+
| _ -> []
35+
in
2136
match Sys.getenv_opt "TZ" with
22-
| Some name -> [ name ]
37+
| Some name -> (
38+
let name =
39+
if String.starts_with ~prefix:":" name then (
40+
String.sub name 1 (String.length name - 1)
41+
) else (
42+
name
43+
)
44+
in
45+
if name = etc_localtime then (
46+
process_etc_localtime_symlink ()
47+
) else (
48+
[ name ]
49+
)
50+
)
2351
| None -> (
2452
if Sys.file_exists "/system/bin/getprop" then (
2553
(* if we are under Termux on Android *)
@@ -109,20 +137,6 @@ let local () : string list =
109137
in
110138
match try2 with
111139
| Some name -> [ name ]
112-
| None ->
113-
(* systemd distributions use symlinks that include the zone name *)
114-
let try3 =
115-
try
116-
let file = "/etc/localtime" in
117-
let real_path = Unix.readlink file in
118-
let parts = String.split_on_char '/' real_path in
119-
let combinations, _ =
120-
List.fold_left
121-
(fun (acc, parts) _ -> (parts :: acc, List.tl parts))
122-
([], parts) parts
123-
in
124-
List.map (String.concat "/") combinations
125-
with
126-
| _ -> []
127-
in
128-
try3))
140+
| None -> process_etc_localtime_symlink ()
141+
)
142+
)

0 commit comments

Comments
 (0)