Python:如何将unixtimestamp和timezone转换为datetime对象?

sfa*_*tor 5 python timezone datetime unix-timestamp python-2.7

我有一个csv文件,其日期时间为unixtimestamp格式,毫秒和时区信息也以毫秒为单位.我想将其转换为更实用的日期时间格式,以便进一步处理.

例如,时间1437323953822和时区是-14400000.

我可以使用将时间戳转换为日期时间

datetime.datetime.fromtimestamp(1437323953822/1000)
Run Code Online (Sandbox Code Playgroud)

但是,我现在如何合并时区,即我所知道的-4 UTC时间.

(-14400000 / 1000 / 60 / 60) = -4
Run Code Online (Sandbox Code Playgroud)

如何使用此时区获取实际时间?

Pet*_*ood 6

fromtimestamp还可以采用另一个时区参数,它是 的子类tzinfo

\n\n
\n

classmethod datetime.fromtimestamp(timestamp[, tz])

\n\n

返回与 POSIX 时间戳对应的本地日期和时间,\n 例如由time.time(). 如果可选参数tz为 None 或未指定,则时间戳将转换为平台\xe2\x80\x99s\n 本地日期和时间,并且返回的日期时间对象是朴素的。

\n\n

否则tz必须是类子类的实例tzinfo,并且\n时间戳被转换为tz\xe2\x80\x98s时区。在这种情况下,结果\n 相当于\n tz.fromutc(datetime.utcfromtimestamp(timestamp).replace(tzinfo=tz)).

\n
\n