Use utils.utcnow rather than datetime.utcnow

Fixes bug #1003587

Edit direct use of datetime.utcnow or datetime.now to utilize utils.utcnow

Change-Id: I1130f6e664f252fc5da7304f495dc9f980721224
This commit is contained in:
Zhongyue Luo 2012-05-23 22:17:35 +08:00
parent 709d49e979
commit 5aca2e2df6
12 changed files with 35 additions and 34 deletions

View File

@ -152,7 +152,7 @@ class SimpleTenantUsageController(object):
else:
info['state'] = instance['vm_state']
now = datetime.datetime.utcnow()
now = utils.utcnow()
if info['state'] == 'terminated':
delta = info['ended_at'] - info['started_at']

View File

@ -1592,8 +1592,7 @@ def instance_get_floating_address(context, instance_id):
@require_admin_context
def instance_get_all_hung_in_rebooting(context, reboot_window, session=None):
reboot_window = datetime.datetime.utcnow() - datetime.timedelta(
seconds=reboot_window)
reboot_window = utils.utcnow() - datetime.timedelta(seconds=reboot_window)
if not session:
session = get_session()
@ -3775,8 +3774,8 @@ def migration_get_by_instance_and_status(context, instance_uuid, status):
@require_admin_context
def migration_get_all_unconfirmed(context, confirm_window, session=None):
confirm_window = datetime.datetime.utcnow() - datetime.timedelta(
seconds=confirm_window)
confirm_window = (utils.utcnow() -
datetime.timedelta(seconds=confirm_window))
return model_query(context, models.Migration, session=session,
read_deleted="yes").\

View File

