Date/Time: Restore the previous behavior of date_i18n()
where invalid input would result in current time.
Make `wp_date()` return `false` on invalid timestamp input, for consistency with upstream PHP `date()` function. Props Rarst. Fixes #28636. git-svn-id: https://develop.svn.wordpress.org/trunk@45914 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
07ec966f17
commit
56d4e7fb86
@ -163,8 +163,12 @@ function wp_timezone() {
|
||||
* @return string The date, translated if locale specifies it.
|
||||
*/
|
||||
function date_i18n( $format, $timestamp_with_offset = false, $gmt = false ) {
|
||||
$timestamp = $timestamp_with_offset;
|
||||
|
||||
// If timestamp is omitted it should be current time (summed with offset, unless `$gmt` is true).
|
||||
$timestamp = $timestamp_with_offset ? $timestamp_with_offset : current_time( 'timestamp', $gmt );
|
||||
if ( ! is_numeric( $timestamp ) ) {
|
||||
$timestamp = current_time( 'timestamp', $gmt );
|
||||
}
|
||||
|
||||
/*
|
||||
* This is a legacy implementation quirk that the returned timestamp is also with offset.
|
||||
@ -218,13 +222,15 @@ function date_i18n( $format, $timestamp_with_offset = false, $gmt = false ) {
|
||||
* @param int $timestamp Optional. Unix timestamp. Defaults to current time.
|
||||
* @param DateTimeZone $timezone Optional. Timezone to output result in. Defaults to timezone
|
||||
* from site settings.
|
||||
* @return string The date, translated if locale specifies it.
|
||||
* @return string|false The date, translated if locale specifies it. False on invalid timestamp input.
|
||||
*/
|
||||
function wp_date( $format, $timestamp = null, $timezone = null ) {
|
||||
global $wp_locale;
|
||||
|
||||
if ( ! $timestamp ) {
|
||||
if ( null === $timestamp ) {
|
||||
$timestamp = time();
|
||||
} elseif ( ! is_numeric( $timestamp ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( ! $timezone ) {
|
||||
|
@ -4,7 +4,7 @@
|
||||
* @group date
|
||||
* @group datetime
|
||||
*/
|
||||
class Tests_Date_CurrentTime extends WP_UnitTestCase {
|
||||
class Tests_Date_Current_Time extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* @ticket 37440
|
||||
|
@ -5,6 +5,20 @@
|
||||
* @group datetime
|
||||
*/
|
||||
class Tests_Date_I18n extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* @ticket 28636
|
||||
*/
|
||||
public function test_should_return_current_time_on_invalid_timestamp() {
|
||||
$timezone = 'Europe/Kiev';
|
||||
update_option( 'timezone_string', $timezone );
|
||||
|
||||
$datetime = new DateTime( 'now', new DateTimeZone( $timezone ) );
|
||||
$wp_timestamp = $datetime->getTimestamp() + $datetime->getOffset();
|
||||
|
||||
$this->assertEquals( $wp_timestamp, date_i18n( 'U', 'invalid' ), '', 5 );
|
||||
}
|
||||
|
||||
public function test_should_format_date() {
|
||||
$this->assertEquals( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( date_i18n( 'Y-m-d H:i:s' ) ), 'The dates should be equal', 2 );
|
||||
}
|
||||
@ -74,6 +88,7 @@ class Tests_Date_I18n extends WP_UnitTestCase {
|
||||
update_option( 'timezone_string', '' );
|
||||
$offset = $datetimezone->getOffset( new DateTime() ) / 3600;
|
||||
update_option( 'gmt_offset', $offset );
|
||||
|
||||
$datetime = new DateTime( 'now', $datetimezone );
|
||||
$datetime = new DateTime( $datetime->format( 'P' ) );
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
* @group date
|
||||
* @group datetime
|
||||
*/
|
||||
class Tests_Date_TheDate extends WP_UnitTestCase {
|
||||
class Tests_Date_The_Date extends WP_UnitTestCase {
|
||||
|
||||
/** @var array $hooks_called Count of hooks called. */
|
||||
protected $hooks_called = array(
|
||||
|
15
tests/phpunit/tests/date/wpDate.php
Normal file
15
tests/phpunit/tests/date/wpDate.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group date
|
||||
* @group datetime
|
||||
*/
|
||||
class Tests_Date_WP_Date extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* @ticket 28636
|
||||
*/
|
||||
public function test_should_return_false_on_invalid_timestamp() {
|
||||
$this->assertFalse( wp_date( DATE_RFC3339, 'invalid' ) );
|
||||
}
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
* @group date
|
||||
* @group datetime
|
||||
*/
|
||||
class Tests_WP_Timezone extends WP_UnitTestCase {
|
||||
class Tests_Date_WP_Timezone extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* @ticket 24730
|
||||
|
Loading…
Reference in New Issue
Block a user