Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 482034 | Differences between
and this patch

Collapse All | Expand All

(-)trac/util/datefmt.py (-34 / +28 lines)
Lines 158-208 Link Here
158
    'date': {'short': '%x', 'medium': '%x', 'long': '%x', 'full': '%x'},
158
    'date': {'short': '%x', 'medium': '%x', 'long': '%x', 'full': '%x'},
159
    'time': {'short': '%H:%M', 'medium': '%X', 'long': '%X', 'full': '%X'},
159
    'time': {'short': '%H:%M', 'medium': '%X', 'long': '%X', 'full': '%X'},
160
}
160
}
161
_ISO8601_FORMATS = {
162
    'datetime': {
163
        '%x %X': 'iso8601', '%x': 'iso8601date', '%X': 'iso8601time',
164
        'short': '%Y-%m-%dT%H:%M', 'medium': '%Y-%m-%dT%H:%M:%S',
165
        'long': 'iso8601', 'full': 'iso8601',
166
        'iso8601': 'iso8601', None: 'iso8601'},
167
    'date': {
168
        '%x %X': 'iso8601', '%x': 'iso8601date', '%X': 'iso8601time',
169
        'short': 'iso8601date', 'medium': 'iso8601date',
170
        'long': 'iso8601date', 'full': 'iso8601date',
171
        'iso8601': 'iso8601date', None: 'iso8601date'},
172
    'time': {
173
        '%x %X': 'iso8601', '%x': 'iso8601date', '%X': 'iso8601time',
174
        'short': '%H:%M', 'medium': '%H:%M:%S',
175
        'long': 'iso8601time', 'full': 'iso8601time',
176
        'iso8601': 'iso8601time', None: 'iso8601time'},
177
}
178
_STRFTIME_HINTS = {'%x %X': 'datetime', '%x': 'date', '%X': 'time'}
161
_STRFTIME_HINTS = {'%x %X': 'datetime', '%x': 'date', '%X': 'time'}
179
162
180
def _format_datetime_without_babel(t, format):
163
def _format_datetime_without_babel(t, format):
181
    normalize_Z = False
182
    if format.lower().startswith('iso8601'):
183
        if 'date' in format:
184
            format = '%Y-%m-%d'
185
        elif 'time' in format:
186
            format = '%H:%M:%S%z'
187
            normalize_Z = True
188
        else:
189
            format = '%Y-%m-%dT%H:%M:%S%z'
190
            normalize_Z = True
191
    text = t.strftime(str(format))
164
    text = t.strftime(str(format))
192
    if normalize_Z:
193
        text = text.replace('+0000', 'Z')
194
        if not text.endswith('Z'):
195
            text = text[:-2] + ":" + text[-2:]
196
    encoding = getlocale(LC_TIME)[1] or getpreferredencoding() \
165
    encoding = getlocale(LC_TIME)[1] or getpreferredencoding() \
197
               or sys.getdefaultencoding()
166
               or sys.getdefaultencoding()
198
    return unicode(text, encoding, 'replace')
167
    return unicode(text, encoding, 'replace')
199
168
169
def _format_datetime_iso8601(t, format, hint):
170
    if format != 'full':
171
        t = t.replace(microsecond=0)
172
    text = t.isoformat()  # YYYY-MM-DDThh:mm:ss.SSSSSSĀ±hh:mm
173
    if format == 'short':
174
        text = text[:16]  # YYYY-MM-DDThh:mm
175
    elif format == 'medium':
176
        text = text[:19]  # YYYY-MM-DDThh:mm:ss
177
    elif text.endswith('+00:00'):
178
        text = text[:-6] + 'Z'
179
    if hint == 'date':
180
        text = text.split('T', 1)[0]
181
    elif hint == 'time':
182
        text = text.split('T', 1)[1]
183
    return unicode(text, 'ascii')
184
200
def _format_datetime(t, format, tzinfo, locale, hint):
185
def _format_datetime(t, format, tzinfo, locale, hint):
201
    t = to_datetime(t, tzinfo or localtz)
186
    t = to_datetime(t, tzinfo or localtz)
202
187
203
    if (format in ('iso8601', 'iso8601date', 'iso8601time') or
188
    if format == 'iso8601':
204
        locale == 'iso8601'):
189
        return _format_datetime_iso8601(t, 'long', hint)
205
        format = _ISO8601_FORMATS[hint].get(format, format)
190
    if format in ('iso8601date', 'iso8601time'):
191
        return _format_datetime_iso8601(t, 'long', format[7:])
192
    if locale == 'iso8601':
193
        if format is None:
194
            format = 'long'
195
        elif format in _STRFTIME_HINTS:
196
            hint = _STRFTIME_HINTS[format]
197
            format = 'long'
198
        if format in ('short', 'medium', 'long', 'full'):
199
            return _format_datetime_iso8601(t, format, hint)
206
        return _format_datetime_without_babel(t, format)
