Skip to content
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

Datetimes become timezone aware #918

Closed
CosmicAnalogue465 opened this issue Mar 29, 2015 · 8 comments
Closed

Datetimes become timezone aware #918

CosmicAnalogue465 opened this issue Mar 29, 2015 · 8 comments

Comments

@CosmicAnalogue465
Copy link

CosmicAnalogue465 commented Mar 29, 2015

If I serialize a object using to_json with timezone unaware dates and then load them back with from_json, the datetime object is then timezone aware.

class TestDoc(Document):
    d = DateTimeField()

a = TestDoc()
a.d = datetime.now()

print a.d  # 2015-03-29 11:12:53.963929
a = TestDoc.from_json(a.to_json())
print a.d  # 2015-03-29 11:12:53.963000+00:00
@CosmicAnalogue465 CosmicAnalogue465 changed the title Dates become timezone aware Datetimes become timezone aware Mar 29, 2015
@MRigal
Copy link
Member

MRigal commented Apr 29, 2015

ping @vinczente

@MRigal MRigal modified the milestone: 0.10.1 Jun 23, 2015
@srinivasreddy
Copy link
Contributor

The issue is in bson package
take a look here

>>> import bson
>>> from bson import json_util
>>> from datetime import datetime
>>> k = datetime.now()
>>> k
datetime.datetime(2017, 7, 12, 20, 7, 44, 694669)
>>> json_util.dumps(k)
'{"$date": 1499890064694}'
>>> json_util.loads(json_util.dumps(k))
datetime.datetime(2017, 7, 12, 20, 7, 44, 694000, tzinfo=<bson.tz_util.FixedOffset object at 0x237d750>)
>>> j = json_util.loads(json_util.dumps(k))
>>> k ==j 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 



@srinivasreddy
Copy link
Contributor

(mongoengine)dtdev@dtdev-centos mongoengine (master) $ pip list | grep bson
bson                0.4.8   

@wojcikstefan
Copy link
Member

Could you open a corresponding issue in https://github.com/py-bson/bson?

@srinivasreddy
Copy link
Contributor

raised here. py-bson/bson#64
Till then do you accept a temporary fix to MongoEngine?

@wojcikstefan
Copy link
Member

I think it would be best to not pollute MongoEngine with temporary fixes related to other libraries. In your own code you can just .replace(tzinfo=None) for any datetime you need to compare.

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

No branches or pull requests

5 participants
@srinivasreddy @CosmicAnalogue465 @wojcikstefan @MRigal and others