From f2d52f567e546b3ff760299252b7c1f5c74f884e Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sat, 2 Jul 2022 22:32:27 +0000 Subject: [PATCH] Resolve #22 by using contributed new function from BohwaZ to fix deprecation issue in php8.1 --- .github/workflows/ci.yml | 2 +- Log.php | 19 +++- Log/console.php | 2 +- Log/display.php | 2 +- Log/error_log.php | 2 +- Log/file.php | 2 +- Log/firebug.php | 2 +- Log/mail.php | 2 +- Log/sqlite.php | 2 +- Log/syslog.php | 2 +- Log/win.php | 2 +- php-8.1-strftime.php | 222 +++++++++++++++++++++++++++++++++++++++ 12 files changed, 250 insertions(+), 11 deletions(-) create mode 100644 php-8.1-strftime.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5c17cfe..d4109f2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,7 +14,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-version: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4'] + php-version: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1'] steps: - uses: actions/checkout@v2 diff --git a/Log.php b/Log.php index b1c67a4..caa700b 100644 --- a/Log.php +++ b/Log.php @@ -25,7 +25,10 @@ define('PEAR_LOG_TYPE_DEBUG', 2); /* Use PHP's debugging connection */ define('PEAR_LOG_TYPE_FILE', 3); /* Append to a file */ define('PEAR_LOG_TYPE_SAPI', 4); /* Use the SAPI logging handler */ - +if (!function_exists('PHP81_BC\strftime')) { + require_once 'php-8.1-strftime.php'; +} +use function PHP81_BC\strftime; /** * The Log:: class implements both an abstraction for various logging * mechanisms and the Subject end of a Subject-Observer pattern. @@ -869,4 +872,18 @@ function getIdent() { return $this->_ident; } + + /** + * Returns formatted time string using the PHP81_BC\strftime function to avoid deprecation errors + * @param string $format the format of the string to return + * + * @return string + * + * @access public + * @since 1.13.4 + */ + public function formatTime($format) + { + return strftime($format); + } } diff --git a/Log/console.php b/Log/console.php index 4b5ec85..2cd8e9c 100644 --- a/Log/console.php +++ b/Log/console.php @@ -201,7 +201,7 @@ function log($message, $priority = null) /* Build the string containing the complete log line. */ $line = $this->_format($this->_lineFormat, - strftime($this->_timeFormat), + $this->formatTime($this->_timeFormat), $priority, $message) . "\n"; /* diff --git a/Log/display.php b/Log/display.php index 9711221..8f92f8d 100644 --- a/Log/display.php +++ b/Log/display.php @@ -168,7 +168,7 @@ function log($message, $priority = null) /* Build and output the complete log line. */ echo $this->_format($this->_lineFormat, - strftime($this->_timeFormat), + $this->formatTime($this->_timeFormat), $priority, $message); diff --git a/Log/error_log.php b/Log/error_log.php index 602d7cd..d378eff 100644 --- a/Log/error_log.php +++ b/Log/error_log.php @@ -145,7 +145,7 @@ function log($message, $priority = null) /* Build the string containing the complete log line. */ $line = $this->_format($this->_lineFormat, - strftime($this->_timeFormat), + $this->formatTime($this->_timeFormat), $priority, $message); /* Pass the log line and parameters to the error_log() function. */ diff --git a/Log/file.php b/Log/file.php index 3cd5fec..1a51c38 100644 --- a/Log/file.php +++ b/Log/file.php @@ -291,7 +291,7 @@ function log($message, $priority = null) /* Build the string containing the complete log line. */ $line = $this->_format($this->_lineFormat, - strftime($this->_timeFormat), + $this->formatTime($this->_timeFormat), $priority, $message) . $this->_eol; /* If locking is enabled, acquire an exclusive lock on the file. */ diff --git a/Log/firebug.php b/Log/firebug.php index b30cdbd..2116579 100644 --- a/Log/firebug.php +++ b/Log/firebug.php @@ -185,7 +185,7 @@ function log($message, $priority = null) /* Build the string containing the complete log line. */ $line = $this->_format($this->_lineFormat, - strftime($this->_timeFormat), + $this->formatTime($this->_timeFormat), $priority, $message); diff --git a/Log/mail.php b/Log/mail.php index 5c079ee..19dafaf 100644 --- a/Log/mail.php +++ b/Log/mail.php @@ -282,7 +282,7 @@ function log($message, $priority = null) /* Append the string containing the complete log line. */ $this->_message .= $this->_format($this->_lineFormat, - strftime($this->_timeFormat), + $this->formatTime($this->_timeFormat), $priority, $message) . "\r\n"; $this->_shouldSend = true; diff --git a/Log/sqlite.php b/Log/sqlite.php index ec7ac27..085cc91 100644 --- a/Log/sqlite.php +++ b/Log/sqlite.php @@ -180,7 +180,7 @@ function log($message, $priority = null) $q = sprintf('INSERT INTO [%s] (logtime, ident, priority, message) ' . "VALUES ('%s', '%s', %d, '%s')", $this->_table, - strftime('%Y-%m-%d %H:%M:%S', time()), + $this->formatTime('%Y-%m-%d %H:%M:%S', time()), sqlite_escape_string($this->_ident), $priority, sqlite_escape_string($message)); diff --git a/Log/syslog.php b/Log/syslog.php index 59c27fd..2cdf382 100644 --- a/Log/syslog.php +++ b/Log/syslog.php @@ -180,7 +180,7 @@ function log($message, $priority = null) /* Apply the configured line format to the message string. */ $message = $this->_format($this->_lineFormat, - strftime($this->_timeFormat), + $this->formatTime($this->_timeFormat), $priority, $message); /* Split the string into parts based on our maximum length setting. */ diff --git a/Log/win.php b/Log/win.php index 43d9c4a..ec83975 100644 --- a/Log/win.php +++ b/Log/win.php @@ -268,7 +268,7 @@ function log($message, $priority = null) /* Build the output line that contains the log entry row. */ $line = ''; $line .= sprintf('%s.%s', - strftime('%H:%M:%S', $sec), substr($usec, 2, 2)); + $this->formatTime('%H:%M:%S', $sec), substr($usec, 2, 2)); if (!empty($this->_ident)) { $line .= '' . $this->_ident . ''; } diff --git a/php-8.1-strftime.php b/php-8.1-strftime.php new file mode 100644 index 0000000..0f84da6 --- /dev/null +++ b/php-8.1-strftime.php @@ -0,0 +1,222 @@ + + */ + function strftime ($format, $timestamp = null, $locale = null) { + 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, $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; + }