Beispiel #1
0
    def testFormatWeekDay(self):
        date = datetime.date(2003, 1, 2)
        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. I am assigning a totally new dict here, since dicts are
        # mutable and the value would be changed for the class and all its
        # instances.
        calendar = LocaleCalendarStub()
        calendar.week = {'firstDay': 7, 'minDays': 1}
        format = DateTimeFormat(calendar=calendar)

        self.assertEqual(format.format(date, "E"), '5')
        self.assertEqual(format.format(date, "EE"), '05')
Beispiel #2
0
    def testFormatWeekDay(self):
        date = datetime.date(2003, 1, 2)
        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. I am assigning a totally new dict here, since dicts are
        # mutable and the value would be changed for the class and all its
        # instances.
        calendar = LocaleCalendarStub()
        calendar.week = {"firstDay": 7, "minDays": 1}
        format = DateTimeFormat(calendar=calendar)

        self.assertEqual(format.format(date, "E"), "5")
        self.assertEqual(format.format(date, "EE"), "05")
    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. I am assigning a totally new dict here, since dicts are
        # mutable and the value would be changed for the class and all its
        # instances.
        calendar = LocaleCalendarStub()
        calendar.week = {'firstDay': 7, 'minDays': 1}
        format = DateTimeFormat(calendar=calendar)

        self.assertEqual(format.format(date, "E"),
                         '5')
        self.assertEqual(format.format(date, "EE"),
                         '05')
    def getFormatter(self,
                     category,
                     length=None,
                     name=None,
                     calendar=u'gregorian'):
        """See zope.i18n.interfaces.locales.ILocaleDates"""
        if category not in (u'date', u'time', u'dateTime'):
            raise ValueError('Invalid category: %s' % category)
        if calendar not in (u'gregorian', u'arabic', u'chinese',
                            u'civil-arabic', u'hebrew', u'japanese',
                            u'thai-buddhist'):
            raise ValueError('Invalid calendar: %s' % calendar)
        if length not in (u'short', u'medium', u'long', u'full', None):
            raise ValueError('Invalid format length: %s' % length)

        cal = self.calendars[calendar]

        formats = getattr(cal, category + 'Formats')
        if length is None:
            length = getattr(
                cal, 'default' + category[0].upper() + category[1:] + 'Format',
                formats.keys()[0])

        # 'datetime' is always a bit special; we often do not have a length
        # specification, but we need it for looking up the date and time
        # formatters
        if category == 'dateTime':
            formatLength = formats.get(length, formats[None])
        else:
            formatLength = formats[length]

        if name is None:
            name = formatLength.default

        format = formatLength.formats[name]
        pattern = format.pattern

        if category == 'dateTime':
            date_pat = self.getFormatter('date', length, name,
                                         calendar).getPattern()
            time_pat = self.getFormatter('time', length, name,
                                         calendar).getPattern()
            pattern = pattern.replace('{1}', date_pat)
            pattern = pattern.replace('{0}', time_pat)

        return DateTimeFormat(pattern, cal)
