【发布时间】:2014-04-25 08:59:40
【问题描述】:
我正在 Python 模块中编写一个方法,试图让用户的生活更轻松。此方法实现在该日历中创建事件。
def update_event(start_datetime=None, end_datetime=None, description=None):
'''
Args:
start_date: string datetime in the format YYYY-MM-DD or in RFC 3339
end_date: string datetime in the format YYYY-MM-DD or in RFC 3339
description: string with description (\n are transforrmed into new lines)
'''
如果用户指定 start_date 或 end_date,则应进行检查以确定日期是 YYYY-MM-DD 格式还是日期时间 RFC 3339 格式。
if (start_date is not None):
# Check whether we have an date or datetime value
# Whole day events are represented as YYYY-MM-DD
# Other events are represented as 2014-04-25T10:47:00.000-07:00
whole_day_event = False
try:
new_start_time = datetime.datetime.strptime(start_date,'YYYY-MM-DD')
# Here the event date is updated
try:
new_start_time = datetime.datetime.strptime(start_date,'%Y-%m-%dT%H:%M:%S%z')
#Here the event date is updated
except ValueError:
return (ErrorCodeWhatever)
except ValueError:
return (ErrorCodeWhatever)
这是一个好方法吗?我可以以更好的方式检查我收到的日期吗? 谢谢!
【问题讨论】:
-
你应该
raise错误,不要return他们。 -
你说得对,我只是完成代码来发布问题
标签: python exception datetime strptime