import time
print time.strftime("%a, %d %b %Y %I:%M %p %Z", time.gmtime())
I live in California. For some reason, this code is reporting the time in GMT, instead of respecting the system time zone. I know that strftime knows I’m in pacific, because it still prints ‘PST’ at the end, but it’s still 8 hours ahead. Is anyone else noticing this? Anyone know what’s either wrong with my system or my code?
EDIT:
running date at the command line gives me the correct date. Additionally, I’ve run this on two different computers (mac and linux) and they both report 8 hours ahead. Are you expected to correct for timezone before using strftime?
time.gmtime()returns the time in UTC. What you need istime.localtime(), which is timezone-aware. This behaviour is well-documented in thetimemodule documentation.EDIT:
To convert any
time.time()style timestamp to astruct_time, you can supply an argument totime.localtime():