forked from WordPress/wordpress-develop
/
date.php
209 lines (185 loc) · 5.53 KB
/
date.php
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* @group formatting
* @group datetime
*/
class Tests_Formatting_Date extends WP_UnitTestCase {
/**
* Unpatched, this test passes only when Europe/London is not observing DST.
*
* @ticket 20328
*/
function test_get_date_from_gmt_outside_of_dst() {
update_option( 'timezone_string', 'Europe/London' );
$local = '2012-01-01 12:34:56';
$gmt = $local;
$this->assertEquals( $local, get_date_from_gmt( $gmt ) );
}
/**
* Unpatched, this test passes only when Europe/London is observing DST.
*
* @ticket 20328
*/
function test_get_date_from_gmt_during_dst() {
update_option( 'timezone_string', 'Europe/London' );
$gmt = '2012-06-01 12:34:56';
$local = '2012-06-01 13:34:56';
$this->assertEquals( $local, get_date_from_gmt( $gmt ) );
}
/**
* @ticket 20328
*/
function test_get_gmt_from_date_outside_of_dst() {
update_option( 'timezone_string', 'Europe/London' );
$local = '2012-01-01 12:34:56';
$gmt = $local;
$this->assertEquals( $gmt, get_gmt_from_date( $local ) );
}
/**
* @ticket 20328
*/
function test_get_gmt_from_date_during_dst() {
update_option( 'timezone_string', 'Europe/London' );
$local = '2012-06-01 12:34:56';
$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() {
$local = '2012-01-01 12:34:56';
$gmt = $local;
$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' );
$this->assertEqualsWithDelta( strtotime( $gmt ), strtotime( get_gmt_from_date( $local ) ), 2, 'The dates should be equal' );
}
/**
* @ticket 34279
*/
function test_get_gmt_from_date_string_date_no_timezone() {
$local = 'now';
$gmt = gmdate( 'Y-m-d H:i:s' );
$this->assertEqualsWithDelta( strtotime( $gmt ), strtotime( get_gmt_from_date( $local ) ), 2, 'The dates should be equal' );
}
/**
* @ticket 31809
*
* @dataProvider timezone_provider
*/
public function test_gmt_from_date_correct_time( $timezone_string, $gmt_offset ) {
update_option( 'timezone_string', $timezone_string );
update_option( 'gmt_offset', $gmt_offset );
$local = new DateTimeImmutable( 'now', wp_timezone() );
$utc = $local->setTimezone( new DateTimeZone( 'UTC' ) );
$mysql_local = $local->format( 'Y-m-d H:i:s' );
$this->assertEquals( $utc->format( DATE_RFC3339 ), get_gmt_from_date( $mysql_local, DATE_RFC3339 ) );
}
/**
* @ticket 31809
*
* @dataProvider timezone_provider
*/
public function test_date_from_gmt_correct_time( $timezone_string, $gmt_offset ) {
update_option( 'timezone_string', $timezone_string );
update_option( 'gmt_offset', $gmt_offset );
$local = new DateTimeImmutable( 'now', wp_timezone() );
$utc = $local->setTimezone( new DateTimeZone( 'UTC' ) );
$mysql_utc = $utc->format( 'Y-m-d H:i:s' );
$this->assertEquals( $local->format( DATE_RFC3339 ), get_date_from_gmt( $mysql_utc, DATE_RFC3339 ) );
}
/**
* @ticket 31809
*
* @dataProvider timezone_provider
*/
public function test_is8601_to_datetime_correct_time( $timezone_string, $gmt_offset ) {
update_option( 'timezone_string', $timezone_string );
update_option( 'gmt_offset', $gmt_offset );
$format = 'Ymd\TH:i:sO';
$format_no_tz = 'Ymd\TH:i:s';
$local = new DateTimeImmutable( 'now', wp_timezone() );
$utc = $local->setTimezone( new DateTimeZone( 'UTC' ) );
$this->assertEquals(
$local->format( 'Y-m-d H:i:s' ),
iso8601_to_datetime( $local->format( $format ) ),
'Local time from local time.'
);
$this->assertEquals(
$utc->format( 'Y-m-d H:i:s' ),
iso8601_to_datetime( $local->format( $format ), 'gmt' ),
'UTC time from local time.'
);
$this->assertEquals(
$local->format( 'Y-m-d H:i:s' ),
iso8601_to_datetime( $local->format( $format_no_tz ) ),
'Local time from local time w/o timezone.'
);
$this->assertEquals(
$utc->format( 'Y-m-d H:i:s' ),
iso8601_to_datetime( $local->format( $format_no_tz ), 'gmt' ),
'UTC time from local time w/o timezone.'
);
$this->assertEquals(
$local->format( 'Y-m-d H:i:s' ),
iso8601_to_datetime( $utc->format( $format ) ),
'Local time from UTC time.'
);
$this->assertEquals(
$utc->format( 'Y-m-d H:i:s' ),
iso8601_to_datetime( $utc->format( $format ), 'gmt' ),
'UTC time from UTC time.'
);
$this->assertEquals(
$local->format( 'Y-m-d H:i:s' ),
iso8601_to_datetime( $utc->format( $format_no_tz ) . 'Z' ),
'Local time from UTC w/ Z timezone.'
);
$this->assertEquals(
$utc->format( 'Y-m-d H:i:s' ),
iso8601_to_datetime( $utc->format( $format_no_tz ) . 'Z', 'gmt' ),
'UTC time from UTC w/ Z timezone.'
);
}
/**
* Data provider to test different timezone modes.
*
* @return array
*/
public function timezone_provider() {
return array(
array(
'timezone_string' => 'Europe/Kiev',
'gmt_offset' => 3,
),
array(
'timezone_string' => '',
'gmt_offset' => 3,
),
);
}
}