mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-12 12:50:28 +00:00
Date/Time: Revamp mysql2date() to use wp_date() and handle invalid input in a consistent manner.
Add unit tests, improve documentation. Props Rarst, pbearne. Fixes #28992. git-svn-id: https://develop.svn.wordpress.org/trunk@45908 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group date
|
||||
* @group datetime
|
||||
*/
|
||||
class Tests_Date_mysql2date extends WP_UnitTestCase {
|
||||
|
||||
function tearDown() {
|
||||
date_default_timezone_set( 'UTC' );
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 28992
|
||||
*/
|
||||
function test_mysql2date_should_format_time() {
|
||||
$timezone = 'Europe/Kiev';
|
||||
update_option( 'timezone_string', $timezone );
|
||||
$datetime = new DateTime( 'now', new DateTimeZone( $timezone ) );
|
||||
$rfc3339 = $datetime->format( DATE_RFC3339 );
|
||||
$mysql = $datetime->format( 'Y-m-d H:i:s' );
|
||||
|
||||
$this->assertEquals( $rfc3339, mysql2date( DATE_RFC3339, $mysql ) );
|
||||
$this->assertEquals( $rfc3339, mysql2date( DATE_RFC3339, $mysql, false ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 28992
|
||||
*/
|
||||
function test_mysql2date_should_format_time_with_changed_time_zone() {
|
||||
$timezone = 'Europe/Kiev';
|
||||
date_default_timezone_set( $timezone );
|
||||
update_option( 'timezone_string', $timezone );
|
||||
$datetime = new DateTime( 'now', new DateTimeZone( $timezone ) );
|
||||
$rfc3339 = $datetime->format( DATE_RFC3339 );
|
||||
$mysql = $datetime->format( 'Y-m-d H:i:s' );
|
||||
|
||||
$this->assertEquals( $rfc3339, mysql2date( DATE_RFC3339, $mysql ) );
|
||||
$this->assertEquals( $rfc3339, mysql2date( DATE_RFC3339, $mysql, false ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 28992
|
||||
*/
|
||||
function test_mysql2date_should_return_wp_timestamp() {
|
||||
$timezone = 'Europe/Kiev';
|
||||
update_option( 'timezone_string', $timezone );
|
||||
$datetime = new DateTime( 'now', new DateTimeZone( $timezone ) );
|
||||
$wp_timestamp = $datetime->getTimestamp() + $datetime->getOffset();
|
||||
$mysql = $datetime->format( 'Y-m-d H:i:s' );
|
||||
|
||||
$this->assertEquals( $wp_timestamp, mysql2date( 'U', $mysql, false ) );
|
||||
$this->assertEquals( $wp_timestamp, mysql2date( 'G', $mysql, false ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 28992
|
||||
*/
|
||||
function test_mysql2date_should_return_unix_timestamp_for_gmt_time() {
|
||||
$timezone = 'Europe/Kiev';
|
||||
update_option( 'timezone_string', $timezone );
|
||||
$datetime = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
|
||||
$timestamp = $datetime->getTimestamp();
|
||||
$mysql = $datetime->format( 'Y-m-d H:i:s' );
|
||||
|
||||
$this->assertEquals( $timestamp, mysql2date( 'U', $mysql, false ) );
|
||||
$this->assertEquals( $timestamp, mysql2date( 'G', $mysql, false ) );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user