Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loaddata not dealing with timestamps and timezones properly

I'm using django 1.4.1 with mysql and timezones enabled. I did a dump data to yaml, modified some fields to create some test data, and am trying to load it back in. however, Django keeps complaining about naive datetimes even though a tz is specified

specifically, my loaddata has:

fields: {created_date: !!timestamp '2012-09-15 22:17:44+00:00', ...

but loaddata gives the error:

RuntimeWarning: DateTimeField received a naive datetime (2012-09-15 22:17:44) while time zone support is active.

This doesn't make much sense to me, seeing as its:

  1. a UTC timestamp
  2. the same exact format Django exported using dumpdata

is there some way i can tell django this is a UTC date?

like image 901
IMFletcher Avatar asked Sep 17 '12 15:09

IMFletcher


People also ask

What is the best time zone to store things like when a record was added or updated?

Rule 1: Store Dates In UTC The widely-recommended solution for storing dates and times is to store the date and time in UTC. This means, whenever you have a user that inserts or updates a datetime value in the database, convert it to UTC and store the UTC value in the database column.

How does Django use timezone now?

Time zone support is disabled by default. To enable it, set USE_TZ = True in your settings file. In Django 5.0, time zone support will be enabled by default.

What is NTZ time zone?

TIMESTAMP_NTZ is the datatype for timestamps without a timezone (ntz = no time zone). This is also referred to as “walltime” as it is the time you would get by looking at a random clock on the wall and writing it down.


2 Answers

The problem stems from PyYAML. When loaddata hands off the datetime to PyYAML, it takes the aware datetime, adjusts the time to UTC, and then returns a naive datetime, which generates the warning.

There is a Django ticket, as well as a PyYAML ticket concerning the issue. Both go into far greater detail concerning the unexpected behavior above. Judging by the comments in the tickets, this issue seems unlikely to be resolved anytime soon.

Is you set TIME_ZONE = 'UTC' in settings.py of your project, you will load in the correct time, but you will still get warnings. Should your timezone be set to anything else, Django will treat the datetime as local, and adjust it to UTC, which is probably undesired.

The best way to avoid this is to use JSON as a serialization format.

Hope that helps.

like image 91
Andrew Pinkham Avatar answered Sep 27 '22 16:09

Andrew Pinkham


From the docs...

When serializing an aware datetime, the UTC offset is included, like this:

"2011-09-01T13:20:30+03:00"

For a naive datetime, it obviously isn't:

"2011-09-01T13:20:30"

...so instead of...

created_date: !!timestamp '2012-09-15 22:17:44+00:00'

...either of...

created_date: '2012-09-15T22:17:44+00:00'

...or...

created_date: '2012-09-15T22:17:44Z'

...will work.

like image 32
Aya Avatar answered Sep 27 '22 18:09

Aya