Skip to content

Commit e208f18

Browse files
committed
Adds display of subseconds when no other units are available in in_words().
1 parent 5cc2ae9 commit e208f18

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Added a `in_timestamp` property to the `Pendulum` class to retrieve the behavior of the now deprecated `timestamp` property.
1111
- `start_of()`/`end_of()` now supports `hour`, `minute` and `second` units.
1212
- `astimezone()` now supports timezone strings.
13+
- `in_words()` now displays subseconds when no other units are available.
1314

1415
### Changed
1516

pendulum/mixins/interval.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,19 @@ def in_words(self, locale=None, separator=' ', _periods=None):
3636
unit, count = period
3737
if abs(count) > 0:
3838
parts.append(
39-
self.translator().transchoice(unit, abs(count), {'count': count}, locale=locale)
39+
self.translator().transchoice(
40+
unit, abs(count), {'count': count}, locale=locale
41+
)
4042
)
4143

44+
if not parts and abs(self.microseconds) > 0:
45+
translation = self.translator().transchoice(
46+
'second', 1,
47+
{'count': '{:.2f}'.format(abs(self.microseconds) / 1e6)},
48+
locale=locale
49+
)
50+
parts.append(translation)
51+
4252
return separator.join(parts)
4353

4454
def __str__(self):

tests/interval_tests/test_in_words.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,19 @@ def test_separator(self):
5757
'168 weeks, 1 day, 2 hours, 1 minute, 25 seconds',
5858
pi.in_words(separator=', ')
5959
)
60+
61+
def test_subseconds(self):
62+
pi = Interval(microseconds=123456)
63+
64+
self.assertEqual(
65+
'0.12 second',
66+
pi.in_words()
67+
)
68+
69+
def test_subseconds_with_seconds(self):
70+
pi = Interval(seconds=12, microseconds=123456)
71+
72+
self.assertEqual(
73+
'12 seconds',
74+
pi.in_words()
75+
)

0 commit comments

Comments
 (0)