Make WordPress Core


Ignore:
Timestamp:
08/23/2019 11:02:51 AM (7 years ago)
Author:
SergeyBiryukov
Message:

Date/Time: Rewrite and simplify get_gmt_from_date(), get_date_from_gmt(), and iso8601_to_datetime() using wp_timezone().

Improve unit test coverage.

Props Rarst, goodevilgenius.
Fixes #31809.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/formatting/date.php

    r45588 r45887  
    9797                $this->assertEquals( strtotime( $gmt ), strtotime( get_gmt_from_date( $local ) ), 'The dates should be equal', 2 );
    9898        }
     99
     100        /**
     101         * @ticket 31809
     102         *
     103         * @dataProvider timezone_provider
     104         */
     105        public function test_gmt_from_date_correct_time( $timezone_string, $gmt_offset ) {
     106                update_option( 'timezone_string', $timezone_string );
     107                update_option( 'gmt_offset', $gmt_offset );
     108
     109                $local       = new DateTimeImmutable( 'now', wp_timezone() );
     110                $utc         = $local->setTimezone( new DateTimeZone( 'UTC' ) );
     111                $mysql_local = $local->format( 'Y-m-d H:i:s' );
     112
     113                $this->assertEquals( $utc->format( DATE_RFC3339 ), get_gmt_from_date( $mysql_local, DATE_RFC3339 ) );
     114        }
     115
     116        /**
     117         * @ticket 31809
     118         *
     119         * @dataProvider timezone_provider
     120         */
     121        public function test_date_from_gmt_correct_time( $timezone_string, $gmt_offset ) {
     122                update_option( 'timezone_string', $timezone_string );
     123                update_option( 'gmt_offset', $gmt_offset );
     124
     125                $local     = new DateTimeImmutable( 'now', wp_timezone() );
     126                $utc       = $local->setTimezone( new DateTimeZone( 'UTC' ) );
     127                $mysql_utc = $utc->format( 'Y-m-d H:i:s' );
     128
     129                $this->assertEquals( $local->format( DATE_RFC3339 ), get_date_from_gmt( $mysql_utc, DATE_RFC3339 ) );
     130        }
     131
     132        /**
     133         * @ticket 31809
     134         *
     135         * @dataProvider timezone_provider
     136         */
     137        public function test_is8601_to_datetime_correct_time( $timezone_string, $gmt_offset ) {
     138                update_option( 'timezone_string', $timezone_string );
     139                update_option( 'gmt_offset', $gmt_offset );
     140
     141                $format       = 'Ymd\TH:i:sO';
     142                $format_no_tz = 'Ymd\TH:i:s';
     143
     144                $local = new DateTimeImmutable( 'now', wp_timezone() );
     145                $utc   = $local->setTimezone( new DateTimeZone( 'UTC' ) );
     146
     147                $this->assertEquals(
     148                        $local->format( 'Y-m-d H:i:s' ),
     149                        iso8601_to_datetime( $local->format( $format ) ),
     150                        'Local time from local time.'
     151                );
     152                $this->assertEquals(
     153                        $utc->format( 'Y-m-d H:i:s' ),
     154                        iso8601_to_datetime( $local->format( $format ), 'gmt' ),
     155                        'UTC time from local time.'
     156                );
     157
     158                $this->assertEquals(
     159                        $local->format( 'Y-m-d H:i:s' ),
     160                        iso8601_to_datetime( $local->format( $format_no_tz ) ),
     161                        'Local time from local time w/o timezone.'
     162                );
     163                $this->assertEquals(
     164                        $utc->format( 'Y-m-d H:i:s' ),
     165                        iso8601_to_datetime( $local->format( $format_no_tz ), 'gmt' ),
     166                        'UTC time from local time w/o timezone.'
     167                );
     168
     169                $this->assertEquals(
     170                        $local->format( 'Y-m-d H:i:s' ),
     171                        iso8601_to_datetime( $utc->format( $format ) ),
     172                        'Local time from UTC time.'
     173                );
     174                $this->assertEquals(
     175                        $utc->format( 'Y-m-d H:i:s' ),
     176                        iso8601_to_datetime( $utc->format( $format ), 'gmt' ),
     177                        'UTC time from UTC time.'
     178                );
     179
     180                $this->assertEquals(
     181                        $local->format( 'Y-m-d H:i:s' ),
     182                        iso8601_to_datetime( $utc->format( $format_no_tz ) . 'Z' ),
     183                        'Local time from UTC w/ Z timezone.'
     184                );
     185                $this->assertEquals(
     186                        $utc->format( 'Y-m-d H:i:s' ),
     187                        iso8601_to_datetime( $utc->format( $format_no_tz ) . 'Z', 'gmt' ),
     188                        'UTC time from UTC w/ Z timezone.'
     189                );
     190        }
     191
     192        /**
     193         * Data provider to test different timezone modes.
     194         *
     195         * @return array
     196         */
     197        public function timezone_provider() {
     198                return array(
     199                        array(
     200                                'timezone_string' => 'Europe/Kiev',
     201                                'gmt_offset'      => 3,
     202                        ),
     203                        array(
     204                                'timezone_string' => '',
     205                                'gmt_offset'      => 3,
     206                        ),
     207                );
     208        }
    99209}
Note: See TracChangeset for help on using the changeset viewer.