Skip to content

Commit

Permalink
Remove strftime (deprecated in php 8.1)
Browse files Browse the repository at this point in the history
Add possibility to configure timeFormatter
add test
  • Loading branch information
sveneld committed Jan 7, 2024
1 parent 759e60e commit df20646
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ public function getIdent()
* @param callable|null $timeFormatter function which will be used to format time
* @return string
*/
public function formatTime($time, $timeFormat = self::DEFAULT_TIME_FORMAT, callable $timeFormatter = null)
protected function formatTime($time, $timeFormat = self::DEFAULT_TIME_FORMAT, callable $timeFormatter = null)
{
if (!is_null($timeFormatter) && is_callable($timeFormatter)) {
return call_user_func($timeFormatter, $timeFormat, $time);
Expand Down
59 changes: 59 additions & 0 deletions tests/formatTime.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
--TEST--
Log: Time Format
--INI--
date.timezone=UTC
--FILE--
<?php

require_once 'Log.php';

set_error_handler(function ($errno, $errstr, $errfile, $errline) {
echo $errstr . PHP_EOL;
}, E_USER_NOTICE|E_USER_WARNING);

$tests = array(
array('timeFormat' => 'Y-m-d H:i:s', 'timeFormatter' => null, 'expectedResult' => '2024-01-07 20:17:28'),
array('timeFormat' => 'H:i:s', 'timeFormatter' => null, 'expectedResult' => '20:17:28'),
array('timeFormat' => '%b %d %H:%M:%S', 'timeFormatter' => null, 'expectedResult' => 'Jan 07 20:17:28'),
array('timeFormat' => '%O %d %a %e %A %u %w %j', 'timeFormatter' => null, 'expectedResult' => 'th 07 Sun 7 Sunday 7 0 6'),
array('timeFormat' => '%V', 'timeFormatter' => null, 'expectedResult' => '01'),
array('timeFormat' => '%B %m %b %-m', 'timeFormatter' => null, 'expectedResult' => 'January 01 Jan 1'),
array('timeFormat' => '%G %Y %y', 'timeFormatter' => null, 'expectedResult' => '2024 2024 24'),
array('timeFormat' => '%P %p %l %I %H %M %S', 'timeFormatter' => null, 'expectedResult' => 'pm PM 8 08 20 17 28'),
array('timeFormat' => '%z %Z', 'timeFormatter' => null, 'expectedResult' => '+0000 UTC'),
array('timeFormat' => '%s', 'timeFormatter' => null, 'expectedResult' => '1704658648'),
array('timeFormat' => 'Y-m-d H:i:s', 'timeFormatter' => function($timeFormat, $time) { return $time;}, 'expectedResult' => '1704658648'),
array('timeFormat' => 'Y-m-d H:i:s', 'timeFormatter' => function($timeFormat, $time) { return $timeFormat;}, 'expectedResult' => 'Y-m-d H:i:s'),
);

foreach ($tests as $config) {
$config['lineFormat'] = '%1$s';
$logger = Log::factory('display', '', '', $config);
$reflection = new ReflectionObject($logger);
$reflectionMethod = $reflection->getMethod('formatTime');
$reflectionMethod->setAccessible(true);
$result = $reflectionMethod->invoke($logger, 1704658648, $config['timeFormat'], $config['timeFormatter']);
echo $result . ' ' . ($result == $config['expectedResult'] ? 'OK' : 'FAIL') . PHP_EOL;
}

--EXPECT--
2024-01-07 20:17:28 OK
20:17:28 OK
Using strftime-style formatting is deprecated
Jan 07 20:17:28 OK
Using strftime-style formatting is deprecated
th 07 Sun 7 Sunday 7 0 6 OK
Using strftime-style formatting is deprecated
01 OK
Using strftime-style formatting is deprecated
January 01 Jan 1 OK
Using strftime-style formatting is deprecated
2024 2024 24 OK
Using strftime-style formatting is deprecated
pm PM 8 08 20 17 28 OK
Using strftime-style formatting is deprecated
+0000 UTC OK
Using strftime-style formatting is deprecated
1704658648 OK
1704658648 OK
Y-m-d H:i:s OK

0 comments on commit df20646

Please sign in to comment.