WordPress.org

Make WordPress Core

Opened 3 months ago

Closed 3 months ago

Last modified 3 months ago

#53897 closed defect (bug) (fixed)

PHP 8.1: strftime() is deprecated

Reported by: jrf Owned by: SergeyBiryukov
Milestone: 5.9 Priority: normal
Severity: normal Version:
Component: Build/Test Tools Keywords: has-patch early php81 commit
Focuses: Cc:

Description

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.

Ref: https://wiki.php.net/rfc/deprecations_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).

Ref: https://github.com/php/php-src/blob/1cf4fb739f7a4fa8404a4c0958f13d04eae519d4/UPGRADING#L379-L381

Aside from one instance in SimplePie, the strftime() function is only used within the test suite of WordPress to create formatted timestamps.

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

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

It also is the cause of 17 tests erroring out on PHP 8.1.

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 )

Verification of the principle:
https://3v4l.org/MCURg

Other references:

Attachments (1)

53897.gmstrftime.diff (6.7 KB) - added by SergeyBiryukov 3 months ago.

Download all attachments as: .zip

Change History (6)

This ticket was mentioned in PR #1560 on WordPress/wordpress-develop by jrfnl.


3 months ago

  • Keywords has-unit-tests added

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.

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

Ref: https://wiki.php.net/rfc/deprecations_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).

Ref: https://github.com/php/php-src/blob/1cf4fb739f7a4fa8404a4c0958f13d04eae519d4/UPGRADING#L379-L381

Other references:

Verification of the principle:
https://3v4l.org/MCURg

Trac ticket: https://core.trac.wordpress.org/ticket/53897

#2 @jrf
3 months ago

  • Keywords has-unit-tests removed

A PR has been opened on GitHub with a complete patch (excluding SimplePie).

#3 @jrf
3 months ago

  • Keywords commit added

Reviewed @SergeyBiryukov addition and all looks good.

Good call on removing the duplicate function calls.

I'm curious about why these tests were using gmstrftime() (GMT based), not strftime() like the other tests, but a git blame just got me to the initial test commits, so I guess we're out of luck to try and find out.

Tests on PHP 5.6 - 8.0 all still pass with these changes, so as far as I'm concerned, this is good to go.

#4 @SergeyBiryukov
3 months ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 51587:

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.

#5 @prbot
3 months ago

jrfnl commented on PR #1560:

Closing as committed via 51587

Note: See TracTickets for help on using tickets.