forked from WordPress/wordpress-develop
/
currentTime.php
119 lines (95 loc) · 4.85 KB
/
currentTime.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
<?php
/**
* @group date
* @group datetime
*/
class Tests_Date_Current_Time extends WP_UnitTestCase {
/**
* @ticket 34378
*/
public function test_current_time_with_date_format_string() {
update_option( 'gmt_offset', 6 );
$format = 'F j, Y, g:i a';
$timestamp = time();
$wp_timestamp = $timestamp + 6 * HOUR_IN_SECONDS;
$this->assertEqualsWithDelta( strtotime( gmdate( $format ) ), strtotime( current_time( $format, true ) ), 2, 'The dates should be equal' );
$this->assertEqualsWithDelta( strtotime( gmdate( $format, $wp_timestamp ) ), strtotime( current_time( $format ) ), 2, 'The dates should be equal' );
}
/**
* @ticket 34378
*/
public function test_current_time_with_mysql_format() {
update_option( 'gmt_offset', 6 );
$format = 'Y-m-d H:i:s';
$timestamp = time();
$wp_timestamp = $timestamp + 6 * HOUR_IN_SECONDS;
$this->assertEqualsWithDelta( strtotime( gmdate( $format ) ), strtotime( current_time( 'mysql', true ) ), 2, 'The dates should be equal' );
$this->assertEqualsWithDelta( strtotime( gmdate( $format, $wp_timestamp ) ), strtotime( current_time( 'mysql' ) ), 2, 'The dates should be equal' );
}
/**
* @ticket 34378
*/
public function test_current_time_with_timestamp() {
update_option( 'gmt_offset', 6 );
$timestamp = time();
$wp_timestamp = $timestamp + 6 * HOUR_IN_SECONDS;
// phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.RequestedUTC
$this->assertEqualsWithDelta( $timestamp, current_time( 'timestamp', true ), 2, 'The dates should be equal' );
// phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
$this->assertEqualsWithDelta( $wp_timestamp, current_time( 'timestamp' ), 2, 'The dates should be equal' );
}
/**
* @ticket 37440
*/
public function test_should_work_with_changed_timezone() {
$format = 'Y-m-d H:i:s';
$timezone_string = 'America/Regina';
update_option( 'timezone_string', $timezone_string );
$datetime = new DateTime( 'now', new DateTimeZone( $timezone_string ) );
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
date_default_timezone_set( $timezone_string );
$current_time_custom_timezone_gmt = current_time( $format, true );
$current_time_custom_timezone = current_time( $format );
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.timezone_change_date_default_timezone_set
date_default_timezone_set( 'UTC' );
$current_time_gmt = current_time( $format, true );
$current_time = current_time( $format );
$this->assertEqualsWithDelta( strtotime( gmdate( $format ) ), strtotime( $current_time_custom_timezone_gmt ), 2, 'The dates should be equal' );
$this->assertEqualsWithDelta( strtotime( $datetime->format( $format ) ), strtotime( $current_time_custom_timezone ), 2, 'The dates should be equal' );
$this->assertEqualsWithDelta( strtotime( gmdate( $format ) ), strtotime( $current_time_gmt ), 2, 'The dates should be equal' );
$this->assertEqualsWithDelta( strtotime( $datetime->format( $format ) ), strtotime( $current_time ), 2, 'The dates should be equal' );
}
/**
* @ticket 40653
*/
public function test_should_return_wp_timestamp() {
update_option( 'timezone_string', 'Europe/Kiev' );
$timestamp = time();
$datetime = new DateTime( '@' . $timestamp );
$datetime->setTimezone( wp_timezone() );
$wp_timestamp = $timestamp + $datetime->getOffset();
// phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.RequestedUTC
$this->assertEqualsWithDelta( $timestamp, current_time( 'timestamp', true ), 2, 'The dates should be equal' );
// phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.RequestedUTC
$this->assertEqualsWithDelta( $timestamp, current_time( 'U', true ), 2, 'The dates should be equal' );
// phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
$this->assertEqualsWithDelta( $wp_timestamp, current_time( 'timestamp' ), 2, 'The dates should be equal' );
// phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
$this->assertEqualsWithDelta( $wp_timestamp, current_time( 'U' ), 2, 'The dates should be equal' );
// phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested
$this->assertIsInt( current_time( 'timestamp' ) );
}
/**
* @ticket 40653
*/
public function test_should_return_correct_local_time() {
update_option( 'timezone_string', 'Europe/Kiev' );
$timestamp = time();
$datetime_local = new DateTime( '@' . $timestamp );
$datetime_local->setTimezone( wp_timezone() );
$datetime_utc = new DateTime( '@' . $timestamp );
$datetime_utc->setTimezone( new DateTimeZone( 'UTC' ) );
$this->assertEqualsWithDelta( strtotime( $datetime_local->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C ) ), 2, 'The dates should be equal' );
$this->assertEqualsWithDelta( strtotime( $datetime_utc->format( DATE_W3C ) ), strtotime( current_time( DATE_W3C, true ) ), 2, 'The dates should be equal' );
}
}