Page MenuHomePhabricator

Language::getHumanTimestamp does not do what it says it should do
Open, Stalled, Needs TriagePublic

Description

Language::getHumanTimestamp does not work. With one Timestamp it just gives the day date (4 april). With another it returns nothing when not logged in, or the full formatted string when logged in (2015-11-30T02:49:14 following date format settings).

It also does not add an 'ago' to the end, nor does it put an amount of days or whatever past (such as '19 days ago', which I would have expected from the 4 april one)

It does, however, follow the date format preferences, which does not make sense either.

Event Timeline

Test case:

> $lang = Language::factory( 'en' ); $time = new MWTimestamp( '20151115000000' ); echo $lang->getHumanTimestamp( $time );
00:00, 15 November 2015

Expected result: something like "5 months ago" or so. Or anything that matches the documentation, really, which claims:

	 * Get the timestamp in a human-friendly relative format, e.g., "3 days ago".
	 *
	 * Determine the difference between the timestamp and the current time, and
	 * generate a readable timestamp by returning "<N> <units> ago", where the
	 * largest possible unit is used.

I don't see the word "ago" in the output anywhere, no matter how hard I look.

Tested both on the ShoutWiki codebase (latest 1.26) as well as on my Labs instance, which is running a somewhat outdated 1.27alpha build. Same unexpected/unwanted result on both instead of the desired result.

// Timestamps are in different years: use full timestamp
// Also do full timestamp for future dates

That looks like it may be at fault here, since you're testing with a date from last year.

			// Timestamps are in same year,  but more than 5 days ago: show day and month only.

And that too.

So what you're saying it it only does what it says it will (Get the timestamp in a human-friendly relative format, e.g., "3 days ago") if it's under four days old?

That is incredibly misleading.

Basically, the doc commen is incomplete description of what it does, as half of the description is in inline comments that do not get reflected in the docs. :(

I would recommend a combination of updating the doc comment and... Probably not having the exception for things over 5 days old, which seems oddish.

Relative timestamps offer diminishing returns and increasing costs the farther the time gets (see tracking bug), which is probably the reason of the "exception". A more accurate name would probably be maybeGetMoreConciseTimestamp(), given the goal of those who developed this function was apparently to reduce the total screen estate taken by timestamps in the most common cases.

Any system relying on "ago" is bound to be untranslatable in some languages, so any expansion of that construction should come only after ensuring its actual translatability in several languages.

Note also that on WMF wikis this functionality is usually overwritten by CLDR via the onGetHumanTimestamp hook.

Here are all the current comments in the various branches:

// Timestamps are in different years: use full timestamp
// Also do full timestamp for future dates
 * @todo FIXME: Add better handling of future timestamps.

// Timestamps are in same year,  but more than 5 days ago: show day and month only.

// Timestamp within the past week: show the day of the week and time

// Timestamp was yesterday: say 'yesterday' and the time.

// Timestamp was today, but more than 90 minutes ago: say 'today' and the time.

// From here on in, the timestamp was soon enough ago so that we can simply say
// XX units ago, e.g., "2 hours ago" or "5 minutes ago"

I certainly think it makes no sense to suddenly show a high-resolution timestamp (hours+minutes+seconds) when times are in a different year.

Change 811750 had a related patch set uploaded (by Esanders; author: Esanders):

[mediawiki/core@master] Language::getHumanTimestamp: Don't show time when timestamp in a different year

https://gerrit.wikimedia.org/r/811750

Note that WMF wikis have CLDR installed which overrides this behaviour via a hook, so this logic is mostly unused by us. It does trigger if the language is not supported by CLDR (e.g. en-gb).

Change 811750 merged by jenkins-bot:

[mediawiki/core@master] Language::getHumanTimestamp: Don't show time when timestamp in a different year

https://gerrit.wikimedia.org/r/811750

matmarex changed the task status from Open to Stalled.Jul 7 2022, 6:30 PM
matmarex added a subscriber: matmarex.

I've merged the patch since I think it's an uncontroversial improvement, but I'm not sure if it really resolves this task. Someone should probably define what exactly the expected behavior is.

Or maybe this change and the explanation in T133468#2926679 is enough, in which case please close the task.