Skip to content

Commit 1802f4e

Browse files
committed
fix(summary): set all datetimes explicitly to UTC
1 parent 7945d8c commit 1802f4e

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

commons/data_access_layer/cosmos_db.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import dataclasses
22
import logging
33
import uuid
4-
from datetime import datetime
4+
from datetime import datetime, timezone
55
from typing import Callable, List, Dict
66

77
import azure.cosmos.cosmos_client as cosmos_client
@@ -385,7 +385,7 @@ def init_app(app: Flask) -> None:
385385

386386

387387
def current_datetime() -> datetime:
388-
return datetime.utcnow()
388+
return datetime.now(timezone.utc)
389389

390390

391391
def datetime_str(value: datetime) -> str:
@@ -418,7 +418,7 @@ def get_current_month() -> int:
418418

419419
def get_date_range_of_month(year: int, month: int) -> Dict[str, str]:
420420
first_day_of_month = 1
421-
start_date = datetime(year=year, month=month, day=first_day_of_month)
421+
start_date = datetime(year=year, month=month, day=first_day_of_month,tzinfo=timezone.utc)
422422

423423
last_day_of_month = get_last_day_of_month(year=year, month=month)
424424
end_date = datetime(
@@ -429,6 +429,7 @@ def get_date_range_of_month(year: int, month: int) -> Dict[str, str]:
429429
minute=59,
430430
second=59,
431431
microsecond=999999,
432+
tzinfo=timezone.utc
432433
)
433434

434435
return {

time_tracker_api/time_entries/custom_modules/worked_time.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import datetime, timedelta
1+
from datetime import datetime, timedelta, timezone
22
from commons.data_access_layer.cosmos_db import (
33
current_datetime,
44
current_datetime_str,
@@ -9,7 +9,7 @@
99

1010

1111
def start_datetime_of_current_month() -> datetime:
12-
return datetime(year=get_current_year(), month=get_current_month(), day=1,)
12+
return datetime(year=get_current_year(), month=get_current_month(), day=1, tzinfo=timezone.utc)
1313

1414

1515
def start_datetime_of_current_week() -> datetime:
@@ -30,9 +30,12 @@ def start_datetime_of_current_month_str() -> str:
3030

3131

3232
def str_to_datetime(
33-
value: str, conversion_format: str = '%Y-%m-%dT%H:%M:%S.%f'
33+
value: str, conversion_format: str = '%Y-%m-%dT%H:%M:%S.%fZ'
3434
) -> datetime:
35-
return datetime.strptime(value, conversion_format)
35+
if 'Z' in value:
36+
return datetime.strptime(value, conversion_format).astimezone(timezone.utc)
37+
else:
38+
return datetime.fromisoformat(value).astimezone(timezone.utc)
3639

3740

3841
def date_range():

0 commit comments

Comments
 (0)