2

我正在尝试编写一对函数,plottmand unixtm,它们在正常的 unix 时间(自 1970-01-01 以来的秒数)和 Matplotlib 的日期表示(自 -1BC 的最后一天或某事的最后一天以来的天数,浮点数)之间来回转换.

如果plottmunixtm是适当的逆,那么这段代码将打印相同的日期/时间两次:

import time, datetime
import matplotlib.dates as dt

# Convert a unix time u to plot time p, and vice versa
def plottm(u): return dt.date2num(datetime.datetime.fromtimestamp(u))
def unixtm(p): return time.mktime(dt.num2date(p).timetuple())

u = 1270000000
print datetime.datetime.fromtimestamp(u), "-->", \
      datetime.datetime.fromtimestamp(unixtm(plottm(u)))

唉,它已经关闭了一个小时(这只发生在某些时间戳上,否则我会插入一个偏移量并完成它)。

可能相关:本地时间问题

更新:不特定于 Matplotlib 的相关问题:Convert a unixtime to a datetime object and back again(一对逆时间转换函数)

4

3 回答 3

3

基于@dreeves 的回答,适用于时区感知日期时间的解决方案:

import matplotlib.dates as dt

from calendar import timegm
from datetime import datetime

from pytz import utc


# Convert a unix time u to plot time p, and vice versa
def plottm(u):
    return dt.date2num(datetime.fromtimestamp(u, utc))

def unixtm(p):
    return timegm(dt.num2date(p, utc).utctimetuple())


u = 1270000000
print datetime.fromtimestamp(u, utc), "-->", \
      datetime.fromtimestamp(unixtm(plottm(u)), utc)

输出(测试了几个时区):

2010-03-31 01:46:40+00:00 --> 2010-03-31 01:46:40+00:00
于 2012-11-06T22:00:58.590 回答
3

matplotlib.dates.epoch2num()/num2epoch 函数可以做到这一点:

from datetime import datetime, timedelta
import matplotlib.dates as mpl_dt

matplotlib_epoch = datetime(1, 1, 1)  # utc
posix_epoch = datetime(1970, 1, 1)  # utc
DAY = 86400  # seconds


def plottm(u):
    """posix timestamp -> plot time"""
    td = (datetime.utcfromtimestamp(u) - matplotlib_epoch)
    return td.days + 1 + (1000000 * td.seconds + td.microseconds) / 1e6 / DAY


def unixtm(p):
    """plot time -> posix timestamp"""
    td = timedelta(days=p-1)
    return (matplotlib_epoch + td - posix_epoch).total_seconds()


def main():
    f = datetime.utcfromtimestamp
    u = 1270000000.1234567890
    print(f(u))
    print(mpl_dt.epoch2num(u))
    print(plottm(u))
    print(f(mpl_dt.num2epoch(mpl_dt.epoch2num(u))))
    print(f(mpl_dt.num2epoch(plottm(u))))
    print(f(unixtm(mpl_dt.epoch2num(u))))
    print(f(unixtm(plottm(u))))

    assert abs(mpl_dt.epoch2num(u) - plottm(u)) < 1e-5

    p = 86401.234567890 / DAY
    print(f(mpl_dt.num2epoch(p)))
    print(f(unixtm(p)))
    assert abs(mpl_dt.num2epoch(p) - unixtm(p)) < 1e-5

main()

输出

2010-03-31 01:46:40.123457
733862.074076
733862.074076
2010-03-31 01:46:40.123453
2010-03-31 01:46:40.123453
2010-03-31 01:46:40.123453
2010-03-31 01:46:40.123453
0001-01-01 00:00:01.234566
0001-01-01 00:00:01.234566
于 2012-11-07T00:00:08.040 回答
1

感谢FJ类似问题的回答,我相信以下可能是处理此问题的最佳方法:

import datetime, calendar
import matplotlib.dates as dt

def plottm(u): return dt.date2num(datetime.datetime.utcfromtimestamp(u))
def unixtm(p): return calendar.timegm(dt.num2date(p).timetuple())
于 2012-11-06T23:33:06.620 回答