Skip to content

Commit

Permalink
Merge pull request #41 from TogetherCrew/fix/period-datetime-time-offset
Browse files Browse the repository at this point in the history
Fix/period datetime time offset
  • Loading branch information
amindadgar authored Jan 22, 2025
2 parents f670234 + 54bdf46 commit faf3973
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name="tc-analyzer-lib",
version="1.4.15",
version="1.4.16",
author="Mohammad Amin Dadgar, TogetherCrew",
maintainer="Mohammad Amin Dadgar",
maintainer_email="[email protected]",
Expand Down
2 changes: 1 addition & 1 deletion tc_analyzer_lib/algorithms/member_activity_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def check_past_history(
db_analysis_end_date = None

if db_analysis_end_date:
db_analysis_end_date = db_analysis_end_date.replace(tzinfo=timezone.utc)
db_analysis_end_date = db_analysis_end_date.astimezone(tz=timezone.utc)

new_date_range: list[datetime]
# if for the requested date_range, its results were available in db
Expand Down
19 changes: 13 additions & 6 deletions tc_analyzer_lib/metrics/heatmaps/heatmaps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from datetime import date, datetime, timedelta
from datetime import date, datetime, timedelta, timezone
from typing import Any

from tc_analyzer_lib.metrics.heatmaps import AnalyticsHourly, AnalyticsRaw
Expand Down Expand Up @@ -67,9 +67,15 @@ async def start(

analytics_date: datetime
if last_date is None or from_start:
analytics_date = self.period
# Ensure self.period is offset-aware
analytics_date = (
self.period.replace(tzinfo=timezone.utc)
if self.period.tzinfo is None
else self.period
)
else:
analytics_date = last_date + timedelta(days=1)
# Ensure last_date is offset-aware and add a day
analytics_date = last_date.astimezone(timezone.utc) + timedelta(days=1)

# in order to skip bots
bot_ids = []
Expand All @@ -81,10 +87,11 @@ async def start(
heatmaps_results = []

index = 0
max_index = (analytics_date - datetime.now()).days
while analytics_date.date() < datetime.now().date():
today = datetime.now(tz=timezone.utc)
max_index = (analytics_date - today).days
while analytics_date.date() < today.date():
start_day = analytics_date.replace(
hour=0, minute=0, second=0, microsecond=0
hour=0, minute=0, second=0, microsecond=0, tzinfo=timezone.utc
)
end_day = start_day + timedelta(days=1)
logging.info(
Expand Down
6 changes: 3 additions & 3 deletions tc_analyzer_lib/tc_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(

self.platform_id = platform_id
self.resources = resources
self.period = period
self.period = period.astimezone(tz=timezone.utc)
self.action = action
self.window = window
self.analyzer_config = analyzer_config
Expand Down Expand Up @@ -105,7 +105,7 @@ async def run_once(self):
action_config=self.action,
window_config=self.window,
analyzer_config=self.analyzer_config,
analyzer_period=self.period.replace(tzinfo=timezone.utc),
analyzer_period=self.period,
)
(
member_activities_data,
Expand Down Expand Up @@ -190,7 +190,7 @@ async def recompute(self):
action_config=self.action,
window_config=self.window,
analyzer_config=self.analyzer_config,
analyzer_period=self.period.replace(tzinfo=timezone.utc),
analyzer_period=self.period,
)
(
member_activities_data,
Expand Down

0 comments on commit faf3973

Please sign in to comment.