From a6aa998a049589a7c6396f4210baaefc22392e7e Mon Sep 17 00:00:00 2001 From: Bart274 Date: Sat, 12 Mar 2016 13:17:47 +0100 Subject: [PATCH] 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. --- pyicloud/services/calendar.py | 38 +++------------------------------ pyicloud/services/reminders.py | 39 ++++------------------------------ requirements.txt | 2 +- 3 files changed, 8 insertions(+), 71 deletions(-) diff --git a/pyicloud/services/calendar.py b/pyicloud/services/calendar.py index 1ed03f6..49cf118 100644 --- a/pyicloud/services/calendar.py +++ b/pyicloud/services/calendar.py @@ -3,7 +3,7 @@ from datetime import datetime, timedelta from calendar import monthrange import time -import pytz +from tzlocal import get_localzone class CalendarService(object): @@ -20,45 +20,13 @@ class CalendarService(object): 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): """ Fetches a single event's details by specifying a pguid (a calendar) and a guid (an event's ID). """ 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) req = self.session.get(url, params=params) self.response = req.json() @@ -79,7 +47,7 @@ class CalendarService(object): params = dict(self.params) params.update({ 'lang': 'en-us', - 'usertz': self.get_system_tz(), + 'usertz': get_localzone().zone, 'startDate': from_dt.strftime('%Y-%m-%d'), 'endDate': to_dt.strftime('%Y-%m-%d') }) diff --git a/pyicloud/services/reminders.py b/pyicloud/services/reminders.py index ae26204..38b8d41 100644 --- a/pyicloud/services/reminders.py +++ b/pyicloud/services/reminders.py @@ -2,9 +2,10 @@ from __future__ import absolute_import from datetime import datetime, timedelta import time import uuid -import pytz import json +from tzlocal import get_localzone + class RemindersService(object): def __init__(self, service_root, session, params): @@ -16,44 +17,12 @@ class RemindersService(object): 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): params_reminders = dict(self.params) params_reminders.update({ 'clientVersion': '4.0', 'lang': 'en-us', - 'usertz': self.get_system_tz() + 'usertz': get_localzone().zone }) # Open reminders @@ -108,7 +77,7 @@ class RemindersService(object): params_reminders.update({ 'clientVersion': '4.0', 'lang': 'en-us', - 'usertz': self.get_system_tz() + 'usertz': get_localzone().zone }) req = self.session.post( diff --git a/requirements.txt b/requirements.txt index 3985151..a9385ad 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,6 @@ keyring>=8.0,<9.0 keyrings.alt>=1.0,<2.0 click>=6.0,<7.0 six -pytz +tzlocal certifi bitstring