Unix 时间戳到 iso 8601 时间格式 [英] Unix timestamp to iso 8601 time format

查看:119
本文介绍了Unix 时间戳到 iso 8601 时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将 unix 时间 1463288494 转换为 isoformat 时,我得到 2016-05-14T22:01:34.我怎样才能得到包括 -07:00 的输出.以这种格式 2016-05-14T22:01:34-07:00

When i convert unix time 1463288494 to isoformat i get 2016-05-14T22:01:34. How can I get the output including the -07:00. In this format 2016-05-14T22:01:34-07:00

from datetime import datetime
t =  int("1463288494")
print(datetime.fromtimestamp(t).isoformat())

推荐答案

您可以将表示时区偏移量的 tzinfo 实例传递给 fromtimestamp().那么问题是如何获得 tzinfo 对象.最简单的方法是使用 pytz 模块,它提供了一个 tzinfo 兼容对象:

You can pass a tzinfo instance representing your timezone offset to fromtimestamp(). The problem then is how to get the tzinfo object. The easiest way is to use the pytz module which provides a tzinfo compatible object:

import pytz
from datetime import datetime

tz = pytz.timezone('America/Los_Angeles')
print(datetime.fromtimestamp(1463288494, tz).isoformat())

#2016-05-14T22:01:34-07:00

这篇关于Unix 时间戳到 iso 8601 时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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