Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistent Timezone Abbreviations #5002

Closed
wants to merge 2 commits into from

Conversation

JamieB-gu
Copy link
Contributor

@JamieB-gu JamieB-gu commented May 24, 2022

Why?

TL;DR Spec-compliant differences in browser implementations of the Date API resulted in different variations of the timezone name on Android and iOS.

Publication Timestamp

The publication timestamp at the top of articles looks something like this:

Tue 24 May 2022 11.47 (<current timezone name>)

For apps this timestamp and the corresponding timezone are in the user's local time, and so both the datetime and the timezone name can have a large number of values. We calculate this on the user's device rather than setting it on the server. This differs from dotcom, which I believe uses a handful of timezones based on edition, calculated on the server.

The Bug

We've had a long-standing issue where the publication timestamp at the top of AR articles has sometimes shown the full timezone name, rather than an abbreviation. This means users see "British Summer Time" rather than "BST". This is unusual enough that it's previously been reported as a bug.

It turns out that "sometimes" is actually platform specific. Android users are seeing the full timezone, iOS users are seeing the abbreviation. It also transpires that if you view AR in a normal browser, Safari shows the abbreviated timezone but Firefox shows the long-form version. This suggests that Chrome (Android webviews) and Firefox are displaying timezones one way, and Safari (iOS webviews) are displaying them another.

What's Going On?

For the web publication date, AR has been using JavaScript's Date.toTimeString() method and a regex to retrieve the timezone name. The JavaScript Date API is capable of generating timezone names, but doesn't expose them directly, so you have to extract them from one of the strings produced by other methods (this is awkward).

It turns out that Safari's implementation of this method produces a string that looks like this:

18:33:09 GMT+0100 (BST)

whereas Chrome/Firefox produce one that looks like this:

18:33:09 GMT+0100 (British Summer Time)

As far as I can tell, this isn't due to a bug but actually an ambiguity in the spec. The spec for that method and the spec for the timezone name state that this name is "implementation-defined". So Chrome, Safari and Firefox have decided to implement this differently 🤷.

The Fix

Thanks to @DavidLawes (⭐️) we have an alternate function called localTimeZoneAbbr that instead uses Date.toLocaleString and its ability to explicitly specify a "short" timezone name. So I've switched over to using that 🙂.

I've also dropped the enclosing braces, so the whole timestamp should now appear in a format identical to dotcom's. See the screenshots below.

Node also implements the long-form version, likely because it has the same V8 engine as Chrome. This meant that our tests were previously set up to expect this - I've now updated them to expect the short-form version.

Changes

  • Switched formatLocal over to use localTimeZoneAbbr
  • Removed now-unused localTimeZone
  • Updated tests to expect short-form timezone

Screenshots

Browser Before After
Safari/iOS before-safari after-safari
Chrome/Firefox/Android before-firefox after-firefox

- Switched `formatLocal` over to use `localTimeZoneAbbr`
- `localTimeZone` only produces abbreviated time zones in some browsers
- This browser behaviour is implementation specific in the spec
- Removed now-unused `localTimeZone`
@github-actions
Copy link

github-actions bot commented May 24, 2022

⚡️ Lighthouse report for the changes in this PR

Lighthouse tested 2 URLs

⚠️ Budget exceeded for 2 of 10 audits.

Report for Article

tested url https://www.theguardian.com/commentisfree/2020/feb/08/hungary-now-for-the-new-right-what-venezuela-once-was-for-the-left

Category Status Expected Actual
First Contentful Paint 1500 1130
Largest Contentful Paint 3000 2137
Time to Interactive 3500 1318
Cumulative Layout Shift ⚠️ 0.002 0.004428
accessibility 0.97 1.000000

Report for Front

tested url https://www.theguardian.com/uk

Category Status Expected Actual
First Contentful Paint 1500 1191
Largest Contentful Paint 3000 2669
Time to Interactive 3500 3308
Cumulative Layout Shift 0.002 0.000000
accessibility ⚠️ 0.97 0.950000

@JamieB-gu JamieB-gu linked an issue May 24, 2022 that may be closed by this pull request
@JamieB-gu JamieB-gu marked this pull request as ready for review May 24, 2022 18:09
Copy link
Contributor

@DavidLawes DavidLawes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄 glad this change was easy(ish)! Would be interested to hear any follow-up about differences between safari desktop and safari mobile

@github-actions
Copy link

"This PR is stale because it has been open 30 days with no activity. Unless a comment is added or the “stale” label removed, this will be closed in 3 days"

@github-actions github-actions bot added the Stale label Jun 25, 2022
@github-actions
Copy link

This PR was closed because it has been stalled for 3 days with no activity.

@github-actions github-actions bot closed this Jun 29, 2022
@shtukas shtukas deleted the consistent-timezone-abbreviations branch February 1, 2023 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants