Replace redundant own utilities
Replace some of the utilities to manipulate time by the common implementations from oslo.utils, to reduce duplicate logic. Change-Id: I407024a5966194d0ebf0e1e95b96645eeb0a89f5
This commit is contained in:
@@ -19,8 +19,6 @@ Time related utilities and helper functions.
|
||||
|
||||
import datetime
|
||||
|
||||
import iso8601
|
||||
from oslo_utils import encodeutils
|
||||
from oslo_utils import timeutils
|
||||
|
||||
# ISO 8601 extended time format with microseconds
|
||||
@@ -42,24 +40,6 @@ def isotime(at=None, subsecond=False):
|
||||
return st
|
||||
|
||||
|
||||
def parse_isotime(timestr):
|
||||
"""Parse time from ISO 8601 format."""
|
||||
try:
|
||||
return iso8601.parse_date(timestr)
|
||||
except iso8601.ParseError as e:
|
||||
raise ValueError(encodeutils.exception_to_unicode(e))
|
||||
except TypeError as e:
|
||||
raise ValueError(encodeutils.exception_to_unicode(e))
|
||||
|
||||
|
||||
def normalize_time(timestamp):
|
||||
"""Normalize time in arbitrary timezone to UTC naive object."""
|
||||
offset = timestamp.utcoffset()
|
||||
if offset is None:
|
||||
return timestamp
|
||||
return timestamp.replace(tzinfo=None) - offset
|
||||
|
||||
|
||||
def iso8601_from_timestamp(timestamp, microsecond=False):
|
||||
"""Returns an iso8601 formatted date from timestamp."""
|
||||
return isotime(datetime.datetime.fromtimestamp(
|
||||
|
||||
@@ -36,10 +36,10 @@ from oslo_log import log as logging
|
||||
from oslo_utils import excutils
|
||||
from oslo_utils import netutils
|
||||
from oslo_utils import strutils
|
||||
from oslo_utils import timeutils as oslo_timeutils
|
||||
from webob import exc
|
||||
|
||||
from glance.common import exception
|
||||
from glance.common import timeutils
|
||||
from glance.common import wsgi
|
||||
from glance.i18n import _, _LE, _LW
|
||||
|
||||
@@ -574,7 +574,7 @@ def split_filter_op(expression):
|
||||
# not be partitioned, and a default operator of eq should be
|
||||
# assumed.
|
||||
try:
|
||||
timeutils.parse_isotime(expression)
|
||||
oslo_timeutils.parse_isotime(expression)
|
||||
op = 'eq'
|
||||
threshold = expression
|
||||
except ValueError:
|
||||
|
||||
@@ -23,7 +23,6 @@ from oslo_log import log as logging
|
||||
from oslo_utils import timeutils as oslo_timeutils
|
||||
|
||||
from glance.common import exception
|
||||
from glance.common import timeutils
|
||||
from glance.common import utils
|
||||
from glance.db import utils as db_utils
|
||||
from glance.i18n import _, _LI, _LW
|
||||
@@ -333,8 +332,8 @@ def _filter_images(images, filters, context,
|
||||
elif k in ['created_at', 'updated_at']:
|
||||
attr_value = image.get(key)
|
||||
operator, isotime = utils.split_filter_op(value)
|
||||
parsed_time = timeutils.parse_isotime(isotime)
|
||||
threshold = timeutils.normalize_time(parsed_time)
|
||||
parsed_time = oslo_timeutils.parse_isotime(isotime)
|
||||
threshold = oslo_timeutils.normalize_time(parsed_time)
|
||||
to_add = utils.evaluate_filter_op(attr_value, operator,
|
||||
threshold)
|
||||
elif k in ['name', 'id', 'status',
|
||||
|
||||
@@ -41,7 +41,6 @@ from sqlalchemy import sql
|
||||
import sqlalchemy.sql as sa_sql
|
||||
|
||||
from glance.common import exception
|
||||
from glance.common import timeutils
|
||||
from glance.common import utils
|
||||
from glance.db.sqlalchemy.metadef_api import (resource_type
|
||||
as metadef_resource_type_api)
|
||||
@@ -506,7 +505,8 @@ def _make_conditions_from_filters(filters, is_public=None):
|
||||
if 'changes-since' in filters:
|
||||
# normalize timestamp to UTC, as sqlalchemy doesn't appear to
|
||||
# respect timezone offsets
|
||||
changes_since = timeutils.normalize_time(filters.pop('changes-since'))
|
||||
changes_since = oslo_timeutils.normalize_time(
|
||||
filters.pop('changes-since'))
|
||||
image_conditions.append(models.Image.updated_at > changes_since)
|
||||
|
||||
if 'deleted' in filters:
|
||||
@@ -549,8 +549,8 @@ def _make_conditions_from_filters(filters, is_public=None):
|
||||
attr_value = getattr(models.Image, key)
|
||||
operator, isotime = utils.split_filter_op(filters.pop(k))
|
||||
try:
|
||||
parsed_time = timeutils.parse_isotime(isotime)
|
||||
threshold = timeutils.normalize_time(parsed_time)
|
||||
parsed_time = oslo_timeutils.parse_isotime(isotime)
|
||||
threshold = oslo_timeutils.normalize_time(parsed_time)
|
||||
except ValueError:
|
||||
msg = (_("Bad \"%s\" query filter format. "
|
||||
"Use ISO 8601 DateTime notation.") % k)
|
||||
|
||||
@@ -17,7 +17,6 @@ import calendar
|
||||
import datetime
|
||||
from unittest import mock
|
||||
|
||||
import iso8601
|
||||
from oslo_utils import timeutils as oslo_timeutils
|
||||
|
||||
from glance.common import timeutils
|
||||
@@ -51,145 +50,8 @@ class TimeUtilsTest(test_utils.BaseTestCase):
|
||||
dt = timeutils.isotime(subsecond=True)
|
||||
self.assertEqual(dt, self.skynet_self_aware_time_ms_str)
|
||||
|
||||
def test_parse_isotime(self):
|
||||
expect = timeutils.parse_isotime(self.skynet_self_aware_time_str)
|
||||
skynet_self_aware_time_utc = self.skynet_self_aware_time.replace(
|
||||
tzinfo=iso8601.iso8601.UTC)
|
||||
self.assertEqual(skynet_self_aware_time_utc, expect)
|
||||
|
||||
def test_parse_isotime_micro_second_precision(self):
|
||||
expect = timeutils.parse_isotime(self.skynet_self_aware_time_ms_str)
|
||||
skynet_self_aware_time_ms_utc = self.skynet_self_aware_ms_time.replace(
|
||||
tzinfo=iso8601.iso8601.UTC)
|
||||
self.assertEqual(skynet_self_aware_time_ms_utc, expect)
|
||||
|
||||
def test_iso8601_from_timestamp(self):
|
||||
utcnow = oslo_timeutils.utcnow()
|
||||
iso = timeutils.isotime(utcnow)
|
||||
ts = calendar.timegm(utcnow.timetuple())
|
||||
self.assertEqual(iso, timeutils.iso8601_from_timestamp(ts))
|
||||
|
||||
|
||||
class TestIso8601Time(test_utils.BaseTestCase):
|
||||
|
||||
def _instaneous(self, timestamp, yr, mon, day, hr, minute, sec, micro):
|
||||
self.assertEqual(timestamp.year, yr)
|
||||
self.assertEqual(timestamp.month, mon)
|
||||
self.assertEqual(timestamp.day, day)
|
||||
self.assertEqual(timestamp.hour, hr)
|
||||
self.assertEqual(timestamp.minute, minute)
|
||||
self.assertEqual(timestamp.second, sec)
|
||||
self.assertEqual(timestamp.microsecond, micro)
|
||||
|
||||
def _do_test(self, time_str, yr, mon, day, hr, minute, sec, micro, shift):
|
||||
DAY_SECONDS = 24 * 60 * 60
|
||||
timestamp = timeutils.parse_isotime(time_str)
|
||||
self._instaneous(timestamp, yr, mon, day, hr, minute, sec, micro)
|
||||
offset = timestamp.tzinfo.utcoffset(None)
|
||||
self.assertEqual(offset.seconds + offset.days * DAY_SECONDS, shift)
|
||||
|
||||
def test_zulu(self):
|
||||
time_str = '2012-02-14T20:53:07Z'
|
||||
self._do_test(time_str, 2012, 2, 14, 20, 53, 7, 0, 0)
|
||||
|
||||
def test_zulu_micros(self):
|
||||
time_str = '2012-02-14T20:53:07.123Z'
|
||||
self._do_test(time_str, 2012, 2, 14, 20, 53, 7, 123000, 0)
|
||||
|
||||
def test_offset_east(self):
|
||||
time_str = '2012-02-14T20:53:07+04:30'
|
||||
offset = 4.5 * 60 * 60
|
||||
self._do_test(time_str, 2012, 2, 14, 20, 53, 7, 0, offset)
|
||||
|
||||
def test_offset_east_micros(self):
|
||||
time_str = '2012-02-14T20:53:07.42+04:30'
|
||||
offset = 4.5 * 60 * 60
|
||||
self._do_test(time_str, 2012, 2, 14, 20, 53, 7, 420000, offset)
|
||||
|
||||
def test_offset_west(self):
|
||||
time_str = '2012-02-14T20:53:07-05:30'
|
||||
offset = -5.5 * 60 * 60
|
||||
self._do_test(time_str, 2012, 2, 14, 20, 53, 7, 0, offset)
|
||||
|
||||
def test_offset_west_micros(self):
|
||||
time_str = '2012-02-14T20:53:07.654321-05:30'
|
||||
offset = -5.5 * 60 * 60
|
||||
self._do_test(time_str, 2012, 2, 14, 20, 53, 7, 654321, offset)
|
||||
|
||||
def test_compare(self):
|
||||
zulu = timeutils.parse_isotime('2012-02-14T20:53:07')
|
||||
east = timeutils.parse_isotime('2012-02-14T20:53:07-01:00')
|
||||
west = timeutils.parse_isotime('2012-02-14T20:53:07+01:00')
|
||||
self.assertGreater(east, west)
|
||||
self.assertGreater(east, zulu)
|
||||
self.assertGreater(zulu, west)
|
||||
|
||||
def test_compare_micros(self):
|
||||
zulu = timeutils.parse_isotime('2012-02-14T20:53:07.6544')
|
||||
east = timeutils.parse_isotime('2012-02-14T19:53:07.654321-01:00')
|
||||
west = timeutils.parse_isotime('2012-02-14T21:53:07.655+01:00')
|
||||
self.assertLess(east, west)
|
||||
self.assertLess(east, zulu)
|
||||
self.assertLess(zulu, west)
|
||||
|
||||
def test_zulu_roundtrip(self):
|
||||
time_str = '2012-02-14T20:53:07Z'
|
||||
zulu = timeutils.parse_isotime(time_str)
|
||||
self.assertEqual(zulu.tzinfo, iso8601.iso8601.UTC)
|
||||
self.assertEqual(timeutils.isotime(zulu), time_str)
|
||||
|
||||
def test_east_roundtrip(self):
|
||||
time_str = '2012-02-14T20:53:07-07:00'
|
||||
east = timeutils.parse_isotime(time_str)
|
||||
self.assertEqual(east.tzinfo.tzname(None), '-07:00')
|
||||
self.assertEqual(timeutils.isotime(east), time_str)
|
||||
|
||||
def test_west_roundtrip(self):
|
||||
time_str = '2012-02-14T20:53:07+11:30'
|
||||
west = timeutils.parse_isotime(time_str)
|
||||
self.assertEqual(west.tzinfo.tzname(None), '+11:30')
|
||||
self.assertEqual(timeutils.isotime(west), time_str)
|
||||
|
||||
def test_now_roundtrip(self):
|
||||
time_str = timeutils.isotime()
|
||||
now = timeutils.parse_isotime(time_str)
|
||||
self.assertEqual(now.tzinfo, iso8601.iso8601.UTC)
|
||||
self.assertEqual(timeutils.isotime(now), time_str)
|
||||
|
||||
def test_zulu_normalize(self):
|
||||
time_str = '2012-02-14T20:53:07Z'
|
||||
zulu = timeutils.parse_isotime(time_str)
|
||||
normed = timeutils.normalize_time(zulu)
|
||||
self._instaneous(normed, 2012, 2, 14, 20, 53, 7, 0)
|
||||
|
||||
def test_east_normalize(self):
|
||||
time_str = '2012-02-14T20:53:07-07:00'
|
||||
east = timeutils.parse_isotime(time_str)
|
||||
normed = timeutils.normalize_time(east)
|
||||
self._instaneous(normed, 2012, 2, 15, 3, 53, 7, 0)
|
||||
|
||||
def test_west_normalize(self):
|
||||
time_str = '2012-02-14T20:53:07+21:00'
|
||||
west = timeutils.parse_isotime(time_str)
|
||||
normed = timeutils.normalize_time(west)
|
||||
self._instaneous(normed, 2012, 2, 13, 23, 53, 7, 0)
|
||||
|
||||
def test_normalize_aware_to_naive(self):
|
||||
dt = datetime.datetime(2011, 2, 14, 20, 53, 7)
|
||||
time_str = '2011-02-14T20:53:07+21:00'
|
||||
aware = timeutils.parse_isotime(time_str)
|
||||
naive = timeutils.normalize_time(aware)
|
||||
self.assertLess(naive, dt)
|
||||
|
||||
def test_normalize_zulu_aware_to_naive(self):
|
||||
dt = datetime.datetime(2011, 2, 14, 20, 53, 7)
|
||||
time_str = '2011-02-14T19:53:07Z'
|
||||
aware = timeutils.parse_isotime(time_str)
|
||||
naive = timeutils.normalize_time(aware)
|
||||
self.assertLess(naive, dt)
|
||||
|
||||
def test_normalize_naive(self):
|
||||
dt = datetime.datetime(2011, 2, 14, 20, 53, 7)
|
||||
dtn = datetime.datetime(2011, 2, 14, 19, 53, 7)
|
||||
naive = timeutils.normalize_time(dtn)
|
||||
self.assertLess(naive, dt)
|
||||
|
||||
@@ -48,7 +48,4 @@ debtcollector>=1.19.0 # Apache-2.0
|
||||
cryptography>=2.6.1 # BSD/Apache-2.0
|
||||
cursive>=0.2.1 # Apache-2.0
|
||||
|
||||
# timeutils
|
||||
iso8601>=0.1.11 # MIT
|
||||
|
||||
castellan>=0.17.0 # Apache-2.0
|
||||
|
||||
Reference in New Issue
Block a user