title: Convert a unixtime to a datetime object and back again (pair of time conversion functions that are inverses)
body: <p>I'm trying to write a pair of functions, <code>dt</code> and <code>ut</code>, that convert back and forth between normal unix time (seconds since 1970-01-01 00:00:00 UTC) and a Python datetime object.</p>
  
  <p>If <code>dt</code> and <code>ut</code> were proper inverses then this code would print the same timestamp twice:</p>
  
  <pre><code>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, "--&gt;", ut(dt(u))
  </code></pre>
  
  <p>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 <code>dt</code> and <code>ut</code> so they're true inverses of each other?</p>
  
  <p>Related question: <a href="https://stackoverflow.com/questions/13259875/making-matplotlibs-date2num-and-num2date-perfect-inverses">Making matplotlib&#39;s date2num and num2date perfect inverses</a> </p>