Fix edge case 200 b.c. in NodaSplitTime

This commit is contained in:
Joshua Ramon Enslin 2024-07-08 01:27:03 +02:00
parent 27528c9cf7
commit 58d3569718
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
4 changed files with 47 additions and 11 deletions

View File

@ -306,6 +306,23 @@ final class NodaSplitTime {
false|string $end_date = false,
) {
if (substr($start_year, 0, 1) === '-') {
if (strlen($start_year) > 5) $start_year = '-' . str_pad(trim($start_year, '-'), 4, '-', STR_PAD_LEFT);
}
if ($start_date !== false && str_starts_with($start_date, '-')) {
$parts = explode('-', trim($start_date, '-'));
$parts[0] = str_pad($parts[0], 4, '0', STR_PAD_LEFT);
$start_date = '-' . implode('-', $parts);
}
if (substr($end_year, 0, 1) === '-') {
if (strlen($end_year) > 5) $end_year = '-' . str_pad(trim($end_year, '-'), 4, '-', STR_PAD_LEFT);
}
if ($end_date !== false && str_starts_with($end_date, '-')) {
$parts = explode('-', trim($end_date, '-'));
$parts[0] = str_pad($parts[0], 4, '0', STR_PAD_LEFT);
$end_date = '-' . implode('-', $parts);
}
$this->counting_time_indicator = $counting_time_indicator;
$this->before_after_indicator = $before_after_indicator;

View File

@ -11,7 +11,7 @@ declare(strict_types = 1);
*/
final class NodaTimeSplitter {
const MONTH_NAMES_GERMAN = [
private const MONTH_NAMES_GERMAN = [
"01" => ['Januar', 'Jan.'],
"02" => ['Februar', 'Feb'],
"03" => ['März', 'Mrz.'],
@ -26,7 +26,7 @@ final class NodaTimeSplitter {
"12" => ['Dezember', 'Dez.'],
];
const MONTH_NAMES_ENGLISH = [
private const MONTH_NAMES_ENGLISH = [
"01" => ['January', 'Jan.'],
"02" => ['February', 'Feb'],
"03" => ['March', 'Mar.'],
@ -41,7 +41,7 @@ final class NodaTimeSplitter {
"12" => ['December', 'Dec.'],
];
const MONTH_NAMES_HUNGARIAN = [
private const MONTH_NAMES_HUNGARIAN = [
"01" => ['január', 'januar', 'jan'],
"02" => ['február', 'feb'],
"03" => ['március', 'mar.', 'már.'],
@ -56,7 +56,7 @@ final class NodaTimeSplitter {
"12" => ['december', 'dec.'],
];
const STRINGS_TO_CLEAN = [
private const STRINGS_TO_CLEAN = [
"között" => "",
" рр." => "",
" рр" => "",
@ -79,7 +79,7 @@ final class NodaTimeSplitter {
" до н. е." => "v. Chr.",
];
const STRINGS_TO_CLEAN_START = [
private const STRINGS_TO_CLEAN_START = [
"V. " => "5. ",
"IV. " => "4. ",
"III. " => "3. ",
@ -87,7 +87,7 @@ final class NodaTimeSplitter {
"I. " => "1. ",
];
const STOP_STRINGS_GERMAN = [
private const STOP_STRINGS_GERMAN = [
"-",
",",
";",
@ -108,7 +108,7 @@ final class NodaTimeSplitter {
"ende ",
];
const STOP_STRINGS_HUNGARIAN = [
private const STOP_STRINGS_HUNGARIAN = [
"-",
",",
";",

View File

@ -5,13 +5,16 @@
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
/**
* Tests for setting uniform place names.
*
* @covers \NodaConsolidatedNamesForPlaces
*/
#[small]
#[CoversClass(\NodaConsolidatedNamesForPlaces::class)]
final class NodaConsolidatedNamesForPlacesTest extends TestCase {
/**
* Test that cleanup function returns expected values.

View File

@ -5,13 +5,16 @@
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/
declare(strict_types = 1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
/**
* This script contains tests for the time name splitter.
*
* @covers \NodaTimeSplitter
*/
#[small]
#[CoversClass(\NodaTimeSplitter::class)]
final class NodaTimeSplitterTest extends TestCase {
/**
* Test to check whether the HTML page is correctly generated.
@ -538,6 +541,19 @@ final class NodaTimeSplitterTest extends TestCase {
*/
public function testSplitSimpleDatesHungarian():void {
$output = NodaTimeSplitter::attempt_splitting("Kr. e. 2. század");
self::assertNotEmpty($output);
self::assertEquals($output->toOldFormat(), [
0 => "-200",
1 => "-101",
2 => "00",
3 => "00",
4 => "-",
5 => "",
]);
self::assertEquals($output->toTimeName(), "200-101 v. Chr.");
self::assertEquals(NodaTimeSplitter::timePartsToCountingYear($output), 150);
$output = NodaTimeSplitter::attempt_splitting("2020.01.02.");
self::assertNotEmpty($output);
self::assertEquals($output->toOldFormat(), [