File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -54,6 +54,14 @@ const VideoAge = (function() {
5454 round : true // Round to nearest unit
5555 } ;
5656
57+ /**
58+ * Milliseconds in a day. Video dates have day granularity (server computes
59+ * age from a DateField), so any age below this means "published today".
60+ * Without this guard, a same-day video humanizes to "0 seconds ago" — the
61+ * symptom reported in issue #1091.
62+ */
63+ const ONE_DAY_MS = 24 * 60 * 60 * 1000 ;
64+
5765
5866 // ==========================================================================
5967 // INITIALIZATION
@@ -134,9 +142,13 @@ const VideoAge = (function() {
134142 *
135143 * @private
136144 * @param {number } ageInMs - Age in milliseconds
137- * @returns {string } Formatted string like "2 years ago"
145+ * @returns {string } Formatted string like "2 years ago", or "Today" for a
146+ * video published today (avoids "0 seconds ago" — issue #1091)
138147 */
139148 function formatPastAge ( ageInMs ) {
149+ if ( ageInMs < ONE_DAY_MS ) {
150+ return 'Today' ;
151+ }
140152 try {
141153 const humanized = humanizeDuration ( ageInMs , HUMANIZE_OPTIONS ) ;
142154 return humanized + ' ago' ;
Original file line number Diff line number Diff line change @@ -26,6 +26,18 @@ def test_past_date_returns_positive_int(self):
2626 # 7 days in ms, allowing slack for the now() call crossing midnight.
2727 self .assertGreaterEqual (age , 6 * 24 * 60 * 60 * 1000 )
2828
29+ def test_today_is_below_one_day (self ):
30+ # A video published today must yield an age below one day in ms. This
31+ # is the contract video-age.js relies on to render "Today" instead of
32+ # "0 seconds ago" (issue #1091); pin it server-side since the JS guard
33+ # itself has no test runner in this repo.
34+ from website .models import Video
35+ one_day_ms = 24 * 60 * 60 * 1000
36+ age = Video (date = date .today ()).get_age_in_ms ()
37+ self .assertIsInstance (age , int )
38+ self .assertGreaterEqual (age , 0 )
39+ self .assertLess (age , one_day_ms )
40+
2941 def test_future_date_returns_negative_int (self ):
3042 from website .models import Video
3143 age = Video (date = date .today () + timedelta (days = 7 )).get_age_in_ms ()
You can’t perform that action at this time.
0 commit comments