How to check if date is in UTC format using pytz or datetime?
Asked Answered
B

5

6

I'm using the pytz module to translate a date from America/Los_Angeles timezone to UTC using the code below:

TZ = 'America/Los_Angeles'
from = pytz.timezone(TZ)
utc = from.localize(original_date).astimezone(pytz.utc)

Now, I want to test if the UTC value is actually in UTC format or not. How to do that with pytz or datetime ?

Brae answered 15/7, 2011 at 11:43 Comment(0)
S
7
utc.tzinfo == pytz.utc # returns True if utc in UTC

Example:

now = datetime.datetime.now(pytz.utc)
now.tzinfo == pytz.utc # returns True

now = now.astimezone(pytz.timezone('America/Los_Angeles'))
now.tzinfo == pytz.utc # returns False
Slumberland answered 15/7, 2011 at 12:0 Comment(3)
Any example without pytz?Keli
@alexche8 , import dateutil import datetime now = datetime.datetime.now(dateutil.tz.tzutc()) now.tzinfo == dateutil.tz.tzutc()Kean
datetime.timezone.utc != pytz.utc, so this fails or dates using a standard utc timezone object.Unsparing
B
10

The accepted answer will not work for anything else other than pytz objects. As pytz is actually pretty bad at doing conversions [1] (e.g. properly doing daylight savings, etc.) it is probably better to do a cross-implementation check.

now = datetime.datetime.now(pytz.utc)
if now.tzinfo:
    now.utcoffset().total_seconds() == 0 # returns true

[1] https://pendulum.eustace.io/blog/a-faster-alternative-to-pyz.html

Both answered 6/8, 2018 at 8:45 Comment(3)
Thanks, this one bit me when accepting input from a library that used dateutil, while I used pytz.Collateral
Just as a note, total_seconds() returns a float. While it's unlikely that you'll encounter a situation where comparison to integer 0 fails, you could be sure by using int(now.utcoffset().total_seconds()) == 0Treasure
@Treasure int(now.utcoffset().total_seconds()) == 0 gives True if the timezone is GMT... datetime.now(pytz.timezone("GMT")). GMT and UTC time are the same apparently.Mediation
S
7
utc.tzinfo == pytz.utc # returns True if utc in UTC

Example:

now = datetime.datetime.now(pytz.utc)
now.tzinfo == pytz.utc # returns True

now = now.astimezone(pytz.timezone('America/Los_Angeles'))
now.tzinfo == pytz.utc # returns False
Slumberland answered 15/7, 2011 at 12:0 Comment(3)
Any example without pytz?Keli
@alexche8 , import dateutil import datetime now = datetime.datetime.now(dateutil.tz.tzutc()) now.tzinfo == dateutil.tz.tzutc()Kean
datetime.timezone.utc != pytz.utc, so this fails or dates using a standard utc timezone object.Unsparing
M
1

You can do it simply like this:

from datetime import datetime, timezone
lunch_time = datetime.now(timezone.utc)

if lunch_time.format('%Z') == 'UTC':
     print("Eat food")

This will also work with a naive time object because lunch_time.format('%Z') will return an empty string. This method will also work with pytz or any other module because you are simply checking the timezone as string not as an object (the accepted answer won't work with the above timezone module case, only pytz).

from datetime import datetime
import pytz
dinner_time = datetime.now(pytz.timezone('UTC'))

if dinner_time.format('%Z') == 'UTC':
     print("Hungry!")

Note: This will also eliminate the possibility of the timezone being GMT timezone rather than UTC timezone. The other answer now.utcoffset().total_seconds() == 0 will be True for GMT which may not be what you want.

The %Z specifier is documented here:
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

Mediation answered 8/3, 2022 at 3:47 Comment(0)
P
0

Similar to a previous answer, you can do it like this:

If you have a given datetime object, you want to first convert it to a string with the strftime() method, then pass in the %Z specifier which gives the timezone name returning one of '(empty), UTC, GMT' according to the documentation.

so an example is:


def check_if_utc(my_datetime):
   return my_datetime.strftime('%Z') == 'UTC'
    
Pteridology answered 3/6, 2023 at 13:34 Comment(0)
S
0
timezone.get_default_timezone()
time = timezone.make_aware(time, default_timezone)

The above code gets you the default timezone, which is mostly UTC. You can make the datetime timezone aware using the above code.

If using python 3.8 : time.tzinfo == pytz.utc

If using python 3.9 > : time.tzinfo == datetime.timezone.utc

Note : pytz.utc != datetime.timezone.utc so do check your the type of your date, using type(date).

Swansdown answered 20/6 at 5:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.