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.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,418评论 0 10
  • 作者:王亚楠 指导老师:刘艳 思维导图的五大亮点以及目标制定的六字关键词,用两个分支清晰的呈现出来。
    我才不闲阅读 199评论 0 0
  • 女儿本月19完婚,24日娘仨兴高采烈回到奥克兰了。我准备11月2日返回吉林松原上班。这几天我吃饭开始打游击了。一...
    杨伟堃阅读 373评论 0 0
  • 今天(8月5日)常规功课完成:静心(30分钟)、当下练习1千次零极限、读诵《大学》《中庸》、奇迹功课、日志。觉察每...
    宇宙云英阅读 121评论 0 0