Skip to content

Commit a5efba4

Browse files
Minku-KooSecrus
andauthored
Replace hardcoded time units with named constants (#867)
* 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. * refactor: extract function-level constants to module-level - Ensures better adherence to PEP 8 naming conventions and improves code readability. * Add endline after pre-commit black --------- Co-authored-by: Bartosz Sokorski <[email protected]>
1 parent 4d7e42a commit a5efba4

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

src/pendulum/formatting/difference_formatter.py

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,23 @@
88
if t.TYPE_CHECKING:
99
from pendulum import Duration
1010

11+
DAYS_THRESHOLD_FOR_HALF_WEEK = 3
12+
DAYS_THRESHOLD_FOR_HALF_MONTH = 15
13+
MONTHS_THRESHOLD_FOR_HALF_YEAR = 6
14+
15+
HOURS_IN_NEARLY_A_DAY = 22
16+
DAYS_IN_NEARLY_A_MONTH = 27
17+
MONTHS_IN_NEARLY_A_YEAR = 11
18+
19+
DAYS_OF_WEEK = 7
20+
SECONDS_OF_MINUTE = 60
21+
FEW_SECONDS_MAX = 10
22+
23+
KEY_FUTURE = ".future"
24+
KEY_PAST = ".past"
25+
KEY_AFTER = ".after"
26+
KEY_BEFORE = ".before"
27+
1128

1229
class DifferenceFormatter:
1330
"""
@@ -38,36 +55,41 @@ def format(
3855
unit = "year"
3956
count = diff.years
4057

41-
if diff.months > 6:
58+
if diff.months > MONTHS_THRESHOLD_FOR_HALF_YEAR:
4259
count += 1
43-
elif diff.months == 11 and (diff.weeks * 7 + diff.remaining_days) > 15:
60+
elif (diff.months == MONTHS_IN_NEARLY_A_YEAR) and (
61+
(diff.weeks * DAYS_OF_WEEK + diff.remaining_days)
62+
> DAYS_THRESHOLD_FOR_HALF_MONTH
63+
):
4464
unit = "year"
4565
count = 1
4666
elif diff.months > 0:
4767
unit = "month"
4868
count = diff.months
4969

50-
if (diff.weeks * 7 + diff.remaining_days) >= 27:
70+
if (
71+
diff.weeks * DAYS_OF_WEEK + diff.remaining_days
72+
) >= DAYS_IN_NEARLY_A_MONTH:
5173
count += 1
5274
elif diff.weeks > 0:
5375
unit = "week"
5476
count = diff.weeks
5577

56-
if diff.remaining_days > 3:
78+
if diff.remaining_days > DAYS_THRESHOLD_FOR_HALF_WEEK:
5779
count += 1
5880
elif diff.remaining_days > 0:
5981
unit = "day"
6082
count = diff.remaining_days
6183

62-
if diff.hours >= 22:
84+
if diff.hours >= HOURS_IN_NEARLY_A_DAY:
6385
count += 1
6486
elif diff.hours > 0:
6587
unit = "hour"
6688
count = diff.hours
6789
elif diff.minutes > 0:
6890
unit = "minute"
6991
count = diff.minutes
70-
elif 10 < diff.remaining_seconds <= 59:
92+
elif FEW_SECONDS_MAX < diff.remaining_seconds < SECONDS_OF_MINUTE:
7193
unit = "second"
7294
count = diff.remaining_seconds
7395
else:
@@ -86,42 +108,39 @@ def format(
86108
key += ".ago"
87109
else:
88110
if is_future:
89-
key += ".after"
111+
key += KEY_AFTER
90112
else:
91-
key += ".before"
113+
key += KEY_BEFORE
92114

93115
return t.cast("str", locale.get(key).format(time))
94116
else:
95117
unit = "second"
96118
count = diff.remaining_seconds
97-
98119
if count == 0:
99120
count = 1
100-
101121
if absolute:
102122
key = f"translations.units.{unit}"
103123
else:
104124
is_future = diff.invert
105-
106125
if is_now:
107126
# Relative to now, so we can use
108127
# the CLDR data
109128
key = f"translations.relative.{unit}"
110129

111130
if is_future:
112-
key += ".future"
131+
key += KEY_FUTURE
113132
else:
114-
key += ".past"
133+
key += KEY_PAST
115134
else:
116135
# Absolute comparison
117136
# So we have to use the custom locale data
118137

119138
# Checking for special pluralization rules
120139
key = "custom.units_relative"
121140
if is_future:
122-
key += f".{unit}.future"
141+
key += f".{unit}{KEY_FUTURE}"
123142
else:
124-
key += f".{unit}.past"
143+
key += f".{unit}{KEY_PAST}"
125144

126145
trans = locale.get(key)
127146
if not trans:
@@ -133,9 +152,9 @@ def format(
133152

134153
key = "custom"
135154
if is_future:
136-
key += ".after"
155+
key += KEY_AFTER
137156
else:
138-
key += ".before"
157+
key += KEY_BEFORE
139158

140159
return t.cast("str", locale.get(key).format(time))
141160

0 commit comments

Comments
 (0)