Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_excessive_subsecond_precision(self):
self.assertEqual(
ciso8601.parse_datetime('20140203T103527.234567891234'),
datetime.datetime(2014, 2, 3, 10, 35, 27, 234567)
)
def test_parse_as_naive_auto_generated_valid_formats(self):
for (timestamp, expected_datetime) in generate_valid_timestamp_and_datetime():
try:
self.assertEqual(ciso8601.parse_datetime_as_naive(timestamp), expected_datetime.replace(tzinfo=None))
except Exception:
print("Had problems parsing: {timestamp}".format(timestamp=timestamp))
raise
"2018-01-02", # Missing mandatory time
"2018-01-02T03", # Missing mandatory minute and second
"2018-01-02T03Z", # Missing mandatory minute and second
"2018-01-02T03:04", # Missing mandatory minute and second
"2018-01-02T03:04Z", # Missing mandatory minute and second
"2018-01-02T03:04:01+04", # Missing mandatory offset minute
"2018-01-02T03:04:05", # Missing mandatory offset
"2018-01-02T03:04:05.12345", # Missing mandatory offset
"2018-01-02T24:00:00Z", # 24:00:00 is not valid in RFC 3339
'20180102T03:04:05-12:34', # Missing mandatory date separators
'2018-01-02T030405-12:34', # Missing mandatory time separators
'2018-01-02T03:04:05-1234', # Missing mandatory offset separator
'2018-01-02T03:04:05,12345Z' # Invalid comma fractional second separator
]:
with self.assertRaisesRegex(ValueError, r"RFC 3339", msg="Timestamp '{0}' was supposed to be invalid, but parsing it didn't raise ValueError.".format(timestamp)):
ciso8601.parse_rfc3339(timestamp)
Validate that valid RFC 3339 datetimes are parseable by parse_rfc3339
and produce the same result as parse_datetime.
"""
for string in [
'2018-01-02T03:04:05Z',
'2018-01-02t03:04:05z',
'2018-01-02 03:04:05z',
'2018-01-02T03:04:05+00:00',
'2018-01-02T03:04:05-00:00',
'2018-01-02T03:04:05.12345Z',
'2018-01-02T03:04:05+01:23',
'2018-01-02T03:04:05-12:34',
'2018-01-02T03:04:05-12:34',
]:
self.assertEqual(ciso8601.parse_datetime(string),
ciso8601.parse_rfc3339(string))
def get_row(self, item):
time = item['scheduledCall']['scheduledDepartureTime']
if not time:
return
live = item.get('expectedDepartureTime')
if live:
live = ciso8601.parse_datetime(live)
return {
'time': ciso8601.parse_datetime(time),
'live': live,
'service': self.get_service(item['routeInfo']['lineName']),
'destination': item['tripInfo']['headsign'],
'vehicle': item['vehicleRTI']['vehicleID'],
'operator': item['agencyCode'],
}
def decode_datetime_objects(nested_value):
if isinstance(nested_value, list):
return [decode_datetime_objects(item) for item in nested_value]
elif isinstance(nested_value, dict):
for key, value in nested_value.iteritems():
if isinstance(value, dict) and 'type' in value.keys():
if value['type'] == 'encoded_datetime':
nested_value[key] = ciso8601.parse_datetime(value['value'])
if value['type'] == 'encoded_date':
nested_value[key] = ciso8601.parse_datetime(value['value']).date()
if value['type'] == 'encoded_time':
nested_value[key] = ciso8601.parse_datetime(value['value']).time()
if value['type'] == 'encoded_decimal':
nested_value[key] = Decimal(value['value'])
elif isinstance(value, dict):
nested_value[key] = decode_datetime_objects(value)
elif isinstance(value, list):
nested_value[key] = decode_datetime_objects(value)
return nested_value
return nested_value
print(item)
elif operator.pk == 'SCCM':
operator_options = ('SCCM', 'SCPB', 'SCHU', 'SCBD')
elif operator.pk == 'CBBH':
operator_options = ('CBBH', 'CBNL')
vehicle, created = self.get_vehicle(operator_options or operator, item)
journey = None
if 'PublishedLineName' in item:
line_name = item['PublishedLineName']
journey_code = item['DatedVehicleJourneyRef']
if journey_code == 'UNKNOWN':
journey_code = ''
departure_time = ciso8601.parse_datetime(item['OriginAimedDepartureTime'])
else:
line_name = ''
journey_code = ''
departure_time = None
recorded_at_time = ciso8601.parse_datetime(item['RecordedAtTime'])
if not created and vehicle.latest_location:
location = vehicle.latest_location
latest_journey = location.journey
if line_name == latest_journey.route_name and journey_code == latest_journey.code:
if departure_time is None or departure_time == latest_journey.datetime:
if recorded_at_time - location.datetime < timedelta(hours=1):
journey = journey = latest_journey
else:
location = VehicleLocation()
if isinstance(end, int):
# TODO: add check for future (negative values) and ensure that start < end
if not isinstance(start_time, timedelta):
raise ValueError("Can't mix relative and absolute times")
end_time = timedelta(seconds=end)
elif isinstance(end, timedelta):
if not isinstance(start_time, timedelta):
raise ValueError("Can't mix relative and absolute times")
end_time = end
elif end is None:
end_time = utcnow() # TODO: or MAX_DATE?
elif isinstance(end, datetime):
end_time = end.replace(tzinfo=UTC)
else:
end_time = ciso8601.parse_datetime(end).replace(tzinfo=UTC)
if isinstance(start_time, timedelta):
return RelativeTimeInterval(start=start_time, end=end_time)
else:
return TimeInterval(start=start_time, end=end_time)
def get_datetime(item):
try:
return timezone.make_aware(parse_datetime(item['live']['timestamp']['dateTime']))
except AmbiguousTimeError:
return timezone.make_aware(parse_datetime(item['live']['timestamp']['dateTime']), is_dst=True)