我正在嘗試從位元組形式解碼日期時間。我嘗試了各種方法(1-1-1970 的秒、分鐘、小時、1-1-1 的分鐘等)。我還嘗試了 mysql 編碼(https://dev.mysql.com/doc/internals/en/date-and-time-data-type-representation.html)也沒有效果。請幫我找到日期時間保存鍵。
bf aa b8 c3 e5 2f
d7 be ba c3 e5 2f
80 a0 c0 c3 e5 2f
a7 fc bf c3 e5 2f
ae fd f2 c3 e5 2f
9e dd fa c3 e5 2f
c7 ce fa c3 e5 2f
b9 f5 82 c4 e5 2f
f8 95 f2 c3 e5 2f
一切都在 01/14/2022 12:00 左右
uj5u.com熱心網友回復:
每個值都是編碼為VARINT的時間戳。很久以前,我制作了下一個函式來解碼/編碼 VARINT:
def to_varint(src):
buf = b""
while True:
towrite = src & 0x7f
src >>= 7
if src:
buf = bytes((towrite | 0x80,))
else:
buf = bytes((towrite,))
break
return buf
def from_varint(src):
shift = 0
result = 0
for i in src:
result |= (i & 0x7f) << shift
shift = 7
return result
所以使用這個函式我們可以解碼你的值:
from datetime import datetime
...
values = """\
bf aa b8 c3 e5 2f
d7 be ba c3 e5 2f
80 a0 c0 c3 e5 2f
a7 fc bf c3 e5 2f
ae fd f2 c3 e5 2f
9e dd fa c3 e5 2f
c7 ce fa c3 e5 2f
b9 f5 82 c4 e5 2f
f8 95 f2 c3 e5 2f"""
for value in values.splitlines():
timestamp = from_varint(bytes.fromhex(value))
dt = datetime.fromtimestamp(timestamp / 1000)
print(dt)
要獲取此編碼中的當前時間戳,您可以使用下一個代碼:
current = to_varint(int(datetime.now().timestamp() * 1000)).hex(" ")
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/412815.html
標籤:
上一篇:檢查日期時間是否在小時范圍內
下一篇:檢查日期格式是否包含年或月?
