def _parse_time(time_str, property_name): """ Parse the time from the given string, convert to UTC, and return the datetime object Parameters ---------- time_str : str The time to parse property_name : str Name of the property where this time came from. Used in the exception raised if time is not parseable Returns ------- datetime.datetime Parsed datetime object Raises ------ samcli.commands.exceptions.UserException If the string cannot be parsed as a timestamp """ if not time_str: return None parsed = parse_date(time_str) if not parsed: raise InvalidTimestampError( "Unable to parse the time provided by '{}'".format( property_name)) return to_utc(parsed)
def test_with_timezone(self): date = parse_date("2018-07-06 13:09:54 PDT") expected = datetime.datetime(2018, 7, 6, 20, 9, 54) result = to_utc(date) self.assertEqual(expected, result)
def test_without_timezone(self): date = parse_date("2018-07-06T13:09:54Z").replace(tzinfo=None) expected = datetime.datetime(2018, 7, 6, 13, 9, 54) result = to_utc(date) self.assertEqual(expected, result)
def test_without_timezone(self): date = parse_date("2018-07-06T13:09:54Z").replace(tzinfo=None) expected = datetime.datetime(2018, 7, 6, 13, 9, 54) result = to_utc(date) self.assertEquals(expected, result)
def test_with_timezone(self): date = parse_date("2018-07-06 13:09:54 PDT") expected = datetime.datetime(2018, 7, 6, 20, 9, 54) result = to_utc(date) self.assertEquals(expected, result)
def _parse_time(time_str, property_name): """ Parse the time from the given string, convert to UTC, and return the datetime object Parameters ---------- time_str : str The time to parse property_name : str Name of the property where this time came from. Used in the exception raised if time is not parseable Returns ------- datetime.datetime Parsed datetime object Raises ------ samcli.commands.exceptions.UserException If the string cannot be parsed as a timestamp """ if not time_str: return parsed = parse_date(time_str) if not parsed: raise UserException("Unable to parse the time provided by '{}'".format(property_name)) return to_utc(parsed)
def test_must_parse_relative_time_in_utc(self): now = datetime.datetime.utcnow() date_str = "1hour ago" # Strip out microseconds & seconds since we only care about hours onwards expected = (now - datetime.timedelta(hours=1)).replace(microsecond=0, second=0) result = parse_date(date_str).replace(microsecond=0, second=0) self.assertEqual(expected, result)
def test_must_parse_relative_time_in_utc(self): now = datetime.datetime.utcnow() date_str = "1hour ago" # Strip out microseconds & seconds since we only care about hours onwards expected = (now - datetime.timedelta(hours=1)).replace(microsecond=0, second=0) result = parse_date(date_str).replace(microsecond=0, second=0) self.assertEquals(expected, result)
def test_must_parse_date(self): date_str = "2018-07-06T13:09:54" expected = datetime.datetime(2018, 7, 6, 13, 9, 54) self.assertEqual(expected, parse_date(date_str))
def test_must_parse_date(self): date_str = "2018-07-06T13:09:54" expected = datetime.datetime(2018, 7, 6, 13, 9, 54) self.assertEquals(expected, parse_date(date_str))