In Python, how do you find what UTC time offset the computer is set to?
utcoffset() Method with Example. 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)” ...
The JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.
Definition and Usage. getTimezoneOffset() returns the difference between UTC time and local time. getTimezoneOffset() returns the difference in minutes. For example, if your time zone is GMT+2, -120 will be returned.
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.
gmtime()
will return the UTC time and localtime()
will return the local time so subtracting the two should give you the utc offset.
From https://pubs.opengroup.org/onlinepubs/009695399/functions/gmtime.html
The gmtime() function shall convert the time in seconds since the Epoch pointed to by timer into a broken-down time, expressed as Coordinated Universal Time (UTC).
So, despite the name gmttime
, the function returns UTC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With