您的問題是,由time.strptime(s, "%d %H %M %S ")
創建的timetuple是:
(tm_year=1900, tm_mon=1, tm_mday=20, tm_hour=3, tm_min=59, tm_sec=3, tm_wday=5, tm_yday=20, tm_isdst=-1)
...和time.mktime()
國家(重點煤礦)的文檔:
time.mktime(t) This is the inverse function of localtime(). Its argument is the struct_time or full 9-tuple (since the dst flag is needed; use -1 as the dst flag if it is unknown) which expresses the time in local time, not UTC. It returns a floating point number, for compatibility with time(). If the input value cannot be represented as a valid time, either OverflowError or ValueError will be raised (which depends on whether the invalid value is caught by Python or the underlying C libraries). The earliest date for which it can generate a time is platform-dependent.
所以這表明1900
爲時尚早。在我的系統(Win 7的),我也得到一個錯誤,但如果我改變你的代碼,包括最近的一年:
>>> s = "1970 20 3 59 3 "
>>> t = time.mktime(time.strptime(s, "%Y %d %H %M %S "))
>>> print t
1655943.0
我沒有錯誤,但如果我改變了今年1950
,我得到OverflowError
。
因此,解決方案是在您的字符串中包含一年,即time.mktime()
可以轉換。
問題可能是'%H'和'%S'應該填零,並且您正在測試的數字不是。 – elethan
這不會解決我的問題,但是,謝謝。 – WodkaRHR
此外,您的測試代碼不會爲我提出錯誤。我把'-2207311257.0'打印到控制檯上。 – elethan