Add option to split and translate times with start and end dates
Close #1
This commit is contained in:
parent
fcc63c4ea0
commit
54764e741a
@ -24,6 +24,7 @@ final class NodaTimeAutotranslater {
|
||||
const TRANSLATABLE_CENTURY = 6;
|
||||
const TRANSLATABLE_DECADE = 7;
|
||||
const TRANSLATABLE_TIMESPAN_YEARS = 8;
|
||||
const TRANSLATABLE_TIMESPAN_DATES = 9;
|
||||
|
||||
const LANGS_TO_LOCALES = [
|
||||
'ar' => 'ar_SY.utf8',
|
||||
@ -753,6 +754,42 @@ final class NodaTimeAutotranslater {
|
||||
*/
|
||||
public static function getTranslations(array $timeInfo):array {
|
||||
|
||||
if (!empty($timespanDates = NodaTimeSplitter::attempt_splitting_from_till($timeInfo['zeit_name']))) {
|
||||
|
||||
$output = [];
|
||||
|
||||
$start = NodaTimeSplitter::attempt_splitting($timespanDates['start_name']);
|
||||
$startTimeInfo = [
|
||||
"zeit_name" => $timespanDates['start_name'],
|
||||
"zeit_beginn" => $start[0],
|
||||
"zeit_ende" => $start[1],
|
||||
"zeit_zaehlzeit_jahr" => NodaTimeSplitter::timePartsToCountingYear($start),
|
||||
"zeit_zaehlzeit_monat" => $start[2],
|
||||
"zeit_zaehlzeit_tag" => $start[3],
|
||||
"zeit_zaehlzeit_vorzeichen" => $start[4],
|
||||
];
|
||||
|
||||
$end = NodaTimeSplitter::attempt_splitting($timespanDates['end_name']);
|
||||
$endTimeInfo = [
|
||||
"zeit_name" => $timespanDates['end_name'],
|
||||
"zeit_beginn" => $end[0],
|
||||
"zeit_ende" => $end[1],
|
||||
"zeit_zaehlzeit_jahr" => NodaTimeSplitter::timePartsToCountingYear($end),
|
||||
"zeit_zaehlzeit_monat" => $end[2],
|
||||
"zeit_zaehlzeit_tag" => $end[3],
|
||||
"zeit_zaehlzeit_vorzeichen" => $end[4],
|
||||
];
|
||||
|
||||
$output = [];
|
||||
foreach (self::LANGS_YEARSPAN_FORMAT as $tLang => $format) {
|
||||
$start_term = self::getTranslations($startTimeInfo)[$tLang];
|
||||
$end_term = self::getTranslations($endTimeInfo)[$tLang];
|
||||
|
||||
$output[$tLang] = \sprintf($format, $start_term, $end_term);
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
if (!($translation_type = self::check_translatability((string)$timeInfo['zeit_beginn'], (string)$timeInfo['zeit_ende'], (string)$timeInfo['zeit_zaehlzeit_monat']))) {
|
||||
throw new MDgenericInvalidInputsException("Non-translatable date: {$timeInfo['zeit_beginn']} - {$timeInfo['zeit_ende']}");
|
||||
}
|
||||
|
@ -963,6 +963,73 @@ final class NodaTimeSplitter {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the string is a time span with given start and end dates.
|
||||
*
|
||||
* @param string $datum Date.
|
||||
*
|
||||
* @return array<array<string>>
|
||||
*/
|
||||
public static function check_is_timespan_from_till(string $datum):array {
|
||||
|
||||
if (substr_count($datum, '-') !== 1) return [];
|
||||
|
||||
list($start_str, $end_str) = explode('-', $datum);
|
||||
|
||||
if (empty($start = self::attempt_splitting($start_str))) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (empty($end = self::attempt_splitting($end_str))) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [$start, $end];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the string is a time span with given start and end dates.
|
||||
*
|
||||
* @param string $datum Date.
|
||||
*
|
||||
* @return array<array<string>>
|
||||
*/
|
||||
public static function attempt_splitting_from_till(string $datum):array {
|
||||
|
||||
if (empty($startEnd = self::check_is_timespan_from_till($datum))) {
|
||||
return [];
|
||||
}
|
||||
list($start, $end) = $startEnd;
|
||||
|
||||
if ($start[4] === '-') return [];
|
||||
|
||||
$startDate = new DateTime($start[4] . $start[0] . '-' . $start[2] . '-' . $start[3]);
|
||||
$endDate = new DateTime($end[4] . $end[1] . '-' . $end[2] . '-' . $end[3]);
|
||||
|
||||
$interval = $startDate->diff($endDate);
|
||||
$days_diff = (int)$interval->format('%a');
|
||||
|
||||
$middle_substraction = round($days_diff / 2);
|
||||
|
||||
$middle_day = date('Y-m-d',
|
||||
strtotime('+' . $middle_substraction . ' days', strtotime($startDate->format('Y-m-d'))));
|
||||
|
||||
$output = [
|
||||
"start_name" => self::timePartsToTimeName($start),
|
||||
"end_name" => self::timePartsToTimeName($end),
|
||||
"start_year" => $start[0],
|
||||
"end_year" => $end[1],
|
||||
"counting_time_year" => substr($middle_day, 0, 4),
|
||||
"counting_time_month" => substr($middle_day, 5, 2),
|
||||
"counting_time_day" => substr($middle_day, 8, 2),
|
||||
"counting_time_bcce" => "+",
|
||||
];
|
||||
|
||||
return $output;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper to check if any splitting command works.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user