7

I'm comparing two time objects with different timezones, and looks like it's actually ignoring the timezone, testing only the hour/minute/second components.

Let's create two time objects:

from datetime import time
import pytz

CET = pytz.timezone('CET')
Japan = pytz.timezone('Japan')

t1 = time(1,2,3, tzinfo=CET)
t2 = time(1,2,3, tzinfo=Japan)

Printing them, we see that they're pretty different:

datetime.time(1, 2, 3, tzinfo=<DstTzInfo 'CET' CET+1:00:00 STD>)
datetime.time(1, 2, 3, tzinfo=<DstTzInfo 'Japan' JST+9:00:00 STD>)

Now, let's compare them:

t1 == t2
#-> True

Ehm, what? How is it possible that Python treats them equal?

kolypto
  • 31,774
  • 17
  • 105
  • 99
  • Probably related: [Comparing a time in UTC with a time in Eastern time](http://stackoverflow.com/questions/10524165/comparing-a-time-in-utc-with-a-time-in-eastern-time-using-python?rq=1) – kolypto Sep 07 '14 at 02:23
  • What version of Python are you using? – MattDMo Sep 07 '14 at 02:45
  • @MattDMo, it's 2.7. Sorry :) – kolypto Sep 07 '14 at 02:46
  • Derp's should be the correct answer, as it explains **why** the time objects are naive - they have no date associated with them, and can never be aware. – MattDMo Sep 07 '14 at 03:38

3 Answers3

8

Both of your time objects are "naive" according to https://docs.python.org/2/library/datetime.html#datetime.tzinfo:

A time object t is aware if t.tzinfo is not None and t.tzinfo.utcoffset(None) does not return None. Otherwise, t is naive.

print(t1.tzinfo, t1.tzinfo.utcoffset(None))
print(t2.tzinfo, t2.tzinfo.utcoffset(None))

Gives us:

(<DstTzInfo 'CET' CET+1:00:00 STD>, None)
(<DstTzInfo 'Japan' JST+9:00:00 STD>, None)

https://docs.python.org/2/library/datetime.html#module-datetime

A naive object does not contain enough information to unambiguously locate itself relative to other date/time objects.


In other words: the objects have no date and so it cannot be determined whether or not daylight saving time applies. They're ambiguous, and running t.utcoffset() for either will return None. Which leaves the timezones being ignored entirely in the comparison because they're effectively meaningless.

user3942918
  • 25,539
  • 11
  • 55
  • 67
  • even if the code had used `datetime`; it would be incorrect. `tz.localize()` should be used, to make a naive datetime object timezone-aware. – jfs Sep 14 '14 at 16:23
2

From the docs regarding naive and aware time objects:

A time object t is aware if t.tzinfo is not None and t.tzinfo.utcoffset(None) does not return None.

In your case, both t1 and t2 return None for t1.tzinfo.utcoffset(None) and t2.tzinfo.utcoffset(None). Hence your objects are naive and not aware.

So effectively you're comparing '01:02:03' with '01:02:03' which is True.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
0

I would use (t1,t1.tzinfo) == (t2, t2.tzinfo) or t1.strftime('%H %M %S %Z') == t2.strftime('%H %M %S %Z') this to check equality.

derp explained why Python treated t1 and t2 equal and also refer this supported operations (Python docs).

More references:

According to pytz docs,

The preferred way of dealing with times is to always work in UTC, converting to localtime only when generating output to be read by humans.

pytz docs shows the way to convert (using localize and/or replace) naive time objects to aware time objects and also present ways to perform arithmetic over time objects.

Community
  • 1
  • 1
praba230890
  • 2,192
  • 21
  • 37