Python datetime get utc offset

time.timezone:

import time

print -time.timezone

It prints UTC offset in seconds (to take into account Daylight Saving Time (DST) see time.altzone:

is_dst = time.daylight and time.localtime().tm_isdst > 0
utc_offset = - (time.altzone if is_dst else time.timezone)

where utc offset is defined via: "To get local time, add utc offset to utc time."

In Python 3.3+ there is tm_gmtoff attribute if underlying C library supports it:

utc_offset = time.localtime().tm_gmtoff

Note: time.daylight may give a wrong result in some edge cases.

tm_gmtoff is used automatically by datetime if it is available on Python 3.3+:

from datetime import datetime, timedelta, timezone

d = datetime.now(timezone.utc).astimezone()
utc_offset = d.utcoffset() // timedelta(seconds=1)

To get the current UTC offset in a way that workarounds the time.daylight issue and that works even if tm_gmtoff is not available, @jts's suggestion to substruct the local and UTC time can be used:

import time
from datetime import datetime

ts = time.time()
utc_offset = (datetime.fromtimestamp(ts) -
              datetime.utcfromtimestamp(ts)).total_seconds()

To get UTC offset for past/future dates, pytz timezones could be used:

from datetime import datetime
from tzlocal import get_localzone # $ pip install tzlocal

tz = get_localzone() # local timezone 
d = datetime.now(tz) # or some other local date 
utc_offset = d.utcoffset().total_seconds()

It works during DST transitions, it works for past/future dates even if the local timezone had different UTC offset at the time e.g., Europe/Moscow timezone in 2010-2015 period.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The utcoffset() function is used to return a timedelta object that represents the difference between the local time and UTC time.

    • This function is used in used in the datetime class of module datetime.
    • Here range of the utcoffset is “timedelta(hours=24) <= offset <= timedelta(hours=24)”.
    • If the offset is east of UTC, then it is considered positive and if the offset is west of UTC, then it is considered negative. Since there are 24 hours in a day, -timedelta(24) and timedelta(24) are the largest values possible.

    Syntax: utcoffset()

    Parameters: This function does not accept any parameter.

    Return values: This function returns a timedelta object representing the difference between the local time and UTC time.

    Example 1: Here output is None because now() function returns date and time in UTC format, hence the difference between the local time i.e, returned by now() function and UTC time is none.

    Python3

    from datetime import datetime

    import pytz

    date_time = datetime.now()

    print(date_time.utcoffset())

    Output:

    None

    Example 2: Finding the offset of time

    Python3

    from datetime import datetime

    import pytz

    naive = datetime.now()

    timezone = pytz.timezone("Asia/Kolkata")

    aware1 = timezone.localize(naive)

    print("Time ahead of UTC by:", aware1.utcoffset())

    Output:

    Time ahead of UTC by: 5:30:00

    Example 3: Finding time offset

    Python3

    from datetime import datetime

    import pytz

    naive = datetime.now()

    timezone = pytz.timezone("Asia/Tokyo")

    aware1 = timezone.localize(naive)

    print("Time ahead of UTC by:", aware1.utcoffset())

    Output:

    Time ahead of UTC by: 9:00:00


    How do I fetch UTC time in python?

    Getting the UTC timestamp Use the datetime. datetime. now() to get the current date and time. Then use tzinfo class to convert our datetime to UTC.

    What is Utcoffset in Python?

    The utcoffset() function is used to return a timedelta object that represents the difference between the local time and UTC time. This function is used in used in the datetime class of module datetime. Here range of the utcoffset is “timedelta(hours=24) <= offset <= timedelta(hours=24)”.

    How does Python calculate timezone offset?

    tzinfo.tzname(dt) : Returns the time zone name corresponding to the datetime object dt . This method returns the name that is used while creating the timezone object..
    tzinfo.utcoffset(dt) : This method returns the total offset from the UTC which should be a timedelta object. ... .
    tzinfo..

    What is Utcnow in Python?

    utcnow() Return the current UTC date and time, with tzinfo None. This is like now(), but returns the current UTC date and time, as a naive datetime object.