I'm trying to write a pair of functions, plottm
and unixtm
, which convert back and forth between normal unix time (seconds since 1970-01-01) and Matplotlib's date representation (days since the last day of -1BC or something, a float).
If plottm
and unixtm
were proper inverses then this code would print the same date/time twice:
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)))
Alas, it's off by an hour (which only happens for some timestamps, otherwise I'd insert an offset and be done with it).
Probably related: Problems with Localtime
UPDATE: Related question that isn't specific to Matplotlib: Convert a unixtime to a datetime object and back again (pair of time conversion functions that are inverses)