Merge "Replace datetime calculations with utility functions"

This commit is contained in:
Jenkins 2015-05-28 23:20:45 +00:00 committed by Gerrit Code Review
commit e1872739c6
6 changed files with 39 additions and 11 deletions

View File

@ -10,6 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import functools
import inspect
import logging
@ -192,3 +193,35 @@ def parse_isotime(timestr):
raise ValueError(six.text_type(e))
except TypeError as e:
raise ValueError(six.text_type(e))
def from_utcnow(**timedelta_kwargs):
"""Calculate the time in the future from utcnow.
:param \*\*timedelta_kwargs:
Passed directly to :class:`datetime.timedelta` to add to the current
time in UTC.
:returns:
The time in the future based on ``timedelta_kwargs``.
:rtype:
datetime.datetime
"""
now = datetime.datetime.utcnow()
delta = datetime.timedelta(**timedelta_kwargs)
return now + delta
def before_utcnow(**timedelta_kwargs):
"""Calculate the time in the past from utcnow.
:param \*\*timedelta_kwargs:
Passed directly to :class:`datetime.timedelta` to subtract from the
current time in UTC.
:returns:
The time in the past based on ``timedelta_kwargs``.
:rtype:
datetime.datetime
"""
now = datetime.datetime.utcnow()
delta = datetime.timedelta(**timedelta_kwargs)
return now - delta

View File

@ -14,7 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import functools
from keystoneauth import _utils as utils
@ -86,8 +85,7 @@ class AccessInfo(object):
norm_expires = utils.normalize_time(self.expires)
# (gyee) should we move auth_token.will_expire_soon() to timeutils
# instead of duplicating code here?
soon = (datetime.datetime.utcnow() + datetime.timedelta(
seconds=stale_duration))
soon = utils.from_utcnow(seconds=stale_duration)
return norm_expires < soon
def has_service_catalog(self):

View File

@ -10,8 +10,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from keystoneauth import _utils as utils
__all__ = ['DiscoveryList',
@ -38,8 +36,7 @@ class DiscoveryBase(dict):
self.id = id
self.status = status or 'stable'
self.updated = updated or (datetime.datetime.utcnow() -
datetime.timedelta(days=_DEFAULT_DAYS_AGO))
self.updated = updated or utils.before_utcnow(days=_DEFAULT_DAYS_AGO)
@property
def id(self):

View File

@ -53,7 +53,7 @@ class Token(dict):
self.audit_id = audit_id or uuid.uuid4().hex
if not issued:
issued = datetime.datetime.utcnow() - datetime.timedelta(minutes=2)
issued = _utils.before_utcnow(minutes=2)
if not expires:
expires = issued + datetime.timedelta(hours=1)

View File

@ -76,7 +76,7 @@ class Token(dict):
self.methods.extend(methods)
if not issued:
issued = datetime.datetime.utcnow() - datetime.timedelta(minutes=2)
issued = _utils.before_utcnow(minutes=2)
try:
self.issued = issued

View File

@ -11,11 +11,11 @@
# under the License.
import abc
import datetime
import uuid
import six
from keystoneauth import _utils
from keystoneauth import access
from keystoneauth.auth import base
from keystoneauth.auth import identity
@ -190,7 +190,7 @@ class CommonIdentityTests(object):
self.assertEqual(self.TEST_URL, auth_url)
def _create_expired_auth_plugin(self, **kwargs):
expires = datetime.datetime.utcnow() - datetime.timedelta(minutes=20)
expires = _utils.before_utcnow(minutes=20)
expired_token = self.get_auth_data(expires=expires)
expired_auth_ref = access.create(body=expired_token)