Replace deprecated datetime.utcnow()

The datetime.utcnow() is deprecated in Python 3.12.
Replace datetime.utcnow() with oslo_utils.timeutils.utcnow().
This bumps oslo.utils to 7.0.0.

Change-Id: Icccbb0549add686a744a72b354932471cbf91c92
Signed-off-by: Takashi Natsume <takanattie@gmail.com>
This commit is contained in:
Takashi Natsume
2024-10-02 22:17:02 +09:00
parent 566a830f64
commit 61a7dd85ca
24 changed files with 94 additions and 89 deletions

View File

@@ -19,9 +19,10 @@
import datetime
from dateutil import tz
from croniter import croniter
from dateutil import tz
from oslo_utils import timeutils
from watcher.common import context
from watcher.common import scheduling
@@ -97,7 +98,7 @@ class ContinuousAuditHandler(base.AuditHandler):
@staticmethod
def _next_cron_time(audit):
if utils.is_cron_like(audit.interval):
return croniter(audit.interval, datetime.datetime.utcnow()
return croniter(audit.interval, timeutils.utcnow()
).get_next(datetime.datetime)
@classmethod
@@ -111,7 +112,7 @@ class ContinuousAuditHandler(base.AuditHandler):
finally:
if utils.is_int_like(audit.interval):
audit.next_run_time = (
datetime.datetime.utcnow() +
timeutils.utcnow() +
datetime.timedelta(seconds=int(audit.interval)))
else:
audit.next_run_time = self._next_cron_time(audit)
@@ -129,7 +130,7 @@ class ContinuousAuditHandler(base.AuditHandler):
**trigger_args)
def check_audit_expired(self, audit):
current = datetime.datetime.utcnow()
current = timeutils.utcnow()
# Note: if audit still didn't get into the timeframe,
# skip it
if audit.start_time and audit.start_time > current:
@@ -196,7 +197,7 @@ class ContinuousAuditHandler(base.AuditHandler):
# to restore it after shutdown
if audit.next_run_time is not None:
old_run_time = audit.next_run_time
current = datetime.datetime.utcnow()
current = timeutils.utcnow()
if old_run_time < current:
delta = datetime.timedelta(
seconds=(int(audit.interval) - (
@@ -206,7 +207,7 @@ class ContinuousAuditHandler(base.AuditHandler):
next_run_time = audit.next_run_time
# if audit is new one
else:
next_run_time = datetime.datetime.utcnow()
next_run_time = timeutils.utcnow()
self._add_job('interval', audit, audit_context,
seconds=int(audit.interval),
next_run_time=next_run_time)

View File

@@ -158,7 +158,7 @@ class CeilometerHelper(base.DataSourceBase):
def statistic_aggregation(self, resource=None, resource_type=None,
meter_name=None, period=300, granularity=300,
aggregate='mean'):
end_time = datetime.datetime.utcnow()
end_time = timeutils.utcnow()
start_time = end_time - datetime.timedelta(seconds=int(period))
meter = self._get_meter(meter_name)

View File

@@ -16,12 +16,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from datetime import datetime
from datetime import timedelta
from gnocchiclient import exceptions as gnc_exc
from oslo_config import cfg
from oslo_log import log
from oslo_utils import timeutils
from watcher.common import clients
from watcher.decision_engine.datasources import base
@@ -69,7 +69,7 @@ class GnocchiHelper(base.DataSourceBase):
def statistic_aggregation(self, resource=None, resource_type=None,
meter_name=None, period=300, aggregate='mean',
granularity=300):
stop_time = datetime.utcnow()
stop_time = timeutils.utcnow()
start_time = stop_time - timedelta(seconds=(int(period)))
meter = self._get_meter(meter_name)

View File

@@ -19,6 +19,7 @@
import datetime
from monascaclient import exc
from oslo_utils import timeutils
from watcher.common import clients
from watcher.decision_engine.datasources import base
@@ -58,8 +59,7 @@ class MonascaHelper(base.DataSourceBase):
period = int(datetime.timedelta(hours=3).total_seconds())
if not start_time:
start_time = (
datetime.datetime.utcnow() -
datetime.timedelta(seconds=period))
timeutils.utcnow() - datetime.timedelta(seconds=period))
start_timestamp = None if not start_time else start_time.isoformat()
end_timestamp = None if not end_time else end_time.isoformat()
@@ -86,7 +86,7 @@ class MonascaHelper(base.DataSourceBase):
def statistic_aggregation(self, resource=None, resource_type=None,
meter_name=None, period=300, aggregate='mean',
granularity=300):
stop_time = datetime.datetime.utcnow()
stop_time = timeutils.utcnow()
start_time = stop_time - datetime.timedelta(seconds=(int(period)))
meter = self._get_meter(meter_name)