//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
如何(在代码中的任何一点)将日期转换为UTC格式?我花了一个世纪的时间研究API,却找不到任何我能工作的东西。有人能帮忙吗?我相信它正在把它转变成东部时间(不过我在格林尼治标准时间,但想要UTC)。
编辑:我给了那个和我最后发现的最接近的人答案。
datetime1 = datetime.strptime(somestring, someformat)
timeInSeconds = calendar.timegm(datetime1.utctimetuple())
timeInMillis = timeInSeconds * 1000
:)
^{} 可能是您要找的:
这将本地时间转换为UTC
http://feihonghsu.blogspot.com/2008/02/converting-from-local-time-to-utc.html
如果使用mktime将结构时间转换为自纪元完成以来的秒,则 在本地时区中转换为。没有办法告诉它使用任何特定的时区,甚至不只是UTC。标准的“time”包总是假设时间在本地时区内。
我认为您可以使用
utcoffset()
方法:文档给出了一个使用
astimezone()
方法here的例子。另外,如果您要处理时区,您可能需要查看PyTZ library,它有很多有用的工具,可以将datetime转换为各种时区(包括EST和UTC之间的时区)
与PyTZ:
相关问题 更多 >
编程相关推荐