Skip to content

Commit

Permalink
0.13dev: Follow-up to [10571], fixed parsing time string with AM/PM m…
Browse files Browse the repository at this point in the history
…arker and failure tests with non C locale.

git-svn-id: http://trac.edgewall.org/intertrac/log:/trunk@10583 af82e41b-90c4-0310-8c96-b1721e28e2e2
  • Loading branch information
jomae committed Feb 22, 2011
1 parent 3577874 commit a2f4830
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion trac/ticket/tests/functional.py
Expand Up @@ -454,7 +454,7 @@ def runTest(self):
tc.url(milestone_url + '/' + name)
tc.formvalue('modifymilestone', 'completed', True)
cdate = datetime.now(tz=utc) + timedelta(days=1)
cdate_string = format_date(cdate, tzinfo=localtz)
cdate_string = format_date(cdate, tzinfo=localtz, locale=locale_en)
tc.formvalue('modifymilestone', 'completeddate', cdate_string)
tc.submit('save')
tc.find('Completion date may not be in the future')
Expand Down
7 changes: 5 additions & 2 deletions trac/util/datefmt.py
Expand Up @@ -487,8 +487,11 @@ def _i18n_parse_date_0(text, order, regexp, period_names, month_names, tzinfo):
values.setdefault('m', 0)
values.setdefault('s', 0)

if values['h'] < 12 and period == 'pm':
values['h'] += 12
if period and values['h'] <= 12:
if period == 'am':
values['h'] %= 12
elif period == 'pm':
values['h'] = values['h'] % 12 + 12

return tzinfo.localize(datetime(values['y'], values['M'], values['d'],
values['h'], values['m'], values['s']))
Expand Down
42 changes: 39 additions & 3 deletions trac/util/tests/datefmt.py
Expand Up @@ -382,6 +382,39 @@ def test_i18n_parse_date_datetime(self):
datefmt.parse_date(u'2010-8-28 01:45下午', tz,
zh_CN))

def test_i18n_parse_date_datetime_meridiem(self):
tz = datefmt.timezone('GMT +2:00')
expected_am = datetime.datetime(2011, 2, 22, 0, 45, 56, 0, tz)
expected_pm = datetime.datetime(2011, 2, 22, 12, 45, 56, 0, tz)
en_US = Locale.parse('en_US')
zh_CN = Locale.parse('zh_CN')

self.assertEqual(expected_am,
datefmt.parse_date('Feb 22, 2011 0:45:56 AM', tz,
en_US))
self.assertEqual(expected_am,
datefmt.parse_date('Feb 22, 2011 12:45:56 AM', tz,
en_US))
self.assertEqual(expected_am,
datefmt.parse_date(u'2011-2-22 上午0:45:56', tz,
zh_CN))
self.assertEqual(expected_am,
datefmt.parse_date(u'2011-2-22 上午12:45:56', tz,
zh_CN))

self.assertEqual(expected_pm,
datefmt.parse_date('Feb 22, 2011 0:45:56 PM', tz,
en_US))
self.assertEqual(expected_pm,
datefmt.parse_date('Feb 22, 2011 12:45:56 PM', tz,
en_US))
self.assertEqual(expected_pm,
datefmt.parse_date(u'2011-2-22 下午0:45:56', tz,
zh_CN))
self.assertEqual(expected_pm,
datefmt.parse_date(u'2011-2-22 下午12:45:56', tz,
zh_CN))

def test_i18n_parse_date_date(self):
tz = datefmt.timezone('GMT +2:00')
expected = datetime.datetime(2010, 8, 28, 0, 0, 0, 0, tz)
Expand Down Expand Up @@ -456,6 +489,7 @@ def roundtrip(locale):
def test_format_compatibility(self):
tz = datefmt.timezone('GMT +2:00')
t = datetime.datetime(2010, 8, 28, 11, 45, 56, 123456, datefmt.utc)
tz_t = datetime.datetime(2010, 8, 28, 13, 45, 56, 123456, tz)
en_US = Locale.parse('en_US')

# Converting default format to babel's format
Expand All @@ -471,10 +505,12 @@ def test_format_compatibility(self):
datefmt.format_time(t, '%X', tz, en_US))

# Converting babel's format to strftime format
self.assertEqual('08/28/10 13:45:56',
self.assertEqual(tz_t.strftime('%x %X').decode('utf-8'),
datefmt.format_datetime(t, 'medium', tz))
self.assertEqual('08/28/10', datefmt.format_date(t, 'medium', tz))
self.assertEqual('13:45:56', datefmt.format_time(t, 'medium', tz))
self.assertEqual(tz_t.strftime('%x').decode('utf-8'),
datefmt.format_date(t, 'medium', tz))
self.assertEqual(tz_t.strftime('%X').decode('utf-8'),
datefmt.format_time(t, 'medium', tz))


def suite():
Expand Down

0 comments on commit a2f4830

Please sign in to comment.