From: Yaarit Hatuka Date: Mon, 4 Oct 2021 18:50:01 +0000 (+0000) Subject: mgr/devicehealth: fix missing timezone from time delta calculation X-Git-Tag: v15.2.17~66^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=fb1c1bfd5fc42ab28ae13e993dc2563e736755a2;p=ceph.git mgr/devicehealth: fix missing timezone from time delta calculation An error occurs when subtracting a datetime object that is offset-naive (i.e. unaware of timezone) from a datetime object which is offset-aware. datetime.utcnow() is missing timezone info, e.g.: '2021-09-22 13:18:45.021712', while life_expectancy_max is in the format of: '2021-09-28 00:00:00.000000+00:00', hence we need to add timezone info to the former when calculating their time delta. Please note that we calculate time delta using `datetime.utcnow()` in `serve()` in this module, but there we refer to the delta in seconds, which works fine. Fixes: https://tracker.ceph.com/issues/52327 Signed-off-by: Yaarit Hatuka (cherry picked from commit 05902d943bba4a64abbd943270b56cbdd1650e62) Conflicts: src/pybind/mgr/devicehealth/module.py The import typing line needed to be removed --- diff --git a/src/pybind/mgr/devicehealth/module.py b/src/pybind/mgr/devicehealth/module.py index c4e46854b9c..24b39dff925 100644 --- a/src/pybind/mgr/devicehealth/module.py +++ b/src/pybind/mgr/devicehealth/module.py @@ -8,7 +8,7 @@ from mgr_module import MgrModule, CommandResult import operator import rados from threading import Event -from datetime import datetime, timedelta, date, time +from datetime import datetime, timedelta, date, time, timezone from six import iteritems TIME_FORMAT = '%Y%m%d-%H%M%S' @@ -491,7 +491,7 @@ class Module(MgrModule): devs = self.get("devices") osds_in = {} osds_out = {} - now = datetime.utcnow() + now = datetime.now(timezone.utc) # e.g. '2021-09-22 13:18:45.021712+00:00' osdmap = self.get("osd_map") assert osdmap is not None for dev in devs['devices']: @@ -506,7 +506,7 @@ class Module(MgrModule): continue # life_expectancy_(min/max) is in the format of: # '%Y-%m-%dT%H:%M:%S.%f%z', e.g.: - # '2019-01-20T21:12:12.000000Z' + # '2019-01-20 21:12:12.000000+00:00' life_expectancy_max = datetime.strptime( dev['life_expectancy_max'], '%Y-%m-%dT%H:%M:%S.%f%z')