1- #!/usr/bin/python3
1+ #!/usr/bin/env python3
22
33import re
4+ from argparse import ArgumentParser
5+ from pathlib import Path
6+
7+ COORDS_RE = re .compile (r"([+-])([0-9]+)([+-])([0-9]+)" )
48
59d = {}
610
11+ parser = ArgumentParser (prog = 'generate-tz-header' ,
12+ description = 'Generate tz-coords.h header from timezone-data' )
13+ parser .add_argument ('-i' , '--zone_tab' , nargs = '?' , default = '/usr/share/zoneinfo/zone.tab' , type = Path )
14+ parser .add_argument ('-o' , '--out_file' , nargs = '?' , default = 'tz-coords.h' , type = Path )
15+ args = parser .parse_args ()
716
8- with open ("/usr/share/zoneinfo/zone.tab" , "r" ) as f :
17+ with open (args . zone_tab , "r" ) as f :
918 for line in f :
10- if line .startswith ("#" ):
19+ line = line .strip ()
20+ if not line or line .startswith ("#" ):
1121 continue
1222
13- res = re .search (r"([A-Z]{2})\s([0-9-+]+)\s([\w/_\-]+)\s" , line )
14- code , coords , tz = res .groups ()
15-
16- res = re .search (r"([+-]{1})([0-9]+)([+-]{1})([0-9]+)" , coords )
17- lat_sign , lat_val , long_sign , long_val = res .groups ()
23+ coords , tz = line .split ('\t ' )[1 :3 ]
24+ lat_sign , lat_val , long_sign , long_val = COORDS_RE .search (coords ).groups ()
1825
1926 lat_str = lat_sign + lat_val [0 :2 ] + "." + lat_val [2 :]
2027 long_str = long_sign + long_val [0 :3 ] + "." + long_val [3 :]
2532 d [tz ] = [lat , long ]
2633
2734header = """
28- // Generated from /usr/share/zoneinfo/zone.tab , used by csd-nightlight.c to calculate sunrise and sunset based on the system timezone
35+ // Generated from %s , used by csd-nightlight.c to calculate sunrise and sunset based on the system timezone
2936
3037typedef struct
3138{
32- const char *timezone;
39+ const gchar *timezone;
3340 double latitude;
3441 double longitude;
3542} TZCoords;
3643
3744static TZCoords tz_coord_list[] = {
38- """
45+ """ % ( args . zone_tab )
3946
4047for zone in sorted (d .keys ()):
4148 latitude , longitude = d [zone ]
4451
4552header += "};"
4653
47- with open ("tz-coords.h" , "w" ) as f :
54+ with open (args . out_file , "w" ) as f :
4855 f .write (header )
4956
50- quit ()
57+ quit ()
0 commit comments