Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/Horde/Date/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
},
Expand Down
31 changes: 31 additions & 0 deletions test/Unit/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
Loading