Is Date Timezone Aware?

Description: Checks to see if a date object is timezone aware or not.
Tested Platform: Python 3.11
Language: Python
from datetime import datetime

def is_date_aware(dt):
    """Determine if a datetime object is timezone aware."""
    if not isinstance(dt, datetime):
        raise TypeError('Datetime specified is not a datetime object.')
    return dt.tzinfo is not None and dt.tzinfo.utcoffset(dt) is not None

Posted: March 21, 2023

Return to the snippets listing