-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Fix timedelta JSON serialization on Python 2.6. #2430
Fix timedelta JSON serialization on Python 2.6. #2430
Conversation
if hasattr(obj, 'total_seconds'): | ||
return six.text_type(obj.total_seconds()) | ||
else: # Python 2.6 compatibility | ||
return six.text_type((obj.microseconds + (obj.seconds + obj.days * 24 * 3600) * 10**6) / 10**6) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We isolate any version branching code into compat.py
.
Have a function named total_seconds(timedelta)
that provides this in compat.py
and then call that from here.
Otherwise seems fine.
@@ -4,7 +4,7 @@ | |||
""" | |||
|
|||
# flake8: noqa | |||
from __future__ import unicode_literals | |||
from __future__ import unicode_literals, division |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we actually need this - the only division we've added will always be float on both 2 and 3, since we're dividing by a float.
…liser-python26-support Fix timedelta JSON serialization on Python 2.6.
Nice work, thanks! |
Timedelta serialisation does not work on Python 2.6.
timedelta.total_seconds()
is only supported since Python 2.7 (https://docs.python.org/2.7/library/datetime.html#datetime.timedelta.total_seconds), so we need a fallback for python 2.6.