From cb5872c1a05b05ab4d780e2210827194ef687c6d Mon Sep 17 00:00:00 2001 From: Philipp Weissmann Date: Thu, 2 Oct 2014 10:55:33 +0200 Subject: [PATCH] Use pytz instead of /etc/timezone; Python3 compatible --- pyicloud/services/calendar.py | 36 ++++++++++++++++++++++++++++++----- requirements.txt | 1 + 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/pyicloud/services/calendar.py b/pyicloud/services/calendar.py index 4f9ae07..47d3403 100644 --- a/pyicloud/services/calendar.py +++ b/pyicloud/services/calendar.py @@ -1,7 +1,8 @@ from __future__ import absolute_import -import os -from datetime import datetime +from datetime import datetime, timedelta from calendar import monthrange +import time +import pytz class CalendarService(object): @@ -16,12 +17,37 @@ class CalendarService(object): self._calendar_refresh_url = '%s/events' % self._calendar_endpoint self._calendar_event_detail_url = '%s/eventdetail' % self._calendar_endpoint + def get_possible_timezones(self): + """ + Return all possible timezones in Olzon 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: http://stackoverflow.com/a/7841417 + Retrieves the system's timezone from a list of possible options. + Just take the first one """ - return '/'.join(os.readlink('/etc/localtime').split('/')[-2:]) + return self.get_possible_timezones()[0] def get_event_detail(self, pguid, guid): """ diff --git a/requirements.txt b/requirements.txt index ed7dc8f..6b77c7d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ requests>=1.2 six +pytz