Skip to content

Commit 11040b1

Browse files
committed
feat: 添加获取当前UTC时间的功能,支持Python版本兼容
1 parent 1d04ec4 commit 11040b1

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

systemInfo/dev/main.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ def is_internet_available():
135135
if True not in res.values():
136136
res["status"] = False
137137
return res
138+
139+
def get_utc_time():
140+
"""获取当前UTC时间"""
141+
# python < 3.11 时,需要使用 datetime.UTC 而不是 datetime.timezone.utc
142+
if sys.version_info < (3, 11):
143+
return datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S')
144+
else:
145+
return datetime.datetime.now(datetime.UTC).strftime('%Y-%m-%d %H:%M:%S')
138146

139147

140148
def main():
@@ -170,7 +178,7 @@ def main():
170178
# ======================
171179
center_title("时间信息")
172180
print(f"当前时间(本地): {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
173-
print(f"当前时间(UTC): {datetime.datetime.now(datetime.UTC).strftime('%Y-%m-%d %H:%M:%S')}")
181+
print(f"当前时间(UTC): {get_utc_time()}")
174182
print(f"当前时区: {time.strftime('%Z')} (UTC{time.strftime('%z')})")
175183

176184
# ======================

0 commit comments

Comments
 (0)