diff --git a/.gitignore b/.gitignore index a5b8120..659557e 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,7 @@ Thumbs.db ####################### /.vscode /composer.lock +.phpunit.result.cache # vim *~ *.swp diff --git a/src/IntlLocaleFormatter.php b/src/IntlLocaleFormatter.php index 5bb62e4..783a0c1 100644 --- a/src/IntlLocaleFormatter.php +++ b/src/IntlLocaleFormatter.php @@ -14,9 +14,9 @@ class IntlLocaleFormatter extends AbstractLocaleFormatter /** @var string[] strftime to ICU placeholders */ protected $formats = [ - '%a' => 'EEE', // An abbreviated textual representation of the day Sun through Sat + '%a' => 'ccc', // 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' => 'LLL', // 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 ]; diff --git a/src/php-8.1-strftime.php b/src/php-8.1-strftime.php index f8a8c79..75a6bb3 100644 --- a/src/php-8.1-strftime.php +++ b/src/php-8.1-strftime.php @@ -2,10 +2,13 @@ namespace PHP81_BC; use DateTime; - use DateTimeInterface; use DateTimeZone; + use DateTimeInterface; use Exception; use InvalidArgumentException; + use Locale; + use PHP81_BC\strftime\DateLocaleFormatter; + use PHP81_BC\strftime\IntlLocaleFormatter; /** * Locale-formatted strftime using IntlDateFormatter (PHP 8.1 compatible) @@ -22,7 +25,9 @@ * * @param string $format Date format * @param integer|string|DateTime $timestamp Timestamp + * @param string|null $locale locale * @return string + * @throws InvalidArgumentException * @author BohwaZ */ function strftime (string $format, $timestamp = null, ?string $locale = null) : string { @@ -34,15 +39,15 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) : } 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())); + $timestamp->setTimezone(new DateTimeZone(date_default_timezone_get())); + } if (class_exists('\\IntlDateFormatter') && !isset($_SERVER['STRFTIME_NO_INTL'])) { - $locale = \Locale::canonicalize($locale ?? setlocale(LC_TIME, '0')); - $locale_formatter = new \PHP81_BC\strftime\IntlLocaleFormatter($locale); + $locale = Locale::canonicalize($locale ?? (Locale::getDefault() ?? setlocale(LC_TIME, '0'))); + $locale_formatter = new IntlLocaleFormatter($locale); } else { - $locale_formatter = new \PHP81_BC\strftime\DateLocaleFormatter($locale); + $locale_formatter = new DateLocaleFormatter($locale); } // Same order as https://www.php.net/manual/en/function.strftime.php @@ -151,7 +156,7 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) : case '#': case '-': // remove leading zeros but keep last char if also zero - return preg_replace('/^0+(?=.)/', '', $result); + return preg_replace('/^[0\s]+(?=.)/', '', $result); } return $result; diff --git a/tests/strftimeTest.php b/tests/strftimeTest.php index 48d86a6..4c78163 100644 --- a/tests/strftimeTest.php +++ b/tests/strftimeTest.php @@ -46,6 +46,12 @@ public function testDayFormats () { $result = strftime('%e', '20220306 13:02:03'); $this->assertEquals(' 6', $result, '%e: Day of the month, with a space preceding single digits'); + $result = strftime('%#e', '20220306 13:02:03'); + $this->assertEquals('6', $result, '%#e: Day of the month, without leading space'); + + $result = strftime('%-e', '20220306 13:02:03'); + $this->assertEquals('6', $result, '%-e: Day of the month, without leading space'); + $result = strftime('%j', '20220306 13:02:03'); $this->assertEquals('065', $result, '%j: Day of the year, 3 digits with leading zeros');