From a95985070f95f996a76fa05e5ea6d738cb76ec40 Mon Sep 17 00:00:00 2001 From: Alec Smecher Date: Mon, 18 Dec 2023 08:13:47 -0800 Subject: [PATCH 01/16] #15 Fix fatal error with non-Gregorian locales --- src/php-8.1-strftime.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/php-8.1-strftime.php b/src/php-8.1-strftime.php index 1c7ee37..ebb3f70 100644 --- a/src/php-8.1-strftime.php +++ b/src/php-8.1-strftime.php @@ -95,7 +95,9 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) : // in formatted strings. // To adjust for this, a custom calendar can be supplied with a cutover date arbitrarily far in the past. $calendar = IntlGregorianCalendar::createInstance(); - $calendar->setGregorianChange(PHP_INT_MIN); + // NOTE: IntlGregorianCalendar::createInstance DOES NOT return an IntlGregorianCalendar instance when + // using a non-Gregorian locale (e.g. fa_IR)! In that case, setGregorianChange will not exist. + if (method_exists($calendar, 'setGregorianChange')) $calendar->setGregorianChange(PHP_INT_MIN); return (new IntlDateFormatter($locale, $date_type, $time_type, $tz, $calendar, $pattern))->format($timestamp); }; From 8b7206011c36b74c5b39cf9f4f2af89e91226a2a Mon Sep 17 00:00:00 2001 From: Alec Smecher Date: Mon, 18 Dec 2023 08:40:48 -0800 Subject: [PATCH 02/16] Code review tweaks --- src/php-8.1-strftime.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/php-8.1-strftime.php b/src/php-8.1-strftime.php index ebb3f70..4f8f916 100644 --- a/src/php-8.1-strftime.php +++ b/src/php-8.1-strftime.php @@ -97,7 +97,9 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) : $calendar = IntlGregorianCalendar::createInstance(); // NOTE: IntlGregorianCalendar::createInstance DOES NOT return an IntlGregorianCalendar instance when // using a non-Gregorian locale (e.g. fa_IR)! In that case, setGregorianChange will not exist. - if (method_exists($calendar, 'setGregorianChange')) $calendar->setGregorianChange(PHP_INT_MIN); + if ($calendar instanceof IntlGregorianCalendar) { + $calendar->setGregorianChange(PHP_INT_MIN); + } return (new IntlDateFormatter($locale, $date_type, $time_type, $tz, $calendar, $pattern))->format($timestamp); }; From 4c13c946d0599d362797a436b7ebd8a38e7cdce0 Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Fri, 26 Jan 2024 00:39:48 +0100 Subject: [PATCH 03/16] Fix fatal error when using the default PHP locale #17 --- src/php-8.1-strftime.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/php-8.1-strftime.php b/src/php-8.1-strftime.php index 4f8f916..076538e 100644 --- a/src/php-8.1-strftime.php +++ b/src/php-8.1-strftime.php @@ -8,6 +8,7 @@ use IntlDateFormatter; use IntlGregorianCalendar; use InvalidArgumentException; + use Locale; /** * Locale-formatted strftime using IntlDateFormatter (PHP 8.1 compatible) @@ -40,12 +41,7 @@ function strftime (string $format, $timestamp = null, ?string $locale = null) : $timestamp->setTimezone(new DateTimeZone(date_default_timezone_get())); - if (empty($locale)) { - // get current locale - $locale = setlocale(LC_TIME, '0'); - } - // remove trailing part not supported by ext-intl locale - $locale = preg_replace('/[^\w-].*$/', '', $locale); + $locale = Locale::canonicalize($locale ?? setlocale(LC_TIME, '0')); $intl_formats = [ '%a' => 'EEE', // An abbreviated textual representation of the day Sun through Sat From 5128044af1c4fceb641a7b704f6b56646fefacc0 Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Fri, 26 Jan 2024 01:14:37 +0100 Subject: [PATCH 04/16] Test on matrix --- .github/workflows/php.yml | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 318786f..faa674a 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -12,10 +12,24 @@ permissions: jobs: build: - runs-on: ubuntu-latest + runs-on: ${{ matrix.operating-system }} + + strategy: + matrix: + operating-system: [ubuntu-latest, windows-latest, macOS-latest] + php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3'] + + name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} steps: - - uses: actions/checkout@v3 + - name: Checkout + uses: actions/checkout@v3 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + extensions: intl - name: Validate composer.json and composer.lock run: composer validate --strict From 1a8a39cd903467064c0bf0c23362e8b3f7bf82c9 Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Fri, 26 Jan 2024 01:19:27 +0100 Subject: [PATCH 05/16] Test only on ubuntu-latest --- .github/workflows/php.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index faa674a..ee3343e 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -16,7 +16,7 @@ jobs: strategy: matrix: - operating-system: [ubuntu-latest, windows-latest, macOS-latest] + operating-system: [ubuntu-latest] php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3'] name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} From 0b68f16d868123cd429bd836154ee8ec405b4954 Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Fri, 26 Jan 2024 01:29:24 +0100 Subject: [PATCH 06/16] Actions: update checkout and cache --- .github/workflows/php.yml | 4 ++-- tests/LocaleTests/Locale_en_EN_TestTrait.php | 4 ++-- tests/LocaleTests/Locale_es_ES_TestTrait.php | 6 +++--- tests/LocaleTests/Locale_eu_TestTrait.php | 2 +- tests/LocaleTests/Locale_it_CH_TestTrait.php | 2 +- tests/LocaleTests/Locale_it_IT_TestTrait.php | 2 +- tests/strftimeTest.php | 4 ++-- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index ee3343e..60a682d 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -23,7 +23,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Install PHP uses: shivammathur/setup-php@v2 @@ -36,7 +36,7 @@ jobs: - name: Cache Composer packages id: composer-cache - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: vendor key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} diff --git a/tests/LocaleTests/Locale_en_EN_TestTrait.php b/tests/LocaleTests/Locale_en_EN_TestTrait.php index f991b03..5a70bec 100644 --- a/tests/LocaleTests/Locale_en_EN_TestTrait.php +++ b/tests/LocaleTests/Locale_en_EN_TestTrait.php @@ -26,10 +26,10 @@ public function testLocale_en_EN () { $this->assertEquals('Mar', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)'); $result = strftime('%X', '20220306 13:02:03', $locale); - $this->assertEquals('1:02:03 PM', $result, '%X: Preferred time representation based on locale, without the date'); + $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertEquals('March 6, 2022 at 1:02 PM', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertEquals('March 6, 2022 at 13:02', $result, '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('3/6/22', $result, '%x: Preferred date representation based on locale, without the time'); diff --git a/tests/LocaleTests/Locale_es_ES_TestTrait.php b/tests/LocaleTests/Locale_es_ES_TestTrait.php index c810e53..e45cd72 100644 --- a/tests/LocaleTests/Locale_es_ES_TestTrait.php +++ b/tests/LocaleTests/Locale_es_ES_TestTrait.php @@ -11,19 +11,19 @@ public function testLocale_es_ES () { $locale = 'es-ES'; $result = strftime('%a', '20220306 13:02:03', $locale); - $this->assertEquals('dom.', $result, '%a: An abbreviated textual representation of the day'); + $this->assertEquals('dom', $result, '%a: An abbreviated textual representation of the day'); $result = strftime('%A', '20220306 13:02:03', $locale); $this->assertEquals('domingo', $result, '%A: A full textual representation of the day'); $result = strftime('%b', '20220306 13:02:03', $locale); - $this->assertEquals('mar.', $result, '%b: Abbreviated month name, based on the locale'); + $this->assertEquals('mar', $result, '%b: Abbreviated month name, based on the locale'); $result = strftime('%B', '20220306 13:02:03', $locale); $this->assertEquals('marzo', $result, '%B: Full month name, based on the locale'); $result = strftime('%h', '20220306 13:02:03', $locale); - $this->assertEquals('mar.', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)'); + $this->assertEquals('mar', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)'); $result = strftime('%X', '20220306 13:02:03', $locale); $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); diff --git a/tests/LocaleTests/Locale_eu_TestTrait.php b/tests/LocaleTests/Locale_eu_TestTrait.php index fabeb0a..70e2083 100644 --- a/tests/LocaleTests/Locale_eu_TestTrait.php +++ b/tests/LocaleTests/Locale_eu_TestTrait.php @@ -29,7 +29,7 @@ public function testLocale_eu () { $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertEquals('2022(e)ko martxoaren 6(a) 13:02', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertEquals('2022(e)ko martxoaren 6(a) (13:02)', $result, '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('22/3/6', $result, '%x: Preferred date representation based on locale, without the time'); diff --git a/tests/LocaleTests/Locale_it_CH_TestTrait.php b/tests/LocaleTests/Locale_it_CH_TestTrait.php index 9400b85..1e31b2f 100644 --- a/tests/LocaleTests/Locale_it_CH_TestTrait.php +++ b/tests/LocaleTests/Locale_it_CH_TestTrait.php @@ -29,7 +29,7 @@ public function testLocale_it_CH () { $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertEquals('6 marzo 2022 13:02', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertEquals('6 marzo 2022 alle ore 13:02', $result, '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('06.03.22', $result, '%x: Preferred date representation based on locale, without the time'); diff --git a/tests/LocaleTests/Locale_it_IT_TestTrait.php b/tests/LocaleTests/Locale_it_IT_TestTrait.php index b0dd9b0..053d88e 100644 --- a/tests/LocaleTests/Locale_it_IT_TestTrait.php +++ b/tests/LocaleTests/Locale_it_IT_TestTrait.php @@ -29,7 +29,7 @@ public function testLocale_it_IT () { $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertEquals('6 marzo 2022 13:02', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertEquals('6 marzo 2022 alle ore 13:02', $result, '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('06/03/22', $result, '%x: Preferred date representation based on locale, without the time'); diff --git a/tests/strftimeTest.php b/tests/strftimeTest.php index 04f7a4e..5cbbf7d 100644 --- a/tests/strftimeTest.php +++ b/tests/strftimeTest.php @@ -135,7 +135,7 @@ public function testTimeFormats () { $this->assertEquals('13:02:03', $result, '%T: Same as "%H:%M:%S"'); $result = strftime('%X', '20220306 13:02:03', 'en-EN'); - $this->assertEquals('1:02:03 PM', $result, '%X: Preferred time representation based on locale, without the date'); + $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%z', '20220306 13:02:03'); $this->assertEquals('+0100', $result, '%z: The time zone offset'); @@ -146,7 +146,7 @@ public function testTimeFormats () { public function testStampsFormats () { $result = strftime('%c', '20220306 13:02:03', 'en-EN'); - $this->assertEquals('March 6, 2022 at 1:02 PM', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertEquals('March 6, 2022 at 13:02', $result, '%c: Preferred date and time stamp based on locale'); $result = strftime('%D', '20220306 13:02:03'); $this->assertEquals('03/06/2022', $result, '%D: Same as "%m/%d/%y"'); From 5b49dded2df1683a65892a5a2b9cc62ade451b5b Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Fri, 26 Jan 2024 01:32:41 +0100 Subject: [PATCH 07/16] Fix test with PHP-8.3 --- tests/LocaleTests/Locale_eu_TestTrait.php | 2 +- tests/LocaleTests/Locale_it_CH_TestTrait.php | 2 +- tests/LocaleTests/Locale_it_IT_TestTrait.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/LocaleTests/Locale_eu_TestTrait.php b/tests/LocaleTests/Locale_eu_TestTrait.php index 70e2083..e8346e8 100644 --- a/tests/LocaleTests/Locale_eu_TestTrait.php +++ b/tests/LocaleTests/Locale_eu_TestTrait.php @@ -20,7 +20,7 @@ public function testLocale_eu () { $this->assertEquals('mar.', $result, '%b: Abbreviated month name, based on the locale'); $result = strftime('%B', '20220306 13:02:03', $locale); - $this->assertEquals('martxoa', $result, '%B: Full month name, based on the locale'); + $this->assertEquals('martxoak', $result, '%B: Full month name, based on the locale'); $result = strftime('%h', '20220306 13:02:03', $locale); $this->assertEquals('mar.', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)'); diff --git a/tests/LocaleTests/Locale_it_CH_TestTrait.php b/tests/LocaleTests/Locale_it_CH_TestTrait.php index 1e31b2f..9400b85 100644 --- a/tests/LocaleTests/Locale_it_CH_TestTrait.php +++ b/tests/LocaleTests/Locale_it_CH_TestTrait.php @@ -29,7 +29,7 @@ public function testLocale_it_CH () { $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertEquals('6 marzo 2022 alle ore 13:02', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertEquals('6 marzo 2022 13:02', $result, '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('06.03.22', $result, '%x: Preferred date representation based on locale, without the time'); diff --git a/tests/LocaleTests/Locale_it_IT_TestTrait.php b/tests/LocaleTests/Locale_it_IT_TestTrait.php index 053d88e..b0dd9b0 100644 --- a/tests/LocaleTests/Locale_it_IT_TestTrait.php +++ b/tests/LocaleTests/Locale_it_IT_TestTrait.php @@ -29,7 +29,7 @@ public function testLocale_it_IT () { $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertEquals('6 marzo 2022 alle ore 13:02', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertEquals('6 marzo 2022 13:02', $result, '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('06/03/22', $result, '%x: Preferred date representation based on locale, without the time'); From 35bcac15f8efb2f821464972383fc2a4a86d8456 Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Fri, 26 Jan 2024 01:35:21 +0100 Subject: [PATCH 08/16] Fix test locale eu --- tests/LocaleTests/Locale_eu_TestTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/LocaleTests/Locale_eu_TestTrait.php b/tests/LocaleTests/Locale_eu_TestTrait.php index e8346e8..27a69e7 100644 --- a/tests/LocaleTests/Locale_eu_TestTrait.php +++ b/tests/LocaleTests/Locale_eu_TestTrait.php @@ -29,7 +29,7 @@ public function testLocale_eu () { $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertEquals('2022(e)ko martxoaren 6(a) (13:02)', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertEquals('2022(e)ko martxoakren 6(a) 13:02', $result, '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('22/3/6', $result, '%x: Preferred date representation based on locale, without the time'); From 63e87e21e2c95d301d7e6b83961471ccf7178743 Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Fri, 26 Jan 2024 01:37:37 +0100 Subject: [PATCH 09/16] Action: add Windows and macOS test --- .github/workflows/php.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 60a682d..56eda52 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -16,7 +16,7 @@ jobs: strategy: matrix: - operating-system: [ubuntu-latest] + operating-system: [ubuntu-latest, windows-latest, macOS-latest] php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3'] name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} From 335e77704661bdf4e5ea9c265f055db3b99723b9 Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Fri, 26 Jan 2024 01:46:17 +0100 Subject: [PATCH 10/16] Fix test --- tests/LocaleTests/Locale_eu_TestTrait.php | 4 ++-- tests/LocaleTests/Locale_it_CH_TestTrait.php | 2 +- tests/LocaleTests/Locale_it_IT_TestTrait.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/LocaleTests/Locale_eu_TestTrait.php b/tests/LocaleTests/Locale_eu_TestTrait.php index 27a69e7..f9abb7b 100644 --- a/tests/LocaleTests/Locale_eu_TestTrait.php +++ b/tests/LocaleTests/Locale_eu_TestTrait.php @@ -20,7 +20,7 @@ public function testLocale_eu () { $this->assertEquals('mar.', $result, '%b: Abbreviated month name, based on the locale'); $result = strftime('%B', '20220306 13:02:03', $locale); - $this->assertEquals('martxoak', $result, '%B: Full month name, based on the locale'); + $this->assertMatchesRegularExpression('|martxoak?|', $result, '%B: Full month name, based on the locale'); $result = strftime('%h', '20220306 13:02:03', $locale); $this->assertEquals('mar.', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)'); @@ -29,7 +29,7 @@ public function testLocale_eu () { $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertEquals('2022(e)ko martxoakren 6(a) 13:02', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertMatchesRegularExpression('|2022\(e\)ko martxoak?ren 6\(a\) \(?13:02\)?|', $result, '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('22/3/6', $result, '%x: Preferred date representation based on locale, without the time'); diff --git a/tests/LocaleTests/Locale_it_CH_TestTrait.php b/tests/LocaleTests/Locale_it_CH_TestTrait.php index 9400b85..81f89e5 100644 --- a/tests/LocaleTests/Locale_it_CH_TestTrait.php +++ b/tests/LocaleTests/Locale_it_CH_TestTrait.php @@ -29,7 +29,7 @@ public function testLocale_it_CH () { $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertEquals('6 marzo 2022 13:02', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertMatchesRegularExpression('|6 marzo 2022 (alle ore )?13:02|', $result, '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('06.03.22', $result, '%x: Preferred date representation based on locale, without the time'); diff --git a/tests/LocaleTests/Locale_it_IT_TestTrait.php b/tests/LocaleTests/Locale_it_IT_TestTrait.php index b0dd9b0..f0dc844 100644 --- a/tests/LocaleTests/Locale_it_IT_TestTrait.php +++ b/tests/LocaleTests/Locale_it_IT_TestTrait.php @@ -29,7 +29,7 @@ public function testLocale_it_IT () { $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertEquals('6 marzo 2022 13:02', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertMatchesRegularExpression('|6 marzo 2022 (alle ore )?13:02|', $result, '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('06/03/22', $result, '%x: Preferred date representation based on locale, without the time'); From 6c127a75a13b79238df1cfcab374dfe8f3bc4f73 Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Fri, 26 Jan 2024 01:54:45 +0100 Subject: [PATCH 11/16] Fix tests --- tests/LocaleTests/Locale_en_EN_TestTrait.php | 2 +- tests/LocaleTests/Locale_es_ES_TestTrait.php | 2 +- tests/LocaleTests/Locale_eu_TestTrait.php | 4 ++-- tests/LocaleTests/Locale_it_CH_TestTrait.php | 2 +- tests/LocaleTests/Locale_it_IT_TestTrait.php | 2 +- tests/strftimeTest.php | 4 ++-- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/LocaleTests/Locale_en_EN_TestTrait.php b/tests/LocaleTests/Locale_en_EN_TestTrait.php index 5a70bec..49fc99b 100644 --- a/tests/LocaleTests/Locale_en_EN_TestTrait.php +++ b/tests/LocaleTests/Locale_en_EN_TestTrait.php @@ -26,7 +26,7 @@ public function testLocale_en_EN () { $this->assertEquals('Mar', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)'); $result = strftime('%X', '20220306 13:02:03', $locale); - $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); + $this->assertMatchesRegularExpression('~1:02:03 PM|13:02:03~', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); $this->assertEquals('March 6, 2022 at 13:02', $result, '%c: Preferred date and time stamp based on locale'); diff --git a/tests/LocaleTests/Locale_es_ES_TestTrait.php b/tests/LocaleTests/Locale_es_ES_TestTrait.php index e45cd72..37d43d4 100644 --- a/tests/LocaleTests/Locale_es_ES_TestTrait.php +++ b/tests/LocaleTests/Locale_es_ES_TestTrait.php @@ -11,7 +11,7 @@ public function testLocale_es_ES () { $locale = 'es-ES'; $result = strftime('%a', '20220306 13:02:03', $locale); - $this->assertEquals('dom', $result, '%a: An abbreviated textual representation of the day'); + $this->assertMatchesRegularExpression('~dom\.?~', $result, '%a: An abbreviated textual representation of the day'); $result = strftime('%A', '20220306 13:02:03', $locale); $this->assertEquals('domingo', $result, '%A: A full textual representation of the day'); diff --git a/tests/LocaleTests/Locale_eu_TestTrait.php b/tests/LocaleTests/Locale_eu_TestTrait.php index f9abb7b..7e03a0c 100644 --- a/tests/LocaleTests/Locale_eu_TestTrait.php +++ b/tests/LocaleTests/Locale_eu_TestTrait.php @@ -20,7 +20,7 @@ public function testLocale_eu () { $this->assertEquals('mar.', $result, '%b: Abbreviated month name, based on the locale'); $result = strftime('%B', '20220306 13:02:03', $locale); - $this->assertMatchesRegularExpression('|martxoak?|', $result, '%B: Full month name, based on the locale'); + $this->assertMatchesRegularExpression('~martxoak?~', $result, '%B: Full month name, based on the locale'); $result = strftime('%h', '20220306 13:02:03', $locale); $this->assertEquals('mar.', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)'); @@ -29,7 +29,7 @@ public function testLocale_eu () { $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertMatchesRegularExpression('|2022\(e\)ko martxoak?ren 6\(a\) \(?13:02\)?|', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertMatchesRegularExpression('~2022\(e\)ko martxoak?ren 6\(a\) \(?13:02\)?~', $result, '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('22/3/6', $result, '%x: Preferred date representation based on locale, without the time'); diff --git a/tests/LocaleTests/Locale_it_CH_TestTrait.php b/tests/LocaleTests/Locale_it_CH_TestTrait.php index 81f89e5..2ba2fcc 100644 --- a/tests/LocaleTests/Locale_it_CH_TestTrait.php +++ b/tests/LocaleTests/Locale_it_CH_TestTrait.php @@ -29,7 +29,7 @@ public function testLocale_it_CH () { $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertMatchesRegularExpression('|6 marzo 2022 (alle ore )?13:02|', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertMatchesRegularExpression('~6 marzo 2022 (alle ore )?13:02~', $result, '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('06.03.22', $result, '%x: Preferred date representation based on locale, without the time'); diff --git a/tests/LocaleTests/Locale_it_IT_TestTrait.php b/tests/LocaleTests/Locale_it_IT_TestTrait.php index f0dc844..c398ad3 100644 --- a/tests/LocaleTests/Locale_it_IT_TestTrait.php +++ b/tests/LocaleTests/Locale_it_IT_TestTrait.php @@ -29,7 +29,7 @@ public function testLocale_it_IT () { $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertMatchesRegularExpression('|6 marzo 2022 (alle ore )?13:02|', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertMatchesRegularExpression('~6 marzo 2022 (alle ore )?13:02~', $result, '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('06/03/22', $result, '%x: Preferred date representation based on locale, without the time'); diff --git a/tests/strftimeTest.php b/tests/strftimeTest.php index 5cbbf7d..42cbea6 100644 --- a/tests/strftimeTest.php +++ b/tests/strftimeTest.php @@ -135,7 +135,7 @@ public function testTimeFormats () { $this->assertEquals('13:02:03', $result, '%T: Same as "%H:%M:%S"'); $result = strftime('%X', '20220306 13:02:03', 'en-EN'); - $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); + $this->assertMatchesRegularExpression('~1:02:03 PM|13:02:03~', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%z', '20220306 13:02:03'); $this->assertEquals('+0100', $result, '%z: The time zone offset'); @@ -146,7 +146,7 @@ public function testTimeFormats () { public function testStampsFormats () { $result = strftime('%c', '20220306 13:02:03', 'en-EN'); - $this->assertEquals('March 6, 2022 at 13:02', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertMatchesRegularExpression('~March 6, 2022 at (1:02 PM|13:02)~', $result, '%c: Preferred date and time stamp based on locale'); $result = strftime('%D', '20220306 13:02:03'); $this->assertEquals('03/06/2022', $result, '%D: Same as "%m/%d/%y"'); From 7bfa538530d5c877501da8567e9c4a6c401285f4 Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Fri, 26 Jan 2024 01:58:25 +0100 Subject: [PATCH 12/16] Fix tests: PHP-7.4 on Windows --- tests/LocaleTests/Locale_en_EN_TestTrait.php | 2 +- tests/LocaleTests/Locale_es_ES_TestTrait.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/LocaleTests/Locale_en_EN_TestTrait.php b/tests/LocaleTests/Locale_en_EN_TestTrait.php index 49fc99b..4f0baca 100644 --- a/tests/LocaleTests/Locale_en_EN_TestTrait.php +++ b/tests/LocaleTests/Locale_en_EN_TestTrait.php @@ -29,7 +29,7 @@ public function testLocale_en_EN () { $this->assertMatchesRegularExpression('~1:02:03 PM|13:02:03~', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertEquals('March 6, 2022 at 13:02', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertMatchesRegularExpression('~March 6, 2022 at (1:02 PM|13:02)~', $result, '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('3/6/22', $result, '%x: Preferred date representation based on locale, without the time'); diff --git a/tests/LocaleTests/Locale_es_ES_TestTrait.php b/tests/LocaleTests/Locale_es_ES_TestTrait.php index 37d43d4..681efc5 100644 --- a/tests/LocaleTests/Locale_es_ES_TestTrait.php +++ b/tests/LocaleTests/Locale_es_ES_TestTrait.php @@ -17,7 +17,7 @@ public function testLocale_es_ES () { $this->assertEquals('domingo', $result, '%A: A full textual representation of the day'); $result = strftime('%b', '20220306 13:02:03', $locale); - $this->assertEquals('mar', $result, '%b: Abbreviated month name, based on the locale'); + $this->assertMatchesRegularExpression('~mar\.?~', $result, '%b: Abbreviated month name, based on the locale'); $result = strftime('%B', '20220306 13:02:03', $locale); $this->assertEquals('marzo', $result, '%B: Full month name, based on the locale'); From 2a8c562d2d2a6a4fd6a2cb7a675aaaa4d98e95f6 Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Fri, 26 Jan 2024 02:00:47 +0100 Subject: [PATCH 13/16] Fix tests: PHP-7.4 on Windows --- tests/LocaleTests/Locale_es_ES_TestTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/LocaleTests/Locale_es_ES_TestTrait.php b/tests/LocaleTests/Locale_es_ES_TestTrait.php index 681efc5..0b626a0 100644 --- a/tests/LocaleTests/Locale_es_ES_TestTrait.php +++ b/tests/LocaleTests/Locale_es_ES_TestTrait.php @@ -23,7 +23,7 @@ public function testLocale_es_ES () { $this->assertEquals('marzo', $result, '%B: Full month name, based on the locale'); $result = strftime('%h', '20220306 13:02:03', $locale); - $this->assertEquals('mar', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)'); + $this->assertMatchesRegularExpression('~mar\.?~', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)'); $result = strftime('%X', '20220306 13:02:03', $locale); $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); From 17f841965baad5985cb69d056347713578fb3ecb Mon Sep 17 00:00:00 2001 From: Fernando Herrero Date: Fri, 26 Jan 2024 03:03:31 +0100 Subject: [PATCH 14/16] Tests: use assertThat instead assertMatchesRegularExpression --- tests/LocaleTests/Locale_en_EN_TestTrait.php | 10 ++++++++-- tests/LocaleTests/Locale_es_ES_TestTrait.php | 15 ++++++++++++--- tests/LocaleTests/Locale_eu_TestTrait.php | 11 +++++++++-- tests/LocaleTests/Locale_it_CH_TestTrait.php | 5 ++++- tests/LocaleTests/Locale_it_IT_TestTrait.php | 5 ++++- tests/strftimeTest.php | 10 ++++++++-- 6 files changed, 45 insertions(+), 11 deletions(-) diff --git a/tests/LocaleTests/Locale_en_EN_TestTrait.php b/tests/LocaleTests/Locale_en_EN_TestTrait.php index 4f0baca..a11be1e 100644 --- a/tests/LocaleTests/Locale_en_EN_TestTrait.php +++ b/tests/LocaleTests/Locale_en_EN_TestTrait.php @@ -26,10 +26,16 @@ public function testLocale_en_EN () { $this->assertEquals('Mar', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)'); $result = strftime('%X', '20220306 13:02:03', $locale); - $this->assertMatchesRegularExpression('~1:02:03 PM|13:02:03~', $result, '%X: Preferred time representation based on locale, without the date'); + $this->assertThat($result, $this->logicalOr( + $this->equalTo('1:02:03 PM'), // PHP-7 + $this->equalTo('13:02:03') // PHP-8 + ), '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertMatchesRegularExpression('~March 6, 2022 at (1:02 PM|13:02)~', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertThat($result, $this->logicalOr( + $this->equalTo('March 6, 2022 at 1:02 PM'), // PHP-7 + $this->equalTo('March 6, 2022 at 13:02') // PHP-8 + ), '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('3/6/22', $result, '%x: Preferred date representation based on locale, without the time'); diff --git a/tests/LocaleTests/Locale_es_ES_TestTrait.php b/tests/LocaleTests/Locale_es_ES_TestTrait.php index 0b626a0..a009735 100644 --- a/tests/LocaleTests/Locale_es_ES_TestTrait.php +++ b/tests/LocaleTests/Locale_es_ES_TestTrait.php @@ -11,19 +11,28 @@ public function testLocale_es_ES () { $locale = 'es-ES'; $result = strftime('%a', '20220306 13:02:03', $locale); - $this->assertMatchesRegularExpression('~dom\.?~', $result, '%a: An abbreviated textual representation of the day'); + $this->assertThat($result, $this->logicalOr( + $this->equalTo('dom.'), // PHP-7 + $this->equalTo('dom') // PHP-8 + ), '%a: An abbreviated textual representation of the day'); $result = strftime('%A', '20220306 13:02:03', $locale); $this->assertEquals('domingo', $result, '%A: A full textual representation of the day'); $result = strftime('%b', '20220306 13:02:03', $locale); - $this->assertMatchesRegularExpression('~mar\.?~', $result, '%b: Abbreviated month name, based on the locale'); + $this->assertThat($result, $this->logicalOr( + $this->equalTo('mar.'), // PHP-7 + $this->equalTo('mar') // PHP-8 + ), '%b: Abbreviated month name, based on the locale'); $result = strftime('%B', '20220306 13:02:03', $locale); $this->assertEquals('marzo', $result, '%B: Full month name, based on the locale'); $result = strftime('%h', '20220306 13:02:03', $locale); - $this->assertMatchesRegularExpression('~mar\.?~', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)'); + $this->assertThat($result, $this->logicalOr( + $this->equalTo('mar.'), // PHP-7 + $this->equalTo('mar') // PHP-8 + ), '%h: Abbreviated month name, based on the locale (an alias of %b)'); $result = strftime('%X', '20220306 13:02:03', $locale); $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); diff --git a/tests/LocaleTests/Locale_eu_TestTrait.php b/tests/LocaleTests/Locale_eu_TestTrait.php index 7e03a0c..2d30502 100644 --- a/tests/LocaleTests/Locale_eu_TestTrait.php +++ b/tests/LocaleTests/Locale_eu_TestTrait.php @@ -20,7 +20,10 @@ public function testLocale_eu () { $this->assertEquals('mar.', $result, '%b: Abbreviated month name, based on the locale'); $result = strftime('%B', '20220306 13:02:03', $locale); - $this->assertMatchesRegularExpression('~martxoak?~', $result, '%B: Full month name, based on the locale'); + $this->assertThat($result, $this->logicalOr( + $this->equalTo('martxoak'), + $this->equalTo('martxoa') // PHP-7.4, PHP-8.3 + ), '%B: Full month name, based on the locale'); $result = strftime('%h', '20220306 13:02:03', $locale); $this->assertEquals('mar.', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)'); @@ -29,7 +32,11 @@ public function testLocale_eu () { $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertMatchesRegularExpression('~2022\(e\)ko martxoak?ren 6\(a\) \(?13:02\)?~', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertThat($result, $this->logicalOr( + $this->equalTo('2022(e)ko martxoaren 6(a) 13:02'), // PHP-7.4 + $this->equalTo('2022(e)ko martxoakren 6(a) 13:02'), + $this->equalTo('2022(e)ko martxoaren 6(a) (13:02)') // PHP-8.3 + ), '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('22/3/6', $result, '%x: Preferred date representation based on locale, without the time'); diff --git a/tests/LocaleTests/Locale_it_CH_TestTrait.php b/tests/LocaleTests/Locale_it_CH_TestTrait.php index 2ba2fcc..335b3be 100644 --- a/tests/LocaleTests/Locale_it_CH_TestTrait.php +++ b/tests/LocaleTests/Locale_it_CH_TestTrait.php @@ -29,7 +29,10 @@ public function testLocale_it_CH () { $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertMatchesRegularExpression('~6 marzo 2022 (alle ore )?13:02~', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertThat($result, $this->logicalOr( + $this->equalTo('6 marzo 2022 13:02'), + $this->equalTo('6 marzo 2022 alle ore 13:02') // PHP-8.3 + ), '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('06.03.22', $result, '%x: Preferred date representation based on locale, without the time'); diff --git a/tests/LocaleTests/Locale_it_IT_TestTrait.php b/tests/LocaleTests/Locale_it_IT_TestTrait.php index c398ad3..ab6737e 100644 --- a/tests/LocaleTests/Locale_it_IT_TestTrait.php +++ b/tests/LocaleTests/Locale_it_IT_TestTrait.php @@ -29,7 +29,10 @@ public function testLocale_it_IT () { $this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date'); $result = strftime('%c', '20220306 13:02:03', $locale); - $this->assertMatchesRegularExpression('~6 marzo 2022 (alle ore )?13:02~', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertThat($result, $this->logicalOr( + $this->equalTo('6 marzo 2022 13:02'), + $this->equalTo('6 marzo 2022 alle ore 13:02') // PHP-8.3 + ), '%c: Preferred date and time stamp based on locale'); $result = strftime('%x', '20220306 13:02:03', $locale); $this->assertEquals('06/03/22', $result, '%x: Preferred date representation based on locale, without the time'); diff --git a/tests/strftimeTest.php b/tests/strftimeTest.php index 42cbea6..a0ca3fe 100644 --- a/tests/strftimeTest.php +++ b/tests/strftimeTest.php @@ -135,7 +135,10 @@ public function testTimeFormats () { $this->assertEquals('13:02:03', $result, '%T: Same as "%H:%M:%S"'); $result = strftime('%X', '20220306 13:02:03', 'en-EN'); - $this->assertMatchesRegularExpression('~1:02:03 PM|13:02:03~', $result, '%X: Preferred time representation based on locale, without the date'); + $this->assertThat($result, $this->logicalOr( + $this->equalTo('1:02:03 PM'), + $this->equalTo('13:02:03') // PHP-8 + ), '%X: Preferred time representation based on locale, without the date'); $result = strftime('%z', '20220306 13:02:03'); $this->assertEquals('+0100', $result, '%z: The time zone offset'); @@ -146,7 +149,10 @@ public function testTimeFormats () { public function testStampsFormats () { $result = strftime('%c', '20220306 13:02:03', 'en-EN'); - $this->assertMatchesRegularExpression('~March 6, 2022 at (1:02 PM|13:02)~', $result, '%c: Preferred date and time stamp based on locale'); + $this->assertThat($result, $this->logicalOr( + $this->equalTo('March 6, 2022 at 1:02 PM'), + $this->equalTo('March 6, 2022 at 13:02') // PHP-8 + ), '%c: Preferred date and time stamp based on locale'); $result = strftime('%D', '20220306 13:02:03'); $this->assertEquals('03/06/2022', $result, '%D: Same as "%m/%d/%y"'); From b6376b3217e76aa2472b6daaa3ff5606c2342795 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 26 Jan 2024 18:41:50 +0100 Subject: [PATCH 15/16] phpunit 10 compatibility In phpunit 10 expectNotice is no longer available. This converts the notice into an ErrorException and tests for that. --- tests/DateLocaleFormatterTest.php | 8 ++++++++ tests/LocaleTests/Locale_en_EN_TestTrait.php | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/DateLocaleFormatterTest.php b/tests/DateLocaleFormatterTest.php index 2223bbe..6b8a84d 100644 --- a/tests/DateLocaleFormatterTest.php +++ b/tests/DateLocaleFormatterTest.php @@ -13,9 +13,17 @@ class DateLocaleFormatterTest extends TestCase { public static function setUpBeforeClass () : void { date_default_timezone_set('Europe/Madrid'); $_SERVER['STRFTIME_NO_INTL'] = true; + + set_error_handler( + static function ( $errno, $errstr ) { + throw new \ErrorException( $errstr, $errno ); + }, + E_ALL + ); } public static function tearDownAfterClass () : void { unset($_SERVER['STRFTIME_NO_INTL']); + restore_error_handler(); } } diff --git a/tests/LocaleTests/Locale_en_EN_TestTrait.php b/tests/LocaleTests/Locale_en_EN_TestTrait.php index e11e61d..1446364 100644 --- a/tests/LocaleTests/Locale_en_EN_TestTrait.php +++ b/tests/LocaleTests/Locale_en_EN_TestTrait.php @@ -11,8 +11,8 @@ public function testLocale_en_EN () { $locale = 'en-EN'; if (__CLASS__ == 'PHP81_BC\Tests\DateLocaleFormatterTest') { - $this->expectNotice(); - $this->expectNoticeMessage('Formatting without \\IntlDateFormatter only return english formats'); + $this->expectException(\ErrorException::class); + $this->expectExceptionMessage('Formatting without \\IntlDateFormatter only return english formats'); } $result = strftime('%a', '20220306 13:02:03', $locale); From 0fb385efe17f59c2e6d68574df831c0be76f9589 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 26 Jan 2024 18:43:56 +0100 Subject: [PATCH 16/16] Use Locale::canonicalize only if intl is available When intl isn't available, we don't really care about the locale anyway, since only english results are returned --- src/php-8.1-strftime.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/php-8.1-strftime.php b/src/php-8.1-strftime.php index d31318a..f8a8c79 100644 --- a/src/php-8.1-strftime.php +++ b/src/php-8.1-strftime.php @@ -6,7 +6,6 @@ use DateTimeZone; use Exception; use InvalidArgumentException; - use Locale; /** * Locale-formatted strftime using IntlDateFormatter (PHP 8.1 compatible) @@ -39,9 +38,8 @@ 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')); - 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); } else { $locale_formatter = new \PHP81_BC\strftime\DateLocaleFormatter($locale);