200
        return _format_datetime_without_babel(t, format)
207
201
208
    if babel and locale:
202
    if babel and locale:
(-)trac/util/tests/datefmt.py (-6 / +46 lines)
Lines 559-564 Link Here
559
        self.assertEqual(datefmt.format_time(t, 'iso8601', gmt01),
559
        self.assertEqual(datefmt.format_time(t, 'iso8601', gmt01),
560
                         expected.split('T')[1])
560
                         expected.split('T')[1])
561
561
562
    def test_format_iso8601_before_1900(self):
563
        t = datetime.datetime(1899, 12, 30, 23, 58, 59, 123456, datefmt.utc)
564
        self.assertEqual('1899-12-30T23:58:59Z',
565
                         datefmt.format_datetime(t, 'iso8601', datefmt.utc))
566
        self.assertEqual('1899-12-30',
567
                         datefmt.format_datetime(t, 'iso8601date',
568
                                                 datefmt.utc))
569
        self.assertEqual('1899-12-30',
570
                         datefmt.format_date(t, 'iso8601', datefmt.utc))
571
        self.assertEqual('23:58:59Z',
572
                         datefmt.format_datetime(t, 'iso8601time',
573
                                                 datefmt.utc))
574
        self.assertEqual('23:58:59Z',
575
                         datefmt.format_time(t, 'iso8601', datefmt.utc))
576
562
    def test_format_date_accepts_date_instances(self):
577
    def test_format_date_accepts_date_instances(self):
563
        a_date = datetime.date(2009, 8, 20)
578
        a_date = datetime.date(2009, 8, 20)
564
        self.assertEqual('2009-08-20',
579
        self.assertEqual('2009-08-20',
Lines 660-671 Link Here
660
                         datefmt.format_time(t, 'medium', tz, 'iso8601'))
675
                         datefmt.format_time(t, 'medium', tz, 'iso8601'))
661
        self.assertEqual('2010-08-28T11:45:56',
676
        self.assertEqual('2010-08-28T11:45:56',
662
                         datefmt.format_datetime(t, 'medium', tz, 'iso8601'))
677
                         datefmt.format_datetime(t, 'medium', tz, 'iso8601'))
663
        for f in ('long', 'full'):
678
        self.assertEqual('11:45:56+02:00',
664
            self.assertEqual('11:45:56+02:00',
679
                         datefmt.format_time(t, 'long', tz, 'iso8601'))
665
                             datefmt.format_time(t, f, tz, 'iso8601'))
680
        self.assertEqual('2010-08-28T11:45:56+02:00',
666
            self.assertEqual('2010-08-28T11:45:56+02:00',
681
                         datefmt.format_datetime(t, 'long', tz, 'iso8601'))
667
                             datefmt.format_datetime(t, f, tz, 'iso8601'))
682
        self.assertEqual('11:45:56.123456+02:00',
683
                         datefmt.format_time(t, 'full', tz, 'iso8601'))
684
        self.assertEqual('2010-08-28T11:45:56.123456+02:00',
685
                         datefmt.format_datetime(t, 'full', tz, 'iso8601'))
668
686
687
    def test_with_babel_format_before_1900(self):
688
        tz = datefmt.timezone('GMT +2:00')
689
        t = datetime.datetime(1899, 8, 28, 11, 45, 56, 123456, tz)
690
        for f in ('short', 'medium', 'long', 'full'):
691
            self.assertEqual('1899-08-28',
692
                             datefmt.format_date(t, f, tz, 'iso8601'))
693
        self.assertEqual('11:45',
694
                         datefmt.format_time(t, 'short', tz, 'iso8601'))
695
        self.assertEqual('1899-08-28T11:45',
696
                         datefmt.format_datetime(t, 'short', tz, 'iso8601'))
697
        self.assertEqual('11:45:56',
698
                         datefmt.format_time(t, 'medium', tz, 'iso8601'))
699
        self.assertEqual('1899-08-28T11:45:56',
700
                         datefmt.format_datetime(t, 'medium', tz, 'iso8601'))
701
        self.assertEqual('11:45:56+02:00',
702
                         datefmt.format_time(t, 'long', tz, 'iso8601'))
703
        self.assertEqual('1899-08-28T11:45:56+02:00',
704
                         datefmt.format_datetime(t, 'long', tz, 'iso8601'))
705
        self.assertEqual('11:45:56.123456+02:00',
706
                         datefmt.format_time(t, 'full', tz, 'iso8601'))
707
        self.assertEqual('1899-08-28T11:45:56.123456+02:00',
708
                         datefmt.format_datetime(t, 'full', tz, 'iso8601'))
709
669
    def test_hint(self):
710
    def test_hint(self):
670
        try:
711
        try:
671
            datefmt.parse_date('***', locale='iso8601', hint='date')
712
            datefmt.parse_date('***', locale='iso8601', hint='date')
672
-----------------------------------------------------------------------

Return to bug 482034