def timestamp_parameter(timestamp, allow_none=True):
"""Function to check the conformance of timestamps passed by users.
This will check that a string is a valid format and allow users to pass a
datetime object which we will then convert to a proper ISO8601 date-time
string.
:param timestamp: string to be validated or datetime object to be
converted.
:param bool allow_none: whether or not to allow timestamp to be None.
Default: ``True``
:returns: valid ISO8601 string
:rtype: str
:raises: ValueError
"""
if timestamp is None:
if allow_none:
return None
raise ValueError("Timestamp value cannot be None")
if isinstance(timestamp, datetime.datetime):
return timestamp.isoformat() + 'Z'
if isinstance(timestamp, compat.basestring):
if not ISO_8601.match(timestamp):
raise ValueError(("Invalid timestamp: %s is not a valid ISO-8601"
" formatted date") % timestamp)
return timestamp
raise ValueError("Cannot accept type %s for timestamp" % type(timestamp))
评论列表
文章目录