Make WordPress Core

Ticket #24730: wp-timezone.patch

File wp-timezone.patch, 4.2 KB (added by Rarst, 5 years ago)
  • tests/phpunit/tests/date/wpTimezone.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
     1<?php
     2
     3/**
     4 * @group date
     5 * @group datetime
     6 */
     7class Tests_Wp_Timezone extends WP_UnitTestCase {
     8
     9        /**
     10         * @dataProvider timezone_offset_provider
     11         *
     12         * @param float  $gmt_offset Numeric offset from UTC.
     13         * @param string $tz_name    Expected timezone name.
     14         */
     15        public function test_should_convert_gmt_offset( $gmt_offset, $tz_name ) {
     16
     17                delete_option( 'timezone_string' );
     18                update_option( 'gmt_offset', $gmt_offset );
     19
     20                $this->assertEquals( $tz_name, wp_timezone_string() );
     21
     22                $timezone = wp_timezone();
     23
     24                $this->assertEquals( $tz_name, $timezone->getName() );
     25        }
     26
     27        public function test_should_return_timezone_string() {
     28
     29                update_option( 'timezone_string', 'Europe/Kiev' );
     30
     31                $this->assertEquals( 'Europe/Kiev', wp_timezone_string() );
     32
     33                $timezone = wp_timezone();
     34
     35                $this->assertEquals( 'Europe/Kiev', $timezone->getName() );
     36        }
     37
     38        /**
     39         * Data provider to test numeric offset conversion.
     40         *
     41         * @return array
     42         */
     43        public function timezone_offset_provider() {
     44
     45                return [
     46                        [ - 12, '-12:00' ],
     47                        [ - 11.5, '-11:30' ],
     48                        [ - 11, '-11:00' ],
     49                        [ - 10.5, '-10:30' ],
     50                        [ - 10, '-10:00' ],
     51                        [ - 9.5, '-09:30' ],
     52                        [ - 9, '-09:00' ],
     53                        [ - 8.5, '-08:30' ],
     54                        [ - 8, '-08:00' ],
     55                        [ - 7.5, '-07:30' ],
     56                        [ - 7, '-07:00' ],
     57                        [ - 6.5, '-06:30' ],
     58                        [ - 6, '-06:00' ],
     59                        [ - 5.5, '-05:30' ],
     60                        [ - 5, '-05:00' ],
     61                        [ - 4.5, '-04:30' ],
     62                        [ - 4, '-04:00' ],
     63                        [ - 3.5, '-03:30' ],
     64                        [ - 3, '-03:00' ],
     65                        [ - 2.5, '-02:30' ],
     66                        [ - 2, '-02:00' ],
     67                        [ '-1.5', '-01:30' ],
     68                        [ - 1.5, '-01:30' ],
     69                        [ - 1, '-01:00' ],
     70                        [ - 0.5, '-00:30' ],
     71                        [ 0, '+00:00' ],
     72                        [ '0', '+00:00' ],
     73                        [ 0.5, '+00:30' ],
     74                        [ 1, '+01:00' ],
     75                        [ 1.5, '+01:30' ],
     76                        [ '1.5', '+01:30' ],
     77                        [ 2, '+02:00' ],
     78                        [ 2.5, '+02:30' ],
     79                        [ 3, '+03:00' ],
     80                        [ 3.5, '+03:30' ],
     81                        [ 4, '+04:00' ],
     82                        [ 4.5, '+04:30' ],
     83                        [ 5, '+05:00' ],
     84                        [ 5.5, '+05:30' ],
     85                        [ 5.75, '+05:45' ],
     86                        [ 6, '+06:00' ],
     87                        [ 6.5, '+06:30' ],
     88                        [ 7, '+07:00' ],
     89                        [ 7.5, '+07:30' ],
     90                        [ 8, '+08:00' ],
     91                        [ 8.5, '+08:30' ],
     92                        [ 8.75, '+08:45' ],
     93                        [ 9, '+09:00' ],
     94                        [ 9.5, '+09:30' ],
     95                        [ 10, '+10:00' ],
     96                        [ 10.5, '+10:30' ],
     97                        [ 11, '+11:00' ],
     98                        [ 11.5, '+11:30' ],
     99                        [ 12, '+12:00' ],
     100                        [ 12.75, '+12:45' ],
     101                        [ 13, '+13:00' ],
     102                        [ 13.75, '+13:45' ],
     103                        [ 14, '+14:00' ],
     104                ];
     105        }
     106}
     107
     108
  • src/wp-includes/functions.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    194194        return $j;
    195195}
    196196
     197/**
     198 * Retrieve the current settings timezone as string.
     199 *
     200 * Return might be a PHP timezone string or a ±NN:NN offset.
     201 *
     202 * @return string Timezone string.
     203 */
     204function wp_timezone_string() {
     205
     206        $timezone_string = get_option( 'timezone_string' );
     207
     208        if ( ! empty( $timezone_string ) ) {
     209                return $timezone_string;
     210        }
     211
     212        $offset  = (float) get_option( 'gmt_offset' );
     213        $hours   = (int) $offset;
     214        $minutes = ( $offset - $hours );
     215
     216        $sign      = ( $offset < 0 ) ? '-' : '+';
     217        $abs_hour  = abs( $hours );
     218        $abs_mins  = abs( $minutes * 60 );
     219        $tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins );
     220
     221        return $tz_offset;
     222}
     223
     224/**
     225 * Retrieve the current settings timezone as `DateTimeZone` object.
     226 *
     227 * Timezone might be based on a PHP timezone string or a ±NN:NN offset.
     228 *
     229 * @return DateTimeZone Timezone object.
     230 */
     231function wp_timezone() {
     232
     233        return new DateTimeZone( wp_timezone_string() );
     234}
     235
    197236/**
    198237 * Determines if the date should be declined.
    199238 *