From ad7220ce99c21272592e47d8e422a78dfa3648f2 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 26 Jan 2024 18:51:49 +0100 Subject: [PATCH 1/6] use "standalone" names for month and day. fixes #12 --- src/php-8.1-strftime.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/php-8.1-strftime.php b/src/php-8.1-strftime.php index 076538e..18fc178 100644 --- a/src/php-8.1-strftime.php +++ b/src/php-8.1-strftime.php @@ -44,9 +44,9 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) : $locale = Locale::canonicalize($locale ?? setlocale(LC_TIME, '0')); $intl_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 ]; From 2ee69eb339267939cfa1c663c98a444216806a32 Mon Sep 17 00:00:00 2001 From: maqc1 Date: Fri, 26 Jan 2024 16:20:33 -0500 Subject: [PATCH 2/6] Minus prefix should also remove leading space (#13) * Minus prefix should also remove space Standard PHP strftime() also remove leading space when using the minus prefix. * Add asserts for test '%#e' and '%-e' --------- Co-authored-by: Fernando Herrero --- src/php-8.1-strftime.php | 2 +- tests/strftimeTest.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/php-8.1-strftime.php b/src/php-8.1-strftime.php index 18fc178..d34f065 100644 --- a/src/php-8.1-strftime.php +++ b/src/php-8.1-strftime.php @@ -206,7 +206,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 a0ca3fe..2540afc 100644 --- a/tests/strftimeTest.php +++ b/tests/strftimeTest.php @@ -48,6 +48,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'); From 10c13faf2f65bfaec17dfa769918e0c7a4c9b3cc Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Tue, 12 Mar 2024 12:46:29 +0100 Subject: [PATCH 3/6] Use Locale::getDefault() before setlocale(LC_TIME, '0') --- src/php-8.1-strftime.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/php-8.1-strftime.php b/src/php-8.1-strftime.php index d34f065..40eef9d 100644 --- a/src/php-8.1-strftime.php +++ b/src/php-8.1-strftime.php @@ -41,7 +41,7 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) : $timestamp->setTimezone(new DateTimeZone(date_default_timezone_get())); - $locale = Locale::canonicalize($locale ?? setlocale(LC_TIME, '0')); + $locale = Locale::canonicalize($locale ?? (Locale::getDefault() ?? setlocale(LC_TIME, '0'))); $intl_formats = [ '%a' => 'ccc', // An abbreviated textual representation of the day Sun through Sat From ced926ce5ab7110a5932c4644f299913c16a4528 Mon Sep 17 00:00:00 2001 From: alexgit2k Date: Tue, 9 Apr 2024 10:23:08 +0200 Subject: [PATCH 4/6] Add locale-param to PHPDoc-block (#20) --- src/php-8.1-strftime.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/php-8.1-strftime.php b/src/php-8.1-strftime.php index 40eef9d..18d6a7c 100644 --- a/src/php-8.1-strftime.php +++ b/src/php-8.1-strftime.php @@ -25,6 +25,7 @@ * * @param string $format Date format * @param integer|string|DateTime $timestamp Timestamp + * @param string|null $locale locale * @return string * @author BohwaZ */ From bb8a957858303b22db6f6cd9caac1d18031a4971 Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Wed, 15 May 2024 10:33:19 +0200 Subject: [PATCH 5/6] Don't override the timezone when called with a DateTime object --- .gitignore | 1 + src/php-8.1-strftime.php | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) 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/php-8.1-strftime.php b/src/php-8.1-strftime.php index 18d6a7c..b642dc4 100644 --- a/src/php-8.1-strftime.php +++ b/src/php-8.1-strftime.php @@ -38,9 +38,9 @@ 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())); + } $locale = Locale::canonicalize($locale ?? (Locale::getDefault() ?? setlocale(LC_TIME, '0'))); From d115595bccd137660d25e9e05fb656143594b404 Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Tue, 19 Nov 2024 15:01:36 +0100 Subject: [PATCH 6/6] Update PHPDoc: missing throws InvalidArgumentException --- src/php-8.1-strftime.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/php-8.1-strftime.php b/src/php-8.1-strftime.php index b642dc4..14e8038 100644 --- a/src/php-8.1-strftime.php +++ b/src/php-8.1-strftime.php @@ -27,6 +27,7 @@ * @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 {