Skip to content

Commit aba5225

Browse files
committed
Makes __format__() use the configured formatter.
Fixes #90
1 parent 79d7c16 commit aba5225

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Changed
66

77
- Improved `diff_for_humans()` method to display more intuitive strings on edge cases.
8+
- Formatting (with f-strings or `format()`) will now use the configured formatter.
89

910

1011
## [1.0.1]

pendulum/mixins/default.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def for_json(self):
158158

159159
def __format__(self, format_spec):
160160
if len(format_spec) > 0:
161-
return self.strftime(format_spec)
161+
return self.format(format_spec)
162162

163163
return str(self)
164164

tests/date_tests/test_strings.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3+
import pendulum
34
from pendulum import Date
45
from .. import AbstractTestCase
56

@@ -78,3 +79,11 @@ def test_format(self):
7879
d = Date(1975, 12, 25)
7980
self.assertEqual('1975-12-25', '{}'.format(d))
8081
self.assertEqual('1975', '{:%Y}'.format(d))
82+
83+
def test_format_alternative_formatter(self):
84+
pendulum.set_formatter('alternative')
85+
86+
d = Date(1975, 12, 25)
87+
self.assertEqual('1975-12-25', '{}'.format(d))
88+
self.assertEqual('1975', '{:YYYY}'.format(d))
89+
self.assertEqual('%1975', '{:%Y}'.format(d))

tests/pendulum_tests/test_strings.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,11 @@ def test_format(self):
148148
d = Pendulum(1975, 12, 25, 14, 15, 16, tzinfo='Europe/Paris')
149149
self.assertEqual('1975-12-25T14:15:16+01:00', '{}'.format(d))
150150
self.assertEqual('1975', '{:%Y}'.format(d))
151+
152+
def test_format_alternative_formatter(self):
153+
pendulum.set_formatter('alternative')
154+
d = Pendulum(1975, 12, 25, 14, 15, 16, tzinfo='Europe/Paris')
155+
156+
self.assertEqual('1975-12-25T14:15:16+01:00', '{}'.format(d))
157+
self.assertEqual('1975', '{:YYYY}'.format(d))
158+
self.assertEqual('%1975', '{:%Y}'.format(d))

0 commit comments

Comments
 (0)