Author: jacob
Date: 2009-05-08 08:39:37 -0500 (Fri, 08 May 2009)
New Revision: 10716

Added:
   django/trunk/tests/regressiontests/utils/dateformat.py
Modified:
   django/trunk/django/utils/dateformat.py
   django/trunk/docs/ref/templates/builtins.txt
   django/trunk/tests/regressiontests/dateformat/tests.py
   django/trunk/tests/regressiontests/utils/tests.py
Log:
Fixed #10825: fixed the 'U' format code to dateformat (and the date/now 
filter/tag). Thanks to gsong and mir.

Modified: django/trunk/django/utils/dateformat.py
===================================================================
--- django/trunk/django/utils/dateformat.py     2009-05-08 12:58:57 UTC (rev 
10715)
+++ django/trunk/django/utils/dateformat.py     2009-05-08 13:39:37 UTC (rev 
10716)
@@ -11,12 +11,13 @@
 >>>
 """
 
+import re
+import time
+import calendar
 from django.utils.dates import MONTHS, MONTHS_3, MONTHS_AP, WEEKDAYS, 
WEEKDAYS_ABBR
 from django.utils.tzinfo import LocalTimezone
 from django.utils.translation import ugettext as _
 from django.utils.encoding import force_unicode
-from calendar import isleap, monthrange
-import re, time
 
 re_formatchars = re.compile(r'(?<!\\)([aAbBdDfFgGhHiIjlLmMnNOPrsStTUwWyYzZ])')
 re_escaped = re.compile(r'\\(.)')
@@ -146,7 +147,7 @@
 
     def L(self):
         "Boolean for whether it is a leap year; i.e. True or False"
-        return isleap(self.data.year)
+        return calendar.isleap(self.data.year)
 
     def m(self):
         "Month; i.e. '01' to '12'"
@@ -188,7 +189,7 @@
 
     def t(self):
         "Number of days in the given month; i.e. '28' to '31'"
-        return u'%02d' % monthrange(self.data.year, self.data.month)[1]
+        return u'%02d' % calendar.monthrange(self.data.year, 
self.data.month)[1]
 
     def T(self):
         "Time zone of this machine; e.g. 'EST' or 'MDT'"
@@ -199,8 +200,10 @@
 
     def U(self):
         "Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)"
-        off = self.timezone and self.timezone.utcoffset(self.data) or 0
-        return int(time.mktime(self.data.timetuple())) + off.seconds * 60
+        if getattr(self.data, 'tzinfo', None):
+            return int(calendar.timegm(self.data.utctimetuple()))
+        else:
+            return int(time.mktime(self.data.timetuple()))
 
     def w(self):
         "Day of the week, numeric, i.e. '0' (Sunday) to '6' (Saturday)"
@@ -214,12 +217,12 @@
         weekday = self.data.weekday() + 1
         day_of_year = self.z()
         if day_of_year <= (8 - jan1_weekday) and jan1_weekday > 4:
-            if jan1_weekday == 5 or (jan1_weekday == 6 and 
isleap(self.data.year-1)):
+            if jan1_weekday == 5 or (jan1_weekday == 6 and 
calendar.isleap(self.data.year-1)):
                 week_number = 53
             else:
                 week_number = 52
         else:
-            if isleap(self.data.year):
+            if calendar.isleap(self.data.year):
                 i = 366
             else:
                 i = 365

Modified: django/trunk/docs/ref/templates/builtins.txt
===================================================================
--- django/trunk/docs/ref/templates/builtins.txt        2009-05-08 12:58:57 UTC 
(rev 10715)
+++ django/trunk/docs/ref/templates/builtins.txt        2009-05-08 13:39:37 UTC 
(rev 10716)
@@ -506,7 +506,8 @@
                       month, 2 characters.
     t                 Number of days in the given month.        ``28`` to 
``31``
     T                 Time zone of this machine.                ``'EST'``, 
``'MDT'``
-    U                 Not implemented.
+    U                 Seconds since the Unix Epoch
+                      (January 1 1970 00:00:00 UTC).
     w                 Day of the week, digits without           ``'0'`` 
(Sunday) to ``'6'`` (Saturday)
                       leading zeros.
     W                 ISO-8601 week number of year, with        ``1``, ``53``

Modified: django/trunk/tests/regressiontests/dateformat/tests.py
===================================================================
--- django/trunk/tests/regressiontests/dateformat/tests.py      2009-05-08 
12:58:57 UTC (rev 10715)
+++ django/trunk/tests/regressiontests/dateformat/tests.py      2009-05-08 
13:39:37 UTC (rev 10716)
@@ -37,7 +37,7 @@
 u'31'
 >>> no_tz or format(my_birthday, 'T') == 'CET'
 True
->>> no_tz or format(my_birthday, 'U') == '300531600'
+>>> no_tz or format(my_birthday, 'U') == '300315600'
 True
 >>> format(my_birthday, 'w')
 u'0'

Added: django/trunk/tests/regressiontests/utils/dateformat.py
===================================================================
--- django/trunk/tests/regressiontests/utils/dateformat.py                      
        (rev 0)
+++ django/trunk/tests/regressiontests/utils/dateformat.py      2009-05-08 
13:39:37 UTC (rev 10716)
@@ -0,0 +1,48 @@
+"""
+>>> from datetime import datetime, date
+>>> from django.utils.dateformat import format
+>>> from django.utils.tzinfo import FixedOffset, LocalTimezone
+
+# date
+>>> d = date(2009, 5, 16)
+>>> date.fromtimestamp(int(format(d, 'U'))) == d
+True
+
+# Naive datetime
+>>> dt = datetime(2009, 5, 16, 5, 30, 30)
+>>> datetime.fromtimestamp(int(format(dt, 'U'))) == dt
+True
+
+# datetime with local tzinfo
+>>> ltz = LocalTimezone(datetime.now())
+>>> dt = datetime(2009, 5, 16, 5, 30, 30, tzinfo=ltz)
+>>> datetime.fromtimestamp(int(format(dt, 'U')), ltz) == dt
+True
+>>> datetime.fromtimestamp(int(format(dt, 'U'))) == dt.replace(tzinfo=None)
+True
+
+# datetime with arbitrary tzinfo
+>>> tz = FixedOffset(-510)
+>>> ltz = LocalTimezone(datetime.now())
+>>> dt = datetime(2009, 5, 16, 5, 30, 30, tzinfo=tz)
+>>> datetime.fromtimestamp(int(format(dt, 'U')), tz) == dt
+True
+>>> datetime.fromtimestamp(int(format(dt, 'U')), ltz) == dt
+True
+>>> datetime.fromtimestamp(int(format(dt, 'U'))) == 
dt.astimezone(ltz).replace(tzinfo=None)
+True
+>>> datetime.fromtimestamp(int(format(dt, 'U')), tz).utctimetuple() == 
dt.utctimetuple()
+True
+>>> datetime.fromtimestamp(int(format(dt, 'U')), ltz).utctimetuple() == 
dt.utctimetuple()
+True
+
+# Epoch
+>>> utc = FixedOffset(0)
+>>> udt = datetime(1970, 1, 1, tzinfo=utc)
+>>> format(udt, 'U')
+u'0'
+"""
+
+if __name__ == "__main__":
+    import doctest
+    doctest.testmod()

Modified: django/trunk/tests/regressiontests/utils/tests.py
===================================================================
--- django/trunk/tests/regressiontests/utils/tests.py   2009-05-08 12:58:57 UTC 
(rev 10715)
+++ django/trunk/tests/regressiontests/utils/tests.py   2009-05-08 13:39:37 UTC 
(rev 10716)
@@ -8,6 +8,7 @@
 
 import timesince
 import datastructures
+import dateformat
 import itercompat
 from decorators import DecoratorFromMiddlewareTests
 
@@ -22,6 +23,7 @@
 __test__ = {
     'timesince': timesince,
     'datastructures': datastructures,
+    'dateformat': dateformat,
     'itercompat': itercompat,
 }
 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to