Beispiel #5
0
class TestDateTimeFormat(TestCase):
    """Test the functionality of an implmentation of the ILocaleProvider
    interface."""

    format = DateTimeFormat(calendar=LocaleCalendarStub())

    def testInterfaceConformity(self):
        self.assertTrue(IDateTimeFormat.providedBy(self.format))

    def testParseSimpleDateTime(self):
        # German short
        self.assertEqual(
            self.format.parse('02.01.03 21:48', 'dd.MM.yy HH:mm'),
            datetime.datetime(2003, 1, 2, 21, 48))

    def testParseRealDateTime(self):
        # German medium
        self.assertEqual(
            self.format.parse('02.01.2003 21:48:01', 'dd.MM.yyyy HH:mm:ss'),
            datetime.datetime(2003, 1, 2, 21, 48, 1))

        # German long
        # TODO: The parser does not support timezones yet.
        self.assertEqual(self.format.parse(
            '2. Januar 2003 21:48:01 +100',
            'd. MMMM yyyy HH:mm:ss z'),
            pytz.timezone('Europe/Berlin').localize(
                datetime.datetime(2003, 1, 2, 21, 48, 1)))

        # German full
        # TODO: The parser does not support timezones yet.
        self.assertEqual(self.format.parse(
            'Donnerstag, 2. Januar 2003 21:48 Uhr +100',
            "EEEE, d. MMMM yyyy H:mm' Uhr 'z"),
            pytz.timezone('Europe/Berlin').localize(
                datetime.datetime(2003, 1, 2, 21, 48)))

    def testParseAMPMDateTime(self):
        self.assertEqual(
            self.format.parse('02.01.03 09:48 nachm.', 'dd.MM.yy hh:mm a'),
            datetime.datetime(2003, 1, 2, 21, 48))

    def testParseTimeZone(self):
        dt = self.format.parse('09:48 -600', 'HH:mm z')
        self.assertEqual(pickle.loads(pickle.dumps(dt)), dt)
        self.assertEqual(dt.tzinfo.utcoffset(dt), datetime.timedelta(hours=-6))
        self.assertEqual(dt.tzinfo.zone, None)
        self.assertEqual(dt.tzinfo.tzname(dt), None)

        dt = self.format.parse('09:48 -06:00', 'HH:mm zz')
        self.assertEqual(pickle.loads(pickle.dumps(dt)), dt)
        self.assertEqual(dt.tzinfo.utcoffset(dt), datetime.timedelta(hours=-6))
        self.assertEqual(dt.tzinfo.zone, None)
        self.assertEqual(dt.tzinfo.tzname(dt), None)

    def testParseTimeZoneNames(self):
        # Note that EST is a deprecated timezone name since it is a US
        # interpretation (other countries also use the EST timezone
        # abbreviation)
        dt = self.format.parse('01.01.2003 09:48 EST', 'dd.MM.yyyy HH:mm zzz')
        self.assertEqual(pickle.loads(pickle.dumps(dt)), dt)
        self.assertEqual(dt.tzinfo.utcoffset(dt), datetime.timedelta(hours=-5))
        self.assertEqual(dt.tzinfo.zone, 'EST')
        self.assertEqual(dt.tzinfo.tzname(dt), 'EST')

        dt = self.format.parse('01.01.2003 09:48 US/Eastern',
                               'dd.MM.yyyy HH:mm zzzz')
        self.assertEqual(pickle.loads(pickle.dumps(dt)), dt)
        self.assertEqual(dt.tzinfo.utcoffset(dt), datetime.timedelta(hours=-5))
        self.assertEqual(dt.tzinfo.zone, 'US/Eastern')
        self.assertEqual(dt.tzinfo.tzname(dt), 'EST')

        dt = self.format.parse('01.01.2003 09:48 Canada/Eastern',
                               'dd.MM.yyyy HH:mm zzzz')
        self.assertEqual(dt.tzinfo.utcoffset(dt), datetime.timedelta(hours=-5))
        self.assertEqual(dt.tzinfo.zone, 'Canada/Eastern')
        self.assertEqual(dt.tzinfo.tzname(dt), 'EST')

        # Note that historical and future (as far as known)
        # timezones are handled happily using the pytz timezone database
        # US DST transition points are changing in 2007
        dt = self.format.parse('01.04.2006 09:48 US/Eastern',
                               'dd.MM.yyyy HH:mm zzzz')
        self.assertEqual(dt.tzinfo.zone, 'US/Eastern')
        self.assertEqual(dt.tzinfo.tzname(dt), 'EST')
        self.assertEqual(dt.tzinfo.utcoffset(dt), datetime.timedelta(hours=-5))
        dt = self.format.parse('01.04.2007 09:48 US/Eastern',
                               'dd.MM.yyyy HH:mm zzzz')
        self.assertEqual(dt.tzinfo.zone, 'US/Eastern')
        self.assertEqual(dt.tzinfo.tzname(dt), 'EDT')
        self.assertEqual(dt.tzinfo.utcoffset(dt), datetime.timedelta(hours=-4))

    def testDateTimeParseError(self):
        self.assertRaises(DateTimeParseError,
            self.format.parse, '02.01.03 21:48', 'dd.MM.yyyy HH:mm')
        self.assertRaises(DateTimeParseError,
            self.format.parse, '02.01.2003', 'dd.MM.yy')
        self.assertRaises(DateTimeParseError,
            self.format.parse, 'ff02.01.03', 'dd.MM.yy')

    def testParse12PM(self):
        self.assertEqual(
            self.format.parse('01.01.03 12:00 nachm.', 'dd.MM.yy hh:mm a'),
            datetime.datetime(2003, 1, 1, 12, 00, 00, 00))

    def testParseUnusualFormats(self):
        self.assertEqual(
            self.format.parse('001. Januar 03 0012:00',
                              'ddd. MMMMM yy HHHH:mm'),
            datetime.datetime(2003, 1, 1, 12, 00, 00, 00))
        self.assertEqual(
            self.format.parse('0001. Jan 2003 0012:00 vorm.',
                              'dddd. MMM yyyy hhhh:mm a'),
            datetime.datetime(2003, 1, 1, 00, 00, 00, 00))

    def testFormatSimpleDateTime(self):
        # German short
        self.assertEqual(
            self.format.format(datetime.datetime(2003, 1, 2, 21, 48),
                              'dd.MM.yy HH:mm'),
            '02.01.03 21:48')

    def testFormatRealDateTime(self):
        tz = pytz.timezone('Europe/Berlin')
        dt = tz.localize(datetime.datetime(2003, 1, 2, 21, 48, 1))
        # German medium
        self.assertEqual(
            self.format.format(dt, 'dd.MM.yyyy HH:mm:ss'),
            '02.01.2003 21:48:01')

        # German long
        self.assertEqual(
            self.format.format(dt, 'd. MMMM yyyy HH:mm:ss z'),
            '2. Januar 2003 21:48:01 +100')

        # German full
        self.assertEqual(self.format.format(
            dt, "EEEE, d. MMMM yyyy H:mm' Uhr 'z"),
            'Donnerstag, 2. Januar 2003 21:48 Uhr +100')

    def testFormatAMPMDateTime(self):
        self.assertEqual(self.format.format(
            datetime.datetime(2003, 1, 2, 21, 48),
            'dd.MM.yy hh:mm a'),
            '02.01.03 09:48 nachm.')

    def testFormatAllWeekdays(self):
        for day in range(1, 8):
            self.assertEqual(self.format.format(
                datetime.datetime(2003, 1, day+5, 21, 48),
                "EEEE, d. MMMM yyyy H:mm' Uhr 'z"),
                '%s, %i. Januar 2003 21:48 Uhr +000' %(
                self.format.calendar.days[day][0], day+5))

    def testFormatTimeZone(self):
        self.assertEqual(self.format.format(
            datetime.datetime(2003, 1, 2, 12, 00), 'z'),
            '+000')
        self.assertEqual(self.format.format(
            datetime.datetime(2003, 1, 2, 12, 00), 'zz'),
            '+00:00')
        self.assertEqual(self.format.format(
            datetime.datetime(2003, 1, 2, 12, 00), 'zzz'),
            'UTC')
        self.assertEqual(self.format.format(
            datetime.datetime(2003, 1, 2, 12, 00), 'zzzz'),
            'UTC')
        tz = pytz.timezone('US/Eastern')
        self.assertEqual(self.format.format(
            tz.localize(datetime.datetime(2003, 1, 2, 12)), 'z'),
            '-500')
        self.assertEqual(self.format.format(
            tz.localize(datetime.datetime(2003, 1, 2, 12)), 'zz'),
            '-05:00')
        self.assertEqual(self.format.format(
            tz.localize(datetime.datetime(2003, 1, 2, 12)), 'zzz'),
            'EST')
        self.assertEqual(self.format.format(
            tz.localize(datetime.datetime(2003, 1, 2, 12)), 'zzzz'),
            'US/Eastern')

    def testFormatWeekDay(self):
        date = datetime.date(2003, 1, 2)
        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. I am assigning a totally new dict here, since dicts are
        # mutable and the value would be changed for the class and all its
        # instances.
        calendar = LocaleCalendarStub()
        calendar.week = {'firstDay': 7, 'minDays': 1}
        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, 1, 2)
        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, 1, 2, 23, 00),
                               'dd.MM.yy h:mm:ss a'),
            '02.01.03 11:00:00 nachm.')
        self.assertEqual(
            self.format.format(datetime.datetime(2003, 1, 2, 2, 00),
                               'dd.MM.yy h:mm:ss a'),
            '02.01.03 2:00:00 vorm.')
        self.assertEqual(
            self.format.format(datetime.time(0, 15), 'h:mm a'),
            '12:15 vorm.')
        self.assertEqual(
            self.format.format(datetime.time(1, 15), 'h:mm a'),
            '1:15 vorm.')
        self.assertEqual(
            self.format.format(datetime.time(12, 15), 'h:mm a'),
            '12:15 nachm.')
        self.assertEqual(
            self.format.format(datetime.time(13, 15), 'h:mm a'),
            '1:15 nachm.')

    def testFormatDayInYear(self):
        self.assertEqual(
            self.format.format(datetime.date(2003, 1, 3), 'D'),
            _u("3"))
        self.assertEqual(
            self.format.format(datetime.date(2003, 1, 3), 'DD'),
            _u("03"))
        self.assertEqual(
            self.format.format(datetime.date(2003, 1, 3), 'DDD'),
            _u("003"))
        self.assertEqual(
            self.format.format(datetime.date(2003, 12, 31), 'D'),
            _u("365"))
        self.assertEqual(
            self.format.format(datetime.date(2003, 12, 31), 'DD'),
            _u("365"))
        self.assertEqual(
            self.format.format(datetime.date(2003, 12, 31), 'DDD'),
            _u("365"))
        self.assertEqual(
            self.format.format(datetime.date(2004, 12, 31), 'DDD'),
            _u("366"))

    def testFormatDayOfWeekInMOnth(self):
        self.assertEqual(
            self.format.format(datetime.date(2003, 1, 3), 'F'),
            _u("1"))
        self.assertEqual(
            self.format.format(datetime.date(2003, 1, 10), 'F'),
            _u("2"))
        self.assertEqual(
            self.format.format(datetime.date(2003, 1, 17), 'F'),
            _u("3"))
        self.assertEqual(
            self.format.format(datetime.date(2003, 1, 24), 'F'),
            _u("4"))
        self.assertEqual(
            self.format.format(datetime.date(2003, 1, 31), 'F'),
            _u("5"))
        self.assertEqual(
            self.format.format(datetime.date(2003, 1, 6), 'F'),
            _u("1"))

    def testFormatUnusualFormats(self):
        self.assertEqual(
            self.format.format(datetime.date(2003, 1, 3), 'DDD-yyyy'),
            _u("003-2003"))
        self.assertEqual(
            self.format.format(datetime.date(2003, 1, 10),
                               "F. EEEE 'im' MMMM, yyyy"),
            _u("2. Freitag im Januar, 2003"))