@ -1,3 +1,5 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright (c) 2011 OpenStack, LLC.
# All Rights Reserved.
#
@ -27,6 +29,7 @@ import os
from nova import flags
from nova import log as logging
from nova.openstack.common import cfg
from nova import utils
scheduler_json_config_location_opt = cfg.StrOpt(
@ -78,7 +81,7 @@ class SchedulerOptions(object):
def _get_time_now(self):
"""Get current UTC. Broken out for testing."""
return datetime.datetime.utcnow()
return utils.utcnow()
def get_configuration(self, filename=None):
"""Check the json file for changes and load it if needed."""

View File

@ -13,8 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from lxml import etree
from nova.api.openstack import common
@ -156,7 +154,7 @@ class CloudpipesXMLSerializerTest(test.TestCase):
public_ip='1.2.3.4',
public_port='321',
instance_id='1234-1234-1234-1234',
created_at=utils.isotime(datetime.datetime.utcnow()),
created_at=utils.isotime(utils.utcnow()),
state='running')),
dict(cloudpipe=dict(
project_id='4321',

View File

@ -29,6 +29,7 @@ from nova import flags
from nova import policy
from nova import test
from nova.tests.api.openstack import fakes
from nova import utils
FLAGS = flags.FLAGS
@ -40,7 +41,7 @@ ROOT_GB = 10
EPHEMERAL_GB = 20
MEMORY_MB = 1024
VCPUS = 2
STOP = datetime.datetime.utcnow()
STOP = utils.utcnow()
START = STOP - datetime.timedelta(hours=HOURS)
@ -211,7 +212,7 @@ class SimpleTenantUsageSerializerTest(test.TestCase):
def test_serializer_show(self):
serializer = simple_tenant_usage.SimpleTenantUsageTemplate()
today = datetime.datetime.now()
today = utils.utcnow()
yesterday = today - datetime.timedelta(days=1)
raw_usage = dict(
tenant_id='tenant',
@ -257,7 +258,7 @@ class SimpleTenantUsageSerializerTest(test.TestCase):
def test_serializer_index(self):
serializer = simple_tenant_usage.SimpleTenantUsagesTemplate()
today = datetime.datetime.now()
today = utils.utcnow()
yesterday = today - datetime.timedelta(days=1)
raw_usages = [dict(
tenant_id='tenant1',

View File

@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import json
from lxml import etree
@ -26,6 +25,7 @@ from nova import flags
from nova import log as logging
from nova import test
from nova.tests.api.openstack import fakes
from nova import utils
from nova import volume
FLAGS = flags.FLAGS
@ -250,7 +250,7 @@ class SnapshotSerializerTest(test.TestCase):
id='snap_id',
status='snap_status',
size=1024,
createdAt=datetime.datetime.now(),
createdAt=utils.utcnow(),
displayName='snap_name',
displayDescription='snap_desc',
volumeId='vol_id',
@ -268,7 +268,7 @@ class SnapshotSerializerTest(test.TestCase):
id='snap1_id',
status='snap1_status',
size=1024,
createdAt=datetime.datetime.now(),
createdAt=utils.utcnow(),
displayName='snap1_name',
displayDescription='snap1_desc',
volumeId='vol1_id',
@ -277,7 +277,7 @@ class SnapshotSerializerTest(test.TestCase):
id='snap2_id',
status='snap2_status',
size=1024,
createdAt=datetime.datetime.now(),
createdAt=utils.utcnow(),
displayName='snap2_name',
displayDescription='snap2_desc',
volumeId='vol2_id',

View File

@ -27,6 +27,7 @@ import nova.db
from nova import flags
from nova import test
from nova.tests.api.openstack import fakes
from nova import utils
from nova import volume
from webob import exc
@ -363,7 +364,7 @@ class VolumeSerializerTest(test.TestCase):
status='vol_status',
size=1024,
availabilityZone='vol_availability',
createdAt=datetime.datetime.now(),
createdAt=utils.utcnow(),
attachments=[dict(
id='vol_id',
volumeId='vol_id',
@ -392,7 +393,7 @@ class VolumeSerializerTest(test.TestCase):
status='vol1_status',
size=1024,
availabilityZone='vol1_availability',
createdAt=datetime.datetime.now(),
createdAt=utils.utcnow(),
attachments=[dict(
id='vol1_id',
volumeId='vol1_id',
@ -412,7 +413,7 @@ class VolumeSerializerTest(test.TestCase):
status='vol2_status',
size=1024,
availabilityZone='vol2_availability',
createdAt=datetime.datetime.now(),
createdAt=utils.utcnow(),
attachments=[dict(
id='vol2_id',
volumeId='vol2_id',

View File

@ -13,8 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from lxml import etree
import webob
@ -24,6 +22,7 @@ from nova import flags
from nova import log as logging
from nova import test
from nova.tests.api.openstack import fakes
from nova import utils
from nova import volume
FLAGS = flags.FLAGS
@ -171,7 +170,7 @@ class SnapshotSerializerTest(test.TestCase):
id='snap_id',
status='snap_status',
size=1024,
created_at=datetime.datetime.now(),
created_at=utils.utcnow(),
display_name='snap_name',
display_description='snap_desc',
volume_id='vol_id',
@ -189,7 +188,7 @@ class SnapshotSerializerTest(test.TestCase):
id='snap1_id',
status='snap1_status',
size=1024,
created_at=datetime.datetime.now(),
created_at=utils.utcnow(),
display_name='snap1_name',
display_description='snap1_desc',
volume_id='vol1_id',
@ -198,7 +197,7 @@ class SnapshotSerializerTest(test.TestCase):
id='snap2_id',
status='snap2_status',
size=1024,
created_at=datetime.datetime.now(),
created_at=utils.utcnow(),
display_name='snap2_name',
display_description='snap2_desc',
volume_id='vol2_id',

View File

@ -22,6 +22,7 @@ from nova.api.openstack.volume import volumes
from nova import flags
from nova import test
from nova.tests.api.openstack import fakes
from nova import utils
from nova.volume import api as volume_api
@ -214,7 +215,7 @@ class VolumeSerializerTest(test.TestCase):
status='vol_status',
size=1024,
availability_zone='vol_availability',
created_at=datetime.datetime.now(),
created_at=utils.utcnow(),
attachments=[dict(
id='vol_id',
volume_id='vol_id',
@ -243,7 +244,7 @@ class VolumeSerializerTest(test.TestCase):
status='vol1_status',
size=1024,
availability_zone='vol1_availability',
created_at=datetime.datetime.now(),
created_at=utils.utcnow(),
attachments=[dict(
id='vol1_id',
volume_id='vol1_id',
@ -263,7 +264,7 @@ class VolumeSerializerTest(test.TestCase):
status='vol2_status',
size=1024,
availability_zone='vol2_availability',
created_at=datetime.datetime.now(),
created_at=utils.utcnow(),
attachments=[dict(
id='vol2_id',
volume_id='vol2_id',

View File

@ -96,7 +96,7 @@ class DbApiTestCase(test.TestCase):
db.migration_update(ctxt, migration.id, {"status": "CONFIRMED"})
# Ensure the new migration is not returned.
updated_at = datetime.datetime.utcnow()
updated_at = utils.utcnow()
values = {"status": "finished", "updated_at": updated_at}
migration = db.migration_create(ctxt, values)
results = db.migration_get_all_unconfirmed(ctxt, 10)
@ -120,7 +120,7 @@ class DbApiTestCase(test.TestCase):
db.instance_update(ctxt, instance.id, {"task_state": None})
# Ensure the newly rebooted instance is not returned.
updated_at = datetime.datetime.utcnow()
updated_at = utils.utcnow()
values = {"task_state": "rebooting", "updated_at": updated_at}
instance = db.instance_create(ctxt, values)
results = db.instance_get_all_hung_in_rebooting(ctxt, 10)

View File

@ -18,7 +18,6 @@
import ast
import contextlib
import datetime
import functools
import os
import re
@ -43,6 +42,7 @@ from nova.tests import fake_network
from nova.tests import fake_utils
from nova.tests.glance import stubs as glance_stubs
from nova.tests.xenapi import stubs
from nova import utils
from nova.virt.xenapi import connection as xenapi_conn
from nova.virt.xenapi import fake as xenapi_fake
from nova.virt.xenapi import vm_utils
@ -1398,8 +1398,7 @@ class XenAPIBWUsageTestCase(test.TestCase):
self.name = "instance-0001"
self.uuid = "1-2-3-4-5"
result = self.conn.get_all_bw_usage([testinstance()],
datetime.datetime.utcnow())
result = self.conn.get_all_bw_usage([testinstance()], utils.utcnow())
self.assertEqual(result, [])

View File

@ -488,7 +488,7 @@ def utcnow_ts():
return time.mktime(utcnow().timetuple())
def set_time_override(override_time=datetime.datetime.utcnow()):
def set_time_override(override_time=utcnow()):
"""Override utils.utcnow to return a constant time."""
utcnow.override_time = override_time
@ -524,7 +524,7 @@ def parse_strtime(timestr, fmt=PERFECT_TIME_FORMAT):
def isotime(at=None):
"""Stringify time in ISO 8601 format"""
if not at:
at = datetime.datetime.utcnow()
at = utcnow()
str = at.strftime(ISO_TIME_FORMAT)
tz = at.tzinfo.tzname(None) if at.tzinfo else 'UTC'
str += ('Z' if tz == 'UTC' else tz)