Code

Ticket #4121: dateformat.py.diff

File dateformat.py.diff, 2.8 KB (added by Ciantic, 7 years ago)

Adds missing functionality for DateFormat and TimeFormat

Line 
1Index: django/utils/dateformat.py
2===================================================================
3--- django/utils/dateformat.py  (revision 5059)
4+++ django/utils/dateformat.py  (working copy)
5@@ -13,11 +13,11 @@
6 
7 from django.utils.dates import MONTHS, MONTHS_3, MONTHS_AP, WEEKDAYS
8 from django.utils.tzinfo import LocalTimezone
9-from django.utils.translation import gettext as _
10+from django.utils.translation import get_date_formats, get_partial_date_formats, gettext as _
11 from calendar import isleap, monthrange
12 import re, time
13 
14-re_formatchars = re.compile(r'(?<!\\)([aAbBdDfFgGhHiIjlLmMnNOPrsStTUwWyYzZ])')
15+re_formatchars = re.compile(r'(?<!\\)([aAbcBdDfFgGhHiIjkKlLmMnNOPrsStTUwWxXyYzZ])')
16 re_escaped = re.compile(r'\\(.)')
17 
18 class Formatter(object):
19@@ -100,6 +100,11 @@
20     def s(self):
21         "Seconds; i.e. '00' to '59'"
22         return '%02d' % self.data.second
23+   
24+    def X(self):
25+        "Time, appropriate time representation. e.g. '8:00'"
26+        (date_format, datetime_format, time_format) = get_date_formats()
27+        return format(self.data, time_format)     
28 
29 class DateFormat(TimeFormat):
30     year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
31@@ -114,6 +119,11 @@
32     def b(self):
33         "Month, textual, 3 letters, lowercase; e.g. 'jan'"
34         return MONTHS_3[self.data.month]
35+   
36+    def c(self):
37+        "Datetime, appropriate date and time representation.; e.g. '2007-04-23 05:00'"
38+        (date_format, datetime_format, time_format) = get_date_formats()
39+        return format(self.data, datetime_format)
40 
41     def d(self):
42         "Day of the month, 2 digits with leading zeros; i.e. '01' to '31'"
43@@ -137,7 +147,17 @@
44     def j(self):
45         "Day of the month without leading zeros; i.e. '1' to '31'"
46         return self.data.day
47-
48+   
49+    def k(self):
50+        "Month day, appropriate month day representation. e.g. 'January 5'"
51+        (year_month_format, month_day_format) = get_partial_date_formats()
52+        return format(self.data, month_day_format)           
53+   
54+    def K(self):
55+        "Year month, appropriate year month representation. e.g. '2007 January'"
56+        (year_month_format, month_day_format) = get_partial_date_formats()
57+        return format(self.data, year_month_format)   
58+   
59     def l(self):
60         "Day of the week, textual, long; e.g. 'Friday'"
61         return WEEKDAYS[self.data.weekday()]
62@@ -237,6 +257,11 @@
63     def Y(self):
64         "Year, 4 digits; e.g. '1999'"
65         return self.data.year
66+   
67+    def x(self):
68+        "Date, appropriate date representation. e.g. '2007-04-23'"
69+        (date_format, datetime_format, time_format) = get_date_formats()
70+        return format(self.data, date_format)       
71 
72     def z(self):
73         "Day of the year; i.e. '0' to '365'"