openstreetmap / openstreetmap-website

The Rails application that powers OpenStreetMap
https://www.openstreetmap.org/
GNU General Public License v2.0
2.2k stars 911 forks source link

Localise message timestamps #4067

Open Dimitar5555 opened 1 year ago

Dimitar5555 commented 1 year ago

Problem

Currently timestamps in the inbox and outbox are in UTC, regardless of the user's timezone. Would it be possible to localise them in the user's timezone or alternatively make them like the timestamps in changesets ("about 4 hours ago", "a few seconds ago" and so on).

Description

No response

Screenshots

No response

tomhughes commented 1 year ago

We have no idea what the user's timezone is, so no we can't do that.

While we could show them as relative times that would still be relative to UTC not to the user's timezone, and relative times tend to be quite unpopular and to generate complaints.

The only way we could do what you want is if we allowed a user to set a timezone in the preferences and stored it in the user record in the database.

Dimitar5555 commented 1 year ago

We don't need to know the user's timezone, it can be processed by the browser. If the date is supplied as Unix timestamp (<time datetime="unix_timestamp_in_UTC">fallback</time>), then we can have some JS code to convert into the local time

document.onload(()=>{
var time_els = document..querySelectorAll("time");
for (i = 0; i < time_els.length; i++) {
  time_els.innerText = new Date(new Date(time_els.getAttribute("datetime") * 1000) + "UTC").toLocaleString("xx");
}
});

The fallback is optional, it is needed for ancient browsers and browsers that don't support Javascript.

xx is the locale, it can be "en" or "en-us", regardless it will produce correct results.

toLocaleString seems to be supported by all major browsers (see https://caniuse.com/mdn-javascript_builtins_date_tolocalestring).

mmd-osm commented 2 months ago

We could check out https://github.com/basecamp/local_time