Merge "Replace dateutils usage with datetime and oslo.utils"

This commit is contained in:
Zuul
2025-08-07 20:46:25 +00:00
committed by Gerrit Code Review
5 changed files with 22 additions and 21 deletions

View File

@@ -0,0 +1,5 @@
---
fixes:
- |
Removed the ``python-dateutil`` dependency from Watcher to reduce the
number of external dependencies and improve maintainability.

View File

@@ -30,7 +30,7 @@ states, visit :ref:`the Audit State machine <audit_state_machine>`.
""" """
import datetime import datetime
from dateutil import tz from datetime import timezone
from http import HTTPStatus from http import HTTPStatus
import jsonschema import jsonschema
@@ -635,13 +635,11 @@ class AuditsController(rest.RestController):
start_time_value = audit_dict.get('start_time') start_time_value = audit_dict.get('start_time')
end_time_value = audit_dict.get('end_time') end_time_value = audit_dict.get('end_time')
if start_time_value: if start_time_value:
audit_dict['start_time'] = start_time_value.replace( audit_dict['start_time'] = start_time_value.astimezone(
tzinfo=tz.tzlocal()).astimezone( timezone.utc).replace(tzinfo=None)
tz.tzutc()).replace(tzinfo=None)
if end_time_value: if end_time_value:
audit_dict['end_time'] = end_time_value.replace( audit_dict['end_time'] = end_time_value.astimezone(
tzinfo=tz.tzlocal()).astimezone( timezone.utc).replace(tzinfo=None)
tz.tzutc()).replace(tzinfo=None)
new_audit = objects.Audit(context, **audit_dict) new_audit = objects.Audit(context, **audit_dict)
new_audit.create() new_audit.create()
@@ -688,9 +686,8 @@ class AuditsController(rest.RestController):
patch_value = api_utils.get_patch_value(patch, patch_path) patch_value = api_utils.get_patch_value(patch, patch_path)
# convert string format to UTC time # convert string format to UTC time
new_patch_value = wutils.parse_isodatetime( new_patch_value = wutils.parse_isodatetime(
patch_value).replace( patch_value).astimezone(
tzinfo=tz.tzlocal()).astimezone( timezone.utc).replace(tzinfo=None)
tz.tzutc()).replace(tzinfo=None)
api_utils.set_patch_value(patch, patch_path, new_patch_value) api_utils.set_patch_value(patch, patch_path, new_patch_value)
audit = Audit(**api_utils.apply_jsonpatch(audit_dict, patch)) audit = Audit(**api_utils.apply_jsonpatch(audit_dict, patch))

View File

@@ -21,7 +21,7 @@
import datetime import datetime
from croniter import croniter from croniter import croniter
from dateutil import tz from datetime import timezone
from oslo_utils import timeutils from oslo_utils import timeutils
from watcher.common import context from watcher.common import context
@@ -123,7 +123,7 @@ class ContinuousAuditHandler(base.AuditHandler):
'next_run_time') else 'run_date' 'next_run_time') else 'run_date'
# We should convert UTC time to local time without tzinfo # We should convert UTC time to local time without tzinfo
trigger_args[time_var] = trigger_args[time_var].replace( trigger_args[time_var] = trigger_args[time_var].replace(
tzinfo=tz.tzutc()).astimezone(tz.tzlocal()).replace(tzinfo=None) tzinfo=timezone.utc).astimezone().replace(tzinfo=None)
self.scheduler.add_job(self.execute_audit, trigger, self.scheduler.add_job(self.execute_audit, trigger,
args=[audit, audit_context], args=[audit, audit_context],
name='execute_audit', name='execute_audit',

View File

@@ -12,9 +12,8 @@
# limitations under the License. # limitations under the License.
# #
from dateutil.parser import parse
from oslo_log import log from oslo_log import log
from oslo_utils import timeutils
from cinderclient.v3.volumes import Volume from cinderclient.v3.volumes import Volume
from novaclient.v2.servers import Server from novaclient.v2.servers import Server
@@ -893,7 +892,8 @@ class ComputeSpecSortFilter(BaseFilter):
reverse=True) reverse=True)
elif sort_key == 'created_at': elif sort_key == 'created_at':
result = sorted(items, result = sorted(items,
key=lambda x: parse(getattr(x, sort_key)), key=lambda x: timeutils.parse_isotime(
getattr(x, sort_key)),
reverse=False) reverse=False)
return result return result
@@ -959,7 +959,8 @@ class StorageSpecSortFilter(BaseFilter):
if sort_key == 'created_at': if sort_key == 'created_at':
result = sorted(items, result = sorted(items,
key=lambda x: parse(getattr(x, sort_key)), key=lambda x: timeutils.parse_isotime(
getattr(x, sort_key)),
reverse=False) reverse=False)
else: else:
result = sorted(items, result = sorted(items,

View File

@@ -11,7 +11,7 @@
# limitations under the License. # limitations under the License.
import datetime import datetime
from dateutil import tz from datetime import timezone
import itertools import itertools
from unittest import mock from unittest import mock
from urllib import parse as urlparse from urllib import parse as urlparse
@@ -942,10 +942,8 @@ class TestPost(api_base.FunctionalTest):
response.json['start_time']) response.json['start_time'])
return_end_time = timeutils.parse_isotime( return_end_time = timeutils.parse_isotime(
response.json['end_time']) response.json['end_time'])
iso_start_time = start_time.replace( iso_start_time = start_time.astimezone(timezone.utc)
tzinfo=tz.tzlocal()).astimezone(tz.tzutc()) iso_end_time = end_time.astimezone(timezone.utc)
iso_end_time = end_time.replace(
tzinfo=tz.tzlocal()).astimezone(tz.tzutc())
self.assertEqual(iso_start_time, return_start_time) self.assertEqual(iso_start_time, return_start_time)
self.assertEqual(iso_end_time, return_end_time) self.assertEqual(iso_end_time, return_end_time)