Python 转换为UTC时间戳
我如何(在代码中的任何时候)将日期转换为UTC格式?我在API上苦读了一个世纪,却找不到任何可以工作的东西。有人能帮忙吗?我相信现在是东部时间了(不过我在格林尼治标准时间,但想要UTC) 编辑:我给了那个和我最终发现的最接近的人答案Python 转换为UTC时间戳,python,datetime,utc,Python,Datetime,Utc,我如何(在代码中的任何时候)将日期转换为UTC格式?我在API上苦读了一个世纪,却找不到任何可以工作的东西。有人能帮忙吗?我相信现在是东部时间了(不过我在格林尼治标准时间,但想要UTC) 编辑:我给了那个和我最终发现的最接近的人答案 //parses some string into that format. datetime1 = datetime.strptime(somestring, "%Y-%m-%dT%H:%M:%S") //gets the seconds from the ab
//parses some string into that format.
datetime1 = datetime.strptime(somestring, "%Y-%m-%dT%H:%M:%S")
//gets the seconds from the above date.
timestamp1 = time.mktime(datetime1.timetuple())
//adds milliseconds to the above seconds.
timeInMillis = int(timestamp1) * 1000
:)可能就是你想要的:
datetime1 = datetime.strptime(somestring, someformat)
timeInSeconds = calendar.timegm(datetime1.utctimetuple())
timeInMillis = timeInSeconds * 1000
我认为您可以使用
utcoffset()
方法:
>>> timestamp1 = time.mktime(datetime.now().timetuple())
>>> timestamp1
1256049553.0
>>> datetime.utcfromtimestamp(timestamp1)
datetime.datetime(2009, 10, 20, 14, 39, 13)
文档给出了使用astimezone()
方法的示例
此外,如果您要处理时区问题,您可能需要查看,其中有许多有用的工具可用于将日期时间转换为各种时区(包括EST和UTC之间的时区)
对于PyTZ:
utc_time = datetime1 - datetime1.utcoffset()
您可能需要以下两种中的一种:
from datetime import datetime
import pytz
utc = pytz.utc
eastern = pytz.timezone('US/Eastern')
# Using datetime1 from the question
datetime1 = datetime.strptime(somestring, "%Y-%m-%dT%H:%M:%S")
# First, tell Python what timezone that string was in (you said Eastern)
eastern_time = eastern.localize(datetime1)
# Then convert it from Eastern to UTC
utc_time = eastern_time.astimezone(utc)
这两个输出如下所示
import time
import datetime
from email.Utils import formatdate
rightnow = time.time()
utc = datetime.datetime.utcfromtimestamp(rightnow)
print utc
print formatdate(rightnow)
这将本地时间转换为UTC
def getDateAndTime(seconds=None):
"""
Converts seconds since the Epoch to a time tuple expressing UTC.
When 'seconds' is not passed in, convert the current time instead.
:Parameters:
- `seconds`: time in seconds from the epoch.
:Return:
Time in UTC format.
"""
return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(seconds))`
如果自使用mktime完成历元后,将结构时间转换为秒,则
转换在本地时区。没有办法告诉它使用任何特定的时区,甚至不只是UTC。标准的“time”包始终假定时间在本地时区。-1:
getDateAndTime()
是不相关的(它接受问题首先询问如何获取的秒数),并且它被破坏:实现与其docstring不对应。如果您有utc\u time
,那么调用calendar.timegm()
(localtime和mktime都是不必要的,可能会产生错误的结果)。您可以指定中的时区somestring
?是UTC还是当地时区<如果datetime1
不在UTC中,则code>timegm(datetime1.utctimetuple())
将不起作用utctimetuple()
不会将其转换为UTC,除非给定了aware datetime对象。+1表示pytz的localize()。注意:datetime1
问题中的是一个简单的datetime对象,即datetime1.utcoffset()
返回None
(您不能这样获得UTC时间)。仅适用于python 3。为什么这只适用于python 3?它在2.7.A-1中似乎运行良好,因为AttributeError:module'datetime'没有属性'utcfromtimestamp'
def getDateAndTime(seconds=None):
"""
Converts seconds since the Epoch to a time tuple expressing UTC.
When 'seconds' is not passed in, convert the current time instead.
:Parameters:
- `seconds`: time in seconds from the epoch.
:Return:
Time in UTC format.
"""
return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(seconds))`
time.mktime(time.localtime(calendar.timegm(utc_time)))