title: Get Unix time in Python
body: <p>In Python, I want to get the current Unix timestamp and then store the value for the long term and be handled by non-Python systems. (I am not merely trying to compute the difference between two timestamps within the same program run.)</p>
  <p>Calling the function <a href="https://docs.python.org/3/library/time.html#time.time" rel="noreferrer"><code>time.time()</code></a> seems to be a very reasonable and concise way to get the desired timestamp... until I read the documentation:</p>
  <blockquote>
  <p>Return the time in seconds since the <a href="https://docs.python.org/3.9/library/time.html#epoch" rel="noreferrer">epoch</a> as a floating point number. The specific date of the epoch and the handling of leap seconds is platform dependent. On Windows and most Unix systems, the epoch is January 1, 1970, 00:00:00 (UTC) and leap seconds are not counted towards the time in seconds since the epoch. This is commonly referred to as Unix time. To find out what the epoch is on a given platform, look at <a href="https://docs.python.org/3.9/library/time.html#time.gmtime" rel="noreferrer"><code>gmtime</code></a><code>(0)</code>.</p>
  <p>[...]</p>
  <p>(Versions: <a href="https://docs.python.org/3.5/library/time.html#time.time" rel="noreferrer">3.5</a>~<a href="https://docs.python.org/3.9/library/time.html#time.time" rel="noreferrer">3.9</a>)</p>
  </blockquote>
  <p>The phrase &quot;epoch ... is platform dependent&quot; is a warning sign. A weasel phrase is &quot;<em>most</em> Unix systems&quot;. What are examples of Unix or non-Unix systems where Python's <code>time.time()</code>'s epoch is <em>not</em> 1970-01-01T00:00:00Z?</p>
  <p>Is <code>time.time()</code> subtly unsafe for my goal? Should I look to alternatives like <code>datetime.datetime.now().timestamp()</code>?</p>
  <hr />
  <p>Digging deeper, I also noticed that previous versions of Python didn't seem to have these caveats for <code>time.time()</code>:</p>
  <blockquote>
  <p>Return the time in seconds since the epoch as a floating point number. Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.</p>
  <p>(Versions: <a href="https://docs.python.org/2.7/library/time.html#time.time" rel="noreferrer">2.7</a>, <a href="https://docs.python.org/3.2/library/time.html#time.time" rel="noreferrer">3.2</a>~<a href="https://docs.python.org/3.4/library/time.html#time.time" rel="noreferrer">3.4</a>)</p>
  </blockquote>
  <p>And even older wording:</p>
  <blockquote>
  <p>Return the time as a floating point number expressed in seconds since the epoch, in UTC. Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.</p>
  <p>(Versions: <a href="https://docs.python.org/2.2/lib/module-time.html#l2h-1399" rel="noreferrer">2.2</a>~<a href="https://docs.python.org/2.6/library/time.html#time.time" rel="noreferrer">2.6</a>, <a href="https://docs.python.org/3.0/library/time.html#time.time" rel="noreferrer">3.0</a>~<a href="https://docs.python.org/3.1/library/time.html#time.time" rel="noreferrer">3.1</a>)</p>
  </blockquote>