Skip to content

Fix datetime serialization - #84

Merged
tbarbugli merged 2 commits into
GetStream:masterfrom
Logitech:fix/datetime-serialization
Jul 20, 2018
Merged

tbarbugli merged 2 commits into
GetStream:masterfrom
Logitech:fix/datetime-serialization

Conversation

@followben

@followben followben commented Jul 19, 2018 •

Copy link
Copy Markdown
Contributor

Summary:

stream.py encodes dates to the Stream API using isoformat() which drops microseconds if they are zero. However on decoding from the Stream API, stream.py expects microseconds to exist.

In most instances, calling datetime.now() will provide non-zero microseconds. However where an activity is created with datetime values set by a user (from the date widget in Django's admin panel for instance), microseconds will not exist. These fields will be currently be deserialized (incorrectly) as strings by stream.py.

This PR adds extra logic to the serializer test case to verify the issue, and updates serialization to use the same string format for reading and writing datetime objects. I could have kept isoformat and specified obj.isoformat(sep='T', timespec='microseconds'), but that arg is only available in Python 3.6+. Instead I've used strftime when writing to mirror the existing use of strptime when reading.

Submitter checklist:

  • CHANGELOG updated or N/A
  • Documentation updated or N/A

Merger checklist:

  • ALL tests have passed
  • Code Review is done
  • Dependencies satisfied

@followben followben changed the title Fix/datetime serialization Fix datetime serialization Jul 19, 2018
@tbarbugli
tbarbugli merged commit 254d02e into GetStream:master Jul 20, 2018
@tbarbugli

Copy link
Copy Markdown
Member

@followben thank you for the patch and the detailed explanation of the problem 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants