From 7bbb5434bf5975e57abc07e3723643e1fb7f7950 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Mon, 11 Jul 2022 15:57:38 +0300 Subject: [PATCH] MDL-75100 core: replace function strftime deprecated in PHP 8.1 --- admin/mailout-debugger.php | 2 +- .../classes/local/action/rollback.php | 2 +- blocks/rss_client/classes/output/renderer.php | 3 +- calendar/tests/behat/behat_calendar.php | 8 +- calendar/type/gregorian/classes/structure.php | 4 +- lib/classes/date.php | 205 ++++++++++++++++++ lib/moodlelib.php | 4 +- lib/odslib.class.php | 6 +- lib/tests/date_legacy_test.php | 2 +- mod/wiki/pagelib.php | 2 +- 10 files changed, 219 insertions(+), 19 deletions(-) diff --git a/admin/mailout-debugger.php b/admin/mailout-debugger.php index ae9fc85e816..bab5e998322 100644 --- a/admin/mailout-debugger.php +++ b/admin/mailout-debugger.php @@ -45,7 +45,7 @@ if (isset($_ENV['TMPDIR']) && is_dir($_ENV['TMPDIR'])) { $tmpfile = $tmpdir . '/moodle-mailout.log'; $fh = fopen($tmpfile, 'a+', false) or mdie("Error openning $tmpfile on append\n"); -fwrite($fh, "==== ".strftime("%a %b %e %H:%M:%S %Y", time())." ====\n"); +fwrite($fh, "==== ".date("D M d H:i:s Y", time())." ====\n"); fwrite($fh, "==== Commandline: " . implode(' ',$argv) . "\n"); $stdin = fopen('php://stdin', 'r'); diff --git a/admin/tool/admin_presets/classes/local/action/rollback.php b/admin/tool/admin_presets/classes/local/action/rollback.php index 18ff777b583..aa7dd61f966 100644 --- a/admin/tool/admin_presets/classes/local/action/rollback.php +++ b/admin/tool/admin_presets/classes/local/action/rollback.php @@ -52,7 +52,7 @@ class rollback extends base { ); $context->applications[] = [ - 'timeapplied' => strftime($format, $application->time), + 'timeapplied' => \core_date::strftime($format, (int)$application->time), 'user' => fullname($user), 'action' => $rollbacklink->out(false), ]; diff --git a/blocks/rss_client/classes/output/renderer.php b/blocks/rss_client/classes/output/renderer.php index 7a03280fbdc..e6a24899124 100644 --- a/blocks/rss_client/classes/output/renderer.php +++ b/blocks/rss_client/classes/output/renderer.php @@ -92,8 +92,7 @@ class renderer extends \plugin_renderer_base { * @return string */ public function format_published_date($timestamp) { - return strftime(get_string('strftimerecentfull', 'langconfig'), $timestamp); - return date('j F Y, g:i a', $timestamp); + return \core_date::strftime(get_string('strftimerecentfull', 'langconfig'), $timestamp); } /** diff --git a/calendar/tests/behat/behat_calendar.php b/calendar/tests/behat/behat_calendar.php index e7029fa9fde..44b11d31e27 100644 --- a/calendar/tests/behat/behat_calendar.php +++ b/calendar/tests/behat/behat_calendar.php @@ -118,9 +118,7 @@ class behat_calendar extends behat_base { * @Given /^I hover over today in the mini-calendar block$/ */ public function i_hover_over_today_in_mini_calendar_block(): void { - // For window's compatibility, using %d and not %e. - $todaysday = trim(strftime('%d')); - $todaysday = ltrim($todaysday, '0'); + $todaysday = date('j'); $this->i_hover_over_day_of_this_month_in_mini_calendar_block($todaysday); } @@ -130,9 +128,7 @@ class behat_calendar extends behat_base { * @Given /^I hover over today in the calendar$/ */ public function i_hover_over_today_in_the_calendar() { - // For window's compatibility, using %d and not %e. - $todaysday = trim(strftime('%d')); - $todaysday = ltrim($todaysday, '0'); + $todaysday = date('j'); return $this->i_hover_over_day_of_this_month_in_calendar($todaysday); } diff --git a/calendar/type/gregorian/classes/structure.php b/calendar/type/gregorian/classes/structure.php index cfcabe94ffd..bb0012666b2 100644 --- a/calendar/type/gregorian/classes/structure.php +++ b/calendar/type/gregorian/classes/structure.php @@ -329,11 +329,11 @@ class structure extends type_base { date_default_timezone_set(\core_date::get_user_timezone($timezone)); if ($fixday) { - $daystring = ltrim(str_replace(array(' 0', ' '), '', strftime(' %d', $time))); + $daystring = ltrim(str_replace(array(' 0', ' '), '', date(' d', $time))); $datestring = str_replace('DD', $daystring, $datestring); } if ($fixhour) { - $hourstring = ltrim(str_replace(array(' 0', ' '), '', strftime(' %I', $time))); + $hourstring = ltrim(str_replace(array(' 0', ' '), '', date(' h', $time))); $datestring = str_replace('HH', $hourstring, $datestring); } diff --git a/lib/classes/date.php b/lib/classes/date.php index 9702d43f8bb..bde01d29b05 100644 --- a/lib/classes/date.php +++ b/lib/classes/date.php @@ -692,4 +692,209 @@ class core_date { } } } + + /** + * Locale-formatted strftime using IntlDateFormatter (PHP 8.1 compatible) + * This provides a cross-platform alternative to strftime() for when it will be removed from PHP. + * Note that output can be slightly different between libc sprintf and this function as it is using ICU. + * + * From: + * https://github.com/alphp/strftime + * + * @param string $format Date format + * @param int|string|DateTime $timestamp Timestamp + * @param string|null $locale + * @return string + * @author BohwaZ + */ + public static function strftime(string $format, $timestamp = null, ?string $locale = null) : string { + // phpcs:disable + if (!($timestamp instanceof DateTimeInterface)) { + $timestamp = is_int($timestamp) ? '@' . $timestamp : (string) $timestamp; + + try { + $timestamp = new DateTime($timestamp); + } catch (Exception $e) { + throw new InvalidArgumentException('$timestamp argument is neither a valid UNIX timestamp, a valid date-time string or a DateTime object.', 0, $e); + } + } + + $timestamp->setTimezone(new DateTimeZone(date_default_timezone_get())); + + if (empty($locale)) { + // get current locale + $locale = setlocale(LC_TIME, '0'); + } + // remove trailing part not supported by ext-intl locale + $locale = preg_replace('/[^\w-].*$/', '', $locale); + + $intl_formats = [ + '%a' => 'EEE', // An abbreviated textual representation of the day Sun through Sat + '%A' => 'EEEE', // A full textual representation of the day Sunday through Saturday + '%b' => 'MMM', // Abbreviated month name, based on the locale Jan through Dec + '%B' => 'MMMM', // Full month name, based on the locale January through December + '%h' => 'MMM', // Abbreviated month name, based on the locale (an alias of %b) Jan through Dec + ]; + + $intl_formatter = function (DateTimeInterface $timestamp, string $format) use ($intl_formats, $locale) { + $tz = $timestamp->getTimezone(); + $date_type = IntlDateFormatter::FULL; + $time_type = IntlDateFormatter::FULL; + $pattern = ''; + + switch ($format) { + // %c = Preferred date and time stamp based on locale + // Example: Tue Feb 5 00:45:10 2009 for February 5, 2009 at 12:45:10 AM + case '%c': + $date_type = IntlDateFormatter::LONG; + $time_type = IntlDateFormatter::SHORT; + break; + + // %x = Preferred date representation based on locale, without the time + // Example: 02/05/09 for February 5, 2009 + case '%x': + $date_type = IntlDateFormatter::SHORT; + $time_type = IntlDateFormatter::NONE; + break; + + // Localized time format + case '%X': + $date_type = IntlDateFormatter::NONE; + $time_type = IntlDateFormatter::MEDIUM; + break; + + default: + $pattern = $intl_formats[$format]; + } + + // In October 1582, the Gregorian calendar replaced the Julian in much of Europe, and + // the 4th October was followed by the 15th October. + // ICU (including IntlDateFormattter) interprets and formats dates based on this cutover. + // Posix (including strftime) and timelib (including DateTimeImmutable) instead use + // a "proleptic Gregorian calendar" - they pretend the Gregorian calendar has existed forever. + // This leads to the same instants in time, as expressed in Unix time, having different representations + // in formatted strings. + // To adjust for this, a custom calendar can be supplied with a cutover date arbitrarily far in the past. + $calendar = IntlGregorianCalendar::createInstance(); + $calendar->setGregorianChange(PHP_INT_MIN); + + return (new IntlDateFormatter($locale, $date_type, $time_type, $tz, $calendar, $pattern))->format($timestamp); + }; + + // Same order as https://www.php.net/manual/en/function.strftime.php + $translation_table = [ + // Day + '%a' => $intl_formatter, + '%A' => $intl_formatter, + '%d' => 'd', + '%e' => function ($timestamp) { + return sprintf('% 2u', $timestamp->format('j')); + }, + '%j' => function ($timestamp) { + // Day number in year, 001 to 366 + return sprintf('%03d', $timestamp->format('z')+1); + }, + '%u' => 'N', + '%w' => 'w', + + // Week + '%U' => function ($timestamp) { + // Number of weeks between date and first Sunday of year + $day = new DateTime(sprintf('%d-01 Sunday', $timestamp->format('Y'))); + return sprintf('%02u', 1 + ($timestamp->format('z') - $day->format('z')) / 7); + }, + '%V' => 'W', + '%W' => function ($timestamp) { + // Number of weeks between date and first Monday of year + $day = new DateTime(sprintf('%d-01 Monday', $timestamp->format('Y'))); + return sprintf('%02u', 1 + ($timestamp->format('z') - $day->format('z')) / 7); + }, + + // Month + '%b' => $intl_formatter, + '%B' => $intl_formatter, + '%h' => $intl_formatter, + '%m' => 'm', + + // Year + '%C' => function ($timestamp) { + // Century (-1): 19 for 20th century + return floor($timestamp->format('Y') / 100); + }, + '%g' => function ($timestamp) { + return substr($timestamp->format('o'), -2); + }, + '%G' => 'o', + '%y' => 'y', + '%Y' => 'Y', + + // Time + '%H' => 'H', + '%k' => function ($timestamp) { + return sprintf('% 2u', $timestamp->format('G')); + }, + '%I' => 'h', + '%l' => function ($timestamp) { + return sprintf('% 2u', $timestamp->format('g')); + }, + '%M' => 'i', + '%p' => 'A', // AM PM (this is reversed on purpose!) + '%P' => 'a', // am pm + '%r' => 'h:i:s A', // %I:%M:%S %p + '%R' => 'H:i', // %H:%M + '%S' => 's', + '%T' => 'H:i:s', // %H:%M:%S + '%X' => $intl_formatter, // Preferred time representation based on locale, without the date + + // Timezone + '%z' => 'O', + '%Z' => 'T', + + // Time and Date Stamps + '%c' => $intl_formatter, + '%D' => 'm/d/Y', + '%F' => 'Y-m-d', + '%s' => 'U', + '%x' => $intl_formatter, + ]; + + $out = preg_replace_callback('/(?format($replace); + } else { + $result = $replace($timestamp, $pattern); + } + + switch ($prefix) { + case '_': + // replace leading zeros with spaces but keep last char if also zero + return preg_replace('/\G0(?=.)/', ' ', $result); + case '#': + case '-': + // remove leading zeros but keep last char if also zero + return preg_replace('/^0+(?=.)/', '', $result); + } + + return $result; + }, $format); + + $out = str_replace('%%', '%', $out); + return $out; + // phpcs:enable + } } diff --git a/lib/moodlelib.php b/lib/moodlelib.php index c631840ebcc..6d0cc74c325 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -2377,7 +2377,7 @@ function date_format_string($date, $format, $tz = 99) { date_default_timezone_set(core_date::get_user_timezone($tz)); - if (strftime('%p', 0) === strftime('%p', HOURSECS * 18)) { + if (date('A', 0) === date('A', HOURSECS * 18)) { $datearray = getdate($date); $format = str_replace([ '%P', @@ -2388,7 +2388,7 @@ function date_format_string($date, $format, $tz = 99) { ], $format); } - $datestring = strftime($format, $date); + $datestring = core_date::strftime($format, $date); core_date::set_default_server_timezone(); if ($localewincharset) { diff --git a/lib/odslib.class.php b/lib/odslib.class.php index 7ac88b31a5e..3f49756dd6e 100644 --- a/lib/odslib.class.php +++ b/lib/odslib.class.php @@ -1177,8 +1177,8 @@ class MoodleODSWriter { if (isset($cell->formula)) { $buffer .= ''."\n"; } else if ($cell->type == 'date') { - $buffer .= '' - . $pretext . strftime('%Y-%m-%dT%H:%M:%S', $cell->value) . $posttext + $buffer .= 'value) . '"'.$extra.'>' + . $pretext . date("Y-m-d\\TH:i:s", $cell->value) . $posttext . ''."\n"; } else if ($cell->type == 'float') { $buffer .= '' @@ -1267,7 +1267,7 @@ class MoodleODSWriter { Moodle '.$CFG->release.' ' . htmlspecialchars(fullname($USER, true), ENT_QUOTES, 'utf-8') . ' - '.strftime('%Y-%m-%dT%H:%M:%S').' + '.date("Y-m-d\\TH:i:s").' '; diff --git a/lib/tests/date_legacy_test.php b/lib/tests/date_legacy_test.php index 542a02bd8b8..f26e164a1f1 100644 --- a/lib/tests/date_legacy_test.php +++ b/lib/tests/date_legacy_test.php @@ -276,7 +276,7 @@ class date_legacy_test extends \advanced_testcase { $expected->setTimezone(new \DateTimeZone(($user->timezone == 99 ? 'Pacific/Auckland' : $user->timezone))); $result = userdate($expected->getTimestamp(), '', 99, false, false); date_default_timezone_set($expected->getTimezone()->getName()); - $ex = strftime($format, $expected->getTimestamp()); + $ex = \core_date::strftime($format, $expected->getTimestamp()); date_default_timezone_set($CFG->timezone); $this->assertSame($ex, $result); } diff --git a/mod/wiki/pagelib.php b/mod/wiki/pagelib.php index cea90fef5c4..535ad17c5c7 100644 --- a/mod/wiki/pagelib.php +++ b/mod/wiki/pagelib.php @@ -1771,7 +1771,7 @@ class page_wiki_map extends page_wiki { $strdataux = ''; foreach ($pages as $page) { $user = wiki_get_user_info($page->userid); - $strdata = strftime('%d %b %Y', $page->timemodified); + $strdata = date('d M Y', $page->timemodified); if ($strdata != $strdataux) { $table->data[] = array($OUTPUT->heading($strdata, 4)); $strdataux = $strdata;