title: Making matplotlib's date2num and num2date perfect inverses
body: <p>I'm trying to write a pair of functions, <code>plottm</code> and <code>unixtm</code>, 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).</p>
  
  <p>If <code>plottm</code> and <code>unixtm</code> were proper inverses then this code would print the same date/time twice:</p>
  
  <pre><code>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), "--&gt;", \
        datetime.datetime.fromtimestamp(unixtm(plottm(u)))
  </code></pre>
  
  <p>Alas, it's off by an hour (which only happens for some timestamps, otherwise I'd insert an offset and be done with it).</p>
  
  <p>Probably related: <a href="http://pytz.sourceforge.net/#problems-with-localtime" rel="nofollow noreferrer">Problems with Localtime</a></p>
  
  <p>UPDATE: Related question that isn't specific to Matplotlib: <a href="https://stackoverflow.com/questions/13260863/convert-a-unixtime-to-a-datetime-object-and-back-again-pair-of-time-conversion">Convert a unixtime to a datetime object and back again (pair of time conversion functions that are inverses)</a></p>