From 7430b66862346fe3a6a100ab25e35a8711446717 Mon Sep 17 00:00:00 2001 From: Johannes Raggam Date: Thu, 22 Nov 2012 17:33:41 +0100 Subject: [PATCH] Correctly handle tzinfo objects parsed with dateutil. Fixes #77. Tests missing. --- docs/changelog.rst | 3 +++ src/icalendar/parser.py | 8 ++++++++ src/icalendar/prop.py | 21 ++++++++++----------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 564d028..76de7ca 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -4,6 +4,9 @@ Changelog 3.2dev ------ +* Correctly handle tzinfo objects parsed with dateutil. Fixes #77. + [warvariuc, thet] + * Text values are escaped correclty. Fixes #74. [warvariuc] diff --git a/src/icalendar/parser.py b/src/icalendar/parser.py index 2d407be..731124a 100644 --- a/src/icalendar/parser.py +++ b/src/icalendar/parser.py @@ -32,6 +32,14 @@ def unescape_char(text): .replace(r'\;', ';')\ .replace('\\\\', '\\') +def tzinfo_from_dt(dt): + tzid = None + if hasattr(dt.tzinfo, 'zone'): + tzid = dt.tzinfo.zone # pytz implementation + elif hasattr(dt.tzinfo, 'tzname'): + tzid = dt.tzinfo.tzname(dt) # dateutil implementation + return tzid + def foldline(text, length=75, newline='\r\n'): """Make a string folded per RFC5545 (each line must be less than 75 octets) diff --git a/src/icalendar/prop.py b/src/icalendar/prop.py index 3ccf779..eaa926a 100644 --- a/src/icalendar/prop.py +++ b/src/icalendar/prop.py @@ -53,6 +53,7 @@ from icalendar.caselessdict import CaselessDict from icalendar.parser import Parameters from icalendar.parser import escape_char from icalendar.parser import unescape_char +from icalendar.parser import tzinfo_from_dt SequenceTypes = [TupleType, ListType] @@ -557,18 +558,15 @@ class vDatetime: def to_ical(self): dt = self.dt - if hasattr(dt.tzinfo, 'zone'): - tzid = dt.tzinfo and dt.tzinfo.zone or None - else: - tzid = dt.tzinfo and dt.tzinfo.tzname(self.dt) or None + tzid = tzinfo_from_dt(dt) s = "%04d%02d%02dT%02d%02d%02d" % ( - self.dt.year, - self.dt.month, - self.dt.day, - self.dt.hour, - self.dt.minute, - self.dt.second + dt.year, + dt.month, + dt.day, + dt.hour, + dt.minute, + dt.second ) if tzid == 'UTC': s += "Z" @@ -784,7 +782,8 @@ class vPeriod: self.params = Parameters() # set the timezone identifier - tzid = start.tzinfo and start.tzinfo.zone or None + # does not support different timezones for start and end + tzid = tzinfo_from_dt(start) if tzid: self.params['TZID'] = tzid