简体   繁体   English

从datetime转换为纪元时间(知道时区)

[英]Convert from datetime to epoch time (timezone aware)

I want to convert a datetime object in python 2.7, from UTC time to Unix (aka epoch) time. 我想在python 2.7中转换一个datetime对象,从UTC时间转换为Unix(又名epoch)时间。 As far as I understand, the following should give me back the timestamp I started with (ts -> UTC -> ts): 据我了解,以下内容应该给我回溯我开始的时间戳(ts - > UTC - > ts):

from datetime import datetime
from dateutil import tz
datetime.fromtimestamp(1538000000, tz.tzutc()).strftime('%s')

However, the output is different: '1537992800' (I am using fromtimestamp and not utcfromtimestamp as the latter returns a naive (unaware) object). 但是,输出是不同的: '1537992800' (我使用fromtimestamp而不是utcfromtimestamp因为后者返回一个天真(不知道)对象)。

This is the code though 这是代码

import time
time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM