Commit e4fba9d277a65db2c7a1aee5270564d441f8b139
1 parent
d8ec1623
Exists in
master
and in
109 other branches
Import tt: parse doy-dates
Showing
1 changed file
with
19 additions
and
39 deletions
Show diff stats
php/classes/TimeTableMgr.php
... | ... | @@ -243,7 +243,7 @@ class TimeTableMgr extends AmdaObjectMgr |
243 | 243 | public function getUploadedObject($name, $format, $onlyDescription = false) |
244 | 244 | { |
245 | 245 | if (strpos($name, '.txt') !== false || strpos($name, '.asc') !== false || strpos($name, '.') == false) { |
246 | - $attributesToReturn = $this->text2amda(USERTEMPDIR . $name, $onlyDescription); | |
246 | + $attributesToReturn = $this->textToAmda(USERTEMPDIR . $name, $onlyDescription); | |
247 | 247 | $attributesToReturn['objName'] = $name; |
248 | 248 | $attributesToReturn['objFormat'] = $format; |
249 | 249 | |
... | ... | @@ -323,7 +323,7 @@ class TimeTableMgr extends AmdaObjectMgr |
323 | 323 | * @param bool $onlyDescription |
324 | 324 | * @return mixed |
325 | 325 | */ |
326 | - protected function text2amda($tmp_file, $onlyDescription = false) | |
326 | + protected function textToAmda($tmp_file, $onlyDescription = false) | |
327 | 327 | { |
328 | 328 | $suffix = explode('.', basename($tmp_file)); |
329 | 329 | $lines = file($tmp_file, FILE_SKIP_EMPTY_LINES); |
... | ... | @@ -333,59 +333,39 @@ class TimeTableMgr extends AmdaObjectMgr |
333 | 333 | $descNumber = 0; |
334 | 334 | |
335 | 335 | foreach ($lines as $line) { |
336 | - if ($line[0] == '#') { | |
336 | + $line = trim($line); | |
337 | + if ($line[0] == '#') { // Comment | |
337 | 338 | $description = $description . PHP_EOL . substr($line, 1, -1); |
338 | 339 | } else { |
339 | - $date = explode(' ', trim(preg_replace('!\s+!', ' ', $line))); | |
340 | - | |
341 | - if (!strtotime(trim($date[0]))) { | |
340 | + $isoFormat = 'Y-m-dTH:i:s'; | |
341 | + $doyFormat = 'Y z H i s'; | |
342 | + $isoRegex = '(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})'; | |
343 | + $doyRegex = '(\d{4}) (\d{3}) (\d{2}) (\d{2}) (\d{2})'; | |
344 | + | |
345 | + if (preg_match('/^' . $isoRegex . ' ' . $isoRegex . '$/', $line)) { | |
346 | + $startDate = substr($line, 0, 19); | |
347 | + $stopDate = substr($line, 20); | |
348 | + } elseif (preg_match('/^' . $doyRegex . ' ' . $doyRegex . '$/', $line)) { | |
349 | + $startDate = DateTime::createFromFormat($doyFormat, substr($line, 0, 17))->format($isoFormat); | |
350 | + $stopDate = DateTime::createFromFormat($doyFormat, substr($line, 18))->format($isoFormat); | |
351 | + } else { | |
342 | 352 | $description = $description . PHP_EOL . $line; |
343 | 353 | $descNumber++; |
344 | 354 | continue; |
345 | 355 | } |
346 | - // check if it is ISO format | |
347 | - if (!isset($isIso)) { | |
348 | - $isIso = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})$/', trim($date[0])); | |
349 | - } | |
350 | - | |
351 | - if (!$isIso) { | |
352 | - // y-m-d h:m:s for example | |
353 | - $dateLength = count($date) / 2; | |
354 | - | |
355 | - $tempStart = $date[0]; | |
356 | - $tempStop = $date[$dateLength]; | |
357 | - | |
358 | - if ($dateLength > 1) { | |
359 | - for ($iDate = 1; $iDate < $dateLength; $iDate++) { | |
360 | - $tempStart .= $date[$iDate]; | |
361 | - } | |
362 | - | |
363 | - for ($iDate = $dateLength + 1; $iDate < $dateLength * 2; $iDate++) { | |
364 | - $tempStop .= $date[$iDate]; | |
365 | - } | |
366 | - } | |
367 | - | |
368 | - $startDate = date('Y-m-d\TH:i:s', strtotime($tempStart)); | |
369 | - $stopDate = date('Y-m-d\TH:i:s', strtotime($tempStop)); | |
370 | 356 | |
371 | - if (!$onlyDescription) { | |
372 | - $attributesToReturn['intervals'][] = ['start' => $startDate, 'stop' => $stopDate]; | |
373 | - } | |
374 | - } else { | |
375 | - if (!$onlyDescription) { | |
376 | - $attributesToReturn['intervals'][] = ['start' => trim($date[0]), 'stop' => trim($date[1])]; | |
377 | - } | |
357 | + if (!$onlyDescription) { | |
358 | + $attributesToReturn['intervals'][] = ['start' => $startDate, 'stop' => $stopDate]; | |
378 | 359 | } |
379 | 360 | } |
380 | 361 | } |
381 | 362 | if ($recordsNumber == $descNumber) { |
382 | - $description = "Looks like we can not read your time format..." . PHP_EOL . $description; | |
363 | + $description = 'Looks like we can not read your time format...' . PHP_EOL . $description; | |
383 | 364 | } |
384 | 365 | |
385 | 366 | $attributesToReturn['description'] = $description; |
386 | 367 | $attributesToReturn['name'] = basename($tmp_file, '.' . $suffix[1]); |
387 | 368 | $attributesToReturn['created'] = date('Y-m-d\TH:i:s'); |
388 | - | |
389 | 369 | return $attributesToReturn; |
390 | 370 | } |
391 | 371 | ... | ... |