Python Forum
pytz: get integer utc offset from Olsen region/city
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pytz: get integer utc offset from Olsen region/city
#1
Hello folks,

Newbie Python programmer here. I am just beginning to learn the pytz time zone module.

Can you please explain how I get an UTC offset integer value from an Olsen / IANA timezone with the "region/city" format?

For example, I would like to get the integer value "2" as output from the input "Europe/Copenhagen", because this timezone is UTC+2 (during daylight saving, else UTC+1)

Cheers, and thanks in advance!

Mike
Reply
#2
The offset depends on when you are asking (can be affected by daylight saving time). So you must supply a reference time. datetime.now() will work if you are interested in the offset when the program runs.

Given that, you can ask for the utcoffset, put it all into seconds, then divide by the number of seconds in an hour.

>>> cop_tz = timezone('Europe/Copenhagen') >>> cop_tz.utcoffset(datetime.now()).total_seconds() / (60*60) # "Now" is during summer/DST 2.0 >>> cop_tz.utcoffset(datetime(2020, 1, 1, 0, 0, 0)).total_seconds() / (60*60) # During winter/ST 1.0 >>> timezone('America/Los_Angeles').utcoffset(datetime.now()).total_seconds() / (60*60) # Negative/westerly offset -7.0 >>> timezone('Asia/Kolkata').utcoffset(datetime.now()).total_seconds() / (60*60) # Noninteger offset 5.5
Reply
#3
Thanks, this solves my problem! I forgot that you can't simply translate an IANA / Olson timezone into a UTC offset, because it will vary over time due to daylight saving. You need a specific date/time to get a useful UTC offset.

I also found this link very helpful:
https://stackoverflow.com/questions/2956...m-timezone

import pytz import datetime tz = pytz.timezone('Asia/Calcutta') dt = datetime.datetime.utcnow() offset_seconds = tz.utcoffset(dt).seconds offset_hours = offset_seconds / 3600.0 print "{:+d}:{:02d}".format(int(offset_hours), int((offset_hours % 1) * 60)) # +5:30
Cheers, Mike
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using locationtagger to extract locations found in a specific country/region lord_of_cinder 1 2,612 Oct-04-2022, 12:46 AM
Last Post: Larz60+
  naming entities(city abbreviations) tirumalaramakrishna 1 2,263 May-06-2022, 11:22 AM
Last Post: jefsummers
  I have problem with ACO, I want to always start in the first city and end in the last pKKy 0 2,403 Jan-28-2021, 05:58 PM
Last Post: pKKy
  Add new line after finding last string in a region Nigel11 1 2,878 Aug-08-2020, 10:00 PM
Last Post: Larz60+
  pyautogui.screenshot region is not working alexlu 6 10,934 Jun-04-2020, 10:46 AM
Last Post: alexlu
  how to write offset number to disk use python? Pyguys 4 5,168 Apr-11-2020, 07:53 AM
Last Post: Pyguys
  search binary file and list all founded keyword offset Pyguys 4 5,164 Mar-17-2020, 06:46 AM
Last Post: Pyguys
  Python function executes twice when region loop is present bluethundr 2 3,853 Jan-07-2020, 03:01 PM
Last Post: bluethundr
  code for finding a cables crossed the bondary of the city spider_pytthon 8 5,910 Dec-24-2019, 07:23 AM
Last Post: spider_pytthon
  pyautogui screenshotting region relative to image found on screen Bmart6969 3 10,582 Oct-05-2019, 06:20 PM
Last Post: Bmart6969

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.