forked from WordPress/wordpress-develop
/
dateI18n.php
201 lines (160 loc) · 6.57 KB
/
dateI18n.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
<?php
/**
* @group date
* @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->assertEqualsWithDelta( $wp_timestamp, date_i18n( 'U', 'invalid' ), 5 );
}
/**
* @ticket 28636
*/
public function test_should_handle_zero_timestamp() {
$timezone = 'Europe/Kiev';
update_option( 'timezone_string', $timezone );
$datetime = DateTimeImmutable::createFromFormat(
'Y-m-d H:i:s',
'1970-01-01 00:00:00',
new DateTimeZone( $timezone )
);
$rfc3339 = $datetime->format( DATE_RFC3339 );
$this->assertEquals( 0, date_i18n( 'U', 0 ) );
$this->assertEquals( $rfc3339, date_i18n( DATE_RFC3339, 0 ) );
}
public function test_should_format_date() {
$this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s' ) ), strtotime( date_i18n( 'Y-m-d H:i:s' ) ), 2, 'The dates should be equal' );
}
public function test_should_use_custom_timestamp() {
$this->assertEquals( '2012-12-01 00:00:00', date_i18n( 'Y-m-d H:i:s', strtotime( '2012-12-01 00:00:00' ) ) );
}
public function test_date_should_be_in_gmt() {
$this->assertEqualsWithDelta( strtotime( gmdate( DATE_RFC3339 ) ), strtotime( date_i18n( DATE_RFC3339, false, true ) ), 2, 'The dates should be equal' );
}
public function test_custom_timezone_setting() {
update_option( 'timezone_string', 'America/Regina' );
$this->assertEqualsWithDelta( strtotime( gmdate( 'Y-m-d H:i:s', time() + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ), strtotime( date_i18n( 'Y-m-d H:i:s' ) ), 2, 'The dates should be equal' );
}
public function test_date_should_be_in_gmt_with_custom_timezone_setting() {
update_option( 'timezone_string', 'America/Regina' );
$this->assertEqualsWithDelta( strtotime( gmdate( DATE_RFC3339 ) ), strtotime( date_i18n( DATE_RFC3339, false, true ) ), 2, 'The dates should be equal' );
}
public function test_date_should_be_in_gmt_with_custom_timezone_setting_and_timestamp() {
update_option( 'timezone_string', 'America/Regina' );
$this->assertEquals( '2012-12-01 00:00:00', date_i18n( 'Y-m-d H:i:s', strtotime( '2012-12-01 00:00:00' ) ) );
}
public function test_adjusts_format_based_on_locale() {
$original_locale = $GLOBALS['wp_locale'];
/* @var WP_Locale $locale */
$locale = clone $GLOBALS['wp_locale'];
$locale->weekday[6] = 'Saturday_Translated';
$locale->weekday_abbrev['Saturday_Translated'] = 'Sat_Translated';
$locale->month[12] = 'December_Translated';
$locale->month_abbrev['December_Translated'] = 'Dec_Translated';
$locale->meridiem['am'] = 'am_Translated';
$locale->meridiem['AM'] = 'AM_Translated';
$GLOBALS['wp_locale'] = $locale;
$expected = 'Saturday_Translated (Sat_Translated) 01 December_Translated (Dec_Translated) 00:00:00 am_Translated AM_Translated';
$actual = date_i18n( 'l (D) d F (M) H:i:s a A', strtotime( '2012-12-01 00:00:00' ) );
// Restore original locale.
$GLOBALS['wp_locale'] = $original_locale;
$this->assertEquals( $expected, $actual );
}
public function test_adjusts_format_based_on_timezone_string() {
update_option( 'timezone_string', 'America/Regina' );
$this->assertEquals( '2012-12-01 00:00:00 CST -06:00 America/Regina', date_i18n( 'Y-m-d H:i:s T P e', strtotime( '2012-12-01 00:00:00' ) ) );
}
/**
* @ticket 34835
*/
public function test_gmt_offset_should_output_correct_timezone() {
$timezone_formats = 'P I O T Z e';
$timezone_string = 'America/Regina';
$datetimezone = new DateTimeZone( $timezone_string );
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' ) );
$this->assertEquals( $datetime->format( $timezone_formats ), date_i18n( $timezone_formats ) );
}
/**
* @ticket 20973
*
* @dataProvider data_formats
*/
public function test_date_i18n_handles_shorthand_formats( $short, $full ) {
update_option( 'timezone_string', 'America/Regina' );
$this->assertEqualsWithDelta( strtotime( date_i18n( $full ) ), strtotime( date_i18n( $short ) ), 2, 'The dates should be equal' );
$this->assertEquals( $short, date_i18n( '\\' . $short ) );
}
public function data_formats() {
return array(
array(
'c',
'Y-m-d\TH:i:sP',
),
array(
'r',
'D, d M Y H:i:s O',
),
);
}
/**
* @ticket 25768
*/
public function test_should_return_wp_timestamp() {
update_option( 'timezone_string', 'Europe/Kiev' );
$datetime = new DateTimeImmutable( 'now', wp_timezone() );
$timestamp = $datetime->getTimestamp();
$wp_timestamp = $timestamp + $datetime->getOffset();
$this->assertEqualsWithDelta( $wp_timestamp, date_i18n( 'U' ), 2, 'The dates should be equal' );
$this->assertEqualsWithDelta( $timestamp, date_i18n( 'U', false, true ), 2, 'The dates should be equal' );
$this->assertEquals( $wp_timestamp, date_i18n( 'U', $wp_timestamp ) );
}
/**
* @ticket 43530
*/
public function test_swatch_internet_time_with_wp_timestamp() {
update_option( 'timezone_string', 'America/Regina' );
$this->assertEquals( gmdate( 'B' ), date_i18n( 'B' ) );
}
/**
* @ticket 25768
*/
public function test_should_handle_escaped_formats() {
$format = 'D | \D | \\D | \\\D | \\\\D | \\\\\D | \\\\\\D';
$this->assertEquals( gmdate( $format ), date_i18n( $format ) );
}
/**
* @ticket 25768
*
* @dataProvider dst_times
*
* @param string $time Time to test in Y-m-d H:i:s format.
* @param string $timezone PHP timezone string to use.
*/
public function test_should_handle_dst( $time, $timezone ) {
update_option( 'timezone_string', $timezone );
$timezone = new DateTimeZone( $timezone );
$datetime = new DateTime( $time, $timezone );
$wp_timestamp = strtotime( $time );
$format = 'I ' . DATE_RFC3339;
$this->assertEquals( $datetime->format( $format ), date_i18n( $format, $wp_timestamp ) );
}
public function dst_times() {
return array(
'Before DST start' => array( '2019-03-31 02:59:00', 'Europe/Kiev' ),
'After DST start' => array( '2019-03-31 04:01:00', 'Europe/Kiev' ),
'Before DST end' => array( '2019-10-27 02:59:00', 'Europe/Kiev' ),
'After DST end' => array( '2019-10-27 04:01:00', 'Europe/Kiev' ),
);
}
}