Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparison of two `time` objects with different timezones

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?

like image 513
kolypto Avatar asked Sep 07 '14 02:09

kolypto


People also ask

How does Python compare UTC time?

Use the timezone() function(gets the time zone of a specific location) of the pytz module, to get the timezone of “CET”(local TimeZone) and store it in a variable. Get the UTC timezone using the timezone() function and store it in another variable. Convert the above time to another time zone using the datetime.

How do you compare a timestamp in python?

Comparison between pandas timestamp objects is carried out using simple comparison operators: >, <,==,< = , >=. The difference can be calculated using a simple '–' operator. Given time can be converted to pandas timestamp using pandas. Timestamp() method.

What is Tzinfo in Python?

tzinfo is an abstract base class. It cannot be instantiated directly. A concrete subclass has to derive it and implement the methods provided by this abstract class. The instance of the tzinfo class can be passed to the constructors of the datetime and time objects.


2 Answers

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.

like image 55
user3942918 Avatar answered Oct 18 '22 03:10

user3942918


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.

like image 32
Simeon Visser Avatar answered Oct 18 '22 04:10

Simeon Visser