Skip to content

Commit

Permalink
#225: Fix timezone-related issues found by merging master
Browse files Browse the repository at this point in the history
- These issues were a result of writing new code that involved timezones before master got merged in, which had the original fixes for timezones on DB backend
  • Loading branch information
MRichards99 committed May 14, 2021
1 parent bfa8c84 commit dc583a8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions datagateway_api/common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import logging

from dateutil.tz.tz import tzlocal
from flask import request
from flask_restful import reqparse
from sqlalchemy.exc import IntegrityError
Expand Down Expand Up @@ -157,6 +158,10 @@ def map_distinct_attributes_to_results(distinct_attributes, query_result):
split_attr_name = attr_name.split(".")

if isinstance(data, datetime):
# Workaround for when this function is used on DB backend, where usually
# `_make_serializable()` would fix tzinfo
if data.tzinfo is None:
data = data.replace(tzinfo=tzlocal())
data = DateHandler.datetime_object_to_str(data)

# Attribute name is from the 'origin' entity (i.e. not a related entity)
Expand Down
1 change: 0 additions & 1 deletion test/icat/test_query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import datetime

from dateutil.tz import tzlocal
from icat.entity import Entity
import pytest

Expand Down
5 changes: 3 additions & 2 deletions test/test_map_distinct_attrs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime, timezone
from datetime import datetime

from dateutil.tz import tzlocal
import pytest

from datagateway_api.common.helpers import map_distinct_attributes_to_results
Expand All @@ -25,7 +26,7 @@ class TestMapDistinctAttrs:
hour=1,
minute=1,
second=1,
tzinfo=timezone.utc,
tzinfo=tzlocal(),
),
),
{"startDate": "2020-01-04 01:01:01+00:00"},
Expand Down

0 comments on commit dc583a8

Please sign in to comment.