[Zope3-checkins] SVN: Zope3/trunk/ Implemented all missing date/time formatters for zope.i18n.format. I also

Stephan Richter srichter at cosmos.phy.tufts.edu
Tue Feb 15 00:34:59 EST 2005


Log message for revision 29145:
  Implemented all missing date/time formatters for zope.i18n.format. I also 
  noticed that the week information was not put into the calendar, so I 
  fixed that too.
  
  Added a couple more tests.
  
  I started all this because I looked at Marius' reported problems. However, 
  the formatter seems to be solid, so it must be the root locale.
  
  

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/i18n/format.py
  U   Zope3/trunk/src/zope/i18n/interfaces/__init__.py
  U   Zope3/trunk/src/zope/i18n/locales/__init__.py
  U   Zope3/trunk/src/zope/i18n/locales/tests/test_locales.py
  U   Zope3/trunk/src/zope/i18n/locales/xmlfactory.py
  U   Zope3/trunk/src/zope/i18n/tests/test_formats.py

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2005-02-15 04:00:26 UTC (rev 29144)
+++ Zope3/trunk/doc/CHANGES.txt	2005-02-15 05:34:57 UTC (rev 29145)
@@ -375,6 +375,9 @@
 
     Bug Fixes
 
+      - Fixed most formatting characters for i18n's datetime formatter. Only
+        timezones are left.
+
       - Partially fixed issue #306 (Problem 2: Browser page and view directive
         cannot register serveral pages views within different layers for
         the same for, menu and title attributes.)

Modified: Zope3/trunk/src/zope/i18n/format.py
===================================================================
--- Zope3/trunk/src/zope/i18n/format.py	2005-02-15 04:00:26 UTC (rev 29144)
+++ Zope3/trunk/src/zope/i18n/format.py	2005-02-15 05:34:57 UTC (rev 29145)
@@ -540,8 +540,12 @@
     if h == 0:
         h = 12
 
-    weekday = dt.weekday()+1
+    weekday = (dt.weekday() + (8 - calendar.week['firstDay'])) % 7 + 1
 
