Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(timestamps): generate timestamps correctly and remove the deprecated pytz #461

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/bot/scheduler/scheduler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import zoneinfo
import config
import datetime
import slack_sdk
Expand All @@ -14,7 +15,6 @@
store_slack_user_list_db,
)
from iblog import logger
from pytz import timezone
from typing import List


Expand All @@ -26,7 +26,7 @@ class TaskScheduler:
def __init__(self):
self.scheduler = BackgroundScheduler(
jobstores=jobstores,
timezone=timezone(application_timezone),
timezone=zoneinfo.ZoneInfo(application_timezone),
)

def list_jobs(self) -> List[Job]:
Expand Down
17 changes: 11 additions & 6 deletions backend/bot/shared/tools.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import config
import zoneinfo
import ipaddress
import itertools
import random
import string

from datetime import datetime
from iblog import logger
from pytz import timezone
from typing import Any, List


Expand All @@ -19,18 +19,23 @@
application_timezone = config.active.options.get("timezone")


def fetch_timestamp(short: bool = False):
def fetch_timestamp(short: bool = False, timezone: str | None = None) -> str:
"""Return a localized, formatted timestamp using datetime.now()"""
now = datetime.now()
localized = timezone(application_timezone).localize(now)
localized = datetime.now(
zoneinfo.ZoneInfo(timezone or application_timezone)
)
if short:
return localized.strftime(timestamp_fmt_short)
return localized.strftime(timestamp_fmt)


def fetch_timestamp_from_time_obj(t: datetime):
def fetch_timestamp_from_time_obj(
t: datetime, timezone: str | None = None
) -> str:
"""Return a localized, formatted timestamp using datetime.datetime class"""
return timezone(application_timezone).localize(t).strftime(timestamp_fmt)
return t.astimezone(
zoneinfo.ZoneInfo(timezone or application_timezone)
).strftime(timestamp_fmt)


def find_index_in_list(lst: List, key: Any, value: Any):
Expand Down
2 changes: 1 addition & 1 deletion backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from iblog import logger, log_level
from typing import Dict, List

__version__ = "v1.10.7"
__version__ = "v1.10.8"

# .env parse
dotenv_path = os.path.join(os.path.dirname(__file__), ".env")
Expand Down
1 change: 0 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pytest-mock==3.14.0
pytest-sqlalchemy-mock==0.1.5
python-dotenv==1.0.1
python-json-logger==2.0.7
pytz==2024.1
PyYAML==6.0.1
requests==2.31.0
requests-mock==1.11.0
Expand Down
44 changes: 44 additions & 0 deletions backend/tests/test_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import datetime

from bot.shared.tools import (
fetch_timestamp,
fetch_timestamp_from_time_obj,
timestamp_fmt,
timestamp_fmt_short,
)


class TestTimeHelpers:
def test_fetch_timestamp_generates_the_correct_timestamp(self):
utc_timestamp = fetch_timestamp(timezone="UTC")
melbourne_timestamp = fetch_timestamp(timezone="Australia/Melbourne")

parsed_melbourne = datetime.datetime.strptime(
utc_timestamp[:13], "%Y-%m-%dT%H"
)
parsed_utc = datetime.datetime.strptime(
melbourne_timestamp[:13], "%Y-%m-%dT%H"
)

assert (
parsed_melbourne.hour != parsed_utc.hour
), "The timestamps should have different hours when using differnt timezones"

def test_fetch_timestamp_short(self):
short_timestamp = fetch_timestamp(short=True, timezone="UTC")

assert datetime.datetime.strptime(
short_timestamp, timestamp_fmt_short
), "Should be able to reparse the short format"

def test_fetch_timestamp_from_time_obj(self):
now = datetime.datetime.now(datetime.UTC)

now_as_est = fetch_timestamp_from_time_obj(
now, timezone="Australia/Melbourne"
)

now_as_melbourne_datetime = datetime.datetime.strptime(
now_as_est, timestamp_fmt
)
assert now_as_melbourne_datetime.hour != now.hour
4 changes: 2 additions & 2 deletions deploy/charts/incident-bot/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 1.10.7
version: 1.10.8

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: '1.10.7'
appVersion: '1.10.8'
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ bases:

images:
- name: eb129/incident-bot
newTag: v1.10.7
newTag: v1.10.8

configMapGenerator:
- name: incident-bot-config
Expand Down
2 changes: 1 addition & 1 deletion docs/deploy/overlays/production/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ bases:

images:
- name: eb129/incident-bot-docs
newTag: v1.10.7
newTag: v1.10.8

generatorOptions:
disableNameSuffixHash: true
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.10.7
v1.10.8