Skip to content

Commit e184c60

Browse files
committed
Changes custom directives.
1 parent 3598b21 commit e184c60

File tree

4 files changed

+37
-22
lines changed

4 files changed

+37
-22
lines changed

docs/index.rst

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,24 +227,39 @@ you can create a ``Pendulum`` instance via the ``instance()`` function.
227227
Localization
228228
============
229229

230-
Localization occurs naturally when using the ``format()`` method since it relies on the
231-
native ``strftime`` datetime function.
230+
Localization occurs when using the ``format()`` method which accepts a ``locale`` keyword.
232231

233232
.. code-block:: python
234233
235-
import locale
236234
from pendulum import Pendulum
237235
238236
dt = Pendulum(1975, 5, 21)
239237
240-
locale.setlocale(locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8'))
241-
dt.format('%A %d %B %Y')
238+
dt.format('%A %d %B %Y', locale='de')
242239
'Mittwoch 21 Mai 1975'
243240
244-
locale.setlocale(locale.LC_ALL, locale.getdefaultlocale())
245241
dt.format('%A %d %B %Y')
246242
'Wednesday 21 May 1975'
247243
244+
.. note::
245+
246+
You can also use the ``strftime()`` method, which behaves exactly like the native one.
247+
248+
.. code-block:: python
249+
250+
import locale
251+
from pendulum import Pendulum
252+
253+
dt = Pendulum(1975, 5, 21)
254+
255+
locale.setlocale(locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8'))
256+
dt.format('%A %d %B %Y')
257+
'Mittwoch 21 Mai 1975'
258+
259+
locale.setlocale(locale.LC_ALL, locale.getdefaultlocale())
260+
dt.format('%A %d %B %Y')
261+
'Wednesday 21 May 1975'
262+
248263
``diff_for_humans()`` is also localized, you can set the Pendulum locale
249264
by using the class method ``pendulum.set_locale()``.
250265

@@ -448,13 +463,13 @@ Custom Directives
448463
-----------------
449464

450465
Apart from the `default directives <For localization support see the Localization section.>`_,
451-
Pendulum comes with its own:
466+
Pendulum comes with its own (each custom directive is in the form ``%_{directive}``):
452467

453468
=========== ======================================================================== =================================
454469
Directive Meaning Example
455470
=========== ======================================================================== =================================
456-
``%P`` Difference to Greenwich time (GMT) with colon between hours and minutes ``+02:00``
457-
``%t`` English ordinal suffix for the day of the month, 2 characters ``st``, ``nd``, ``rd`` or ``th``
471+
``%_z`` Difference to Greenwich time (GMT) with colon between hours and minutes ``+02:00``
472+
``%_t`` Ordinal suffix for the day of the month, 2 characters ``st``, ``nd``, ``rd`` or ``th``
458473
=========== ======================================================================== =================================
459474

460475
Common Formats

pendulum/pendulum.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,19 @@ class Pendulum(datetime.datetime, TranslatableMixin):
5656
SECONDS_PER_MINUTE = 60
5757

5858
# Formats
59-
ATOM = '%Y-%m-%dT%H:%M:%S%P'
59+
ATOM = '%Y-%m-%dT%H:%M:%S%_z'
6060
COOKIE = '%A, %d-%b-%Y %H:%M:%S %Z'
61-
ISO8601 = '%Y-%m-%dT%H:%M:%S%P'
62-
ISO8601_EXTENDED = '%Y-%m-%dT%H:%M:%S.%f%P'
61+
ISO8601 = '%Y-%m-%dT%H:%M:%S%_z'
62+
ISO8601_EXTENDED = '%Y-%m-%dT%H:%M:%S.%f%_z'
6363
RFC822 = '%a, %d %b %y %H:%M:%S %z'
6464
RFC850 = '%A, %d-%b-%y %H:%M:%S %Z'
6565
RFC1036 = '%a, %d %b %y %H:%M:%S %z'
6666
RFC1123 = '%a, %d %b %Y %H:%M:%S %z'
6767
RFC2822 = '%a, %d %b %Y %H:%M:%S %z'
68-
RFC3339 = '%Y-%m-%dT%H:%M:%S%P'
69-
RFC3339_EXTENDED = '%Y-%m-%dT%H:%M:%S.%f%P'
68+
RFC3339 = '%Y-%m-%dT%H:%M:%S%_z'
69+
RFC3339_EXTENDED = '%Y-%m-%dT%H:%M:%S.%f%_z'
7070
RSS = '%a, %d %b %Y %H:%M:%S %z'
71-
W3C = '%Y-%m-%dT%H:%M:%S%P'
71+
W3C = '%Y-%m-%dT%H:%M:%S%_z'
7272

7373
# Default format to use for __str__ method when type juggling occurs.
7474
DEFAULT_TO_STRING_FORMAT = None
@@ -92,7 +92,7 @@ class Pendulum(datetime.datetime, TranslatableMixin):
9292

9393
_EPOCH = datetime.datetime(1970, 1, 1, tzinfo=pytz.UTC)
9494

95-
_CUSTOM_FORMATTERS = ['P', 't']
95+
_CUSTOM_FORMATTERS = ['_z', '_t']
9696
_FORMATTERS_REGEX = re.compile('%%(%s)' % '|'.join(_CUSTOM_FORMATTERS))
9797

9898
_MODIFIERS_VALID_UNITS = ['day', 'week', 'month', 'year', 'decade', 'century']
@@ -924,7 +924,7 @@ def _strftime(self, m, locale=None):
924924

925925
fmt = m.group(1)
926926

927-
if fmt == 'P':
927+
if fmt == '_z':
928928
offset = self._datetime.utcoffset() or datetime.timedelta()
929929
minutes = offset.total_seconds() / 60
930930

@@ -936,7 +936,7 @@ def _strftime(self, m, locale=None):
936936
hour, minute = divmod(abs(int(minutes)), 60)
937937

938938
return '{0}{1:02d}:{2:02d}'.format(sign, hour, minute)
939-
elif fmt == 't':
939+
elif fmt == '_t':
940940
translation = self.translator().transchoice('ordinal', self.day, locale=locale)
941941
if translation == 'ordinal':
942942
translation = ''

tests/localization_tests/test_fr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@ def format(self):
7373
self.assertEqual('janvier', d.format('%B'))
7474
self.assertEqual('janv', d.format('%b'))
7575
self.assertEqual('', d.format('%p'))
76-
self.assertEqual('er', d.format('%t'))
77-
self.assertEqual('e', d.add(days=1).format('%t'))
76+
self.assertEqual('er', d.format('%_t'))
77+
self.assertEqual('e', d.add(days=1).format('%_t'))

tests/pendulum_tests/test_strings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def test_to_w3c_string(self):
101101

102102
def test_custom_formatters(self):
103103
d = Pendulum(1975, 12, 25, 14, 15, 16, tzinfo='local')
104-
self.assertEqual('Thursday 25th of December 1975 02:15:16 PM -05:00', d.format('%A %-d%t of %B %Y %I:%M:%S %p %P'))
104+
self.assertEqual('Thursday 25th of December 1975 02:15:16 PM -05:00', d.format('%A %-d%_t of %B %Y %I:%M:%S %p %_z'))
105105

106106
def test_repr(self):
107107
d = Pendulum(1975, 12, 25, 14, 15, 16, tzinfo='local')
@@ -110,4 +110,4 @@ def test_repr(self):
110110
def test_format_with_locale(self):
111111
d = Pendulum(1975, 12, 25, 14, 15, 16, tzinfo='local')
112112
self.assertEqual('jeudi 25e jour de décembre 1975 02:15:16 -05:00',
113-
d.format('%A %-d%t jour de %B %Y %I:%M:%S %p %P', locale='fr'))
113+
d.format('%A %-d%_t jour de %B %Y %I:%M:%S %p %_z', locale='fr'))

0 commit comments

Comments
 (0)