def cp56time2a_hex_str():
# 获取当前时间
now = datetime.now()
# 将当前时间格式化为CP56time2a格式
s = now.strftime("%Y%m%d%H%M%S")[2:]
cp56time2a = [s[i:i + 2] for i in range(0, len(s), 2)]
seconds = cp56time2a[-1:][0]
cp56time2a = cp56time2a[:-1]
seconds_hex = hex(int(seconds) * 1000)[2:] # ms 转hex
# 去除0x,并且按2位分割
seconds_hex_split = [seconds_hex[i:i + 2] for i in range(0, len(seconds_hex), 2)]
cp56time2a = [hex(int(x))[2:].zfill(2) for x in cp56time2a]
cp56time2a_hex_list = cp56time2a + seconds_hex_split
cp56time2a_hex_list.reverse() # 低位在前,高位在后
return "".join(cp56time2a_hex_list)
def format_cp56time2a(s):
hex_arr = [s[i:i+2] for i in range(0, len(s), 2)]
print(hex_arr)
hex_arr.reverse()
# 将前五项转换为10进制
first_five = [str(int(x, 16)) for x in hex_arr[:5]]
# 将最后两项合并并转换为10进制
last_two = [str(int(int(hex_arr[-2] + hex_arr[-1], 16)/1000)).zfill(2)]
return first_five+last_two
hex_str = cp56time2a_hex_str()
print(hex_str)
print(format_cp56time2a(hex_str))
#["年","月","日","时","分","秒"]
感谢作者
参考文档: