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

fix(v2): always use UTC when dealing with blog dates #4983

Merged
merged 1 commit into from
Jun 16, 2021

Conversation

Josh-Cena
Copy link
Collaborator

@Josh-Cena Josh-Cena commented Jun 16, 2021

Motivation

Fix #4881. This issue contains two bugs, the first (wrong displayed date) is related to formattedDate, the second (wrong generated URL) is related to toUrl. By always dealing with the blog date object in the UTC timezone, both bugs should be fixed. (@josh-kaplan inspired me on how to fix the first one.)

Although in the documentation, it is suggested to use YYYY-MM-DD for the blog date, numbers without a preceding zero will not be rejected and will be correctly parsed. However, this causes the date to be treated as a local time object instead of a UTC one when creating the date object, as documented. By forcing the date object to be created in UTC, this problem is solved.

In formattedDate, the default timezone is the one of the runtime instead of UTC (documentation). We need to force it to UTC.

Have you read the Contributing Guidelines on pull requests?

Yep

Test Plan

I can't demonstrate this error as a test case because this timezone is set in the local runtime environment. However, I constructed some examples:

Previous

let d = new Date("2021-5-10") // "Mon May 10 2021 00:00:00 GMT+0800 (China Standard Time)"
// This is because the date is not in the standard "YYYY-MM-DD" format, and therefore uses the local time zone instead of UTC.
toUrl({d, "Hi"}) // "2021/05/09/Hi"; has a one-offset from the actual date
let d = new Date("2021-02-01") // "Sun Jan 31 2021 19:00:00 GMT-0500 (Eastern Standard Time)"
new Intl.DateTimeFormat("en", {
  day: 'numeric',
  month: 'long',
  year: 'numeric',
}).format(d) // "January 31, 2021"; because the timezone is GMT-0500

Now

let d = new Date("2021-5-10Z") // "Mon May 10 2021 08:00:00 GMT+0800 (China Standard Time)"
toUrl({d, "Hi"}) // "2021/05/10/Hi"
let d = new Date("2021-02-01Z") // "Sun Jan 31 2021 19:00:00 GMT-0500 (Eastern Standard Time)"
new Intl.DateTimeFormat("en", {
  day: 'numeric',
  month: 'long',
  year: 'numeric',
  timeZone: 'UTC'
}).format(d) // "February 1, 2021"

Let me know how to create a test case if there's such a need...

Signed-off-by: Josh-Cena <sidachen2003@gmail.com>
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Jun 16, 2021
@netlify
Copy link

netlify bot commented Jun 16, 2021

✔️ [V2]
Built without sensitive environment variables

🔨 Explore the source changes: 34551ff

🔍 Inspect the deploy log: https://app.netlify.com/sites/docusaurus-2/deploys/60c9cb5ccfbc0b000745fdbc

😎 Browse the preview: https://deploy-preview-4983--docusaurus-2.netlify.app

@github-actions
Copy link

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 63
🟢 Accessibility 97
🟢 Best practices 100
🟢 SEO 100
🟢 PWA 95

Lighthouse ran on https://deploy-preview-4983--docusaurus-2.netlify.app/

@slorber slorber added the pr: bug fix This PR fixes a bug in a past release. label Jun 16, 2021
@slorber
Copy link
Collaborator

slorber commented Jun 16, 2021

thanks, that looks like a valid fix

@slorber slorber merged commit 80b6d97 into facebook:master Jun 16, 2021
@Josh-Cena Josh-Cena deleted the fix-blog-date branch June 16, 2021 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Off-by-one error in blog dates
3 participants