+    day_of_week_in_month = (dt.day - 1) / 7 + 1
+
+    week_in_month = (dt.day + 6 - dt.weekday()) / 7 + 1
+
     return {
         ('a', 1): ampm,
         ('G', 1): 'AD',
@@ -560,10 +564,18 @@
         ('D', 1): dt.strftime('%j'),
         ('w', 1): dt.strftime('%W'),
         ('w', 2): dt.strftime('%.2W'),
+        ('W', 1): "%i" %week_in_month,
+        ('W', 2): "%.2i" %week_in_month,
+        ('F', 1): "%i" %day_of_week_in_month,
+        ('F', 2): "%.2i" %day_of_week_in_month,
         ('h', 1): str(h),
         ('h', 2): "%.2i" %(h),
+        ('K', 1): str(dt.hour%12),
+        ('K', 2): "%.2i" %(dt.hour%12),
         ('H', 1): str(dt.hour),
         ('H', 2): "%.2i" %dt.hour,
+        ('k', 1): str(dt.hour or 24),
+        ('k', 2): "%.2i" %(dt.hour or 24),
         ('m', 1): str(dt.minute),
         ('m', 2): "%.2i" %dt.minute,
         ('s', 1): str(dt.second),
@@ -572,14 +584,6 @@
         ('S', 2): "%.6i" %dt.microsecond,
         # TODO: Implement the following symbols. This requires the handling of
         # timezones.
-        ('F', 1): str(2),
-        ('F', 2): "%.2i" %(2),
-        ('W', 1): str(2),
-        ('W', 2): "%.2i" %(2),
-        ('k', 1): str(dt.hour+1),
-        ('k', 2): "%.2i" %(dt.hour+1),
-        ('K', 1): str(dt.hour%12),
-        ('K', 2): "%.2i" %(dt.hour%12),
         ('z', 1): "+000",
         ('z', 2): "+00:00",
         ('z', 3): "UTC",

Modified: Zope3/trunk/src/zope/i18n/interfaces/__init__.py
===================================================================
--- Zope3/trunk/src/zope/i18n/interfaces/__init__.py	2005-02-15 04:00:26 UTC (rev 29144)
+++ Zope3/trunk/src/zope/i18n/interfaces/__init__.py	2005-02-15 05:34:57 UTC (rev 29145)
@@ -354,8 +354,8 @@
       y      year                  (Number)          1996
       M      month in year         (Text and Number) July and 07
       d      day in month          (Number)          10
-      h      hour in am/pm (1~12)  (Number)          12
-      H      hour in day (0~23)    (Number)          0
+      h      hour in am/pm (1-12)  (Number)          12
+      H      hour in day (0-23)    (Number)          0
       m      minute in hour        (Number)          30
       s      second in minute      (Number)          55
       S      millisecond           (Number)          978
@@ -365,8 +365,8 @@
       w      week in year          (Number)          27
       W      week in month         (Number)          2
       a      am/pm marker          (Text)            pm
-      k      hour in day (1~24)    (Number)          24
-      K      hour in am/pm (0~11)  (Number)          0
+      k      hour in day (1-24)    (Number)          24
+      K      hour in am/pm (0-11)  (Number)          0
       z      time zone             (Text)            Pacific Standard Time
       '      escape for text
       ''     single quote                            '

Modified: Zope3/trunk/src/zope/i18n/locales/__init__.py
===================================================================
--- Zope3/trunk/src/zope/i18n/locales/__init__.py	2005-02-15 04:00:26 UTC (rev 29144)
+++ Zope3/trunk/src/zope/i18n/locales/__init__.py	2005-02-15 05:34:57 UTC (rev 29145)
@@ -368,6 +368,7 @@
       >>> cal.am = Stub.am
       >>> cal.pm = Stub.pm
       >>> cal.eras = Stub.eras
+      >>> cal.week = {'firstDay': 1, 'minDays': 1}
       >>> dates.calendars = {'gregorian': cal}
 
       Setting up and accessing date format through a specific length

Modified: Zope3/trunk/src/zope/i18n/locales/tests/test_locales.py
===================================================================
--- Zope3/trunk/src/zope/i18n/locales/tests/test_locales.py	2005-02-15 04:00:26 UTC (rev 29144)
+++ Zope3/trunk/src/zope/i18n/locales/tests/test_locales.py	2005-02-15 05:34:57 UTC (rev 29145)
@@ -147,3 +147,6 @@
         makeSuite(TestLocaleAndProvider),
         makeSuite(TestGlobalLocaleProvider),
         ))
+
+if __name__ == "__main__":
+    unittest.main(defaultTest='test_suite')

Modified: Zope3/trunk/src/zope/i18n/locales/xmlfactory.py
===================================================================
--- Zope3/trunk/src/zope/i18n/locales/xmlfactory.py	2005-02-15 04:00:26 UTC (rev 29144)
+++ Zope3/trunk/src/zope/i18n/locales/xmlfactory.py	2005-02-15 05:34:57 UTC (rev 29145)
@@ -725,6 +725,8 @@
             self._extractMonths(cal_node, calendar)
             # get weekday names and abbreviations
             self._extractDays(cal_node, calendar)
+            # get week information
+            self._extractWeek(cal_node, calendar)
 
             # get am/pm designation values
             nodes = cal_node.getElementsByTagName('am')

Modified: Zope3/trunk/src/zope/i18n/tests/test_formats.py
===================================================================
--- Zope3/trunk/src/zope/i18n/tests/test_formats.py	2005-02-15 04:00:26 UTC (rev 29144)
+++ Zope3/trunk/src/zope/i18n/tests/test_formats.py	2005-02-15 05:34:57 UTC (rev 29145)
@@ -52,6 +52,8 @@
 
     eras = {1: (None, 'v. Chr.'), 2: (None, 'n. Chr.')}
 
+    week = {'firstDay': 1, 'minDays': 1}
+
     def getMonthNames(self):
         return [self.months.get(type, (None, None))[0] for type in range(1, 13)]
 
