Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Fix for ticket 19370 (date filter returns empty string for midnight) #558

Closed
wants to merge 1 commit into from

2 participants

Danilo Bargen Claude Paroz
Danilo Bargen

Ticket 19370: https://code.djangoproject.com/ticket/19370

Python considers midnight (datetime.time(0, 0)) to be False. The date template filter stumbles over this.

When using {{ midnight|date:"H:i" }}, Django returns an empty string.

See ​http://bugs.python.org/issue13936 for discussion about the bug.

This is already fixed in the |time filter, but not in the |date filter which can also be used to format times.

Claude Paroz
Owner

Thanks, committed in c10aaa7 (master) and 5509eb8 (1.5.x)

Claude Paroz claudep closed this November 27, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Nov 27, 2012
Danilo Bargen Fix for ticket 19370 (date filter returns empty string for midnight) b86508e
This page is out of date. Refresh to see the latest.
2  django/template/defaultfilters.py
@@ -704,7 +704,7 @@ def get_digit(value, arg):
704 704
 @register.filter(expects_localtime=True, is_safe=False)
705 705
 def date(value, arg=None):
706 706
     """Formats a date according to the given format."""
707  
-    if not value:
  707
+    if value in (None, ''):
708 708
         return ''
709 709
     if arg is None:
710 710
         arg = settings.DATE_FORMAT
5  tests/regressiontests/templates/filters.py
@@ -8,7 +8,7 @@
8 8
 """
9 9
 from __future__ import unicode_literals
10 10
 
11  
-from datetime import date, datetime, timedelta
  11
+from datetime import date, datetime, time, timedelta
12 12
 
13 13
 from django.test.utils import str_prefix
14 14
 from django.utils.tzinfo import LocalTimezone, FixedOffset
@@ -356,6 +356,9 @@ def get_filter_tests():
356 356
         # Timezone name
357 357
         'date06': (r'{{ d|date:"e" }}', {'d': datetime(2009, 3, 12, tzinfo=FixedOffset(30))}, '+0030'),
358 358
         'date07': (r'{{ d|date:"e" }}', {'d': datetime(2009, 3, 12)}, ''),
  359
+        # Ticket 19370: Make sure |date doesn't blow up on a midnight time object
  360
+        'date08': (r'{{ t|date:"H:i" }}', {'t': time(0, 1)}, '00:01'),
  361
+        'date09': (r'{{ t|date:"H:i" }}', {'t': time(0, 0)}, '00:00'),
359 362
 
360 363
          # Tests for #11687 and #16676
361 364
          'add01': (r'{{ i|add:"5" }}', {'i': 2000}, '2005'),
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.