diff --git a/packit_service/utils.py b/packit_service/utils.py index ab8da35a5..d65b48788 100644 --- a/packit_service/utils.py +++ b/packit_service/utils.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: MIT import logging +from datetime import datetime, timezone from io import StringIO from logging import StreamHandler from typing import List, Tuple @@ -121,3 +122,33 @@ def collect_packit_logs(buffer: StringIO, handler: StreamHandler) -> str: packit_logger.removeHandler(handler) buffer.seek(0) return buffer.read() + + +def is_timezone_naive_datetime(datetime_to_check: datetime) -> bool: + """ + Check whether the given datetime is timezone naive. + + Args: + datetime_to_check: datetime to check for timezone naiveness + + Returns: + bool: whether the given datetime is timezone naive + """ + # https://docs.python.org/3/library/datetime.html#determining-if-an-object-is-aware-or-naive + return ( + datetime_to_check.tzinfo is None + or datetime_to_check.tzinfo.utcoffset(datetime_to_check) is None + ) + + +def get_timezone_aware_datetime(datetime_to_update: datetime) -> datetime: + """ + Set timezone for datetime so that it is timezone-aware. + + Args: + datetime_to_update: datetime to update + + Result: + timezone-aware datetime + """ + return datetime_to_update.replace(tzinfo=timezone.utc) diff --git a/packit_service/worker/handlers/copr.py b/packit_service/worker/handlers/copr.py index 1fb8329e7..faa2e8458 100644 --- a/packit_service/worker/handlers/copr.py +++ b/packit_service/worker/handlers/copr.py @@ -45,7 +45,12 @@ AbstractPRCommentEvent, ) from packit_service.service.urls import get_copr_build_info_url, get_srpm_build_info_url -from packit_service.utils import dump_job_config, dump_package_config +from packit_service.utils import ( + dump_job_config, + dump_package_config, + is_timezone_naive_datetime, + get_timezone_aware_datetime, +) from packit_service.worker.events.enums import GitlabEventAction from packit_service.worker.helpers.build import CoprBuildJobHelper from packit_service.worker.handlers.abstract import ( @@ -148,13 +153,8 @@ def get_packit_github_installation_time(self) -> Optional[datetime]: def run(self) -> TaskResults: installed_at = self.get_packit_github_installation_time() - # transform timezone-naive datetime - # https://docs.python.org/3/library/datetime.html#determining-if-an-object-is-aware-or-naive - if installed_at and ( - installed_at.tzinfo is None - or installed_at.tzinfo.utcoffset(installed_at) is None - ): - installed_at = installed_at.replace(tzinfo=timezone.utc) + if installed_at and is_timezone_naive_datetime(installed_at): + installed_at = get_timezone_aware_datetime(installed_at) if self.package_config.srpm_build_deps is not None or ( installed_at and installed_at > DATE_OF_DEFAULT_SRPM_BUILD_IN_COPR ): diff --git a/packit_service/worker/monitoring.py b/packit_service/worker/monitoring.py index 893be4297..8607f1325 100644 --- a/packit_service/worker/monitoring.py +++ b/packit_service/worker/monitoring.py @@ -3,10 +3,12 @@ import logging import os -from datetime import datetime, timezone +from datetime import datetime from prometheus_client import CollectorRegistry, Counter, push_to_gateway, Histogram +from packit_service.utils import is_timezone_naive_datetime, get_timezone_aware_datetime + logger = logging.getLogger(__name__) @@ -18,10 +20,10 @@ def measure_time(begin: datetime, end: datetime) -> float: Returns: float seconds between begin and end """ - if begin.tzinfo is None or begin.tzinfo.utcoffset(begin) is None: - begin = begin.replace(tzinfo=timezone.utc) - if end.tzinfo is None or end.tzinfo.utcoffset(end) is None: - end = end.replace(tzinfo=timezone.utc) + if is_timezone_naive_datetime(begin): + begin = get_timezone_aware_datetime(begin) + if is_timezone_naive_datetime(end): + end = get_timezone_aware_datetime(end) return (end - begin).total_seconds()