Make WordPress Core

Ticket #28992: mysql2date-revamp.patch

File mysql2date-revamp.patch, 4.7 KB (added by Rarst, 6 years ago)
  • tests/phpunit/tests/date/mysql2date.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
     1<?php
     2
     3/**
     4 * @group date
     5 * @group datetime
     6 */
     7class Tests_Date_mysql2date extends WP_UnitTestCase {
     8
     9        function tearDown() {
     10
     11                date_default_timezone_set( 'UTC' );
     12
     13                parent::tearDown();
     14        }
     15
     16        function test_mysql2date_should_format_time() {
     17                $timezone = 'Europe/Kiev';
     18                update_option( 'timezone_string', $timezone );
     19                $datetime = new DateTime( 'now', new DateTimeZone( $timezone ) );
     20                $rfc3339  = $datetime->format( DATE_RFC3339 );
     21                $mysql    = $datetime->format( 'Y-m-d H:i:s' );
     22
     23                $this->assertEquals( $rfc3339, mysql2date( DATE_RFC3339, $mysql ) );
     24                $this->assertEquals( $rfc3339, mysql2date( DATE_RFC3339, $mysql, false ) );
     25        }
     26
     27        function test_mysql2date_should_format_time_with_changed_time_zone() {
     28                $timezone = 'Europe/Kiev';
     29                date_default_timezone_set( $timezone );
     30                update_option( 'timezone_string', $timezone );
     31                $datetime = new DateTime( 'now', new DateTimeZone( $timezone ) );
     32                $rfc3339  = $datetime->format( DATE_RFC3339 );
     33                $mysql    = $datetime->format( 'Y-m-d H:i:s' );
     34
     35                $this->assertEquals( $rfc3339, mysql2date( DATE_RFC3339, $mysql ) );
     36                $this->assertEquals( $rfc3339, mysql2date( DATE_RFC3339, $mysql, false ) );
     37        }
     38
     39        function test_mysql2date_should_return_wp_timestamp() {
     40                $timezone = 'Europe/Kiev';
     41                update_option( 'timezone_string', $timezone );
     42                $datetime     = new DateTime( 'now', new DateTimeZone( $timezone ) );
     43                $wp_timestamp = $datetime->getTimestamp() + $datetime->getOffset();
     44                $mysql        = $datetime->format( 'Y-m-d H:i:s' );
     45
     46                $this->assertEquals( $wp_timestamp, mysql2date( 'U', $mysql, false ) );
     47                $this->assertEquals( $wp_timestamp, mysql2date( 'G', $mysql, false ) );
     48        }
     49
     50        function test_mysql2date_should_return_unix_timestamp_for_gmt_time() {
     51                $timezone = 'Europe/Kiev';
     52                update_option( 'timezone_string', $timezone );
     53                $datetime  = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
     54                $timestamp = $datetime->getTimestamp();
     55                $mysql     = $datetime->format( 'Y-m-d H:i:s' );
     56
     57                $this->assertEquals( $timestamp, mysql2date( 'U', $mysql, false ) );
     58                $this->assertEquals( $timestamp, mysql2date( 'G', $mysql, false ) );
     59        }
     60}
  • src/wp-includes/functions.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    88require( ABSPATH . WPINC . '/option.php' );
    99
    1010/**
    11  * Convert given date string into a different format.
     11 * Convert given MySQL date string into a different format.
     12 *
     13 * `$format` should be a PHP date format string.
    1214 *
    13  * $format should be either a PHP date format string, e.g. 'U' for a Unix
    14  * timestamp, or 'G' for a Unix timestamp assuming that $date is GMT.
     15 * 'U' and 'G' formats will return a sum of timestamp with time zone offset.
     16 *
     17 * `$date` is expected to be local time in MySQL format (`Y-m-d H:i:s`).
     18 *
     19 * Historically UTC time could be passed to function to produce Unix timestamp.
    1520 *
    1621 * If $translate is true then the given date and format string will
    17  * be passed to date_i18n() for translation.
     22 * be passed to `wp_date()` for translation.
    1823 *
    1924 * @since 0.71
    2025 *
    2126 * @param string $format    Format of the date to return.
    2227 * @param string $date      Date string to convert.
    2328 * @param bool   $translate Whether the return date should be translated. Default true.
    24  * @return string|int|bool Formatted date string or Unix timestamp. False if $date is empty.
     29 *
     30 * @return string|int|false Formatted date string or sum of Unix timestamp and time zone offset. False on failure.
    2531 */
    2632function mysql2date( $format, $date, $translate = true ) {
    2733        if ( empty( $date ) ) {
    2834                return false;
    2935        }
    3036
    31         if ( 'G' == $format ) {
    32                 return strtotime( $date . ' +0000' );
    33         }
     37        $datetime = date_create( $date, wp_timezone() );
    3438
    35         $i = strtotime( $date );
     39        if ( false === $datetime ) {
     40                return false;
     41        }
    3642
    37         if ( 'U' == $format ) {
    38                 return $i;
     43        // Returns a sum of timestamp with time zone offset. Ideally should never be used.
     44        if ( 'G' === $format || 'U' === $format ) {
     45                return $datetime->getTimestamp() + $datetime->getOffset();
    3946        }
    4047
    4148        if ( $translate ) {
    42                 return date_i18n( $format, $i );
    43         } else {
    44                 return gmdate( $format, $i );
     49                return wp_date( $format, $datetime->getTimestamp() );
    4550        }
     51
     52        return $datetime->format( $format );
    4653}
    4754
    4855/**