@@ -290,7 +292,7 @@
             'dd.MM.yy hh:mm a'),
             '02.01.03 09:48 nachm.')
 
-    def test_formatAllWeekdays(self):
+    def testFormatAllWeekdays(self):
         for day in range(1, 8):
             self.assertEqual(self.format.format(
                 datetime.datetime(2003, 01, day+5, 21, 48),
@@ -298,6 +300,98 @@
                 '%s, %i. Januar 2003 21:48 Uhr +000' %(
                 self.format.calendar.days[day][0], day+5))
 
+    def testFormatWeekDay(self):
+        date = datetime.date(2003, 01, 02)
+        self.assertEqual(self.format.format(date, "E"),
+                         '4')
+        self.assertEqual(self.format.format(date, "EE"),
+                         '04')
+        self.assertEqual(self.format.format(date, "EEE"),
+                         'Do')
+        self.assertEqual(self.format.format(date, "EEEE"),
+                         'Donnerstag')
+
+        # Create custom calendar, which has Sunday as the first day of the
+        # week
+        calendar = LocaleCalendarStub()
+        calendar.week['firstDay'] = 7
+        format = DateTimeFormat(calendar=calendar)
+
+        self.assertEqual(format.format(date, "E"),
+                         '5')
+        self.assertEqual(format.format(date, "EE"),
+                         '05')
+        
+    def testFormatDayOfWeekInMonth(self):
+        date = datetime.date(2003, 01, 02)
+        self.assertEqual(self.format.format(date, "F"),
+                         '1')
+        self.assertEqual(self.format.format(date, "FF"),
+                         '01')
+        self.assertEqual(
+            self.format.format(datetime.date(2003, 1, 9), "F"),
+            '2')
+        self.assertEqual(
+            self.format.format(datetime.date(2003, 1, 16), "F"),
+            '3')
+        self.assertEqual(
+            self.format.format(datetime.date(2003, 1, 23), "F"),
+            '4')
+
+    def testFormatWeekInMonth(self):
+        self.assertEqual(
+            self.format.format(datetime.date(2003, 1, 3), "W"),
+            '1')
+        self.assertEqual(
+            self.format.format(datetime.date(2003, 1, 3), "WW"),
+            '01')
+        self.assertEqual(
+            self.format.format(datetime.date(2003, 1, 8), "W"),
+            '2')
+        self.assertEqual(
+            self.format.format(datetime.date(2003, 1, 19), "W"),
+            '3')
+        self.assertEqual(
+            self.format.format(datetime.date(2003, 1, 20), "W"),
+            '4')
+        self.assertEqual(
+            self.format.format(datetime.date(2003, 1, 31), "W"),
+            '5')
+
+    def testFormatHourInDayOneTo24(self):
+        self.assertEqual(
+            self.format.format(datetime.time(5, 0), "k"),
+            '5')
+        self.assertEqual(
+            self.format.format(datetime.time(5, 0), "kk"),
+            '05')
+        self.assertEqual(
+            self.format.format(datetime.time(0, 0), "k"),
+            '24')
+        self.assertEqual(
+            self.format.format(datetime.time(1, 0), "k"),
+            '1')
+
+    def testFormatHourInDayZeroToEleven(self):
+        self.assertEqual(
+            self.format.format(datetime.time(5, 0), "K"),
+            '5')
+        self.assertEqual(
+            self.format.format(datetime.time(5, 0), "KK"),
+            '05')
+        self.assertEqual(
+            self.format.format(datetime.time(0, 0), "K"),
+            '0')
+        self.assertEqual(
+            self.format.format(datetime.time(12, 0), "K"),
+            '0')
+        self.assertEqual(
+            self.format.format(datetime.time(11, 0), "K"),
+            '11')
+        self.assertEqual(
+            self.format.format(datetime.time(23, 0), "K"),
+            '11')
+
     def testFormatSimpleHourRepresentation(self):
         self.assertEqual(
             self.format.format(datetime.datetime(2003, 01, 02, 23, 00),



More information about the Zope3-Checkins mailing list