How to get computer's UTC offset in Python?

PythonServer Side ProgrammingProgramming

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
raja
Published on 08-Jan-2018 12:30:39
Advertisements