Replace manual time zone handling with use of the tzlocal module
This solves an issue where we would end up with an empty list of time zones, throwing an exception as a result. It also resolves to the user's actual Olson time zone name, instead of the first of all the possible time zone names that match the current time zone of the user. The two helper methods are no longer needed, as we can now use get_localzone() directly.
This commit is contained in:
parent
8e45bf98db
commit
a6aa998a04
3 changed files with 8 additions and 71 deletions
|
@ -3,7 +3,7 @@ from datetime import datetime, timedelta
|
||||||
from calendar import monthrange
|
from calendar import monthrange
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import pytz
|
from tzlocal import get_localzone
|
||||||
|
|
||||||
|
|
||||||
class CalendarService(object):
|
class CalendarService(object):
|
||||||
|
@ -20,45 +20,13 @@ class CalendarService(object):
|
||||||
self._calendar_endpoint,
|
self._calendar_endpoint,
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_all_possible_timezones_of_local_machine(self):
|
|
||||||
"""
|
|
||||||
Return all possible timezones in Olson TZ notation
|
|
||||||
This has been taken from
|
|
||||||
http://stackoverflow.com/questions/7669938
|
|
||||||
"""
|
|
||||||
local_names = []
|
|
||||||
if time.daylight:
|
|
||||||
local_offset = time.altzone
|
|
||||||
localtz = time.tzname[1]
|
|
||||||
else:
|
|
||||||
local_offset = time.timezone
|
|
||||||
localtz = time.tzname[0]
|
|
||||||
|
|
||||||
local_offset = timedelta(seconds=-local_offset)
|
|
||||||
|
|
||||||
for name in pytz.all_timezones:
|
|
||||||
timezone = pytz.timezone(name)
|
|
||||||
if not hasattr(timezone, '_tzinfos'):
|
|
||||||
continue
|
|
||||||
for (utcoffset, daylight, tzname), _ in timezone._tzinfos.items():
|
|
||||||
if utcoffset == local_offset and tzname == localtz:
|
|
||||||
local_names.append(name)
|
|
||||||
return local_names
|
|
||||||
|
|
||||||
def get_system_tz(self):
|
|
||||||
"""
|
|
||||||
Retrieves the system's timezone from a list of possible options.
|
|
||||||
Just take the first one
|
|
||||||
"""
|
|
||||||
return self.get_all_possible_timezones_of_local_machine()[0]
|
|
||||||
|
|
||||||
def get_event_detail(self, pguid, guid):
|
def get_event_detail(self, pguid, guid):
|
||||||
"""
|
"""
|
||||||
Fetches a single event's details by specifying a pguid
|
Fetches a single event's details by specifying a pguid
|
||||||
(a calendar) and a guid (an event's ID).
|
(a calendar) and a guid (an event's ID).
|
||||||
"""
|
"""
|
||||||
params = dict(self.params)
|
params = dict(self.params)
|
||||||
params.update({'lang': 'en-us', 'usertz': self.get_system_tz()})
|
params.update({'lang': 'en-us', 'usertz': get_localzone().zone})
|
||||||
url = '%s/%s/%s' % (self._calendar_event_detail_url, pguid, guid)
|
url = '%s/%s/%s' % (self._calendar_event_detail_url, pguid, guid)
|
||||||
req = self.session.get(url, params=params)
|
req = self.session.get(url, params=params)
|
||||||
self.response = req.json()
|
self.response = req.json()
|
||||||
|
@ -79,7 +47,7 @@ class CalendarService(object):
|
||||||
params = dict(self.params)
|
params = dict(self.params)
|
||||||
params.update({
|
params.update({
|
||||||
'lang': 'en-us',
|
'lang': 'en-us',
|
||||||
'usertz': self.get_system_tz(),
|
'usertz': get_localzone().zone,
|
||||||
'startDate': from_dt.strftime('%Y-%m-%d'),
|
'startDate': from_dt.strftime('%Y-%m-%d'),
|
||||||
'endDate': to_dt.strftime('%Y-%m-%d')
|
'endDate': to_dt.strftime('%Y-%m-%d')
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,9 +2,10 @@ from __future__ import absolute_import
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
import pytz
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
from tzlocal import get_localzone
|
||||||
|
|
||||||
|
|
||||||
class RemindersService(object):
|
class RemindersService(object):
|
||||||
def __init__(self, service_root, session, params):
|
def __init__(self, service_root, session, params):
|
||||||
|
@ -16,44 +17,12 @@ class RemindersService(object):
|
||||||
|
|
||||||
self.refresh()
|
self.refresh()
|
||||||
|
|
||||||
def get_all_possible_timezones_of_local_machine(self):
|
|
||||||
"""
|
|
||||||
Return all possible timezones in Olson TZ notation
|
|
||||||
This has been taken from
|
|
||||||
http://stackoverflow.com/questions/7669938
|
|
||||||
"""
|
|
||||||
local_names = []
|
|
||||||
if time.daylight:
|
|
||||||
local_offset = time.altzone
|
|
||||||
localtz = time.tzname[1]
|
|
||||||
else:
|
|
||||||
local_offset = time.timezone
|
|
||||||
localtz = time.tzname[0]
|
|
||||||
|
|
||||||
local_offset = timedelta(seconds=-local_offset)
|
|
||||||
|
|
||||||
for name in pytz.all_timezones:
|
|
||||||
timezone = pytz.timezone(name)
|
|
||||||
if not hasattr(timezone, '_tzinfos'):
|
|
||||||
continue
|
|
||||||
for (utcoffset, daylight, tzname), _ in timezone._tzinfos.items():
|
|
||||||
if utcoffset == local_offset and tzname == localtz:
|
|
||||||
local_names.append(name)
|
|
||||||
return local_names
|
|
||||||
|
|
||||||
def get_system_tz(self):
|
|
||||||
"""
|
|
||||||
Retrieves the system's timezone from a list of possible options.
|
|
||||||
Just take the first one
|
|
||||||
"""
|
|
||||||
return self.get_all_possible_timezones_of_local_machine()[0]
|
|
||||||
|
|
||||||
def refresh(self):
|
def refresh(self):
|
||||||
params_reminders = dict(self.params)
|
params_reminders = dict(self.params)
|
||||||
params_reminders.update({
|
params_reminders.update({
|
||||||
'clientVersion': '4.0',
|
'clientVersion': '4.0',
|
||||||
'lang': 'en-us',
|
'lang': 'en-us',
|
||||||
'usertz': self.get_system_tz()
|
'usertz': get_localzone().zone
|
||||||
})
|
})
|
||||||
|
|
||||||
# Open reminders
|
# Open reminders
|
||||||
|
@ -108,7 +77,7 @@ class RemindersService(object):
|
||||||
params_reminders.update({
|
params_reminders.update({
|
||||||
'clientVersion': '4.0',
|
'clientVersion': '4.0',
|
||||||
'lang': 'en-us',
|
'lang': 'en-us',
|
||||||
'usertz': self.get_system_tz()
|
'usertz': get_localzone().zone
|
||||||
})
|
})
|
||||||
|
|
||||||
req = self.session.post(
|
req = self.session.post(
|
||||||
|
|
|
@ -3,6 +3,6 @@ keyring>=8.0,<9.0
|
||||||
keyrings.alt>=1.0,<2.0
|
keyrings.alt>=1.0,<2.0
|
||||||
click>=6.0,<7.0
|
click>=6.0,<7.0
|
||||||
six
|
six
|
||||||
pytz
|
tzlocal
|
||||||
certifi
|
certifi
|
||||||
bitstring
|
bitstring
|
||||||
|
|
Loading…
Reference in a new issue