user4358169
user4358169

Reputation:

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

Upvotes: 2

Views: 1889

Answers (4)

NIKHIL RANE
NIKHIL RANE

Reputation: 4092

Try this also

import datetime
datetime.datetime.fromtimestamp(your time in epoch/1000)

Convert time in epoch

int(yourtime.strftime("%s"))*1000

Upvotes: 1

Donovan Solms
Donovan Solms

Reputation: 961

You are getting the Epoch in Milliseconds in Python. You need to get it in seconds for it to work correctly.

Something like

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

Upvotes: 0

davidism
davidism

Reputation: 127320

You've passed a timestamp in milliseconds, but localtime expects a value in seconds.

time.localtime(1415219530834 / 1000)

Upvotes: 1

pbkhrv
pbkhrv

Reputation: 647

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'

Upvotes: 4

Related Questions