From c2a3966f371e249bbc3f5a2ed244c9f4aeb36191 Mon Sep 17 00:00:00 2001 From: snowykami Date: Sat, 5 Oct 2024 03:21:01 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E4=BF=AE=E5=A4=8D=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E6=97=B6=E9=97=B4=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 1 + server_status/api.py | 5 ++ server_status/timezone.py | 136 ++++++++++++++++++++++++++++++++++++++ test_tz.py | 3 + 4 files changed, 145 insertions(+) create mode 100644 server_status/timezone.py create mode 100644 test_tz.py diff --git a/pyproject.toml b/pyproject.toml index f6af2bd..f995eb5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ dependencies = [ "requests>=2.32.3", "psutil>=6.0.0", "arclet-alconna>=1.8.30", + "pytz>=2024.2", ] requires-python = ">=3.10" readme = "README.md" diff --git a/server_status/api.py b/server_status/api.py index 56ccf05..6e6b4fa 100644 --- a/server_status/api.py +++ b/server_status/api.py @@ -6,6 +6,8 @@ from typing import Any import psutil import requests +from server_status.timezone import get_timezone + excluded_partition_prefix = ("/var", "/boot", "/run", "/proc", "/sys", "/dev", "/tmp", "/snap") @@ -39,6 +41,8 @@ class Hardware: disks: dict[str, dict[str, int]] = {} + timezone: str = get_timezone() + net_up: int = 0 net_down: int = 0 net_type: str = "ethernet" @@ -190,6 +194,7 @@ class Client: "start_time": int(self.start_time), # 系统启动的时间 "link": self.link, "observed_at": int(time.time()), + "timezone": self.hardware.timezone, }, "hardware": { "mem": { diff --git a/server_status/timezone.py b/server_status/timezone.py new file mode 100644 index 0000000..8ca149c --- /dev/null +++ b/server_status/timezone.py @@ -0,0 +1,136 @@ +import os +import platform +from zoneinfo import ZoneInfo + +windows_timezone_map = { + "Dateline Standard Time": "Etc/GMT+12", + "UTC-11": "Etc/GMT+11", + "Aleutian Standard Time": "America/Adak", + "Hawaiian Standard Time": "Pacific/Honolulu", + "Marquesas Standard Time": "Pacific/Marquesas", + "Alaskan Standard Time": "America/Anchorage", + "UTC-09": "Etc/GMT+9", + "Pacific Standard Time (Mexico)": "America/Tijuana", + "UTC-08": "Etc/GMT+8", + "Pacific Standard Time": "America/Los_Angeles", + "US Mountain Standard Time": "America/Phoenix", + "Mountain Standard Time (Mexico)": "America/Chihuahua", + "Mountain Standard Time": "America/Denver", + "Central America Standard Time": "America/Guatemala", + "Central Standard Time": "America/Chicago", + "Easter Island Standard Time": "Pacific/Easter", + "Central Standard Time (Mexico)": "America/Mexico_City", + "Canada Central Standard Time": "America/Regina", + "SA Pacific Standard Time": "America/Bogota", + "Eastern Standard Time (Mexico)": "America/Cancun", + "Eastern Standard Time": "America/New_York", + "Haiti Standard Time": "America/Port-au-Prince", + "Cuba Standard Time": "America/Havana", + "US Eastern Standard Time": "America/Indianapolis", + "Turks And Caicos Standard Time": "America/Grand_Turk", + "Paraguay Standard Time": "America/Asuncion", + "Atlantic Standard Time": "America/Halifax", + "Venezuela Standard Time": "America/Caracas", + "Central Brazilian Standard Time": "America/Cuiaba", + "SA Western Standard Time": "America/La_Paz", + "Pacific SA Standard Time": "America/Santiago", + "SA Eastern Standard Time": "America/Cayenne", + "Argentina Standard Time": "America/Buenos_Aires", + "Greenland Standard Time": "America/Godthab", + "Montevideo Standard Time": "America/Montevideo", + "Bahia Standard Time": "America/Bahia", + "UTC-02": "Etc/GMT+2", + "Azores Standard Time": "Atlantic/Azores", + "Cape Verde Standard Time": "Atlantic/Cape_Verde", + "UTC": "Etc/GMT", + "GMT Standard Time": "Europe/London", + "Greenwich Standard Time": "Atlantic/Reykjavik", + "W. Europe Standard Time": "Europe/Berlin", + "Central Europe Standard Time": "Europe/Budapest", + "Romance Standard Time": "Europe/Paris", + "Central European Standard Time": "Europe/Warsaw", + "W. Central Africa Standard Time": "Africa/Lagos", + "Namibia Standard Time": "Africa/Windhoek", + "GTB Standard Time": "Europe/Bucharest", + "Middle East Standard Time": "Asia/Beirut", + "Egypt Standard Time": "Africa/Cairo", + "Syria Standard Time": "Asia/Damascus", + "E. Europe Standard Time": "Europe/Chisinau", + "South Africa Standard Time": "Africa/Johannesburg", + "FLE Standard Time": "Europe/Kiev", + "Turkey Standard Time": "Europe/Istanbul", + "Israel Standard Time": "Asia/Jerusalem", + "Jordan Standard Time": "Asia/Amman", + "Arabic Standard Time": "Asia/Riyadh", + "Kaliningrad Standard Time": "Europe/Kaliningrad", + "Arab Standard Time": "Asia/Riyadh", + "E. Africa Standard Time": "Africa/Nairobi", + "Iran Standard Time": "Asia/Tehran", + "Arabian Standard Time": "Asia/Dubai", + "Astrakhan Standard Time": "Europe/Astrakhan", + "Russian Standard Time": "Europe/Moscow", + "E. Europe Standard Time": "Europe/Chisinau", + "W. Australia Standard Time": "Australia/Perth", + "Moscow Standard Time": "Europe/Moscow", + "Pakistan Standard Time": "Asia/Karachi", + "India Standard Time": "Asia/Kolkata", + "Sri Lanka Standard Time": "Asia/Colombo", + "Nepal Standard Time": "Asia/Kathmandu", + "Bangladesh Standard Time": "Asia/Dhaka", + "Afghanistan Standard Time": "Asia/Kabul", + "Myanmar Standard Time": "Asia/Yangon", + "SE Asia Standard Time": "Asia/Bangkok", + "North Asia Standard Time": "Asia/Krasnoyarsk", + "China Standard Time": "Asia/Shanghai", + "Singapore Standard Time": "Asia/Singapore", + "W. Australia Standard Time": "Australia/Perth", + "Taipei Standard Time": "Asia/Taipei", + "Ulaanbaatar Standard Time": "Asia/Ulaanbaatar", + "North Asia East Standard Time": "Asia/Irkutsk", + "Korea Standard Time": "Asia/Seoul", + "Tokyo Standard Time": "Asia/Tokyo", + "Yakutsk Standard Time": "Asia/Yakutsk", + "Cen. Australia Standard Time": "Australia/Adelaide", + "AUS Central Standard Time": "Australia/Darwin", + "E. Australia Standard Time": "Australia/Brisbane", + "AUS Eastern Standard Time": "Australia/Sydney", + "West Pacific Standard Time": "Pacific/Port_Moresby", + "Tasmania Standard Time": "Australia/Hobart", + "Magadan Standard Time": "Asia/Magadan", + "Vladivostok Standard Time": "Asia/Vladivostok", + "Russia Time Zone 10": "Asia/Srednekolymsk", + "Central Pacific Standard Time": "Pacific/Guadalcanal", + "Fiji Standard Time": "Pacific/Fiji", + "New Zealand Standard Time": "Pacific/Auckland", + "UTC+12": "Etc/GMT-12", + "Kamchatka Standard Time": "Asia/Kamchatka", + "Tonga Standard Time": "Pacific/Tongatapu", + "Samoa Standard Time": "Pacific/Apia", + "Line Islands Standard Time": "Pacific/Kiritimati", +} + + +def get_timezone() -> str: + try: + # 尝试获取系统的本地时区 + if 'TZ' in os.environ: + return os.environ['TZ'] + + # 如果环境变量中没有TZ,尝试获取系统时区 + if platform.system() == "Linux": + # Linux: + with open("/etc/timezone", "r") as f: + return f.read().strip() + elif platform.system() == "Darwin": + # macOS: + return ZoneInfo.from_file(open("/etc/localtime")).key + elif platform.system() == "Windows": + # Windows: + import winreg + with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SYSTEM\CurrentControlSet\Control\TimeZoneInformation") as key: + tz = winreg.QueryValueEx(key, "TimeZoneKeyName")[0] + return windows_timezone_map.get(tz) or tz + else: + return "UTC" + except Exception as e: + return "UTC" diff --git a/test_tz.py b/test_tz.py new file mode 100644 index 0000000..121e66f --- /dev/null +++ b/test_tz.py @@ -0,0 +1,3 @@ +from server_status.timezone import get_timezone + +print(get_timezone()) \ No newline at end of file