py-vobject / vobject

A full-featured Python package for parsing and creating iCalendar and vCard files
http://py-vobject.github.io/
29 stars 7 forks source link

Unable to write iCalendar file using datetime with tzinfo #69

Open da4089 opened 1 month ago

da4089 commented 1 month ago

Import from: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=851982

I get a strange exception when trying to write a iCalendar stream using datetime values with the timezone set. This is the output from the test script below:

 VCALENDAR
    VEVENT
       DTSTART: 2002-01-07 12:15:00+01:00
       UID: unique-uid
       SUMMARY: summary

Traceback (most recent call last):
  File "<stdin>", line 20, in <module>
  File "<stdin>", line 15, in main
  File "/usr/lib/pymodules/python2.7/vobject/base.py", line 186, in serialize
    return behavior.serialize(self, buf, lineLength, validate)
  File "/usr/lib/pymodules/python2.7/vobject/behavior.py", line 147, in serialize
    cls.generateImplicitParameters(obj)
  File "/usr/lib/pymodules/python2.7/vobject/icalendar.py", line 875, in generateImplicitParameters
    obj.add(TimezoneComponent(tzinfo=getTzid(tzid)))
  File "/usr/lib/pymodules/python2.7/vobject/icalendar.py", line 75, in __init__
    self.tzinfo = tzinfo
  File "/usr/lib/pymodules/python2.7/vobject/base.py", line 499, in __setattr__
    prop.fset(self, value)
  File "/usr/lib/pymodules/python2.7/vobject/icalendar.py", line 145, in settzinfo
    transition = getTransition(transitionTo, year, tzinfo)
  File "/usr/lib/pymodules/python2.7/vobject/icalendar.py", line 1897, in getTransition
    uncorrected = firstTransition(generateDates(year, month, day), test)
  File "/usr/lib/pymodules/python2.7/vobject/icalendar.py", line 1857, in firstTransition
    if not test(dt):
  File "/usr/lib/pymodules/python2.7/vobject/icalendar.py", line 1884, in test
    def test(dt): return tzinfo.dst(dt) != zeroDelta
  File "/usr/lib/python2.7/dist-packages/pytz/tzinfo.py", line 445, in dst
    dt = self.localize(dt, is_dst)
  File "/usr/lib/python2.7/dist-packages/pytz/tzinfo.py", line 327, in localize
    raise NonExistentTimeError(dt)
pytz.exceptions.NonExistentTimeError: 2000-03-26 02:00:00

I expected a iCalendar file with the dtstart value stored with timezone information. This is how to reproduce the problem:

% python <<EOF
#!/usr/bin/env python
import codecs
import datetime
import pytz
import vobject
def main():
    cal = vobject.newFromBehavior('vcalendar')
    tz = pytz.timezone('Europe/Oslo')
    ve = cal.add('vevent')
    dtstart = datetime.datetime(2002, 01, 07, 12, 15, 0, tzinfo=tz)
    ve.add('dtstart').value = dtstart
    ve.add('summary').value = 'summary'
    ve.add('uid').value = 'unique-uid'
    cal.prettyPrint()
    stream = cal.serialize()
    f = codecs.open('plan.ics', 'w')
    f.write(stream)
    f.close()
if __name__ == '__main__':
    main()
EOF

Is there a bug in the library or am I using it wrong?