Python - calendar.timegm() 与 time.mktime() [英] Python - calendar.timegm() vs. time.mktime()

查看:45
本文介绍了Python - calendar.timegm() 与 time.mktime()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎很难理解这个问题.

calendar.timegm()time.mktime() 有什么区别?

假设我有一个没有附加 tzinfo 的 datetime.datetime,两者不应该给出相同的输出吗?他们不是都给出了纪元和作为参数传递的日期之间的秒数吗?而且由于传递的日期没有tzinfo,那是不是一样的秒数?

<预><代码>>>>导入日历>>>导入时间>>>导入日期时间>>>d = datetime.datetime(2010, 10, 10)>>>日历.timegm(d.timetuple())1286668800>>>time.mktime(d.timetuple())1286640000.0>>>

解决方案

time.mktime() 假设传递的元组在当地时间,calendar.timegm() 假定它是 GMT/UTC.根据解释,元组表示不同的时间,因此函数返回不同的值(自纪元以来的秒数基于 UTC).

值之间的差异应等于您当地时区的时区偏移量.

I seem to have a hard time getting my head around this.

What's the difference between calendar.timegm() and time.mktime()?

Say I have a datetime.datetime with no tzinfo attached, shouldn't the two give the same output? Don't they both give the number of seconds between epoch and the date passed as a parameter? And since the date passed has no tzinfo, isn't that number of seconds the same?

>>> import calendar
>>> import time
>>> import datetime
>>> d = datetime.datetime(2010, 10, 10)
>>> calendar.timegm(d.timetuple())
1286668800
>>> time.mktime(d.timetuple())
1286640000.0
>>> 

解决方案

time.mktime() assumes that the passed tuple is in local time, calendar.timegm() assumes it's in GMT/UTC. Depending on the interpretation the tuple represents a different time, so the functions return different values (seconds since the epoch are UTC based).

The difference between the values should be equal to the time zone offset of your local time zone.

这篇关于Python - calendar.timegm() 与 time.mktime()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