#18691 closed Bug (wontfix)
No timezone support for TimeField
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.4 |
Severity: | Normal | Keywords: | timezone, datetime, datetimefield, timefield |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Django 1.4 does not store aware times for TimeField, it only does for DateTimeField. Its really annoying, as python datetime.time object does support TZINFO just like datetime.datetime objects.
Steps to reproduce:
In the view i have set the timezone to timezone.activate(timezone) and after that i used form's TimeInput field to get the time. Now when i store it, it does not make the conversion from the current timezone to default timezone (which is set to UTC), it just naive time object. If i follow the same steps with DateTimeField, it works as expected, the datetime is first converted from current timezone to default one and then stored to the db.
Db used:
Postgres 9.1 with psycopg2 adapter
Change History (6)
comment:1 by , 12 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
comment:2 by , 12 years ago
Thanks for the quick response, could you please point me to the direction where i could get a help with AwareTimeField
comment:3 by , 12 years ago
The documentation on writing custom model fields should help you.
Here's how aware-datetime support was added DateTimeField
: https://github.com/django/django/commit/9b1cb755a2#L14L735
You must do similar changes in a custom TimeField
. You can either inherit Django's TimeField
and override a few methods or copy its definition and edit it -- I don't know which way will look more clean.
comment:5 by , 11 years ago
I think this can be handled with just a custom form field. For those looking for a solution, you might try this: https://gist.github.com/jfialkoff/7311019
comment:6 by , 11 years ago
Sorry, I posted that last comment in haste and eventually realized that the proposed solution was not a good way to handle this issue. Here is a better-thought-out solution: http://stackoverflow.com/questions/19780082/how-to-make-timefield-timezone-aware/19780083#19780083. Feel free to delete the last comment.
This is a design decision and it's explained in the documentation. In short aware time objects are pathological creatures :)
It is fairly easy to create a custom AwareTimeField if you need it.