Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format_datetime: using 'S...S' format is incorrect #360

Merged
merged 1 commit into from
Feb 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions babel/dates.py
Original file line number Diff line number Diff line change
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 \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to use the backslash for line continuations in docstrings :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akx Ok. Got it 😄

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
Original file line number Diff line number Diff line change
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