Make WordPress Core


Ignore:
Timestamp:
08/09/2021 07:08:09 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Replace strftime() and gmstrftime() usage in unit tests.

Since PHP 8.1, the strftime() and gmstrftime() functions are deprecated:

The strftime() and gmstrftime() functions exhibit similar issues as strptime(), in that the formats they support, as well as their behavior, is platform-dependent. Unlike strptime(), these functions are available on Windows, though with a different feature set than on Linux. Musl-based distributions like Alpine do not support timezone-related format specifiers correctly. These functions are also locale-based, and as such may exhibit thread-safety issues.

date() or DateTime::format() provide portable alternatives, and IntlDateFormatter::format() provides a more sophisticated, localization-aware alternative.

Reference: PHP RFC: Deprecations for PHP 8.1: strftime() and gmstrftime()

The strftime() and gmstrftime() functions have been deprecated in favor of
date()/DateTime::format() (for locale-independent formatting) or
IntlDateFormatter::format() (for locale-dependent formatting).

Reference: PHP 8.1 Upgrade Notes.

Aside from one instance in SimplePie, the strftime() and gmstrftime() functions are only used within the test suite of WordPress to create formatted timestamps.

As the function is used in test code, this leads to test warnings like this on PHP 8.1:

Deprecated: Function strftime() is deprecated in path/to/tests/phpunit/tests/canonical/postStatus.php on line 37

These calls can all be safely converted to use a pattern along the lines of:

<?php
date_format( date_create( 'time phrase or timestamp' ), $format )

Other references:

Props jrf, SergeyBiryukov.
Fixes #53897.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post.php

    r51568 r51587  
    129129            'post_content' => rand_str(),
    130130            'post_title'   => rand_str(),
    131             'post_date'    => strftime( '%Y-%m-%d %H:%M:%S', $future_date ),
     131            'post_date'    => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ),
    132132        );
    133133
     
    165165            'post_content' => rand_str(),
    166166            'post_title'   => rand_str(),
    167             'post_date'    => strftime( '%Y-%m-%d %H:%M:%S', $future_date_1 ),
     167            'post_date'    => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ),
    168168        );
    169169
     
    183183
    184184        $post['ID']            = $id;
    185         $post['post_date']     = strftime( '%Y-%m-%d %H:%M:%S', $future_date_2 );
     185        $post['post_date']     = date_format( date_create( "@{$future_date_2}" ), 'Y-m-d H:i:s' );
    186186        $post['post_date_gmt'] = null;
    187187        wp_update_post( $post );
     
    210210            'post_content' => rand_str(),
    211211            'post_title'   => rand_str(),
    212             'post_date'    => strftime( '%Y-%m-%d %H:%M:%S', $future_date_1 ),
     212            'post_date'    => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ),
    213213        );
    214214
     
    228228
    229229        $post['ID']            = $id;
    230         $post['post_date']     = strftime( '%Y-%m-%d %H:%M:%S', $future_date_2 );
     230        $post['post_date']     = date_format( date_create( "@{$future_date_2}" ), 'Y-m-d H:i:s' );
    231231        $post['post_date_gmt'] = null;
    232232        wp_update_post( $post );
     
    252252            'post_content' => rand_str(),
    253253            'post_title'   => rand_str(),
    254             'post_date'    => strftime( '%Y-%m-%d %H:%M:%S', $future_date ),
     254            'post_date'    => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ),
    255255        );
    256256
     
    287287            'post_content' => rand_str(),
    288288            'post_title'   => rand_str(),
    289             'post_date'    => strftime( '%Y-%m-%d %H:%M:%S', $future_date_1 ),
     289            'post_date'    => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ),
    290290        );
    291291
     
    331331                'post_content' => rand_str(),
    332332                'post_title'   => rand_str(),
    333                 'post_date'    => strftime( '%Y-%m-%d %H:%M:%S', $future_date_1 ),
     333                'post_date'    => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ),
    334334            );
    335335
     
    373373            'post_content' => rand_str(),
    374374            'post_title'   => rand_str(),
    375             'post_date'    => strftime( '%Y-%m-%d %H:%M:%S', $future_date ),
     375            'post_date'    => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ),
    376376        );
    377377
     
    430430            'post_content' => rand_str(),
    431431            'post_title'   => rand_str(),
    432             'post_date'    => strftime( '%Y-%m-%d %H:%M:%S', $future_date_1 ),
     432            'post_date'    => date_format( date_create( "@{$future_date_1}" ), 'Y-m-d H:i:s' ),
    433433        );
    434434
     
    536536            'post_content' => rand_str(),
    537537            'post_title'   => rand_str(),
    538             'post_date'    => strftime( '%Y-%m-%d %H:%M:%S', $future_date ),
     538            'post_date'    => date_format( date_create( "@{$future_date}" ), 'Y-m-d H:i:s' ),
    539539        );
    540540
Note: See TracChangeset for help on using the changeset viewer.