How to get computer's UTC offset in Python?


The computer's UTC offset is the timezone set on your computer. YOu can get this timezone information using the time module. time.timezone returns UTC offset in seconds.

For example

import time
print(-time.timezone) # India's timezone: +5:30

Output

This will give the output −

19800

You can also use other workarounds to get the timezone information. You can create datetime objects for UTC and local timezones and subtract them and finally get the difference to find the timezone.

For example

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

Output

This will give the output −

19800

Updated on: 12-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements