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

Ensure post_publish_timestamp in AMP_Post_Template is UTC for human_time_diff() #5335

Merged
merged 1 commit into from
Sep 8, 2020

Conversation

westonruter
Copy link
Member

Summary

As reported in a support forum topic, the relative publish time displayed on the legacy post template is incorrect when the timezone is not UTC.

For example, on my site I have the timezone set to America/Los_Angeles. Nevertheless, when I publish my post and I immediately view the legacy AMP post template, I see "7 hours ago". This is because my timezone is UTC-7. The problem is that the post_publish_timestamp was being populated with get_the_date( 'U', $this->post ) which returns a non-GMT “Unix timestamp” (which AFAIK doesn't even make sense, since this value should be timezone-independent). Nevertheless, since get_the_date( 'U', $this->post ) is getting the blog's timezone offset, it then fails to render the right result in the template:

echo esc_html(
sprintf(
/* translators: %s: the human-readable time difference. */
__( '%s ago', 'amp' ),
human_time_diff( $this->get( 'post_publish_timestamp' ), time() )
)
);

Here time() is the Unix timestamp of seconds since January 1, 1970 00:00:00 UTC. But $post_publish_timestamp is actually 7 hours before that on my install.

The fix is to not call get_the_date() directly, but to call the underlying get_post_time() which does allow you to specifically request the GMT time.

Since previously get_the_date() was used, I figured it would be good to continue applying get_the_date filters so that they wouldn't break if sites were using them.

Before After
image image

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@westonruter westonruter added this to the v2.0.2 milestone Sep 4, 2020
@google-cla google-cla bot added the cla: yes Signed the Google CLA label Sep 4, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Sep 4, 2020

Plugin builds for 3060b6b are ready 🛎️!

@pierlon pierlon added the WS:Core Work stream for Plugin core label Sep 8, 2020
@westonruter westonruter merged commit c39f672 into develop Sep 8, 2020
@westonruter westonruter deleted the fix/legacy-amp-post-publish-date branch September 8, 2020 23:39
@jwold jwold self-assigned this Sep 17, 2020
@jwold
Copy link
Collaborator

jwold commented Sep 17, 2020

@westonruter I'm still seeing the problem where it says 10 hours ago instead of 2 hours ago.

Steps I took:

  1. Install Local with Prod link (I checked the downloaded file and it shows: Version: 2.0.2-alpha-20200917T192128Z-ee08122)
  2. Set time stamp in Settings to Los Angeles
  3. Run onboarding wizard: reader mode with legacy template
  4. Create new post, set time to 2 hours previous, and publish
  5. View in AMP mode: https://d.pr/i/CmNXzz

@jwold
Copy link
Collaborator

jwold commented Sep 17, 2020

Followup: I had changed the time to 10 hours in the future instead of 2 in the past (PM was set by default in Gutenberg).

Once I fixed that the post shows as expected, saying 2 hours ago, but it raises the question of why it would say 10 hours ago if it's schedule for 10 hours in the future.

@jwold
Copy link
Collaborator

jwold commented Sep 17, 2020

QA passed.

Re-tried following different steps, namely that I published immediately. The time stamp is now correct based on that action.

@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Sep 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelogged Whether the issue/PR has been added to release notes. cla: yes Signed the Google CLA Reader Mode WS:Core Work stream for Plugin core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants