- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以我有 UTC 时间的 datetime 对象,我想将它们转换为 UTC 时间戳。问题是,time.mktime 对本地时间进行了调整。
下面是一些代码:
import os
import pytz
import time
import datetime
epoch = pytz.utc.localize(datetime.datetime(1970, 1, 1))
print time.mktime(epoch.timetuple())
os.environ['TZ'] = 'UTC+0'
time.tzset()
print time.mktime(epoch.timetuple())
这是一些输出:
Python 2.6.4 (r264:75706, Dec 25 2009, 08:52:16)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import pytz
>>> import time
>>> import datetime
>>>
>>> epoch = pytz.utc.localize(datetime.datetime(1970, 1, 1))
>>> print time.mktime(epoch.timetuple())
25200.0
>>>
>>> os.environ['TZ'] = 'UTC+0'
>>> time.tzset()
>>> print time.mktime(epoch.timetuple())
0.0
所以很明显,如果系统采用 UTC 时间,那没问题,但如果不是,那就有问题了。设置环境变量并调用 time.tzset 是可行的,但是这样安全吗?我不想为整个系统调整它。
还有其他方法吗?或者以这种方式调用 time.tzset 是否安全。
最佳答案
calendar
模块包含calendar.timegm
这解决了这个问题。
calendar.timegm
(元组)
An unrelated but handy function that takes a time tuple such as returned by the
gmtime()
function in thetime
module, and returns the corresponding Unix timestamp value, assuming an epoch of 1970, and the POSIX encoding. In fact,time.gmtime()
andtimegm()
are each others’ inverse.
关于python - 如何创建不针对本地时间进行调整的 unix 时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3315092/
给定一个带有多个 date_time 戳的字符串,我想 提取第一个戳及其前面的文本 候选字符串可以有一个或多个时间戳 后续的 date_time 戳记将被 sep="-" 隔开 后续date_time
是否可以合并从相机拍摄的文本和照片?我想在照片上标记日期和时间,但我在 Google 上找不到任何内容。 最佳答案 使用下面的代码来实现你所需要的。 Bitmap src = Bitm
有没有办法通过 Graph API 戳另一个用户?基于this post ,并使用 Graph Explorer ,我发布到“/USERID/pokes”,我已经授予它(Graph API 应用程序和
我有两个向左浮动的元素。一个是 body 的第一个 child ,另一个是容器的第一个 child ,容器是 body 的第二个 child 。 ...
我是一名优秀的程序员,十分优秀!