diff --git a/lib/Horde/Date/Utils.php b/lib/Horde/Date/Utils.php index b8dba37..a06528d 100644 --- a/lib/Horde/Date/Utils.php +++ b/lib/Horde/Date/Utils.php @@ -146,9 +146,10 @@ public static function relativeDateTime( public static function strftime2date($format) { $provider = null; - if (class_exists('Horde_Nls')) { - $provider = function (int $constant): string { - return Horde_Nls::getLangInfo($constant); + if (class_exists(Horde\Nls\Nls::class)) { + $nls = new Horde\Nls\Nls(); + $provider = function (int $constant) use ($nls): string|false { + return $nls->getLangInfo($constant); }; } return Horde\Date\Utils::strftime2date((string) $format, $provider); diff --git a/src/Utils.php b/src/Utils.php index 2550ec0..511d632 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -133,13 +133,19 @@ public static function strftime2date( $callbackPatterns = [ '/%X/' => function () use ($localeInfoProvider): string { if ($localeInfoProvider !== null) { - return $localeInfoProvider(T_FMT); + $result = $localeInfoProvider(T_FMT); + if ($result !== false) { + return $result; + } } return 'H:i:s'; }, '/%x/' => function () use ($localeInfoProvider): string { if ($localeInfoProvider !== null) { - return $localeInfoProvider(D_FMT); + $result = $localeInfoProvider(D_FMT); + if ($result !== false) { + return $result; + } } return 'm/d/Y'; }, diff --git a/test/Unit/UtilsTest.php b/test/Unit/UtilsTest.php index 0a1b3b5..4c6df44 100644 --- a/test/Unit/UtilsTest.php +++ b/test/Unit/UtilsTest.php @@ -7,6 +7,7 @@ use Horde\Date\Date; use Horde\Date\DateException; use Horde\Date\Utils; +use Horde\Nls\Nls; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -204,4 +205,34 @@ public function testStrftime2dateComposite(): void { $this->assertSame('Y-m-d H:i:s', Utils::strftime2date('%Y-%m-%d %H:%M:%S')); } + + public function testStrftime2dateWithNlsProvider(): void + { + $nls = new Nls(); + $provider = function (int $constant) use ($nls): string|false { + return $nls->getLangInfo($constant); + }; + + $dateFormat = Utils::strftime2date('%x', $provider); + $timeFormat = Utils::strftime2date('%X', $provider); + + // nl_langinfo returns strftime format strings (e.g. %m/%d/%y) + // which are then converted by the second pass of strftime2date. + // Verify the provider's output is processed correctly end-to-end. + $expectedDate = Utils::strftime2date(nl_langinfo(D_FMT)); + $expectedTime = Utils::strftime2date(nl_langinfo(T_FMT)); + + $this->assertSame($expectedDate, $dateFormat); + $this->assertSame($expectedTime, $timeFormat); + } + + public function testStrftime2dateWithNlsProviderFalseReturnFallsBack(): void + { + $provider = function (int $constant): string|false { + return false; + }; + + $this->assertSame('m/d/Y', Utils::strftime2date('%x', $provider)); + $this->assertSame('H:i:s', Utils::strftime2date('%X', $provider)); + } }