From 75c183c2486978631980027d9667ffdd482fe107 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 25 Oct 2019 13:08:05 +0000 Subject: [PATCH] Date/Time: Make sure `get_post_time()` keeps UTC time on timezone change. Add `$source` parameter to `get_post_datetime()` to instantiate from local or UTC time in database. Props Rarst, david.binda. Reviewed by azaozz, SergeyBiryukov. Fixes #48384. Built from https://develop.svn.wordpress.org/trunk@46580 git-svn-id: http://core.svn.wordpress.org/trunk@46377 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/general-template.php | 41 +++++++++++++++++++++++++------- wp-includes/version.php | 2 +- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/wp-includes/general-template.php b/wp-includes/general-template.php index 32d5595bb1..f9b5661f09 100644 --- a/wp-includes/general-template.php +++ b/wp-includes/general-template.php @@ -2565,7 +2565,8 @@ function get_post_time( $d = 'U', $gmt = false, $post = null, $translate = false return false; } - $datetime = get_post_datetime( $post ); + $source = ( $gmt ) ? 'gmt' : 'local'; + $datetime = get_post_datetime( $post, 'date', $source ); if ( false === $datetime ) { return false; @@ -2606,26 +2607,48 @@ function get_post_time( $d = 'U', $gmt = false, $post = null, $translate = false * * The object will be set to the timezone from WordPress settings. * + * For legacy reasons, this function allows to choose to instantiate from local or UTC time in database. + * Normally this should make no difference to the result. However, the values might get out of sync in database, + * typically because of timezone setting changes. The parameter ensures the ability to reproduce backwards + * compatible behaviors in such cases. + * * @since 5.3.0 * - * @param int|WP_Post $post Optional. WP_Post object or ID. Default is global `$post` object. - * @param string $field Optional. Post field to use. Accepts 'date' or 'modified'. + * @param int|WP_Post $post Optional. WP_Post object or ID. Default is global `$post` object. + * @param string $field Optional. Published or modified time to use from database. Accepts 'date' or 'modified'. + * Default 'date'. + * @param string $source Optional. Local or UTC time to use from database. Accepts 'local' or 'gmt'. + * Default 'local'. * @return DateTimeImmutable|false Time object on success, false on failure. */ -function get_post_datetime( $post = null, $field = 'date' ) { +function get_post_datetime( $post = null, $field = 'date', $source = 'local' ) { $post = get_post( $post ); if ( ! $post ) { return false; } - $time = ( 'modified' === $field ) ? $post->post_modified : $post->post_date; + $wp_timezone = wp_timezone(); + + if ( 'gmt' === $source ) { + $time = ( 'modified' === $field ) ? $post->post_modified_gmt : $post->post_date_gmt; + $timezone = new DateTimeZone( 'UTC' ); + } else { + $time = ( 'modified' === $field ) ? $post->post_modified : $post->post_date; + $timezone = $wp_timezone; + } if ( empty( $time ) || '0000-00-00 00:00:00' === $time ) { return false; } - return date_create_immutable_from_format( 'Y-m-d H:i:s', $time, wp_timezone() ); + $datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', $time, $timezone ); + + if ( false === $datetime ) { + return false; + } + + return $datetime->setTimezone( $wp_timezone ); } /** @@ -2637,7 +2660,8 @@ function get_post_datetime( $post = null, $field = 'date' ) { * @since 5.3.0 * * @param int|WP_Post $post Optional. WP_Post object or ID. Default is global `$post` object. - * @param string $field Optional. Post field to use. Accepts 'date' or 'modified'. + * @param string $field Optional. Published or modified time to use from database. Accepts 'date' or 'modified'. + * Default 'date'. * @return int|false Unix timestamp on success, false on failure. */ function get_post_timestamp( $post = null, $field = 'date' ) { @@ -2729,7 +2753,8 @@ function get_post_modified_time( $d = 'U', $gmt = false, $post = null, $translat return false; } - $datetime = get_post_datetime( $post, 'modified' ); + $source = ( $gmt ) ? 'gmt' : 'local'; + $datetime = get_post_datetime( $post, 'modified', $source ); if ( false === $datetime ) { return false; diff --git a/wp-includes/version.php b/wp-includes/version.php index 1067da71c1..b1aa0d6f11 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.3-RC2-46579'; +$wp_version = '5.3-RC2-46580'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.