【发布时间】:2014-02-03 09:45:52
【问题描述】:
我正在使用 vobject 在 Django 中创建一个 ical 事件。我在使用较低级别的代码时遇到问题。看起来 ical 正在尝试使用 obj.add(TimezoneComponent(tzinfo=getTzid(tzid))) 获取时区。但后来我从 pytz 得到raise NonExistentTimeError(dt)。关于做什么的任何建议?当我使用变量 start1 中的打印语句查看它们时,年、月、日显示正确。
File "/home/git/chrono/chrono/requests_app/views.py", line 110, in form_valid
ics_form = create_ics(data)
File "/home/git/chrono/chrono/requests_app/views.py", line 126, in create_ics
response = HttpResponse(cal.serialize(), content_type='text/calendar')
File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/vobject/base.py", line 186, in serialize
return behavior.serialize(self, buf, lineLength, validate)
File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/vobject/behavior.py", line 147, in serialize
cls.generateImplicitParameters(obj)
File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/vobject/icalendar.py", line 853, in generateImplicitParameters
obj.add(TimezoneComponent(tzinfo=getTzid(tzid)))
File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/vobject/icalendar.py", line 75, in __init__
self.tzinfo = tzinfo
File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/vobject/base.py", line 468, in __setattr__
prop.fset(self, value)
File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/vobject/icalendar.py", line 145, in settzinfo
transition = getTransition(transitionTo, year, tzinfo)
File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/vobject/icalendar.py", line 1856, in getTransition
uncorrected = firstTransition(generateDates(year, month, day), test)
File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/vobject/icalendar.py", line 1816, in firstTransition
if not test(dt):
File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/vobject/icalendar.py", line 1843, in test
def test(dt): return tzinfo.dst(dt) != zeroDelta
File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/pytz/tzinfo.py", line 445, in dst
dt = self.localize(dt, is_dst)
File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/pytz/tzinfo.py", line 327, in localize
raise NonExistentTimeError(dt)
NonExistentTimeError: 2000-04-02 02:00:00
def create_ics(data):
start1 = data['date_due']
print start1.day
start2 = datetime.datetime(start1.year, start1.month, start1.day)
start3 = data['action']
cal = vobject.iCalendar()
cal.add('method').value = 'PUBLISH'
vevent = cal.add('vevent')
vevent.add('dtstart').value = start1
vevent.add('dtend').value = start2
vevent.add('dtstamp').value = datetime.datetime.now()
vevent.add('summary').value = data['action'].name
response = HttpResponse(cal.serialize(), content_type='text/calendar')
response['Filename'] = 'filename.ics'
response['Content-Disposition'] = 'attachment; filename=filename.ics'
return response
来自模型,日期时间字段:
date_due = models.DateTimeField()
更新:
发现我必须放置:
>>> utc = vobject.icalendar.utc
>>> start = cal.vevent.add('dtstart')
>>> start.value = datetime.datetime(2006, 2, 16, tzinfo = utc)
进入它,它起作用了。
【问题讨论】:
-
我尝试添加该代码,但我仍然遇到同样的崩溃。有谁知道 2000 年的日期来自哪里,或者为什么添加另一个事件修复了 @dman 的崩溃?
-
已经很久了,在 django 世界里有很多事情可以改变 :)
-
我认为这个错误实际上与 Django 没有任何关系。就我而言,我发现我在转换为 iCalendar 之前不必要地调用了
pytz.timezone.normalize()。我取消了通话,崩溃消失了。 -
我仍然不知道 2000 年的日期是从哪里来的。
标签: python django timezone pytz vobject