Search code examples
pythonepoch

Year wrong on Epoch time conversion


I'm trying to convert epoch time using Python. However, the year always seems to be wrong. In the below example, it should be 2014.

import time
timestamp = time.strftime("%a, %d %b %Y %H:%M:%S +0000", 
                    time.localtime(1415219530834))

What am I doing wrong?

I get this result:

Sat, 09 Jul 46816 16:20:34 +0000

Solution

  • You are passing time in milliseconds, but it should be in seconds. Divide it by 1000

    import time
    timestamp = time.strftime("%a, %d %b %Y %H:%M:%S +0000", 
                        time.localtime(1415219530))
    

    result:

    'Wed, 05 Nov 2014 15:32:10 +0000'