From 55bc482d24c84d623732445405df534f6a0f941e Mon Sep 17 00:00:00 2001
From: Andrey Savchenko <contact@rarst.net>
Date: Thu, 28 Jun 2018 13:29:19 +0300
Subject: [PATCH] Implemented gmt_offset support in date_i18n().
---
src/wp-includes/functions.php | 30 ++++++++++++++++++++++++++++++
tests/phpunit/tests/date/dateI18n.php | 13 +++++++++++++
2 files changed, 43 insertions(+)
diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php
index 3c4a301298..b4df760ec6 100644
a
|
b
|
function date_i18n( $dateformatstring, $timestamp_with_offset = false, $gmt = fa |
138 | 138 | $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) - 1 ); |
139 | 139 | } |
140 | 140 | } |
| 141 | } else { |
| 142 | $offset = get_option( 'gmt_offset' ); |
| 143 | foreach ( $timezone_formats as $timezone_format ) { |
| 144 | if ( 'I' === $timezone_format ) { |
| 145 | continue; |
| 146 | } |
| 147 | |
| 148 | if ( false !== strpos( $dateformatstring, $timezone_format ) ) { |
| 149 | if ( 'Z' === $timezone_format ) { |
| 150 | $formatted = (string) ( $offset * HOUR_IN_SECONDS ); |
| 151 | } else { |
| 152 | $prefix = ''; |
| 153 | $hours = (int) $offset; |
| 154 | $separator = ''; |
| 155 | $minutes = abs( ( $offset - $hours ) * 60 ); |
| 156 | |
| 157 | if ( 'T' === $timezone_format ) { |
| 158 | $prefix = 'GMT'; |
| 159 | } elseif ( 'e' === $timezone_format || 'P' === $timezone_format ) { |
| 160 | $separator = ':'; |
| 161 | } |
| 162 | |
| 163 | $formatted = sprintf( '%s%+03d%s%02d', $prefix, $hours, $separator, $minutes ); |
| 164 | } |
| 165 | |
| 166 | $dateformatstring = ' ' . $dateformatstring; |
| 167 | $dateformatstring = preg_replace( "/([^\\\])$timezone_format/", "\\1" . backslashit( $formatted ), $dateformatstring ); |
| 168 | $dateformatstring = substr( $dateformatstring, 1 ); |
| 169 | } |
| 170 | } |
141 | 171 | } |
142 | 172 | } |
143 | 173 | $j = @date( $dateformatstring, $i ); |
diff --git a/tests/phpunit/tests/date/dateI18n.php b/tests/phpunit/tests/date/dateI18n.php
index e92f58de6f..91bf511245 100644
a
|
b
|
public function test_adjusts_format_based_on_timezone_string() { |
67 | 67 | |
68 | 68 | $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' ) ) ); |
69 | 69 | } |
| 70 | |
| 71 | public function test_gmt_offset_should_output_correct_timezone() { |
| 72 | $timezone_formats = 'P I O T Z e'; |
| 73 | $timezone_string = 'America/Regina'; |
| 74 | $datetimezone = new DateTimeZone( $timezone_string ); |
| 75 | update_option( 'timezone_string', '' ); |
| 76 | $offset = $datetimezone->getOffset( new DateTime() ) / 3600; |
| 77 | update_option( 'gmt_offset', $offset ); |
| 78 | $datetime = new DateTime( 'now', $datetimezone ); |
| 79 | $datetime = new DateTime( $datetime->format( 'P' ) ); |
| 80 | |
| 81 | $this->assertEquals( $datetime->format( $timezone_formats ), date_i18n( $timezone_formats ) ); |
| 82 | } |
70 | 83 | } |