Add option to split and translate times with start and end dates

Close #1
This commit is contained in:
2021-01-06 23:05:26 +01:00
parent fcc63c4ea0
commit 54764e741a
2 changed files with 104 additions and 0 deletions

View File

@ -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.
*