You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.6 KiB

11 months ago
# -*- encoding: utf-8 -*-
"""
@File : time_jx.py
@License : (C)Copyright 2021-2023
@Modify Time @Author @Version @Description
------------ ------- -------- -----------
2023/10/10 14:30 zart20 1.0 None
"""
# 时间解析
def time_parse(time_bytes):
# 从两个字节中解析时间信息
time_bytes = b'\x29\x8B' # 用于演示的两个字节数据
raw_time_value = int.from_bytes(time_bytes, byteorder='little') # 将两个字节合并为一个整数
# 提取小时、分钟和秒
hour = (raw_time_value >> 11) & 0x1F # 前11位表示小时
minute = (raw_time_value >> 5) & 0x3F # 接下来的5位表示分钟
second = (raw_time_value & 0x1F) * 2 # 最后的5位表示秒以2秒的粒度存储
print(f"{hour}:{minute}:{second}")
def date_parse(date_bytes):
import datetime
date_bytes = b'\x47\x57' # 用于演示的两个字节数据
raw_date_value = int.from_bytes(date_bytes, byteorder='little') # 将两个字节合并为一个整数
# 提取年份、月份和日期字段
year_offset = (raw_date_value >> 9) & 0x7F # 取前7位作为年份的偏移值
month = (raw_date_value >> 5) & 0x0F # 取接下来的4位作为月份
day = raw_date_value & 0x1F # 取最后的5位作为日期
# 计算实际年份
year = 1980 + year_offset
# 创建日期对象
date = datetime.date(year, month, day)
# 打印可读的日期表示
print("日期:", date.strftime("%Y-%m-%d"))
if __name__ == '__main__':
time_parse(b'\x29\x8B')
date_parse(b'\x47\x57')