2

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:06 in Greenwich, which I should offset by +08:00. Meaning, it is 20:52:06 in Manila.

B. I'll take it at face value and say

It's 12:52:06 in Manila, and it's offset from UTC by +08:00. Meaning, it is 04:52:06 in Greenwich.

Which is correct? A or B?

Kit
  • 30,365
  • 39
  • 105
  • 149

1 Answers1

5

12:52:06+08:00 in 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 told datetime() that time was in the Manilla time zone. So for this particular case, A would be correct.

Note: datetime.strptime doesn't work with timezones -- you can use the %z format code for datetime.strftime, but not with strptime. If you need to do this, use dateutil, see How to parse dates with -0400 timezone string in python?

Community
  • 1
  • 1
agf
  • 171,228
  • 44
  • 289
  • 238