Skip to content

Commit 6495753

Browse files
committed
Replace hardcoded time units with named constants
- Replaced hardcoded time-related values with named constants. - Facilitates easier adjustments of these values and reduces potential for errors.
1 parent a69761e commit 6495753

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

src/pendulum/formatting/difference_formatter.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,63 @@ def format(
3232
:param absolute: Whether it's an absolute difference or not
3333
:param locale: The locale to use
3434
"""
35+
DAYS_THRESHOLD_FOR_HALF_WEEK = 3
36+
DAYS_THRESHOLD_FOR_HALF_MONTH = 15
37+
MONTHS_THRESHOLD_FOR_HALF_YEAR = 6
38+
39+
HOURS_IN_NEARLY_A_DAY = 22
40+
DAYS_IN_NEARLY_A_MONTH = 27
41+
MONTHS_IN_NEARLY_A_YEAR = 11
42+
43+
DAYS_OF_WEEK = 7
44+
SECONDS_OF_MINUTE = 60
45+
FEW_SECONDS_MAX = 10
46+
47+
KEY_FUTURE = ".future"
48+
KEY_PAST = ".past"
49+
KEY_AFTER = ".after"
50+
KEY_BEFORE = ".before"
3551
locale = self._locale if locale is None else Locale.load(locale)
3652

3753
if diff.years > 0:
3854
unit = "year"
3955
count = diff.years
4056

41-
if diff.months > 6:
57+
if diff.months > MONTHS_THRESHOLD_FOR_HALF_YEAR:
4258
count += 1
43-
elif diff.months == 11 and (diff.weeks * 7 + diff.remaining_days) > 15:
59+
elif (diff.months == MONTHS_IN_NEARLY_A_YEAR) and (
60+
(diff.weeks * DAYS_OF_WEEK + diff.remaining_days)
61+
> DAYS_THRESHOLD_FOR_HALF_MONTH
62+
):
4463
unit = "year"
4564
count = 1
4665
elif diff.months > 0:
4766
unit = "month"
4867
count = diff.months
4968

50-
if (diff.weeks * 7 + diff.remaining_days) >= 27:
69+
if (
70+
diff.weeks * DAYS_OF_WEEK + diff.remaining_days
71+
) >= DAYS_IN_NEARLY_A_MONTH:
5172
count += 1
5273
elif diff.weeks > 0:
5374
unit = "week"
5475
count = diff.weeks
5576

56-
if diff.remaining_days > 3:
77+
if diff.remaining_days > DAYS_THRESHOLD_FOR_HALF_WEEK:
5778
count += 1
5879
elif diff.remaining_days > 0:
5980
unit = "day"
6081
count = diff.remaining_days
6182

62-
if diff.hours >= 22:
83+
if diff.hours >= HOURS_IN_NEARLY_A_DAY:
6384
count += 1
6485
elif diff.hours > 0:
6586
unit = "hour"
6687
count = diff.hours
6788
elif diff.minutes > 0:
6889
unit = "minute"
6990
count = diff.minutes
70-
elif 10 < diff.remaining_seconds <= 59:
91+
elif FEW_SECONDS_MAX < diff.remaining_seconds < SECONDS_OF_MINUTE:
7192
unit = "second"
7293
count = diff.remaining_seconds
7394
else:
@@ -76,7 +97,6 @@ def format(
7697
if time is not None:
7798
if absolute:
7899
return t.cast(str, time)
79-
80100
key = "custom"
81101
is_future = diff.invert
82102
if is_now:
@@ -86,42 +106,39 @@ def format(
86106
key += ".ago"
87107
else:
88108
if is_future:
89-
key += ".after"
109+
key += KEY_AFTER
90110
else:
91-
key += ".before"
111+
key += KEY_BEFORE
92112

93113
return t.cast(str, locale.get(key).format(time))
94114
else:
95115
unit = "second"
96116
count = diff.remaining_seconds
97-
98117
if count == 0:
99118
count = 1
100-
101119
if absolute:
102120
key = f"translations.units.{unit}"
103121
else:
104122
is_future = diff.invert
105-
106123
if is_now:
107124
# Relative to now, so we can use
108125
# the CLDR data
109126
key = f"translations.relative.{unit}"
110127

111128
if is_future:
112-
key += ".future"
129+
key += KEY_FUTURE
113130
else:
114-
key += ".past"
131+
key += KEY_PAST
115132
else:
116133
# Absolute comparison
117134
# So we have to use the custom locale data
118135

119136
# Checking for special pluralization rules
120137
key = "custom.units_relative"
121138
if is_future:
122-
key += f".{unit}.future"
139+
key += f".{unit}{KEY_FUTURE}"
123140
else:
124-
key += f".{unit}.past"
141+
key += f".{unit}{KEY_PAST}"
125142

126143
trans = locale.get(key)
127144
if not trans:
@@ -133,10 +150,9 @@ def format(
133150

134151
key = "custom"
135152
if is_future:
136-
key += ".after"
153+
key += KEY_AFTER
137154
else:
138-
key += ".before"
139-
155+
key += KEY_BEFORE
140156
return t.cast(str, locale.get(key).format(time))
141157

142158
key += f".{locale.plural(count)}"

0 commit comments

Comments
 (0)