Make WordPress Core


Ignore:
Timestamp:
08/23/2019 12:56:21 AM (6 years ago)
Author:
SergeyBiryukov
Message:

Date/Time: Rewrite and simplify date_i18n() using wp_timezone() to address multiple issues with certain date formats and timezones, while preserving some extra handling for legacy use cases.

Improve unit test coverage.

Props Rarst, remcotolsma, raubvogel.
Fixes #25768.

File:
1 edited

Legend:

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

    r45424 r45882  
    1616    public function test_date_should_be_in_gmt() {
    1717        $this->assertEquals( strtotime( gmdate( DATE_RFC3339 ) ), strtotime( date_i18n( DATE_RFC3339, false, true ) ), 'The dates should be equal', 2 );
    18     }
    19 
    20     public function test_custom_timestamp_ignores_gmt_setting() {
    21         $this->assertEquals( '2012-12-01 00:00:00', date_i18n( 'Y-m-d H:i:s', strtotime( '2012-12-01 00:00:00' ) ) );
    2218    }
    2319
     
    8682
    8783    /**
     84     * @ticket 20973
     85     *
    8886     * @dataProvider data_formats
    89      * @ticket 20973
    9087     */
    9188    public function test_date_i18n_handles_shorthand_formats( $short, $full ) {
     
    108105        );
    109106    }
     107
     108    /**
     109     * @ticket 25768
     110     */
     111    public function test_should_return_wp_timestamp() {
     112        update_option( 'timezone_string', 'Europe/Kiev' );
     113
     114        $datetime     = new DateTimeImmutable( 'now', wp_timezone() );
     115        $timestamp    = $datetime->getTimestamp();
     116        $wp_timestamp = $timestamp + $datetime->getOffset();
     117
     118        $this->assertEquals( $wp_timestamp, date_i18n( 'U' ), 2 );
     119        $this->assertEquals( $timestamp, date_i18n( 'U', false, true ), 2 );
     120        $this->assertEquals( $wp_timestamp, date_i18n( 'U', $wp_timestamp ) );
     121    }
     122
     123    /**
     124     * @ticket 43530
     125     */
     126    public function test_swatch_internet_time_with_wp_timestamp() {
     127        update_option( 'timezone_string', 'America/Regina' );
     128
     129        $this->assertEquals( gmdate( 'B' ), date_i18n( 'B' ) );
     130    }
     131
     132    /**
     133     * @ticket 25768
     134     */
     135    public function test_should_handle_escaped_formats() {
     136        $format = 'D | \D | \\D | \\\D | \\\\D | \\\\\D | \\\\\\D';
     137
     138        $this->assertEquals( gmdate( $format ), date_i18n( $format ) );
     139    }
     140
     141    /**
     142     * @ticket 25768
     143     *
     144     * @dataProvider dst_times
     145     *
     146     * @param string $time     Time to test in Y-m-d H:i:s format.
     147     * @param string $timezone PHP timezone string to use.
     148     */
     149    public function test_should_handle_dst( $time, $timezone ) {
     150        update_option( 'timezone_string', $timezone );
     151
     152        $timezone     = new DateTimeZone( $timezone );
     153        $datetime     = new DateTime( $time, $timezone );
     154        $wp_timestamp = strtotime( $time );
     155        $format       = 'I ' . DATE_RFC3339;
     156
     157        $this->assertEquals( $datetime->format( $format ), date_i18n( $format, $wp_timestamp ) );
     158    }
     159
     160    public function dst_times() {
     161        return array(
     162            'Before DST start' => array( '2019-03-31 02:59:00', 'Europe/Kiev' ),
     163            'After DST start'  => array( '2019-03-31 04:01:00', 'Europe/Kiev' ),
     164            'Before DST end'   => array( '2019-10-27 02:59:00', 'Europe/Kiev' ),
     165            'After DST end'    => array( '2019-10-27 04:01:00', 'Europe/Kiev' ),
     166        );
     167    }
    110168}
Note: See TracChangeset for help on using the changeset viewer.