Skip to content

Commit

Permalink
Merge pull request #1652 from lbarcziova/refactor-timezone-awareness
Browse files Browse the repository at this point in the history
Refactor functionality for timezone-awareness checking

Reviewed-by: None <None>
Reviewed-by: Jiri Popelka <None>
  • Loading branch information
softwarefactory-project-zuul[bot] authored Sep 13, 2022
2 parents 6f9dc83 + c50c8e3 commit 8089789
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
31 changes: 31 additions & 0 deletions packit_service/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
16 changes: 8 additions & 8 deletions packit_service/worker/handlers/copr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
):
Expand Down
12 changes: 7 additions & 5 deletions packit_service/worker/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)


Expand All @@ -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()

Expand Down

0 comments on commit 8089789

Please sign in to comment.