Opened 3 years ago

Closed 3 years ago

Last modified 2 years ago

#18691 closed Bug (wontfix)

No timezone support for TimeField

Reported by: tejinderss@… 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


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 Changed 3 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

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.

comment:2 Changed 3 years ago by tejinderss@…

Thanks for the quick response, could you please point me to the direction where i could get a help with AwareTimeField

comment:3 Changed 3 years ago by aaugustin

Here's how aware-datetime support was added DateTimeField:

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.

Version 0, edited 3 years ago by aaugustin (next)

comment:4 Changed 3 years ago by tejinderss@…

Ok i will have a look, thanks a lot.

comment:5 Changed 2 years ago by anonymous

I think this can be handled with just a custom form field. For those looking for a solution, you might try this:

comment:6 Changed 2 years ago by joshua.fialkoff@…

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: Feel free to delete the last comment.

Note: See TracTickets for help on using tickets.
Back to Top