Skip to content

Commit 2885d56

Browse files
committed
Adds date formats support for the alternative formatter.
1 parent bd12790 commit 2885d56

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+604
-67
lines changed

pendulum/formatting/alternative_formatter.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class AlternativeFormatter(Formatter):
2020
'|mm?|ss?|S{1,9}' \
2121
'|x|X' \
2222
'|zz?|ZZ?' \
23+
'|LTS|LT|LL?L?L?' \
2324
')'
2425

2526
_FORMAT_RE = re.compile(_TOKENS)
@@ -89,6 +90,15 @@ class AlternativeFormatter(Formatter):
8990
'zz': lambda dt: '{}'.format(dt.timezone_name),
9091
}
9192

93+
_DEFAULT_DATE_FORMATS = {
94+
'LTS': 'h:mm:ss A',
95+
'LT': 'h:mm A',
96+
'L': 'MM/DD/YYYY',
97+
'LL': 'MMMM D, YYYY',
98+
'LLL': 'MMMM D, YYYY h:mm A',
99+
'LLLL': 'dddd, MMMM D, YYYY h:mm A',
100+
}
101+
92102
def format(self, dt, fmt, locale=None):
93103
"""
94104
Formats a Pendulum instance with a given format and locale.
@@ -131,6 +141,12 @@ def _format_token(self, dt, token, locale):
131141
132142
:rtype: str
133143
"""
144+
if token in self._DEFAULT_DATE_FORMATS:
145+
fmt = dt.translator().transchoice('date_formats', token, locale=locale)
146+
if fmt == 'date_formats':
147+
fmt = self._DEFAULT_DATE_FORMATS[token]
148+
149+
return self.format(dt, fmt, locale)
134150
if token in self._LOCALIZABLE_TOKENS:
135151
return self._format_localizable_token(dt, token, locale)
136152

@@ -209,6 +225,11 @@ def _format_localizable_token(self, dt, token, locale):
209225
trans = dt.translator().transchoice(trans_id, count, locale=locale)
210226

211227
if trans_id == 'ordinal':
212-
return '{:d}{}'.format(count, trans)
228+
trans = '{:d}{}'.format(count, trans)
229+
230+
if trans_id == trans:
231+
# Unable to find the corresponding translation
232+
# Defaulting to english
233+
return self._format_localizable_token(dt, token, 'en')
213234

214235
return trans

pendulum/lang/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from .mk import translations as mk_translations
3535
from .ms import translations as ms_translations
3636
from .nl import translations as nl_translations
37-
from .no import translations as no_translations
37+
from .nn import translations as nn_translations
3838
from .pl import translations as pl_translations
3939
from .pt_br import translations as pt_br_translations
4040
from .ro import translations as ro_translations
@@ -88,7 +88,7 @@
8888
"mk": mk_translations,
8989
"ms": ms_translations,
9090
"nl": nl_translations,
91-
"no": no_translations,
91+
"nn": nn_translations,
9292
"pl": pl_translations,
9393
"pt_br": pt_br_translations,
9494
"ro": ro_translations,

pendulum/lang/af.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,15 @@
6868

6969
# Meridians
7070
'meridian': lambda hour: 'VM' if 0 <= hour < 12 else 'NM',
71+
72+
# Date formats
73+
'date_formats': {
74+
'LTS': 'HH:mm:ss',
75+
'LT': 'HH:mm',
76+
'LLLL': 'dddd, D MMMM YYYY HH:mm',
77+
'LLL': 'D MMMM YYYY HH:mm',
78+
'LL': 'D MMMM YYYY',
79+
'L': 'DD/MM/YYYY',
80+
},
7181
}
7282

pendulum/lang/ar.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,14 @@
107107
'from_now': 'من الآن {time}',
108108
'after': 'بعد {time}',
109109
'before': 'قبل {time}',
110+
111+
# Date formats
112+
'date_formats': {
113+
'LTS': 'HH:mm:ss',
114+
'LT': 'HH:mm',
115+
'LLLL': 'dddd D MMMM YYYY HH:mm',
116+
'LLL': 'D MMMM YYYY HH:mm',
117+
'LL': 'D MMMM YYYY',
118+
'L': 'D/\u200FM/\u200FYYYY',
119+
},
110120
}

