Skip to content

Commit fd97b0f

Browse files
author
Alejandro Casanovas
committed
Fix issue #1011 with timezone conversion (from Windows timezones to IANA ones) using the python 3.9 ZoneInfo objects
1 parent 4c10aed commit fd97b0f

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

O365/connection.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from requests_oauthlib import OAuth2Session
1515
from stringcase import pascalcase, camelcase, snakecase
1616
from tzlocal import get_localzone
17-
from zoneinfo import ZoneInfoNotFoundError
17+
from zoneinfo import ZoneInfoNotFoundError, ZoneInfo
1818
from .utils import ME_RESOURCE, BaseTokenBackend, FileSystemTokenBackend, Token
1919
import datetime as dt
2020

@@ -96,13 +96,19 @@ def __init__(self, *, protocol_url=None, api_version=None,
9696
self.default_resource = default_resource or ME_RESOURCE
9797
self.use_default_casing = True if casing_function is None else False
9898
self.casing_function = casing_function or camelcase
99-
if timezone and isinstance(timezone, str):
100-
timezone = dt.timezone(timezone)
101-
try:
102-
self.timezone = timezone or get_localzone()
103-
except ZoneInfoNotFoundError as e:
104-
log.debug('Timezone not provided and the local timezone could not be found. Default to UTC.')
105-
self.timezone = dt.timezone.utc
99+
if timezone:
100+
if isinstance(timezone, str):
101+
# convert string to ZoneInfo
102+
try:
103+
timezone = ZoneInfo(timezone)
104+
except ZoneInfoNotFoundError:
105+
log.debug(f'Timezone {timezone} could not be found. Will default to UTC.')
106+
timezone = ZoneInfo('UTC')
107+
else:
108+
if not isinstance(timezone, ZoneInfo):
109+
raise ValueError(f'The timezone parameter must be either a string or a valid ZoneInfo instance.')
110+
# get_localzone() from tzlocal will try to get the system local timezone and if not will return UTC
111+
self.timezone = timezone or get_localzone()
106112
self.max_top_value = 500 # Max $top parameter value
107113

108114
# define any keyword that can be different in this protocol

O365/utils/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import datetime as dt
2-
from zoneinfo import ZoneInfoNotFoundError
32
import logging
43
from collections import OrderedDict
54
from enum import Enum
6-
import zoneinfo
5+
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
76

87
from dateutil.parser import parse
98
from stringcase import snakecase
@@ -431,7 +430,7 @@ def _parse_date_time_time_zone(self, date_time_time_zone, is_all_day=False):
431430
local_tz = self.protocol.timezone
432431
if isinstance(date_time_time_zone, dict):
433432
try:
434-
timezone = zoneinfo.ZoneInfo(
433+
timezone = ZoneInfo(
435434
get_iana_tz(date_time_time_zone.get(self._cc('timeZone'), 'UTC')))
436435
except ZoneInfoNotFoundError:
437436
timezone = local_tz

O365/utils/windows_tz.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Mapping from iana timezones to windows timezones and vice versa
33
"""
44
from datetime import tzinfo
5-
from zoneinfo import ZoneInfoNotFoundError
5+
from zoneinfo import ZoneInfoNotFoundError, ZoneInfo
66

77
# noinspection SpellCheckingInspection
88
IANA_TO_WIN = {
@@ -628,16 +628,15 @@ def get_iana_tz(windows_tz):
628628
return timezone
629629

630630

631-
def get_windows_tz(iana_tz):
631+
def get_windows_tz(iana_tz: ZoneInfo) -> str:
632632
""" Returns a valid windows TimeZone from a given pytz TimeZone
633633
(Iana/Olson Timezones)
634634
Note: Windows Timezones are SHIT!... no ... really THEY ARE
635635
HOLY FUCKING SHIT!.
636636
"""
637637
timezone = IANA_TO_WIN.get(
638-
iana_tz.zone if isinstance(iana_tz, tzinfo) else iana_tz)
638+
iana_tz.key if isinstance(iana_tz, tzinfo) else iana_tz)
639639
if timezone is None:
640-
raise ZoneInfoNotFoundError(
641-
"Can't find Iana TimeZone " + iana_tz.zone)
640+
raise ZoneInfoNotFoundError(f"Can't find Iana timezone {iana_tz.key}")
642641

643642
return timezone

0 commit comments

Comments
 (0)