Skip to content
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: 5 additions & 4 deletions babel/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1636,11 +1636,12 @@ def format_period(self, char: str, num: int) -> str:
def format_frac_seconds(self, num: int) -> str:
""" Return fractional seconds.

Rounds the time's microseconds to the precision given by the number \
of digits passed in.
Truncates the time's microseconds to the number of digits passed in,
as specified by UTS #35.
"""
value = self.value.microsecond / 1000000
return self.format(round(value, num) * 10**num, num)
micro = self.value.microsecond
frac = micro // 10**(6 - num) if num <= 6 else micro * 10**(num - 6)
return self.format(frac, num)

def format_milliseconds_in_day(self, num):
msecs = (
Expand Down
2 changes: 1 addition & 1 deletion docs/dates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ Time Fields
+----------+--------+--------------------------------------------------------+
| Second | ``s`` | Use one or two for zero places padding. |
| +--------+--------------------------------------------------------+
| | ``S`` | Fractional second, rounds to the count of letters. |
| | ``S`` | Fractional second, truncates to the count of letters. |
| +--------+--------------------------------------------------------+
| | ``A`` | Milliseconds in day. |
+----------+--------+--------------------------------------------------------+
Expand Down
18 changes: 15 additions & 3 deletions tests/test_date_time_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,32 @@ def test_fractional_seconds():
t = time(8, 3, 9, 799)
assert DateTimeFormat(t, locale='en_US')['S'] == '0'
t = time(8, 3, 1, 799)
assert DateTimeFormat(t, locale='en_US')['SSSS'] == '0008'
assert DateTimeFormat(t, locale='en_US')['SSSS'] == '0007'
t = time(8, 3, 1, 34567)
assert DateTimeFormat(t, locale='en_US')['SSSS'] == '0346'
assert DateTimeFormat(t, locale='en_US')['SSSS'] == '0345'
t = time(8, 3, 1, 345678)
assert DateTimeFormat(t, locale='en_US')['SSSSSS'] == '345678'
t = time(8, 3, 1, 799)
assert DateTimeFormat(t, locale='en_US')['SSSSS'] == '00080'
assert DateTimeFormat(t, locale='en_US')['SSSSS'] == '00079'


def test_fractional_seconds_zero():
t = time(15, 30, 0)
assert DateTimeFormat(t, locale='en_US')['SSSS'] == '0000'


def test_fractional_seconds_truncation_does_not_carry():
t = time(1, 2, 3, 990000)
assert DateTimeFormat(t, locale='en_US')['S'] == '9'
t = time(1, 2, 3, 999500)
assert DateTimeFormat(t, locale='en_US')['SS'] == '99'
t = time(1, 2, 3, 999999)
assert DateTimeFormat(t, locale='en_US')['S'] == '9'
assert DateTimeFormat(t, locale='en_US')['SS'] == '99'
assert DateTimeFormat(t, locale='en_US')['SSSS'] == '9999'
assert DateTimeFormat(t, locale='en_US')['SSSSSSSS'] == '99999900'


def test_milliseconds_in_day():
t = time(15, 30, 12, 345000)
assert DateTimeFormat(t, locale='en_US')['AAAA'] == '55812345'
Expand Down
Loading