mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	This improves usability by helping users quickly recognize timezones with their offsets. Fixes #20988.
		
			
				
	
	
		
			47 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			47 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
#!/usr/bin/env python3
 | 
						|
import json
 | 
						|
import os
 | 
						|
import sys
 | 
						|
import zoneinfo
 | 
						|
from datetime import datetime, timedelta
 | 
						|
 | 
						|
ZULIP_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../")
 | 
						|
sys.path.insert(0, ZULIP_PATH)
 | 
						|
 | 
						|
from zerver.lib.timezone import get_canonical_timezone_map
 | 
						|
 | 
						|
OUT_PATH = os.path.join(ZULIP_PATH, "web", "generated", "timezones.json")
 | 
						|
 | 
						|
 | 
						|
def get_utc_offset(tz_name: str) -> str:
 | 
						|
    """Get UTC offset for a timezone in the format UTC(+HH:MM)."""
 | 
						|
    try:
 | 
						|
        now = datetime.now(zoneinfo.ZoneInfo(tz_name))
 | 
						|
        offset: timedelta | None = now.utcoffset()
 | 
						|
 | 
						|
        if offset is None:
 | 
						|
            return "UTC(+00:00)"
 | 
						|
 | 
						|
        offset_seconds = offset.total_seconds()
 | 
						|
        hours, remainder = divmod(offset_seconds, 3600)
 | 
						|
        minutes = remainder // 60
 | 
						|
        if minutes == 0:
 | 
						|
            return f"UTC{int(hours):+d}"
 | 
						|
        return f"UTC{int(hours):+d}:{int(minutes):02d}"
 | 
						|
 | 
						|
    except Exception as e:
 | 
						|
        print(f"Error processing {tz_name}: {e}")
 | 
						|
        return "UTC(?)"
 | 
						|
 | 
						|
 | 
						|
timezones = sorted(
 | 
						|
    zoneinfo.available_timezones() - {"Factory", "localtime"} - set(get_canonical_timezone_map())
 | 
						|
)
 | 
						|
 | 
						|
 | 
						|
timezone_data = [{"name": tz, "utc_offset": get_utc_offset(tz)} for tz in timezones]
 | 
						|
 | 
						|
 | 
						|
with open(OUT_PATH, "w") as f:
 | 
						|
    json.dump({"timezones": timezone_data}, f)
 |