44
55from pendulum .locales .locale import Locale
66
7+ DAYS_THRESHOLD_FOR_HALF_WEEK = 3
8+ DAYS_THRESHOLD_FOR_HALF_MONTH = 15
9+ MONTHS_THRESHOLD_FOR_HALF_YEAR = 6
10+
11+ HOURS_IN_NEARLY_A_DAY = 22
12+ DAYS_IN_NEARLY_A_MONTH = 27
13+ MONTHS_IN_NEARLY_A_YEAR = 11
14+
15+ DAYS_OF_WEEK = 7
16+ SECONDS_OF_MINUTE = 60
17+ FEW_SECONDS_MAX = 10
718
819if t .TYPE_CHECKING :
920 from pendulum import Duration
@@ -14,6 +25,11 @@ class DifferenceFormatter:
1425 Handles formatting differences in text.
1526 """
1627
28+ KEY_FUTURE = ".future"
29+ KEY_PAST = ".past"
30+ KEY_AFTER = ".after"
31+ KEY_BEFORE = ".before"
32+
1733 def __init__ (self , locale : str = "en" ) -> None :
1834 self ._locale = Locale .load (locale )
1935
@@ -38,36 +54,37 @@ def format(
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
60+ and (diff .weeks * DAYS_OF_WEEK + diff .remaining_days ) > DAYS_THRESHOLD_FOR_HALF_MONTH ):
4461 unit = "year"
4562 count = 1
4663 elif diff .months > 0 :
4764 unit = "month"
4865 count = diff .months
4966
50- if (diff .weeks * 7 + diff .remaining_days ) >= 27 :
67+ if (diff .weeks * DAYS_OF_WEEK + diff .remaining_days ) >= DAYS_IN_NEARLY_A_MONTH :
5168 count += 1
5269 elif diff .weeks > 0 :
5370 unit = "week"
5471 count = diff .weeks
5572
56- if diff .remaining_days > 3 :
73+ if diff .remaining_days > DAYS_THRESHOLD_FOR_HALF_WEEK :
5774 count += 1
5875 elif diff .remaining_days > 0 :
5976 unit = "day"
6077 count = diff .remaining_days
6178
62- if diff .hours >= 22 :
79+ if diff .hours >= HOURS_IN_NEARLY_A_DAY :
6380 count += 1
6481 elif diff .hours > 0 :
6582 unit = "hour"
6683 count = diff .hours
6784 elif diff .minutes > 0 :
6885 unit = "minute"
6986 count = diff .minutes
70- elif 10 < diff .remaining_seconds <= 59 :
87+ elif self . FEW_SECONDS_MAX < diff .remaining_seconds < SECONDS_OF_MINUTE :
7188 unit = "second"
7289 count = diff .remaining_seconds
7390 else :
@@ -86,10 +103,10 @@ def format(
86103 key += ".ago"
87104 else :
88105 if is_future :
89- key += ".after"
106+ key += self . KEY_AFTER
90107 else :
91- key += ".before"
92-
108+ key += self . KEY_BEFORE
109+
93110 return t .cast (str , locale .get (key ).format (time ))
94111 else :
95112 unit = "second"
@@ -109,19 +126,19 @@ def format(
109126 key = f"translations.relative.{ unit } "
110127
111128 if is_future :
112- key += ".future"
129+ key += self . KEY_FUTURE
113130 else :
114- key += ".past"
131+ key += self . 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 } { self . KEY_FUTURE } "
123140 else :
124- key += f".{ unit } .past "
141+ key += f".{ unit } { self . 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 += self . KEY_AFTER
137154 else :
138- key += ".before"
139-
155+ key += self .KEY_BEFORE
140156 return t .cast (str , locale .get (key ).format (time ))
141157
142158 key += f".{ locale .plural (count )} "
0 commit comments