Search code examples
pythondatetimepytz

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


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 ?


Solution

  • 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