Skip to content

Bugfix datetime serialization utc zero offset #1016

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion planet/data_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def date_range_filter(field_name: str,
def _datetime_to_rfc3339(value: datetime) -> str:
"""Converts the datetime to an RFC3339 string"""
iso = value.isoformat()
if not value.utcoffset():
if value.utcoffset() is None:
# rfc3339 needs a Z if there is no timezone offset
iso += 'Z'
return iso
Expand Down
2 changes: 1 addition & 1 deletion planet/subscription_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def planetary_variable_source(
def _datetime_to_rfc3339(value: datetime) -> str:
"""Converts the datetime to an RFC3339 string"""
iso = value.isoformat()
if not value.utcoffset():
if value.utcoffset() is None:
# rfc3339 needs a Z if there is no timezone offset
iso += 'Z'
return iso
Expand Down
18 changes: 5 additions & 13 deletions tests/unit/test_data_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from datetime import datetime, timedelta, tzinfo
from datetime import datetime, timedelta, timezone
import logging
import pytest

Expand Down Expand Up @@ -98,24 +98,16 @@ def _test_callback(x): # pragma: no cover
callback=_test_callback)


class TZTest(tzinfo):

def __init__(self, offset=None):
self.offset = offset
super().__init__()

def utcoffset(self, dt):
return timedelta(hours=self.offset) if self.offset else None


@pytest.mark.parametrize(
"dtime,expected",
[(datetime(2022, 5, 1, 1, 0, 0, 1), '2022-05-01T01:00:00.000001Z'),
(datetime(2022, 5, 1, 1, 0, 1), '2022-05-01T01:00:01Z'),
(datetime(2022, 6, 1, 1, 1), '2022-06-01T01:01:00Z'),
(datetime(2022, 6, 1, 1), '2022-06-01T01:00:00Z'),
(datetime(2022, 6, 1, 1, tzinfo=TZTest(0)), '2022-06-01T01:00:00Z'),
(datetime(2022, 6, 1, 1, tzinfo=TZTest(1)), '2022-06-01T01:00:00+01:00')])
(datetime(2022, 6, 1, 1, tzinfo=timezone(timedelta(hours=1))),
'2022-06-01T01:00:00+01:00'),
(datetime(2022, 6, 1, 1, tzinfo=timezone(timedelta(0))),
'2022-06-01T01:00:00+00:00')])
def test__datetime_to_rfc3339_basic(dtime, expected):
assert data_filter._datetime_to_rfc3339(dtime) == expected

Expand Down