From efb5795428caad8ac851b98f1a66796a1d4010d5 Mon Sep 17 00:00:00 2001 From: Nathanaƫl Jourdane Date: Fri, 1 Jun 2018 17:41:00 +0200 Subject: [PATCH] Parse imported time-table dates better --- php/classes/TimeTableMgr.php | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/php/classes/TimeTableMgr.php b/php/classes/TimeTableMgr.php index b4aa98c..824e788 100644 --- a/php/classes/TimeTableMgr.php +++ b/php/classes/TimeTableMgr.php @@ -333,25 +333,30 @@ class TimeTableMgr extends AmdaObjectMgr $descNumber = 0; foreach ($lines as $line) { - $line = trim($line); + $line = preg_replace('/\s+/', ' ', trim($line)); if ($line[0] == '#') { // Comment $description = $description . PHP_EOL . substr($line, 1, -1); } else { $isoFormat = 'Y-m-dTH:i:s'; $doyFormat = 'Y z H i s'; - $isoRegex = '(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})'; $doyRegex = '(\d{4}) (\d{3}) (\d{2}) (\d{2}) (\d{2})'; - if (preg_match('/^' . $isoRegex . ' ' . $isoRegex . '$/', $line)) { - $startDate = substr($line, 0, 19); - $stopDate = substr($line, 20); - } elseif (preg_match('/^' . $doyRegex . ' ' . $doyRegex . '$/', $line)) { + if (preg_match('/^' . $doyRegex . ' ' . $doyRegex . '$/', $line)) { $startDate = DateTime::createFromFormat($doyFormat, substr($line, 0, 17))->format($isoFormat); $stopDate = DateTime::createFromFormat($doyFormat, substr($line, 18))->format($isoFormat); } else { - $description = $description . PHP_EOL . $line; - $descNumber++; - continue; + $dateLength = round((strlen($line)-1) / 2); + + $startTime = strtotime(substr($line, 0, $dateLength)); + $stopTime = strtotime(substr($line, $dateLength + 1)); + if (is_numeric($startTime) && is_numeric($stopTime)) { + $startDate = date($isoFormat, $startTime); + $stopDate = date($isoFormat, $stopTime); + } else { + $description = $description . PHP_EOL . $line; + $descNumber++; + continue; + } } if (!$onlyDescription) { -- libgit2 0.21.2