1

I have a timestamp which is coming from a remote Linux box. This is the timestamp 1356354496. When I am using the fromtimestamp function I am getting a different output then what it should be.

Example:

from datetime import datetime
import time
print(time.ctime(int("1356354496")))
cwStartTimeDisplay=datetime.fromtimestamp(int("1356354496")).strftime("%a %b %d %H:%M:%S %Y")
print(cwStartTimeDisplay)

Output

Mon Dec 24 05:08:16 2012 Mon Dec 24 05:08:16 2012

Whereas I should be getting 12/24/2012 6:38:16 PM. I am a beginner and don't really know if tz parameter is the answer to this. Can anybody help please?

2
  • 1
    likely a tz issue. what are the local timezones of the two machines? How do you obtain the timestamp on the remote machine? using time.time()? Commented Jun 14, 2013 at 7:46
  • both are IST's. I am getting the timestamp from pywbem Commented Jun 14, 2013 at 8:50

1 Answer 1

3

Your timestamp seems to be UTC, so if you need id represented in IST, you need to convert it.

The recommended library to work with timezone data in python is pytz

from datetime import datetime
import pytz
ist = pytz.timezone("Asia/Kolkata")

utcdate = pytz.utc.localize(datetime.utcfromtimestamp(1356354496))
print("UTC:", utcdate)
istdate = ist.normalize(utcdate)
print("IST:", istdate)

# or shorter:
date = datetime.fromtimestamp(1356354496, ist)
print("IST:", date)

output:

UTC: 2012-12-24 13:08:16+00:00
IST: 2012-12-24 18:38:16+05:30
IST: 2012-12-24 18:38:16+05:30
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mata!! There was a problem with my machine. When I tested the same sample code and the code implemented in the project on another machine it worked fine. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.