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_space_separator():
"""Handle a separator other than T
"""
d = iso8601.parse_date("2007-06-23 06:40:34.00Z")
assert d.year == 2007
assert d.month == 6
assert d.day == 23
assert d.hour == 6
assert d.minute == 40
assert d.second == 34
assert d.microsecond == 0
assert d.tzinfo == iso8601.UTC
def test_at(self):
self.assertJson(
query.at(datetime.fromtimestamp(0, iso8601.UTC), query.get(query.index("widgets"))),
'{"at":{"@ts":"1970-01-01T00:00:00Z"},"expr":{"get":{"index":"widgets"}}}'
)
bowie = Artist(name='David Bowie')
album = Album(
artist=bowie,
title='Hunky Dory',
release_date=datetime.datetime(1971, 12, 17)
)
assert album_schema.serialize(album) == {
'artist': {'name': 'David Bowie'},
'release_date': '1971-12-17T00:00:00',
'title': 'Hunky Dory'
}
assert album_schema.deserialize(album_schema.serialize(album)) == {
'artist': {'name': 'David Bowie'},
'release_date': datetime.datetime(1971, 12, 17, 0, 0, tzinfo=iso8601.UTC),
'title': 'Hunky Dory'
}
input = album_schema.serialize(album)
del input['artist']
errors = None
try:
album_schema.deserialize(input)
except Exception as e:
errors = e.errors
assert errors == {'artist': ['This field is required']}
def test_fauna_time(self):
self.assertJson(FaunaTime('1970-01-01T00:00:00.123456789Z'),
'{"@ts":"1970-01-01T00:00:00.123456789Z"}')
self.assertJson(datetime.fromtimestamp(0, iso8601.UTC),
'{"@ts":"1970-01-01T00:00:00Z"}')
def test_time_conversion(self):
dt = datetime.now(iso8601.UTC)
self.assertEqual(FaunaTime(dt).to_datetime(), dt)
# Must be time zone aware.
self.assertRaises(ValueError, lambda: FaunaTime(datetime.utcnow()))
dt = datetime.fromtimestamp(0, iso8601.UTC)
ft = FaunaTime(dt)
self.assertEqual(ft, FaunaTime("1970-01-01T00:00:00Z"))
self.assertEqual(ft.to_datetime(), dt)
def test_parse_date():
d = iso8601.parse_date("2006-10-20T15:34:56Z")
assert d.year == 2006
assert d.month == 10
assert d.day == 20
assert d.hour == 15
assert d.minute == 34
assert d.second == 56
assert d.tzinfo == iso8601.UTC
def _time_error(LOG, begin, end):
if CONF.start_time:
begin = datetime.datetime.strptime(CONF.start_time,
"%Y-%m-%d %H:%M:%S")
if CONF.end_time:
end = datetime.datetime.strptime(CONF.end_time,
"%Y-%m-%d %H:%M:%S")
begin = begin.replace(tzinfo=iso8601.UTC)
end = end.replace(tzinfo=iso8601.UTC)
if end <= begin:
msg = _("The end time (%(end)s) must be after the start "
"time (%(start)s).") % {'start': begin,
'end': end}
LOG.error(msg)
sys.exit(-1)
return begin, end
"""Validate a datetime or None value."""
if value is None:
return None
if isinstance(value, six.string_types):
# NOTE(danms): Being tolerant of isotime strings here will help us
# during our objects transition
value = timeutils.parse_isotime(value)
elif not isinstance(value, datetime.datetime):
raise ValueError(
_("A datetime.datetime is required here. Got %s"), value)
if value.utcoffset() is None and tzinfo_aware:
# NOTE(danms): Legacy objects from sqlalchemy are stored in UTC,
# but are returned without a timezone attached.
# As a transitional aid, assume a tz-naive object is in UTC.
value = value.replace(tzinfo=iso8601.UTC)
elif not tzinfo_aware:
value = value.replace(tzinfo=None)
return value
def decorated_function(self, spec_obj):
return_value = None
try:
return_value = function(self, spec_obj)
except Exception as e:
# Ignores exception raised from consume_from_request() so that
# booting instance would fail in the resource claim of compute
# node, other suitable node may be chosen during scheduling retry.
LOG.warning("Selected host: %(host)s failed to consume from "
"instance. Error: %(error)s",
{'host': self.host, 'error': e})
else:
now = timeutils.utcnow()
# NOTE(sbauza): Objects are UTC tz-aware by default
self.updated = now.replace(tzinfo=iso8601.UTC)
return return_value
def datetime(value, default_tzinfo=iso8601.UTC, context=None):
"""validates that a a field is an ISO 8601 string, and converts it to a datetime object."""
if not value:
return
try:
return iso8601.parse_date(value, default_timezone=default_tzinfo)
except iso8601.ParseError as e:
raise ValidationException('Invalid date: %s' % (e))