diff --git a/php/classes/TimeTableMgr.php b/php/classes/TimeTableMgr.php index 0dd8a33..b4aa98c 100644 --- a/php/classes/TimeTableMgr.php +++ b/php/classes/TimeTableMgr.php @@ -243,7 +243,7 @@ class TimeTableMgr extends AmdaObjectMgr public function getUploadedObject($name, $format, $onlyDescription = false) { if (strpos($name, '.txt') !== false || strpos($name, '.asc') !== false || strpos($name, '.') == false) { - $attributesToReturn = $this->text2amda(USERTEMPDIR . $name, $onlyDescription); + $attributesToReturn = $this->textToAmda(USERTEMPDIR . $name, $onlyDescription); $attributesToReturn['objName'] = $name; $attributesToReturn['objFormat'] = $format; @@ -323,7 +323,7 @@ class TimeTableMgr extends AmdaObjectMgr * @param bool $onlyDescription * @return mixed */ - protected function text2amda($tmp_file, $onlyDescription = false) + protected function textToAmda($tmp_file, $onlyDescription = false) { $suffix = explode('.', basename($tmp_file)); $lines = file($tmp_file, FILE_SKIP_EMPTY_LINES); @@ -333,59 +333,39 @@ class TimeTableMgr extends AmdaObjectMgr $descNumber = 0; foreach ($lines as $line) { - if ($line[0] == '#') { + $line = trim($line); + if ($line[0] == '#') { // Comment $description = $description . PHP_EOL . substr($line, 1, -1); } else { - $date = explode(' ', trim(preg_replace('!\s+!', ' ', $line))); - - if (!strtotime(trim($date[0]))) { + $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)) { + $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; } - // check if it is ISO format - if (!isset($isIso)) { - $isIso = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})$/', trim($date[0])); - } - - if (!$isIso) { - // y-m-d h:m:s for example - $dateLength = count($date) / 2; - - $tempStart = $date[0]; - $tempStop = $date[$dateLength]; - - if ($dateLength > 1) { - for ($iDate = 1; $iDate < $dateLength; $iDate++) { - $tempStart .= $date[$iDate]; - } - - for ($iDate = $dateLength + 1; $iDate < $dateLength * 2; $iDate++) { - $tempStop .= $date[$iDate]; - } - } - - $startDate = date('Y-m-d\TH:i:s', strtotime($tempStart)); - $stopDate = date('Y-m-d\TH:i:s', strtotime($tempStop)); - if (!$onlyDescription) { - $attributesToReturn['intervals'][] = ['start' => $startDate, 'stop' => $stopDate]; - } - } else { - if (!$onlyDescription) { - $attributesToReturn['intervals'][] = ['start' => trim($date[0]), 'stop' => trim($date[1])]; - } + if (!$onlyDescription) { + $attributesToReturn['intervals'][] = ['start' => $startDate, 'stop' => $stopDate]; } } } if ($recordsNumber == $descNumber) { - $description = "Looks like we can not read your time format..." . PHP_EOL . $description; + $description = 'Looks like we can not read your time format...' . PHP_EOL . $description; } $attributesToReturn['description'] = $description; $attributesToReturn['name'] = basename($tmp_file, '.' . $suffix[1]); $attributesToReturn['created'] = date('Y-m-d\TH:i:s'); - return $attributesToReturn; } -- libgit2 0.21.2