将unixtime转换为datetime对象,然后再次(对转换的时间转换函数) [英] Convert a unixtime to a datetime object and back again (pair of time conversion functions that are inverses)

查看:129
本文介绍了将unixtime转换为datetime对象,然后再次(对转换的时间转换函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试写一个函数, dt ut ,它们之间来回转换正常的unix时间(自1970-01-01 00:00:00 UTC以来的秒数)和Python datetime对象。

I'm trying to write a pair of functions, dt and ut, that convert back and forth between normal unix time (seconds since 1970-01-01 00:00:00 UTC) and a Python datetime object.

如果 dt ut 正确的反转,那么这段代码将打印两次相同的时间戳:

If dt and ut were proper inverses then this code would print the same timestamp twice:

import time, datetime

# Convert a unix time u to a datetime object d, and vice versa
def dt(u): return datetime.datetime.fromtimestamp(u)
def ut(d): return time.mktime(d.timetuple())

u = 1004260000
print u, "-->", ut(dt(u))

唉,第二个时间戳是比第一个时间少3600秒(一小时)。
我认为这只是发生在非常特殊的unixtimes,也许在那个时候,夏令时跳过了。
但是有没有办法写入 dt ut ,以便它们是真正的反转?

Alas, the second timestamp is 3600 seconds (an hour) less than the first. I think this only happens for very particular unixtimes, maybe during that hour that daylight savings time skips over. But is there a way to write dt and ut so they're true inverses of each other?

相关问题:使matplotlib的date2num和num2date完美的反转

推荐答案

你是正确的,这种行为与夏令时相关。避免这种情况的最简单方法是确保您使用无夏令时间的时区,UTC最有用。

You are correct that this behavior is related to daylight savings time. The easiest way to avoid this is to ensure you use a time zone without daylight savings, UTC makes the most sense here.

datetime.datetime.utcfromtimestamp() calendar.timegm() 处理UTC时间,并且是精确的反转。

datetime.datetime.utcfromtimestamp() and calendar.timegm() deal with UTC times, and are exact inverses.

import calendar, datetime

# Convert a unix time u to a datetime object d, and vice versa
def dt(u): return datetime.datetime.utcfromtimestamp(u)
def ut(d): return calendar.timegm(d.timetuple())

这里有一些解释为什么 datetime.datetime.fromtimestamp() 有一个夏令时的问题,从做cs:

Here is a bit of explanation behind why datetime.datetime.fromtimestamp() has an issue with daylight savings time, from the docs:


返回与POSIX时间戳对应的本地日期和时间,
,如 time.time()。如果可选参数tz为None或
未指定,则时间戳记将转换为平台的本地日期
和时间,返回的datetime对象是天真的。

Return the local date and time corresponding to the POSIX timestamp, such as is returned by time.time(). If optional argument tz is None or not specified, the timestamp is converted to the platform’s local date and time, and the returned datetime object is naive.

这里的重要部分是您得到一个天真的 datetime.datetime 对象,这意味着没有时区(或夏令时)信息作为对象的一部分。这意味着,使用 fromtimestamp()时,多个不同的时间戳可映射到相同的 datetime.datetime 对象,如果发生在夏令时间内回放时间:

The important part here is that you get a naive datetime.datetime object, which means there is no timezone (or daylight savings) information as a part of the object. This means that multiple distinct timestamps can map to the same datetime.datetime object when using fromtimestamp(), if you happen to pick times that fall during the daylight savings time roll back:

>>> datetime.datetime.fromtimestamp(1004260000) 
datetime.datetime(2001, 10, 28, 1, 6, 40)
>>> datetime.datetime.fromtimestamp(1004256400)
datetime.datetime(2001, 10, 28, 1, 6, 40)

这篇关于将unixtime转换为datetime对象,然后再次(对转换的时间转换函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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