compare datetime.now() with utc timestamp with python 2.7
<p>I have a timestamp such <code>1474398821633L</code> that I think is in utc. I want to compare it to datetime.datetime.now() to verify if it is expired.</p>

<p>I am using python 2.7</p>

<pre><code>from datetime import datetime

timestamp = 1474398821633L
now = datetime.now()

if datetime.utcfromtimestamp(timestamp) &lt; now:
    print "timestamp expired"
</code></pre>

<p>However I got this error when trying to create a datetime object from the timestamp: <code>ValueError: timestamp out of range for platform localtime()/gmtime() function</code></p>

<p>What can I do?</p>

<p>It <em>looks</em> like your timestamp is in milliseconds.  Python uses timestamps in seconds:</p>

<pre><code>&gt;&gt;&gt; datetime.datetime.utcfromtimestamp(1474398821.633)
datetime.datetime(2016, 9, 20, 19, 13, 41, 633000)
</code></pre>

<p>In other words, you might need to divide your timestamp by <code>1000.</code> in order to get it in the proper range.</p>

<p>Also, you'll probably want to compare <code>datetime.utcnow()</code> instead of <code>datetime.now()</code> to make sure that you're handling timezones correctly :-).</p>

python python-2.7