0

Given a list of pytz.timezone objects, and a start datetime and end datetime defining a range of several months, how do you figure out which timezone has the most-negative UTC offset of all the timezones across the range?

Put another way, find the point in the range at which one of the timezones has a lower UTC offset than any other timezone at any other point in the range, and return the UTC offset.

Ignoring Daylight Saving Time, this is relatively simple: use a method like this to get the UTC offset of each of the timezones in turn for an arbitrary point in the range, and keep the lowest on each iteration.

Taking DST into account makes this more complicated, because you need to care about whether or not the time range contains DST for any of the geographic areas associated with the given timezones. It seems like this is not an officially supported use case for pytz, and that backports.zoneinfo doesn't either but might make it slightly easier.

What might the implementation of this function look like?

def _get_most_utcnegative_timezone(timezones, range_start, range_end):
    pass
Emmett Butler
  • 5,969
  • 2
  • 29
  • 47
  • I think you should use the standard (about maximum and minimum possible offset). Countries may have a specific timezone, but ocean can still have one, and it is legal to use also the others (e.g. when navigating). Else, you will see the data file of timezones, you may want to parse it directly, and extract dates – Giacomo Catenazzi May 27 '21 at 18:38
  • to clarify, what ranges are we talking about, days, months, years? – FObersteiner May 27 '21 at 18:54
  • Edited for clarity - the ranges can be multiple months in size – Emmett Butler May 27 '21 at 20:04
  • For multiple month, I think the "brute force way" to just generate the date ranges (with hourly frequency for example) could be an option. pandas has the tools to do that conveniently. But if you want a nice, cannonical solution, I'm with Giacomo here; loading a tz database and comparing transition times of the time zone seems like the way to go. – FObersteiner May 28 '21 at 05:38

0 Answers0