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
Wrongly serialized utc datetime #8
Comments
That is actually expected behavior, use
Here is an example if you want UTC datetimes: >>> from datetime import datetime
>>> from datetime import timezone
>>> import jsons
>>> datetime_inst = datetime.now(tz=timezone.utc)
>>> jsons.dump(datetime_inst)
'2019-02-16T22:01:19Z' This could have been documented better in the README. I'm working on that. |
I see. But it still seems wrong to assume that datetime without |
I understand your stance, because I have been pondering about this myself repeatedly. My thoughts are as follows:
You can still steer the behavior of jsons though. Apart from creating from jsons import set_serializer, default_datetime_serializer as deflt
jsons.set_serializer(lambda obj, **_: deflt(obj.replace(tzinfo=timezone.utc)), datetime)
print(jsons.dump(datetime.now())) # Notice the datetime.now without tzinfo Prints: '2019-02-17T15:36:34Z' # Notice the 'Z' So currently, I'd rather leave the |
I understand but I am still unconvinced. I think it would be better to not follow RFC3339, when there's not enough information, rather than introducing buggy behavior. Standard library provides |
LOCAL_TIMEZONE = datetime.datetime.now().astimezone().tzinfo
local = datetime.datetime.now()
local_tz = datetime.datetime.now(tz=LOCAL_TIMEZONE)
utc = datetime.datetime.utcnow()
utc_tz = datetime.datetime.now(tz=datetime.timezone.utc)
print(local) # >>> 2019-02-17 21:40:19.422873
print(jsons.dump(local)) # >>> 2019-02-17T21:40:19+01:00
print(local_tz) # >>> 2019-02-17 21:40:19.422873+01:00
print(jsons.dump(local_tz)) # >>> 2019-02-17T21:40:19+01:00
print(utc) # >>> 2019-02-17 20:40:19.422873
print(jsons.dump(utc)) # >>> 2019-02-17T20:40:19+01:00
print(utc_tz) # >>> 2019-02-17 20:40:19.422873+00:00
print(jsons.dump(utc_tz)) # >>> 2019-02-17T20:40:19Z |
I would be even more radical - I would not allow to serialize not-aware dates, as 99.99999% of time this is just bad design and error of use ;-) (you can always provide some custom un-aware dates serializer if you really need to deal with them) No one ever should have to deal with un-aware datetime. I really regret that python even uses the same type for both. Somehow for unicode handling we have "bytes" and "strings" and those two types are never to be confused again. Also no one expects handling same methods for bytes as for strings (at least starting from python 3 :) ) |
I agree that datetimes in Python are messed up. However, raising an Exception would be a bit rude in my opinion, even though I agree that using naive datetimes is a bad decision. As a developer, you may not always have a choice. Suppose that for some project you are forced to call some API that was badly designed - an API that returns or requires naive datetimes. Just as you thought your day couldn't get any worse, jsons abandons you and forces you into catching the error or writing a custom datetime (de)serializer. At most, jsons could trigger a warning message. I might consider that. |
@ramonhagenaars that is probably the most reasonable behavior. |
Noted! :-) |
The datetime instance is wrongly serialized if initialized from
utcnow()
. Example:The text was updated successfully, but these errors were encountered: