Reputation: 23
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?
Upvotes: 1
Views: 2372
Reputation: 69012
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
Upvotes: 3