fromtimestamp() 的反義詞是什麼? (What is the opposite of fromtimestamp()?)


問題描述

fromtimestamp() 的反義詞是什麼? (What is the opposite of fromtimestamp()?)

我正在嘗試弄清楚如何將二進製文件導入 python,但這需要大量的猜測和反複試驗。在這種情況下,我想做的是找到字節流中特定日期出現的位置,即:

import re
import datetime
import binascii
import time

with open('inputfile.nda', 'rb') as f:
    data = f.read()

# This line needs fixing
i = int.oppositeof_fromtimestamp(2019, 10, 10, 14, 53, 57.000) # '2019/10/10 14:53:57.000' 


time_stamp = i.to_bytes(4, byteorder='little')

df = [m.start() for m in re.finditer(time_stamp, data)]

print(df)

是否有一個函數(或一組函數)可以將日期轉換為字節流?


參考解法

方法 1:

You're looking for datetime.timestamp():

import datetime

ts = datetime.datetime(2019, 10, 10, 14, 53, 57)
unix_time = int(ts.timestamp())
unix_time_bytes = unix_time.to_bytes(4, byteorder='little')
print(ts, unix_time, unix_time_bytes)

outputs

2019‑10‑10 14:53:57 1570708437 b'\xd5\x1b\x9f]'

Then you'll probably want to use data.index(unix_time_bytes) to look for the string, not regexps.

(by Frederik HuldAKX)

參考文件

  1. What is the opposite of fromtimestamp()? (CC BY‑SA 2.5/3.0/4.0)

#datetime #Python #bytestream






相關問題

NHibernate:HQL:從日期字段中刪除時間部分 (NHibernate:HQL: Remove time part from date field)

如何獲得在給定時間內發送超過 X 個數據包的 IP (How do I get IPs that sent more than X packets in less than a given time)

Памылка дадання даты пры адніманні ад 0:00 (Dateadd error when subtracting from 0:00)

查找與日曆相比缺失的日期 (Find missing date as compare to calendar)

CodeReview:java Dates diff(以天為單位) (CodeReview: java Dates diff (in day resolution))

顯示兩個給定時間之間的 15 分鐘步長 (display 15-minute steps between two given times)

如何在 C# 中獲取月份名稱? (How to get the month name in C#?)

fromtimestamp() 的反義詞是什麼? (What is the opposite of fromtimestamp()?)

構建 JavaScript 時缺少模塊 (Missing Module When Building JavaScript)

setTimeout 一天中的特定時間,然後停止直到下一個特定時間 (setTimeout for specific hours of day and then stop until next specific time)

將浮點數轉換為 datatime64[ns] (Converting float into datatime64[ns])

Python Dataframe 在連接時防止重複 (Python Dataframe prevent duplicates while concating)







留言討論