question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DateTime and timezone awareness

See original GitHub issue

I’m pulling my hair with datetime TZ awareness issues.

I initiate the connection to MongoDB with tz_aware = False. I’m no expert about this, and I had never thought much about it before now, but from what I gathered, it seems like a reasonable choice to make. Besides, it’s pymongo’s default (but flask-PyMongo’s hardcodes tz_aware to True).

When a document is pulled from the DB, its DateTimeField attribute’s TZ awareness depends only on MongoClient’s tz_aware parameter (no Marshmallow schema involved):

tz_aware=True -> pymongo provides an aware datetime -> umongo returns an aware datetime tz_aware=False -> pymongo provides a naive datetime -> umongo returns a naive datetime

This is one more reason to set tz_aware = False, because I’m passing a document/object to a lib that expects a naive timezone. (OAuth2 lib expects expires timestamp to be naive and compares it to datetime.utcnow() (see code).) I suppose I could alternatively modify my getters to make all datetimes naive before returning tokens/grants, but on some use cases, it could get cumbersome.

Marshmallow, however, returns every datetime as TZ aware (doc). For this reason, umongo’s DateTimeField’s _deserialize method returns a TZ aware datetime. Since I’m using it (via webargs) to parse the inputs to my API, I’m getting TZ aware datetimes. Likewise, calling load() on a document will use _deserialize and result in a TZ aware datetime.

So if I load a date, it becomes TZ aware. Therefore, to compare it to a date from the database, this one needs to be aware as well.

Should I understand that umongo is meant to be used with tz_aware=True, so that dates fetched from the database can be compared to dates loaded thought Marshmallow schemas?

Could there be a flag/meta allowing to specify if a DateTimeField should return a naive datetime?

I made a quick and dirty patch to DateTime’s _deserialize to remove the TZ from the returned output.

    dt = super()._deserialize(value, attr, data)
    return dt.replace(tzinfo=None)

This seems to work on my use case. However, the day we complete pure Marshmallow schema export, the exported schema I pass to webargs for API input parsing won’t have that feature. Unless this is added to Marshmallow as well. (I asked there about it there: https://github.com/marshmallow-code/marshmallow/issues/520.)

Feedback welcome. Those TZ issues are new to me, so I may be totally misguided.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nedwillcommented, Dec 2, 2016

Yeah, that’s what I meant - date.astimezone returns a new date, it doesn’t have side effects/mutate the date.

0reactions
touilleMancommented, Dec 3, 2016

#75 is merged, @nedwill thanks for pointing this 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make a timezone aware datetime object - Stack Overflow
In general, to make a naive datetime timezone-aware, use the localize method: import datetime import pytz unaware = datetime.datetime(2011, ...
Read more >
How to make a timezone aware datetime object in Python
Timezone-aware objects are Python DateTime or time objects that include timezone information. An aware object represents a specific moment ...
Read more >
Working with Datetime Objects and Timezones in Python
To make a datetime object offset aware, you can use the pytz library. First, you have to instantiate a timezone object, and then...
Read more >
Working With TimeZones in Python - PYnative
Create TimeZone Aware Datetime Object Using timezone class ... A datetime or time object is aware if it holds the timezone(tz) value.
Read more >
How to Create and Convert Timezone Aware Dates with Python
Know the timezone you're working with · Assign the timezone to your datetime object if it is not aware with .replace(tzinfo=<timezone>) · Use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found