Convert Cocoa timestamp in Python
<p>I have a Cocoa timestamp (zero time of January 1st, 2001 00:00:00 UTC) that I need to convert in Python.  When I use the following code it assumes a Unix timestamp input.  Besides adding 31 years in seconds (Just under a billion seconds...) what's the best way to convert the time?</p>

<pre><code>import datetime
print(datetime.datetime.fromtimestamp(int("495759456")).strftime('%Y-%m-%d %H:%M:%S'))
</code></pre>

<p>The output for this line of code is '1985-09-16 16:12:03'</p>

<p>Would something like this work for you:</p>

<pre><code>from datetime import datetime

unix = datetime(1970, 1, 1)  # UTC
cocoa = datetime(2001, 1, 1)  # UTC

delta = cocoa - unix  # timedelta instance

timestamp = datetime.fromtimestamp(int("495759456")) + delta

print(timestamp.strftime('%Y-%m-%d %H:%M:%S'))
</code></pre>

<p>I didn't specify timezone information for the two starts of time as they're in the same zone so I assume it shouldn't matter which is used when computing the difference.  For converting the timestamp string, you may need to adjust for your timezone if it's different than the one in which the string was generated.</p>

<p>The above code produces <code>2016-09-16 15:57:36</code></p>

python cocoa timestamp