Ticket #28992: mysql2date-revamp.patch
File mysql2date-revamp.patch, 4.7 KB (added by , 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 */ 7 class 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
8 8 require( ABSPATH . WPINC . '/option.php' ); 9 9 10 10 /** 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. 12 14 * 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. 15 20 * 16 21 * 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. 18 23 * 19 24 * @since 0.71 20 25 * 21 26 * @param string $format Format of the date to return. 22 27 * @param string $date Date string to convert. 23 28 * @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. 25 31 */ 26 32 function mysql2date( $format, $date, $translate = true ) { 27 33 if ( empty( $date ) ) { 28 34 return false; 29 35 } 30 36 31 if ( 'G' == $format ) { 32 return strtotime( $date . ' +0000' ); 33 } 37 $datetime = date_create( $date, wp_timezone() ); 34 38 35 $i = strtotime( $date ); 39 if ( false === $datetime ) { 40 return false; 41 } 36 42 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(); 39 46 } 40 47 41 48 if ( $translate ) { 42 return date_i18n( $format, $i ); 43 } else { 44 return gmdate( $format, $i ); 49 return wp_date( $format, $datetime->getTimestamp() ); 45 50 } 51 52 return $datetime->format( $format ); 46 53 } 47 54 48 55 /**