time 模块 mktime溢出错误

https://stackoverflow.com/questions/39831460/python-time-error-mktime-overflow?noredirect=1

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.

So this suggests that 1900 is too early to convert. On my system (Win 7), I also get an error, but if I change your code to include a recent year:

>>> s = "1970 20 3 59 3 "

>>> t = time.mktime(time.strptime(s, "%Y %d %H %M %S "))

>>> print t

1655943.0

I get no error, but if I change the year to 1950, I get OverflowError.

So the solution is to include a year in your string, that time.mktime() can convert.

你可能感兴趣的:(time 模块 mktime溢出错误)