Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#20693 closed New feature (fixed)

Add timezone support to built-in time filter

Reported by: warren@… Owned by: Warren Smith
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: warren@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently, the built-in time template filter does not support timezone, even if timezone information is present on the time or datetime object that is passed to it.

The built-in date filter DOES support timezone if passed a datetime object, so this is often used as a workaround. However, when attempting to use the DATE_FORMAT and TIME_FORMAT settings in conjunction with naked {{ value|date }} and {{ value|time }} template expressions to get consistent date and time formatting across a site, the lack of timezone support in the time filter is a problem if timezone information needs to be displayed after the time.

There is a discussion of this issue on the Django Developers list: https://groups.google.com/forum/#!topic/django-developers/r-7dMZROTs4

The consensus so far seems to be to modify the django.utils.dateformat.TimeFormat class so that it has similar timezone support for datetime objects as does the DateFormat class in the same module. Since DateFormat is a subclass of TimeFormat, it is likely that the timezone support methods can just be moved from DateFormat into TimeFormat, along with the init() method.

Some modification to the methods will need to occur to account for the case where a time object (not a datetime object) is passed to a filter with a timezone-related format specifier. It was decided in the discussion that the behavior in this case should be that the format method will return an empty string.

Change History (10)

comment:1 by Warren Smith, 11 years ago

Owner: changed from nobody to Warren Smith
Status: newassigned

comment:2 by Warren Smith, 11 years ago

I have the code modified. I'm working on adding tests and tweaking the documentation.

comment:3 by Warren Smith, 11 years ago

Summary: Add timezone support to built-in time template filterAdd timezone support to built-in time filter

I've added tests and have them all passing under SQLite. I don't think I need to test under additional dbs, since this is primarily a template change.

However, since the generic django time formatting behavior is changing slightly (assumption on my part given the location of my changes), I suppose it is possible that there might be some weird db-specific regression or subtle unexpected behavior change that will bite folks.

I've decided not to update the documentation unless directed to do so, as it is not currently wrong, per se, just a bit vague. With the changes in this ticket, assumptions that people might make given the current documentation will actually be valid.

I've submitted https://github.com/django/django/pull/1327.

Last edited 11 years ago by Warren Smith (previous) (diff)

comment:4 by Warren Smith, 11 years ago

Has patch: set

comment:5 by Warren Smith, 11 years ago

Triage Stage: UnreviewedAccepted

comment:6 by Warren Smith, 11 years ago

Version: 1.5master

comment:7 by Steven Cummings, 11 years ago

Triage Stage: AcceptedReady for checkin

Ready for checkin/final review by core dev.

comment:8 by anonymous, 11 years ago

I have re-based my local branch so the pull-request looks clean against the current master. Not sure what else to do.

comment:9 by Warren Smith <warren@…>, 11 years ago

Resolution: fixed
Status: assignedclosed

In dd3a883894a219bc6c69e556c694734ab82b33e9:

Fixed #20693 -- Add timezone support to built-in time filter.

Modified django.utils.dateformat module, moving init() method and
timezone-related format methods from DateFormat class to TimeFormat
base class. Modified timezone-related format methods to return an
empty string when timezone is inappropriate for input value.

comment:10 by Ramiro Morales <cramm0@…>, 11 years ago

In bebb449ac334d7365a015b34b9c894dc4bf63f7f:

Added docs for changes in commit dd3a883894.

Refs #20693.

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