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.

Add load_as_tz_aware (and dump_as_tz_aware ?) parameter to DateTime

See original GitHub issue

As said in the docs,

Schema.load returns datetime objects that are timezone-aware.

I’m using webargs to parse input to an API and the code expects naive datetimes. I would like to get naive datetimes, rather than having to remove the TZ from every date attribute in each resource method.

Maybe I’m wrong working with naive datetimes in the first place. I have no opinion about it. But sometimes you’re working with a lib that expects naive datetimes, and you don’t want to patch it.

I guess the most flexible approach would be to provide a field parameter allowing to choose which format a DateTime field should output.

Could it be possible to have this configured with a flag/metadata in the DateTime field?

Or is there a design reason not to allow this?

(Apparently, I’m not the first one falling into this: https://github.com/marshmallow-code/marshmallow/issues/309.)

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
lafrechcommented, Sep 2, 2018

I ended up subclassing DateTime in a custom Field with a load_as_tz_aware boolean parameter. (Discussion here.)

Basically, it deserializes like this:

        date = super()._deserialize(value, attr, data)
        if self.load_as_tz_aware:
            # If datetime is TZ naive, set UTC timezone
            if date.tzinfo is None or date.tzinfo.utcoffset(date) is None:
                date = date.replace(tzinfo=tzutc())
        else:
            # If datetime is TZ aware, convert it to UTC and remove TZ info
            if date.tzinfo is not None and date.tzinfo.utcoffset(date) is not None:
                date = date.astimezone(tzutc())
            date = date.replace(tzinfo=None)
        return date

I still think this could be addressed in Marshmallow.

Using Marshmallow + webargs to parse API inputs is a typical use case, and currently, you can’t rely on it to ensure the awareness of deserialized dates. This is an issue, especially since comparisons of aware and non-aware dates generate an exception: a user can make the application crash by providing wrong input data. Unless of course the application has specific code to cope with both formats, or ensures awareness by itself, but isn’t this Marshmallow’s purpose?

My subclass only addresses deserialization, but there may also be a use case for a dump_as_tz_aware parameter (which I think is a bit harder to do in a subclass).

0reactions
lafrechcommented, Jul 3, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

Insert datetime parameter to access datebase ODBC
I was using the "AddWithValue" and I could only get it to add a straight date, but I wanted the time portion also....
Read more >
Template functions - date - Azure Resource Manager
Adds a time duration to a base value. ISO 8601 format is expected. In Bicep, use the dateTimeAdd function. Parameters. Parameter, Required, Type ......
Read more >
Adding date filters - Amazon QuickSight - AWS Documentation
You create filters on date fields by selecting the filter conditions and date values that you want to use. There are three filter...
Read more >
Datetime To Date Parameter issue... - SAP Community
What I did is to create a scalar-valued function in SQL server to convert the datetime field to Crystal parameter date format. Add...
Read more >
a!addDateTime() Function - Appian 22.4
Adds the specified increments of time to the startDateTime and returns a date and time value. You can select a process calendar to...
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