Change datetime.now() to timeutils.utcnow() from oslo_utils
We use an UTC time to avoid the difference with time zones.
ref: https://review.opendev.org/c/openstack/cinder/+/159854
diff --git a/staffeln/common/time.py b/staffeln/common/time.py
index 45e6ffe..2fd8cc0 100644
--- a/staffeln/common/time.py
+++ b/staffeln/common/time.py
@@ -1,7 +1,6 @@
import re
-from datetime import datetime
-
from dateutil.relativedelta import relativedelta
+from oslo_utils import timeutils
DEFAULT_TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
@@ -36,11 +35,11 @@
def get_current_time():
- return datetime.now()
+ return timeutils.utcnow()
def get_current_strtime():
- now = datetime.now()
+ now = timeutils.utcnow()
return now.strftime(DEFAULT_TIME_FORMAT)
@@ -48,7 +47,7 @@
years=0, months=0, weeks=0, days=0, hours=0, minutes=0, seconds=0, from_date=None
):
if from_date is None:
- from_date = datetime.now()
+ from_date = timeutils.utcnow()
return from_date - relativedelta(
years=years,
months=months,
diff --git a/staffeln/conductor/backup.py b/staffeln/conductor/backup.py
index 84c4d49..1c69235 100755
--- a/staffeln/conductor/backup.py
+++ b/staffeln/conductor/backup.py
@@ -1,11 +1,12 @@
import collections
-from datetime import datetime, timedelta
+from datetime import timedelta
import staffeln.conf
from openstack.exceptions import HttpException as OpenstackHttpException
from openstack.exceptions import ResourceNotFound as OpenstackResourceNotFound
from openstack.exceptions import SDKException as OpenstackSDKException
from oslo_log import log
+from oslo_utils import timeutils
from staffeln import objects
from staffeln.common import constants, context, openstack
from staffeln.conductor import result
@@ -187,7 +188,7 @@
# but the process to check the remove will eventually starts.
# Note: 315360000 = 10 years. The create time of an backup object will
# not affect report.
- threshold_strtime = datetime.now() - timedelta(seconds=315360000)
+ threshold_strtime = timeutils.utcnow() - timedelta(seconds=315360000)
self._volume_backup(
BackupMapping(
volume_id=task.volume_id,
@@ -346,7 +347,7 @@
# Ignore backup interval
return True
interval = CONF.conductor.backup_min_interval
- threshold_strtime = datetime.now() - timedelta(seconds=interval)
+ threshold_strtime = timeutils.utcnow() - timedelta(seconds=interval)
backups = self.get_backups(
filters={
"volume_id__eq": volume_id,
@@ -536,7 +537,7 @@
backup_status and backup_id in the task queue table.
"""
project_id = task.project_id
- timestamp = int(datetime.now().timestamp())
+ timestamp = int(timeutils.utcnow().timestamp())
# Backup name allows max 255 chars of string
backup_name = ("%(instance_name)s_%(volume_name)s_%(timestamp)s") % {
"instance_name": task.instance_name,
@@ -655,7 +656,7 @@
instance_id=task.instance_id,
backup_completed=1,
incremental=task.incremental,
- created_at=datetime.now(),
+ created_at=timeutils.utcnow(),
)
)
task.backup_status = constants.BACKUP_COMPLETED
diff --git a/staffeln/conductor/manager.py b/staffeln/conductor/manager.py
index d8ead77..dabcca0 100755
--- a/staffeln/conductor/manager.py
+++ b/staffeln/conductor/manager.py
@@ -1,11 +1,12 @@
import threading
import time
-from datetime import datetime, timedelta
+from datetime import timedelta
import cotyledon
import staffeln.conf
from futurist import periodics
from oslo_log import log
+from oslo_utils import timeutils
from staffeln.common import constants, context, lock
from staffeln.common import time as xtime
from staffeln.conductor import backup as backup_controller
@@ -129,7 +130,7 @@
def _report_backup_result(self):
report_period = CONF.conductor.report_period
- threshold_strtime = datetime.now() - timedelta(seconds=report_period)
+ threshold_strtime = timeutils.utcnow() - timedelta(seconds=report_period)
filters = {"created_at__lt": threshold_strtime.astimezone()}
old_tasks = self.controller.get_queues(filters=filters)
for task in old_tasks:
@@ -202,8 +203,8 @@
self.controller.hard_remove_volume_backup(retention_backup)
def is_retention(self, backup):
- now = datetime.now()
- backup_age = now.astimezone() - backup.created_at
+ now = timeutils.utcnow()
+ backup_age = now - backup.created_at
# see if need to be delete.
if backup.instance_id in self.instance_retention_map:
retention_time = now - self.get_time_from_str(