forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql2date.php
More file actions
89 lines (76 loc) · 2.74 KB
/
mysql2date.php
File metadata and controls
89 lines (76 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* @group date
* @group datetime
* @covers ::mysql2date
*/
class Tests_Date_mysql2date extends WP_UnitTestCase {
function tearDown() {
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
date_default_timezone_set( 'UTC' );
parent::tearDown();
}
/**
* @ticket 28310
*/
function test_mysql2date_returns_false_with_no_date() {
$this->assertFalse( mysql2date( 'F j, Y H:i:s', '' ) );
}
/**
* @ticket 28310
*/
function test_mysql2date_returns_gmt_or_unix_timestamp() {
$this->assertSame( 441013392, mysql2date( 'G', '1983-12-23 07:43:12' ) );
$this->assertSame( 441013392, mysql2date( 'U', '1983-12-23 07:43:12' ) );
}
/**
* @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->assertSame( $rfc3339, mysql2date( DATE_RFC3339, $mysql ) );
$this->assertSame( $rfc3339, mysql2date( DATE_RFC3339, $mysql, false ) );
}
/**
* @ticket 28992
*/
function test_mysql2date_should_format_time_with_changed_time_zone() {
$timezone = 'Europe/Kiev';
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
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->assertSame( $rfc3339, mysql2date( DATE_RFC3339, $mysql ) );
$this->assertSame( $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->assertSame( $wp_timestamp, mysql2date( 'U', $mysql, false ) );
$this->assertSame( $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->assertSame( $timestamp, mysql2date( 'U', $mysql, false ) );
$this->assertSame( $timestamp, mysql2date( 'G', $mysql, false ) );
}
}