Formatting: allow date strings to be passed to get_gmt_from_date()
, instead of requiring 'Y-m-d H:i:s'
.
Adds unit tests. Props pbearne. Fixes #34279. git-svn-id: https://develop.svn.wordpress.org/trunk@35284 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
895cf207b5
commit
ea7e8314d2
@ -2587,13 +2587,19 @@ function get_gmt_from_date( $string, $format = 'Y-m-d H:i:s' ) {
|
||||
$tz = get_option( 'timezone_string' );
|
||||
if ( $tz ) {
|
||||
$datetime = date_create( $string, new DateTimeZone( $tz ) );
|
||||
if ( ! $datetime )
|
||||
if ( ! $datetime ) {
|
||||
return gmdate( $format, 0 );
|
||||
}
|
||||
$datetime->setTimezone( new DateTimeZone( 'UTC' ) );
|
||||
$string_gmt = $datetime->format( $format );
|
||||
} else {
|
||||
if ( ! preg_match( '#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches ) )
|
||||
if ( ! preg_match( '#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches ) ) {
|
||||
$datetime = strtotime( $string );
|
||||
if ( false === $datetime ) {
|
||||
return gmdate( $format, 0 );
|
||||
}
|
||||
return gmdate( $format, $datetime );
|
||||
}
|
||||
$string_time = gmmktime( $matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1] );
|
||||
$string_gmt = gmdate( $format, $string_time - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
|
||||
}
|
||||
@ -3879,7 +3885,7 @@ function sanitize_option( $option, $value ) {
|
||||
* @since 4.4.0
|
||||
*
|
||||
* @param mixed $value The array, object, or scalar.
|
||||
* @param callable $function The function to map onto $value.
|
||||
* @param callable $callback The function to map onto $value.
|
||||
* @return The value with the callback applied to all non-arrays and non-objects inside it.
|
||||
*/
|
||||
function map_deep( $value, $callback ) {
|
||||
|
@ -47,4 +47,50 @@ class Tests_Formatting_Date extends WP_UnitTestCase {
|
||||
$gmt = '2012-06-01 11:34:56';
|
||||
$this->assertEquals( $gmt, get_gmt_from_date( $local ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 34279
|
||||
*/
|
||||
function test_get_date_and_time_from_gmt_no_timezone() {
|
||||
$gmt = $local = '2012-01-01 12:34:56';
|
||||
$this->assertEquals( $gmt, get_date_from_gmt( $local ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 34279
|
||||
*/
|
||||
function test_get_gmt_from_date_no_timezone() {
|
||||
$gmt = '2012-12-01 00:00:00';
|
||||
$date = '2012-12-01';
|
||||
$this->assertEquals( $gmt, get_gmt_from_date( $date ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 34279
|
||||
*/
|
||||
function test_get_gmt_from_date_short_date() {
|
||||
update_option( 'timezone_string', 'Europe/London' );
|
||||
$local = '2012-12-01';
|
||||
$gmt = '2012-12-01 00:00:00';
|
||||
$this->assertEquals( $gmt, get_gmt_from_date( $local ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 34279
|
||||
*/
|
||||
function test_get_gmt_from_date_string_date() {
|
||||
update_option( 'timezone_string', 'Europe/London' );
|
||||
$local = 'now';
|
||||
$gmt = gmdate( 'Y-m-d H:i:s', strtotime( 'now' ) );
|
||||
$this->assertEquals( $gmt, get_gmt_from_date( $local ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 34279
|
||||
*/
|
||||
function test_get_gmt_from_date_string_date_no_timezone() {
|
||||
$local = 'now';
|
||||
$gmt = gmdate( 'Y-m-d H:i:s', strtotime( 'now' ) );
|
||||
$this->assertEquals( $gmt, get_gmt_from_date( $local ) );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user