pendulum/lang/az.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@
6464
'ago': '{time} öncə',
6565
'from_now': '{time} sonra',
6666
'after': '{time} sonra',
67-
'before': '{time} öncə'
68-
}
67+
'before': '{time} öncə',
6968

69+
# Date formats
70+
'date_formats': {
71+
'LTS': 'HH:mm:ss',
72+
'LT': 'HH:mm',
73+
'LLLL': 'dddd, D MMMM YYYY HH:mm',
74+
'LLL': 'D MMMM YYYY HH:mm',
75+
'LL': 'D MMMM YYYY',
76+
'L': 'DD.MM.YYYY',
77+
},
78+
}

pendulum/lang/bg.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,14 @@
6565
'from_now': '{time} от сега',
6666
'after': 'след {time}',
6767
'before': 'преди {time}',
68-
}
6968

69+
# Date formats
70+
'date_formats': {
71+
'LTS': 'H:mm:ss',
72+
'LT': 'H:mm',
73+
'LLLL': 'dddd, D MMMM YYYY H:mm',
74+
'LLL': 'D MMMM YYYY H:mm',
75+
'LL': 'D MMMM YYYY',
76+
'L': 'D.MM.YYYY',
77+
},
78+
}

pendulum/lang/bn.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,14 @@
6666

6767
# Meridians
6868
'meridian': lambda hour: 'সকাল' if 0 <= hour < 12 else 'বিকাল',
69-
}
7069

70+
# Date formats
71+
'date_formats': {
72+
'LTS': 'A h:mm:ss সময়',
73+
'LT': 'A h:mm সময়',
74+
'LLLL': 'dddd, D MMMM YYYY, A h:mm সময়',
75+
'LLL': 'D MMMM YYYY, A h:mm সময়',
76+
'LL': 'D MMMM YYYY',
77+
'L': 'DD/MM/YYYY',
78+
},
79+
}

pendulum/lang/ca.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,14 @@
6565
'from_now': 'Dins de {time}',
6666
'after': '{time} després',
6767
'before': '{time} abans',
68-
}
6968

69+
# Date formats
70+
'date_formats': {
71+
'LTS': 'H:mm:ss',
72+
'LT': 'H:mm',
73+
'LLLL': 'dddd D MMMM YYYY H:mm',
74+
'LLL': 'D MMMM YYYY H:mm',
75+
'LL': 'D MMMM YYYY',
76+
'L': 'DD/MM/YYYY',
77+
},
78+
}

pendulum/lang/cs.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,14 @@
6565
'from_now': 'za {time}',
6666
'after': '{time} později',
6767
'before': '{time} předtím',
68-
}
6968

69+
# Date formats
70+
'date_formats': {
71+
'LTS': 'H:mm:ss',
72+
'LT': 'H:mm',
73+
'LLLL': 'dddd D. MMMM YYYY H:mm',
74+
'LLL': 'D. MMMM YYYY H:mm',
75+
'LL': 'D. MMMM YYYY',
76+
'L': 'DD.MM.YYYY',
77+
},
78+
}

pendulum/lang/da.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,14 @@
6565
'from_now': 'om {time}',
6666
'after': '{time} efter',
6767
'before': '{time} før',
68-
}
6968

69+
# Date formats
70+
'date_formats': {
71+
'LTS': 'HH:mm:ss',
72+
'LT': 'HH:mm',
73+
'LLLL': 'dddd [d.] D. MMMM YYYY HH:mm',
74+
'LLL': 'D. MMMM YYYY HH:mm',
75+
'LL': 'D. MMMM YYYY',
76+
'L': 'DD/MM/YYYY',
77+
},
78+
}

0 commit comments

Comments
 (0)