Skip to content

Commit 230e1f7

Browse files
authored
Date format: Use * parametrized format patterns instead of %%0%... (#517)
This is about 60% faster than the old version in a micro-benchmark.
1 parent 653e6d4 commit 230e1f7

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

babel/dates.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,14 +1330,14 @@ def format_year(self, char, num):
13301330
def format_quarter(self, char, num):
13311331
quarter = (self.value.month - 1) // 3 + 1
13321332
if num <= 2:
1333-
return ('%%0%dd' % num) % quarter
1333+
return '%0*d' % (num, quarter)
13341334
width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[num]
13351335
context = {'Q': 'format', 'q': 'stand-alone'}[char]
13361336
return get_quarter_names(width, context, self.locale)[quarter]
13371337

13381338
def format_month(self, char, num):
13391339
if num <= 2:
1340-
return ('%%0%dd' % num) % self.value.month
1340+
return '%0*d' % (num, self.value.month)
13411341
width = {3: 'abbreviated', 4: 'wide', 5: 'narrow'}[num]
13421342
context = {'M': 'format', 'L': 'stand-alone'}[char]
13431343
return get_month_names(width, context, self.locale)[self.value.month]
@@ -1470,7 +1470,7 @@ def format_timezone(self, char, num):
14701470
return get_timezone_gmt(self.value, width='iso8601', locale=self.locale)
14711471

14721472
def format(self, value, length):
1473-
return ('%%0%dd' % length) % value
1473+
return '%0*d' % (length, value)
14741474

14751475
def get_day_of_year(self, date=None):
14761476
if date is None:

0 commit comments

Comments
 (0)