Skip to content

Commit 51a4ef7

Browse files
committed
More tests for locations.
1 parent e27834d commit 51a4ef7

8 files changed

+299
-25
lines changed

composer.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
"test": "Run PHPUnit tests"
2727
},
2828
"autoload": {
29-
"files": ["src/php-8.1-strftime.php"]
29+
"files": [
30+
"src/php-8.1-strftime.php"
31+
]
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"PHP81_BC\\Tests\\": "tests/"
36+
}
3037
}
3138
}

tests/LocaleFormatterTestTrait.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PHP81_BC\Tests;
5+
6+
use PHP81_BC\Tests\LocaleTests\Locale_en_EN_TestTrait;
7+
use PHP81_BC\Tests\LocaleTests\Locale_es_ES_TestTrait;
8+
use PHP81_BC\Tests\LocaleTests\Locale_eu_TestTrait;
9+
use PHP81_BC\Tests\LocaleTests\Locale_it_CH_TestTrait;
10+
use PHP81_BC\Tests\LocaleTests\Locale_it_IT_TestTrait;
11+
12+
trait LocaleFormatterTestTrait {
13+
use Locale_en_EN_TestTrait;
14+
use Locale_es_ES_TestTrait;
15+
use Locale_eu_TestTrait;
16+
use Locale_it_CH_TestTrait;
17+
use Locale_it_IT_TestTrait;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PHP81_BC\Tests\LocaleTests;
5+
6+
use DateTimeImmutable;
7+
use function PHP81_BC\strftime;
8+
9+
trait Locale_en_EN_TestTrait {
10+
public function testLocale_en_EN () {
11+
$locale = 'en-EN';
12+
13+
$result = strftime('%a', '20220306 13:02:03', $locale);
14+
$this->assertEquals('Sun', $result, '%a: An abbreviated textual representation of the day');
15+
16+
$result = strftime('%A', '20220306 13:02:03', $locale);
17+
$this->assertEquals('Sunday', $result, '%A: A full textual representation of the day');
18+
19+
$result = strftime('%b', '20220306 13:02:03', $locale);
20+
$this->assertEquals('Mar', $result, '%b: Abbreviated month name, based on the locale');
21+
22+
$result = strftime('%B', '20220306 13:02:03', $locale);
23+
$this->assertEquals('March', $result, '%B: Full month name, based on the locale');
24+
25+
$result = strftime('%h', '20220306 13:02:03', $locale);
26+
$this->assertEquals('Mar', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)');
27+
28+
$result = strftime('%X', '20220306 13:02:03', $locale);
29+
$this->assertEquals('1:02:03 PM', $result, '%X: Preferred time representation based on locale, without the date');
30+
31+
$result = strftime('%c', '20220306 13:02:03', $locale);
32+
$this->assertEquals('March 6, 2022 at 1:02 PM', $result, '%c: Preferred date and time stamp based on locale');
33+
34+
$result = strftime('%x', '20220306 13:02:03', $locale);
35+
$this->assertEquals('3/6/22', $result, '%x: Preferred date representation based on locale, without the time');
36+
37+
// 1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian
38+
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-01')->getTimestamp();
39+
$result = strftime('%F: %x', $prolepticTimestamp, $locale);
40+
$this->assertEquals('1582-10-01: 10/1/82', $result, '1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian');
41+
42+
// In much of Europe, the 10th October 1582 never existed
43+
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-10')->getTimestamp();
44+
$result = strftime('%F: %x', $prolepticTimestamp, $locale);
45+
$this->assertEquals('1582-10-10: 10/10/82', $result, 'In much of Europe, the 10th October 1582 never existed');
46+
47+
// The 15th October was the first day after the cutover, after which both systems agree
48+
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-15')->getTimestamp();
49+
$result = strftime('%F: %x', $prolepticTimestamp, $locale);
50+
$this->assertEquals('1582-10-15: 10/15/82', $result, 'The 15th October was the first day after the cutover, after which both systems agree');
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PHP81_BC\Tests\LocaleTests;
5+
6+
use DateTimeImmutable;
7+
use function PHP81_BC\strftime;
8+
9+
trait Locale_es_ES_TestTrait {
10+
public function testLocale_es_ES () {
11+
$locale = 'es-ES';
12+
13+
$result = strftime('%a', '20220306 13:02:03', $locale);
14+
$this->assertEquals('dom.', $result, '%a: An abbreviated textual representation of the day');
15+
16+
$result = strftime('%A', '20220306 13:02:03', $locale);
17+
$this->assertEquals('domingo', $result, '%A: A full textual representation of the day');
18+
19+
$result = strftime('%b', '20220306 13:02:03', $locale);
20+
$this->assertEquals('mar.', $result, '%b: Abbreviated month name, based on the locale');
21+
22+
$result = strftime('%B', '20220306 13:02:03', $locale);
23+
$this->assertEquals('marzo', $result, '%B: Full month name, based on the locale');
24+
25+
$result = strftime('%h', '20220306 13:02:03', $locale);
26+
$this->assertEquals('mar.', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)');
27+
28+
$result = strftime('%X', '20220306 13:02:03', $locale);
29+
$this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date');
30+
31+
$result = strftime('%c', '20220306 13:02:03', $locale);
32+
$this->assertEquals('6 de marzo de 2022, 13:02', $result, '%c: Preferred date and time stamp based on locale');
33+
34+
$result = strftime('%x', '20220306 13:02:03', $locale);
35+
$this->assertEquals('6/3/22', $result, '%x: Preferred date representation based on locale, without the time');
36+
37+
// 1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian
38+
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-01')->getTimestamp();
39+
$result = strftime('%F: %x', $prolepticTimestamp, $locale);
40+
$this->assertEquals('1582-10-01: 1/10/82', $result, '1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian');
41+
42+
// In much of Europe, the 10th October 1582 never existed
43+
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-10')->getTimestamp();
44+
$result = strftime('%F: %x', $prolepticTimestamp, $locale);
45+
$this->assertEquals('1582-10-10: 10/10/82', $result, 'In much of Europe, the 10th October 1582 never existed');
46+
47+
// The 15th October was the first day after the cutover, after which both systems agree
48+
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-15')->getTimestamp();
49+
$result = strftime('%F: %x', $prolepticTimestamp, $locale);
50+
$this->assertEquals('1582-10-15: 15/10/82', $result, 'The 15th October was the first day after the cutover, after which both systems agree');
51+
}
52+
}
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PHP81_BC\Tests\LocaleTests;
5+
6+
use DateTimeImmutable;
7+
use function PHP81_BC\strftime;
8+
9+
trait Locale_eu_TestTrait {
10+
public function testLocale_eu () {
11+
$locale = 'eu';
12+
13+
$result = strftime('%a', '20220306 13:02:03', $locale);
14+
$this->assertEquals('ig.', $result, '%a: An abbreviated textual representation of the day');
15+
16+
$result = strftime('%A', '20220306 13:02:03', $locale);
17+
$this->assertEquals('igandea', $result, '%A: A full textual representation of the day');
18+
19+
$result = strftime('%b', '20220306 13:02:03', $locale);
20+
$this->assertEquals('mar.', $result, '%b: Abbreviated month name, based on the locale');
21+
22+
$result = strftime('%B', '20220306 13:02:03', $locale);
23+
$this->assertEquals('martxoa', $result, '%B: Full month name, based on the locale');
24+
25+
$result = strftime('%h', '20220306 13:02:03', $locale);
26+
$this->assertEquals('mar.', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)');
27+
28+
$result = strftime('%X', '20220306 13:02:03', $locale);
29+
$this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date');
30+
31+
$result = strftime('%c', '20220306 13:02:03', $locale);
32+
$this->assertEquals('2022(e)ko martxoaren 6(a) 13:02', $result, '%c: Preferred date and time stamp based on locale');
33+
34+
$result = strftime('%x', '20220306 13:02:03', $locale);
35+
$this->assertEquals('22/3/6', $result, '%x: Preferred date representation based on locale, without the time');
36+
37+
// 1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian
38+
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-01')->getTimestamp();
39+
$result = strftime('%F: %x', $prolepticTimestamp, $locale);
40+
$this->assertEquals('1582-10-01: 82/10/1', $result, '1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian');
41+
42+
// In much of Europe, the 10th October 1582 never existed
43+
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-10')->getTimestamp();
44+
$result = strftime('%F: %x', $prolepticTimestamp, $locale);
45+
$this->assertEquals('1582-10-10: 82/10/10', $result, 'In much of Europe, the 10th October 1582 never existed');
46+
47+
// The 15th October was the first day after the cutover, after which both systems agree
48+
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-15')->getTimestamp();
49+
$result = strftime('%F: %x', $prolepticTimestamp, $locale);
50+
$this->assertEquals('1582-10-15: 82/10/15', $result, 'The 15th October was the first day after the cutover, after which both systems agree');
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PHP81_BC\Tests\LocaleTests;
5+
6+
use DateTimeImmutable;
7+
use function PHP81_BC\strftime;
8+
9+
trait Locale_it_CH_TestTrait {
10+
public function testLocale_it_CH () {
11+
$locale = 'it-CH';
12+
13+
$result = strftime('%a', '20220306 13:02:03', $locale);
14+
$this->assertEquals('dom', $result, '%a: An abbreviated textual representation of the day');
15+
16+
$result = strftime('%A', '20220306 13:02:03', $locale);
17+
$this->assertEquals('domenica', $result, '%A: A full textual representation of the day');
18+
19+
$result = strftime('%b', '20220306 13:02:03', $locale);
20+
$this->assertEquals('mar', $result, '%b: Abbreviated month name, based on the locale');
21+
22+
$result = strftime('%B', '20220306 13:02:03', $locale);
23+
$this->assertEquals('marzo', $result, '%B: Full month name, based on the locale');
24+
25+
$result = strftime('%h', '20220306 13:02:03', $locale);
26+
$this->assertEquals('mar', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)');
27+
28+
$result = strftime('%X', '20220306 13:02:03', $locale);
29+
$this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date');
30+
31+
$result = strftime('%c', '20220306 13:02:03', $locale);
32+
$this->assertEquals('6 marzo 2022 13:02', $result, '%c: Preferred date and time stamp based on locale');
33+
34+
$result = strftime('%x', '20220306 13:02:03', $locale);
35+
$this->assertEquals('06.03.22', $result, '%x: Preferred date representation based on locale, without the time');
36+
37+
// 1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian
38+
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-01')->getTimestamp();
39+
$result = strftime('%F: %x', $prolepticTimestamp, $locale);
40+
$this->assertEquals('1582-10-01: 01.10.82', $result, '1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian');
41+
42+
// In much of Europe, the 10th October 1582 never existed
43+
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-10')->getTimestamp();
44+
$result = strftime('%F: %x', $prolepticTimestamp, $locale);
45+
$this->assertEquals('1582-10-10: 10.10.82', $result, 'In much of Europe, the 10th October 1582 never existed');
46+
47+
// The 15th October was the first day after the cutover, after which both systems agree
48+
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-15')->getTimestamp();
49+
$result = strftime('%F: %x', $prolepticTimestamp, $locale);
50+
$this->assertEquals('1582-10-15: 15.10.82', $result, 'The 15th October was the first day after the cutover, after which both systems agree');
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PHP81_BC\Tests\LocaleTests;
5+
6+
use DateTimeImmutable;
7+
use function PHP81_BC\strftime;
8+
9+
trait Locale_it_IT_TestTrait {
10+
public function testLocale_it_IT () {
11+
$locale = 'it-IT';
12+
13+
$result = strftime('%a', '20220306 13:02:03', $locale);
14+
$this->assertEquals('dom', $result, '%a: An abbreviated textual representation of the day');
15+
16+
$result = strftime('%A', '20220306 13:02:03', $locale);
17+
$this->assertEquals('domenica', $result, '%A: A full textual representation of the day');
18+
19+
$result = strftime('%b', '20220306 13:02:03', $locale);
20+
$this->assertEquals('mar', $result, '%b: Abbreviated month name, based on the locale');
21+
22+
$result = strftime('%B', '20220306 13:02:03', $locale);
23+
$this->assertEquals('marzo', $result, '%B: Full month name, based on the locale');
24+
25+
$result = strftime('%h', '20220306 13:02:03', $locale);
26+
$this->assertEquals('mar', $result, '%h: Abbreviated month name, based on the locale (an alias of %b)');
27+
28+
$result = strftime('%X', '20220306 13:02:03', $locale);
29+
$this->assertEquals('13:02:03', $result, '%X: Preferred time representation based on locale, without the date');
30+
31+
$result = strftime('%c', '20220306 13:02:03', $locale);
32+
$this->assertEquals('6 marzo 2022 13:02', $result, '%c: Preferred date and time stamp based on locale');
33+
34+
$result = strftime('%x', '20220306 13:02:03', $locale);
35+
$this->assertEquals('06/03/22', $result, '%x: Preferred date representation based on locale, without the time');
36+
37+
// 1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian
38+
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-01')->getTimestamp();
39+
$result = strftime('%F: %x', $prolepticTimestamp, $locale);
40+
$this->assertEquals('1582-10-01: 01/10/82', $result, '1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian');
41+
42+
// In much of Europe, the 10th October 1582 never existed
43+
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-10')->getTimestamp();
44+
$result = strftime('%F: %x', $prolepticTimestamp, $locale);
45+
$this->assertEquals('1582-10-10: 10/10/82', $result, 'In much of Europe, the 10th October 1582 never existed');
46+
47+
// The 15th October was the first day after the cutover, after which both systems agree
48+
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-15')->getTimestamp();
49+
$result = strftime('%F: %x', $prolepticTimestamp, $locale);
50+
$this->assertEquals('1582-10-15: 15/10/82', $result, 'The 15th October was the first day after the cutover, after which both systems agree');
51+
}
52+
}

tests/strftimeTest.php

+13-24
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
<?php
22
declare(strict_types=1);
33

4+
namespace PHP81_BC\Tests;
5+
6+
use DateTime;
7+
use DateTimeImmutable;
8+
use InvalidArgumentException;
49
use PHPUnit\Framework\TestCase;
510
use function PHP81_BC\strftime;
611

712
class strftimeTest extends TestCase {
13+
use LocaleFormatterTestTrait;
814

9-
public function setUp () : void {
15+
public static function setUpBeforeClass () : void {
1016
date_default_timezone_set('Europe/Madrid');
1117
}
1218

@@ -166,23 +172,6 @@ public function testMiscellaneousFormats () {
166172
$this->assertEquals('%', $result, '%%: A literal percentage character ("%")');
167173
}
168174

169-
public function testLocale () {
170-
$result = strftime('%x %X', '20220306 13:02:03', 'it_IT');
171-
$this->assertEquals('06/03/22 13:02:03', $result, 'Locale test for it_IT');
172-
173-
$result = strftime('%x %X', '20220306 13:02:03', 'it_CH');
174-
$this->assertEquals('06.03.22 13:02:03', $result, 'Locale test for it_CH');
175-
176-
$result = strftime('%c', '20220306 13:02:03', 'eu');
177-
$this->assertEquals('2022(e)ko martxoaren 6(a) 13:02', $result, '%c: Preferred date and time stamp based on locale');
178-
179-
$result = strftime('%b', '20220306 13:02:03', 'eu');
180-
$this->assertEquals('mar.', $result, '%b: Abbreviated month name, based on the locale');
181-
182-
$result = strftime('%B', '20220306 13:02:03', 'eu');
183-
$this->assertEquals('martxoa', $result, '%B: Full month name, based on the locale');
184-
}
185-
186175
/**
187176
* In October 1582, the Gregorian calendar replaced the Julian in much of Europe, and
188177
* the 4th October was followed by the 15th October.
@@ -195,17 +184,17 @@ public function testLocale () {
195184
public function testJulianCutover () {
196185
// 1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian
197186
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-01')->getTimestamp();
198-
$result = strftime('%x', $prolepticTimestamp, 'eu');
199-
$this->assertEquals('82/10/1', $result, '1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian');
187+
$result = strftime('%F: %x', $prolepticTimestamp, 'en-EN');
188+
$this->assertEquals('1582-10-01: 10/1/82', $result, '1st October 1582 in proleptic Gregorian is the same date as 21st September 1582 Julian');
200189

201190
// In much of Europe, the 10th October 1582 never existed
202191
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-10')->getTimestamp();
203-
$result = strftime('%x', $prolepticTimestamp, 'eu');
204-
$this->assertEquals('82/10/10', $result, 'In much of Europe, the 10th October 1582 never existed');
192+
$result = strftime('%F: %x', $prolepticTimestamp, 'en-EN');
193+
$this->assertEquals('1582-10-10: 10/10/82', $result, 'In much of Europe, the 10th October 1582 never existed');
205194

206195
// The 15th October was the first day after the cutover, after which both systems agree
207196
$prolepticTimestamp = DateTimeImmutable::createFromFormat('Y-m-d|', '1582-10-15')->getTimestamp();
208-
$result = strftime('%x', $prolepticTimestamp, 'eu');
209-
$this->assertEquals('82/10/15', $result, 'The 15th October was the first day after the cutover, after which both systems agree');
197+
$result = strftime('%F: %x', $prolepticTimestamp, 'en-EN');
198+
$this->assertEquals('1582-10-15: 10/15/82', $result, 'The 15th October was the first day after the cutover, after which both systems agree');
210199
}
211200
}

0 commit comments

Comments
 (0)