planetlabs / planet-client-python

Python client for Planet APIs
https://planet-sdk-for-python-v2.readthedocs.io/en/latest/
Apache License 2.0
271 stars 91 forks source link

Bugfix datetime serialization utc zero offset #1016

Closed stefan-jozefowicz-bayer closed 9 months ago

stefan-jozefowicz-bayer commented 9 months ago

Proposed Changes:

For inclusion in changelog (if applicable):

1.

Not intended for changelog:

  1. Change how timezone aware datetimes with UTC tz are converted to datetime string

Diff of User Interface

Old behavior: passing a timezone aware datetime from the UTC zone to some of the request constructors resulted in malformed datetime strings, e.g: 2022-06-01T01:00:00+00:00Z - i.e. strings that have BOTH +00:00 and Z at the end.

At the root of it was value.utcoffset() >> timedelta(hours=0) evaluating as False even though there is a defined offset here:

def _datetime_to_rfc3339(value: datetime) -> str:
    """Converts the datetime to an RFC3339 string"""
    iso = value.isoformat()
    if not value.utcoffset():
        # rfc3339 needs a Z if there is no timezone offset
        iso += 'Z'
    return iso

New behavior: The Z is tagged on only if the utcoffset() is None.

When the TZ is UTC the dt string is: 2022-06-01T01:00:00+00:00

The previous test setup for timezone aware datetimes obfuscated this bug, so I changed the tests as well.

PR Checklist:

(Optional) @mentions for Notifications: