python - Django icalendar dtstart 日期时间问题
问题描述
我在 Django-python 中有一个用于事件程序的表单。我正在尝试使用 icalendar 为事件创建一个 ics 文件,为此,我想从表单中的变量“starttime”和“endtime”中获取值“dtstart”和“dtend”,但我得到了代码:错误的日期时间格式。任何人有任何建议来解决这个问题?
错误
elif not ical[15:]:
return datetime(*timetuple)
elif ical[15:16] == 'Z':
return pytz.utc.localize(datetime(*timetuple))
else:
raise ValueError(ical)
except:
raise ValueError('Wrong datetime format: %s' % ical) …
class vDuration(object):
"""Subclass of timedelta that renders itself in the iCalendar DURATION
format.
"""
代码
def event(request, id=None):
instance = Event_cal()
if id:
instance = get_object_or_404(Event_cal, pk=id)
else:
instance = Event_cal()
form = EventForm(request.POST or None, instance=instance)
if request.POST and form.is_valid():
form.save()
startdate = request.POST.get('starttime')
endate = request.POST.get('endtime')
event = Event()
event.add('summary', 'My Summary')
event.add('dtstart', vDatetime.from_ical(startdate))
event.add('dtend', vDatetime.from_ical(endate))
在此先感谢,我正在学习python,所以我没有太多经验。
解决方案
将日期时间重新格式化为 RFC5545 格式之一。请参阅日期时间格式的 RFC5545 规范说明:https ://www.rfc-editor.org/rfc/rfc5545#section-3.3.5 。
有 3 种可接受的日期时间格式:
- 本地或“浮动”,例如:19980118T230000
- 带有 UTC 时间的日期,例如:19980119T070000Z 和
- 带有本地时间和时区参考的日期,例如:TZID=America/New_York:19980119T020000
推荐阅读
- haskell - 仅在没有 stack.yaml 的情况下构建 LTS 依赖项
- html - 试图居中导航栏
- sql - 我可以每月自动执行一个存储过程而不创建作业吗?
- github-api - 使用 REST API 获取手动调用的 GitHub 工作流的运行 ID
- python - 为什么我们几乎总是要在减少引用计数之前重新分配对象成员?
- html - 如何从 Microsoft Power BI 在 iframe 中加载字体?
- amazon-web-services - 使用 AWS Glue 将数据从 AWS Aurora MySQL 移动到另一个 AWS Aurora MySQL
- c - 本地静态变量何时初始化
- excel - 如何过滤另一个工作表上的另一个数据透视表?
- javascript - React setselectedType 不会立即更新 selectedType