From ce705c6dd5482d5ce2d00b2eb80f9da14611bc0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=9F=E5=85=89-k?= <36470587+kxgx@users.noreply.github.com> Date: Tue, 21 Jan 2025 14:04:07 +0000 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20main.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/main.py | 34 ++-------------------------------- 1 file changed, 2 insertions(+), 32 deletions(-) diff --git a/bin/main.py b/bin/main.py index 7e53d80..8a88ab5 100644 --- a/bin/main.py +++ b/bin/main.py @@ -38,38 +38,8 @@ def get_date():#返回当前年月日及星期几 week_day_dict = {0: '星期一',1: '星期二',2: '星期三',3: '星期四',4: '星期五',5: '星期六',6: '星期日',} day = date.weekday() return time.strftime('%Y年%m月%d日')+''+week_day_dict[day]+''+today.strftime('农历%M月%D') -def get_time_with_hwclock(): - # 执行系统命令 - process = subprocess.Popen(['sudo', 'hwclock', '-r'], stdout=subprocess.PIPE) - output, error = process.communicate() - - if error: - raise Exception("使用hwclock获取时间时出错") - - # 解析输出 - time_str = output.decode('utf-8') - # 假设 hwclock 的输出格式为 "2024-01-21 12:34:56.789+08:00" - # 我们只需要小时和分钟部分 - match = re.search(r'(\d{2}:\d{2})', time_str) - if not match: - raise Exception("无法从hwclock输出中解析时间") - - # 格式化时间 - formatted_time = match.group(1).upper() - return formatted_time - -def get_time_with_strftime(): - # 返回当前时间,不到秒,大写 - return time.strftime('%H:%M').upper() - -def get_time(): - try: - # 首先尝试使用hwclock获取时间 - return get_time_with_hwclock() - except Exception as e: - # 如果hwclock失败,则使用strftime获取时间 - print(f"hwclock失败: {e}, 正在尝试使用strftime获取时间") - return get_time_with_strftime() +def get_time():#返回当前时间,不到秒,大写 + return time.strftime('%H:%M') def Get_address():#获取当前的IP地址 return (subprocess.check_output(u"hostname -I | cut -d\' \' -f1 | head --bytes -1", shell = True ).decode('gbk')) def Get_ipv4_address(): # 获取当前的IP地址