Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix format_timesince showing "60 seconds ago" / "60 minutes ago" / …
…"24 hours ago" instead of rolling over (#4943)

* Roll `format_timesince` over to the next unit at the boundary

`format_timesince` showed "60 seconds ago", "60 minutes ago" and "24 hours ago"
at the exact unit boundaries instead of rolling over to the next unit, because
the per-unit max values were 60/60/24 rather than 59/59/23. Days (6 -> 1 week)
and months (11 -> 1 year) already rolled correctly; this brings seconds,
minutes and hours in line. Adds the boundary cases to the test.

* Update src/huggingface_hub/utils/_parsing.py

---------

Co-authored-by: célina <hanouticelina@gmail.com>
  • Loading branch information
Sreekant13 and hanouticelina authored Sep 21, 2026
commit 4c80e7ee7617b149719bc2d883451c0489f3531a
8 changes: 4 additions & 4 deletions src/huggingface_hub/utils/_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ def format_timesince(ts: float) -> str:
Vaguely inspired by Django's `timesince` formatter.
"""
_TIMESINCE_CHUNKS = (
# Label, divider, max value
("second", 1, 60),
("minute", 60, 60),
("hour", 60 * 60, 24),
# Label, divider, max value shown before rolling over to the next unit (e.g. 59 -> "1 minute ago" at 60s)
("second", 1, 59),
("minute", 60, 59),
("hour", 60 * 60, 23),
("day", 60 * 60 * 24, 6),
("week", 60 * 60 * 24 * 7, 6),
("month", 60 * 60 * 24 * 30, 11),
Expand Down
8 changes: 8 additions & 0 deletions tests/test_utils_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ def test_parse_duration_invalid(value):
(1000, "17 minutes ago"),
(4000, "1 hour ago"),
(8000, "2 hours ago"),
# Unit boundaries must roll over to the next unit, not show e.g.
# "60 seconds ago" / "60 minutes ago" / "24 hours ago".
(59, "59 seconds ago"),
(60, "1 minute ago"),
(59 * 60, "59 minutes ago"),
(60 * 60, "1 hour ago"),
(23 * 60 * 60, "23 hours ago"),
(24 * 60 * 60, "1 day ago"),
],
)
def test_format_timesince(value, expected):
Expand Down
Loading