Fixed some issues

This commit is contained in:
zhaoyafan 2025-03-16 23:41:56 +08:00
parent ce8fcbf913
commit b1f89206f9
1 changed files with 44 additions and 5 deletions

View File

@ -1,8 +1,10 @@
import os
import re
import sys
import time
import json
import ctypes
import ctypes.wintypes
import shutil
import signal
import winreg
@ -214,11 +216,10 @@ class MainWindow(QSystemTrayIcon):
self.setToolTip('')
self.tray_icon_update_timer = QTimer(self)
self.tray_icon_update_timer.timeout.connect(self.on_tray_icon_update)
self.tray_icon_update_timer.start(1500)
self.tray_icon_update_timer.start(3000)
self.time_sync_update_timer = QTimer(self)
self.time_sync_update_timer.timeout.connect(self.sync)
self.time_sync_update_timer.start(1000 * 3600)
QTimer.singleShot(1000 * 12, self.sync)
self.tray_menu = QMenu()
action_list = [
['立即同步', self.sync],
@ -235,6 +236,7 @@ class MainWindow(QSystemTrayIcon):
def init(self):
self.sync()
self.sync_delayed()
@staticmethod
def exit():
@ -262,6 +264,11 @@ class MainWindow(QSystemTrayIcon):
print('Synchronizing...')
threading.Thread(target=self._sync_execution).start()
def sync_delayed(self):
QTimer.singleShot(1000 * 12, self.sync)
QTimer.singleShot(1000 * 35, self.sync)
QTimer.singleShot(1000 * 65, self.sync)
@staticmethod
def format_time(secs):
return '刚刚' if secs < 60 else '%s分钟前' % ((secs // 60),) if secs < 3600 else '%s小时前' % (round(secs / 3600),)
@ -294,7 +301,38 @@ class MainWindow(QSystemTrayIcon):
pass
@staticmethod
def get_server_datetime():
def get_datetime_format():
LOCALE_SSHORTDATE = 0x001F
LOCALE_STIMEFORMAT = 0x1003
GetUserDefaultLCID = ctypes.windll.kernel32.GetUserDefaultLCID
GetUserDefaultLCID.argtypes = []
GetUserDefaultLCID.restype = ctypes.wintypes.LCID
GetLocaleInfoW = ctypes.windll.kernel32.GetLocaleInfoW
GetLocaleInfoW.argtypes = [ctypes.wintypes.LCID, ctypes.wintypes.LCTYPE, ctypes.wintypes.LPWSTR, ctypes.c_int]
GetLocaleInfoW.restype = ctypes.c_int
user_lcid = GetUserDefaultLCID()
date_format = ctypes.create_unicode_buffer(1024)
GetLocaleInfoW(user_lcid, LOCALE_SSHORTDATE, date_format, ctypes.sizeof(date_format))
time_format = ctypes.create_unicode_buffer(1024)
GetLocaleInfoW(user_lcid, LOCALE_STIMEFORMAT, time_format, ctypes.sizeof(time_format))
return date_format.value, time_format.value
@staticmethod
def map_system_format_to_strftime(system_format):
format_mapping = {
'dd': '__DATE__', 'd': '__DATE__', 'MM': '__MONTH__', 'M': '__MONTH__', 'yyyy': '__YEAR4__', 'yy': '__YEAR2__', 'HH': '__HOUR24__', 'H': '__HOUR24__',
'hh': '__HOUR12__', 'h': '__HOUR12__', 'mm': '__MINUTE__', 'm': '__MINUTE__', 'ss': '__SECOND__', 's': '__SECOND__',
'tt': '__AMPM__', 't': '__AMPM__'
}
sorted_formats = sorted(format_mapping.keys(), key=len, reverse=True)
for sys_fmt in sorted_formats:
system_format = re.sub('\\b' + re.escape(sys_fmt) + '\\b', format_mapping[sys_fmt], system_format)
final_mapping = {'__DATE__': '%d', '__MONTH__': '%m', '__YEAR4__': '%Y', '__YEAR2__': '%y', '__HOUR24__': '%H', '__HOUR12__': '%I', '__MINUTE__': '%M', '__SECOND__': '%S', '__AMPM__': '%p'}
for placeholder, str_fmt in final_mapping.items():
system_format = system_format.replace(placeholder, str_fmt)
return system_format
def get_server_datetime(self):
try:
gate = WMI().Win32_NetworkAdapterConfiguration(IPEnabled=True)[0].DefaultIPGateway[0]
except Exception:
@ -315,7 +353,8 @@ class MainWindow(QSystemTrayIcon):
try:
date_out = ori_timezone.localize(datetime.datetime.strptime(date_string, '%a, %d %b %Y %H:%M:%S %Z')).astimezone(tar_timezone) + datetime.timedelta(seconds=1)
if (int(date_out.timestamp()) > 1735689600) == 1:
return str(date_out.date()), str(date_out.time())
dt_fmt = self.get_datetime_format()
return str(date_out.strftime(self.map_system_format_to_strftime(dt_fmt[0]))), str(date_out.strftime(self.map_system_format_to_strftime(dt_fmt[1])))
raise Exception('The time returned by the server is older.')
except ValueError:
raise Exception('Failed to get time from server.')
@ -328,7 +367,7 @@ class MainRunner:
def __init__(self):
signal.signal(signal.SIGINT, self._handle_interrupt)
self.app_name = '时间同步助手'
self.app_version = '1.0.0.0'
self.app_version = '1.0.0.1'
self.app_publisher = 'zhaoyafan'
self.app_publisher_url = 'https://www.fanscloud.net/'
self.application = None