Skip to content

Commit

Permalink
Merge pull request #360 from iamshubh22/master
Browse files Browse the repository at this point in the history
format_datetime: using 'S...S' format is incorrect
  • Loading branch information
akx committed Feb 29, 2016
2 parents e1ce012 + 6adfcea commit ee0e0c1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
9 changes: 7 additions & 2 deletions babel/dates.py
Expand Up @@ -1287,8 +1287,13 @@ def format_period(self, char):
return get_period_names(locale=self.locale)[period]

def format_frac_seconds(self, num):
value = str(self.value.microsecond)
return self.format(round(float('.%s' % value), num) * 10**num, num)
""" Return fractional seconds.
Rounds the time's microseconds to the precision given by the number \
of digits passed in.
"""
value = self.value.microsecond / 1000000
return self.format(round(value, num) * 10**num, num)

def format_milliseconds_in_day(self, num):
msecs = self.value.microsecond // 1000 + self.value.second * 1000 + \
Expand Down
16 changes: 14 additions & 2 deletions tests/test_dates.py
Expand Up @@ -156,9 +156,21 @@ def test_local_day_of_week_standalone(self):
self.assertEqual('4', fmt['c']) # friday is first day of week

def test_fractional_seconds(self):
t = time(15, 30, 12, 34567)
t = time(8, 3, 9, 799)
fmt = dates.DateTimeFormat(t, locale='en_US')
self.assertEqual('3457', fmt['SSSS'])
self.assertEqual('0', fmt['S'])
t = time(8, 3, 1, 799)
fmt = dates.DateTimeFormat(t, locale='en_US')
self.assertEqual('0008', fmt['SSSS'])
t = time(8, 3, 1, 34567)
fmt = dates.DateTimeFormat(t, locale='en_US')
self.assertEqual('0346', fmt['SSSS'])
t = time(8, 3, 1, 345678)
fmt = dates.DateTimeFormat(t, locale='en_US')
self.assertEqual('345678', fmt['SSSSSS'])
t = time(8, 3, 1, 799)
fmt = dates.DateTimeFormat(t, locale='en_US')
self.assertEqual('00080', fmt['SSSSS'])

def test_fractional_seconds_zero(self):
t = time(15, 30, 0)
Expand Down

0 comments on commit ee0e0c1

Please sign in to comment.