Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 13 additions & 1 deletion website/static/website/js/video-age.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ const VideoAge = (function() {
round: true // Round to nearest unit
};

/**
* Milliseconds in a day. Video dates have day granularity (server computes
* age from a DateField), so any age below this means "published today".
* Without this guard, a same-day video humanizes to "0 seconds ago" — the
* symptom reported in issue #1091.
*/
const ONE_DAY_MS = 24 * 60 * 60 * 1000;


// ==========================================================================
// INITIALIZATION
Expand Down Expand Up @@ -134,9 +142,13 @@ const VideoAge = (function() {
*
* @private
* @param {number} ageInMs - Age in milliseconds
* @returns {string} Formatted string like "2 years ago"
* @returns {string} Formatted string like "2 years ago", or "Today" for a
* video published today (avoids "0 seconds ago" — issue #1091)
*/
function formatPastAge(ageInMs) {
if (ageInMs < ONE_DAY_MS) {
return 'Today';
}
try {
const humanized = humanizeDuration(ageInMs, HUMANIZE_OPTIONS);
return humanized + ' ago';
Expand Down
12 changes: 12 additions & 0 deletions website/tests/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ def test_past_date_returns_positive_int(self):
# 7 days in ms, allowing slack for the now() call crossing midnight.
self.assertGreaterEqual(age, 6 * 24 * 60 * 60 * 1000)

def test_today_is_below_one_day(self):
# A video published today must yield an age below one day in ms. This
# is the contract video-age.js relies on to render "Today" instead of
# "0 seconds ago" (issue #1091); pin it server-side since the JS guard
# itself has no test runner in this repo.
from website.models import Video
one_day_ms = 24 * 60 * 60 * 1000
age = Video(date=date.today()).get_age_in_ms()
self.assertIsInstance(age, int)
self.assertGreaterEqual(age, 0)
self.assertLess(age, one_day_ms)

def test_future_date_returns_negative_int(self):
from website.models import Video
age = Video(date=date.today() + timedelta(days=7)).get_age_in_ms()
Expand Down
Loading