I have the following script defining a tzinfo object:
import time
from datetime import datetime, timedelta, tzinfo
class ManilaTime(tzinfo):
def utcoffset(self, dt):
return timedelta(hours=8)
def tzname(self, dt):
return "Manila"
manila = ManilaTime()
Now, I’m going to say
t = datetime(tzinfo=manila, *time.gmtime()[:-3])
print t
which gives me
2011-07-24 12:52:06+08:00
Question: What does 12:52:06+08:00 mean? I want to learn how to read time information which includes a UTC offset, according to standards. Please disregard that I used time.gmtime(). Let’s say I only got the time string itself. How do I read it?
A. I need to perform the addition to get Manila Time. Upon reading this, I should make a calculation and I’ll say
It’s
12:52:06in Greenwich, which I should offset by+08:00. Meaning, it is20:52:06in Manila.
B. I’ll take it at face value and say
It’s
12:52:06in Manila, and it’s offset from UTC by+08:00. Meaning, it is04:52:06in Greenwich.
Which is correct? A or B?
12:52:06+08:00in general means it’s the given time in a timezone 8 hours ahead of UTC. So B would be correct.However, you generated the time string incorrectly.
time.gmtime()just returns a time, without any timezone, and you tolddatetime()that time was in the Manilla time zone. So for this particular case, A would be correct.Note:
datetime.strptimedoesn’t work with timezones — you can use the%zformat code fordatetime.strftime, but not withstrptime. If you need to do this, usedateutil, see How to parse dates with -0400 timezone string in python?