Skip to content

Commit

Permalink
Replace hardcoded time units with named constants
Browse files Browse the repository at this point in the history
- Replaced hardcoded time-related values with named constants.
- Facilitates easier adjustments of these values and reduces potential for errors.
  • Loading branch information
Minku-Koo committed Dec 18, 2024
1 parent a69761e commit 6495753
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions src/pendulum/formatting/difference_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,63 @@ def format(
:param absolute: Whether it's an absolute difference or not
:param locale: The locale to use
"""
DAYS_THRESHOLD_FOR_HALF_WEEK = 3
DAYS_THRESHOLD_FOR_HALF_MONTH = 15
MONTHS_THRESHOLD_FOR_HALF_YEAR = 6

HOURS_IN_NEARLY_A_DAY = 22
DAYS_IN_NEARLY_A_MONTH = 27
MONTHS_IN_NEARLY_A_YEAR = 11

DAYS_OF_WEEK = 7
SECONDS_OF_MINUTE = 60
FEW_SECONDS_MAX = 10

KEY_FUTURE = ".future"
KEY_PAST = ".past"
KEY_AFTER = ".after"
KEY_BEFORE = ".before"
locale = self._locale if locale is None else Locale.load(locale)

if diff.years > 0:
unit = "year"
count = diff.years

if diff.months > 6:
if diff.months > MONTHS_THRESHOLD_FOR_HALF_YEAR:
count += 1
elif diff.months == 11 and (diff.weeks * 7 + diff.remaining_days) > 15:
elif (diff.months == MONTHS_IN_NEARLY_A_YEAR) and (
(diff.weeks * DAYS_OF_WEEK + diff.remaining_days)
> DAYS_THRESHOLD_FOR_HALF_MONTH
):
unit = "year"
count = 1
elif diff.months > 0:
unit = "month"
count = diff.months

if (diff.weeks * 7 + diff.remaining_days) >= 27:
if (
diff.weeks * DAYS_OF_WEEK + diff.remaining_days
) >= DAYS_IN_NEARLY_A_MONTH:
count += 1
elif diff.weeks > 0:
unit = "week"
count = diff.weeks

if diff.remaining_days > 3:
if diff.remaining_days > DAYS_THRESHOLD_FOR_HALF_WEEK:
count += 1
elif diff.remaining_days > 0:
unit = "day"
count = diff.remaining_days

if diff.hours >= 22:
if diff.hours >= HOURS_IN_NEARLY_A_DAY:
count += 1
elif diff.hours > 0:
unit = "hour"
count = diff.hours
elif diff.minutes > 0:
unit = "minute"
count = diff.minutes
elif 10 < diff.remaining_seconds <= 59:
elif FEW_SECONDS_MAX < diff.remaining_seconds < SECONDS_OF_MINUTE:
unit = "second"
count = diff.remaining_seconds
else:
Expand All @@ -76,7 +97,6 @@ def format(
if time is not None:
if absolute:
return t.cast(str, time)

key = "custom"
is_future = diff.invert
if is_now:
Expand All @@ -86,42 +106,39 @@ def format(
key += ".ago"
else:
if is_future:
key += ".after"
key += KEY_AFTER
else:
key += ".before"
key += KEY_BEFORE

return t.cast(str, locale.get(key).format(time))
else:
unit = "second"
count = diff.remaining_seconds

if count == 0:
count = 1

if absolute:
key = f"translations.units.{unit}"
else:
is_future = diff.invert

if is_now:
# Relative to now, so we can use
# the CLDR data
key = f"translations.relative.{unit}"

if is_future:
key += ".future"
key += KEY_FUTURE
else:
key += ".past"
key += KEY_PAST
else:
# Absolute comparison
# So we have to use the custom locale data

# Checking for special pluralization rules
key = "custom.units_relative"
if is_future:
key += f".{unit}.future"
key += f".{unit}{KEY_FUTURE}"
else:
key += f".{unit}.past"
key += f".{unit}{KEY_PAST}"

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

key = "custom"
if is_future:
key += ".after"
key += KEY_AFTER
else:
key += ".before"

key += KEY_BEFORE
return t.cast(str, locale.get(key).format(time))

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

0 comments on commit 6495753

Please sign in to comment.