def parse_timestamp(request, name, default_value=None, required=True): '''Parses any valid ISO datetime, duration, or timestamp parameter from the given request. :param request: The context of an active HTTP request. :type request: :class:`rest_framework.request.Request` :param name: The name of the parameter to parse. :type name: str :param default_value: The name of the parameter to parse. :type default_value: datetime.timedelta :param required: Indicates whether or not the parameter is required. An exception will be raised if the parameter does not exist, there is no default value, and required is True. :type required: bool :returns: The value of the named parameter or the default value if provided. :rtype: datetime.datetime :raises :class:`util.rest.BadParameter`: If the value cannot be parsed. ''' value = _get_param(request, name, default_value, required) if not isinstance(value, basestring): return value try: result = parse_util.parse_timestamp(value) if result: return result raise except: raise BadParameter('Invalid ISO timestamp format for parameter: %s' % name)
def parse_timestamp(request, name, default_value=None, required=True): """Parses any valid ISO datetime, duration, or timestamp parameter from the given request. :param request: The context of an active HTTP request. :type request: :class:`rest_framework.request.Request` :param name: The name of the parameter to parse. :type name: string :param default_value: The name of the parameter to parse. :type default_value: datetime.datetime or datetime.timedelta or string :param required: Indicates whether or not the parameter is required. An exception will be raised if the parameter does not exist, there is no default value, and required is True. :type required: bool :returns: The value of the named parameter or the default value if provided. :rtype: datetime.datetime :raises :class:`util.rest.BadParameter`: If the value cannot be parsed. """ value = _get_param(request, name, default_value, required) if not isinstance(value, basestring): return value try: result = parse_util.parse_timestamp(value) if result: return result raise except: raise BadParameter('Invalid ISO timestamp format for parameter: %s' % name)
def test_parse_timestamp_datetime(self): """Tests parsing a valid ISO datetime.""" self.assertEqual(parse_util.parse_timestamp('2015-01-01T00:00:00Z'), datetime.datetime(2015, 1, 1, tzinfo=utc))
def test_parse_timestamp_duration(self, mock_now): """Tests parsing a valid ISO duration.""" mock_now.return_value = datetime.datetime(2015, 1, 1, 10, tzinfo=utc) self.assertEqual(parse_util.parse_timestamp('PT3H0M0S'), datetime.datetime(2015, 1, 1, 7, tzinfo=utc))
def test_parse_timestamp_datetime(self): """Tests parsing a valid ISO datetime.""" self.assertEqual(parse_util.parse_timestamp('2015-01-01T00:00:00Z'), datetime.datetime(2015, 1, 1, tzinfo=timezone.utc))
def test_parse_timestamp_duration(self, mock_now): """Tests parsing a valid ISO duration.""" mock_now.return_value = datetime.datetime(2015, 1, 1, 10, tzinfo=timezone.utc) self.assertEqual(parse_util.parse_timestamp('PT3H0M0S'), datetime.datetime(2015, 1, 1, 7, tzinfo=timezone.utc))