"""Writes res/raw/zones.txt: one "Area/City|lat|lon" line per time zone, from the system zone.tab. Luma estimates sunrise and sunset at the time zone's main city, so it needs no location permission.""" import pathlib, re, sys source = pathlib.Path(sys.argv[1] if len(sys.argv) > 1 else '/usr/share/zoneinfo/zone.tab') out = pathlib.Path(__file__).resolve().parent.parent / 'res' / 'raw' / 'zones.txt' def degrees(text): sign = -1 if text[0] == '-' else 1 digits = text[1:] whole = 2 if len(digits) in (4, 6) else 3 d, rest = int(digits[:whole]), digits[whole:] m = int(rest[:2]); s = int(rest[2:4]) if len(rest) >= 4 else 0 return sign * (d + m / 60 + s / 3600) lines = ['# Time zone|latitude|longitude, generated by tools/zones.py from zone.tab (public domain).'] for row in source.read_text().splitlines(): if row.startswith('#') or not row.strip(): continue cols = row.split('\t') lat, lon = re.match(r'([+-]\d+)([+-]\d+)', cols[1]).groups() lines.append(f'{cols[2]}|{degrees(lat):.2f}|{degrees(lon):.2f}') out.write_text('\n'.join(lines) + '\n') print(out, len(lines) - 1, 'zones')