Commit efb5795428caad8ac851b98f1a66796a1d4010d5

Authored by Nathanaël Jourdane
1 parent e4fba9d2

Parse imported time-table dates better

Showing 1 changed file with 14 additions and 9 deletions   Show diff stats
php/classes/TimeTableMgr.php
@@ -333,25 +333,30 @@ class TimeTableMgr extends AmdaObjectMgr @@ -333,25 +333,30 @@ class TimeTableMgr extends AmdaObjectMgr
333 $descNumber = 0; 333 $descNumber = 0;
334 334
335 foreach ($lines as $line) { 335 foreach ($lines as $line) {
336 - $line = trim($line); 336 + $line = preg_replace('/\s+/', ' ', trim($line));
337 if ($line[0] == '#') { // Comment 337 if ($line[0] == '#') { // Comment
338 $description = $description . PHP_EOL . substr($line, 1, -1); 338 $description = $description . PHP_EOL . substr($line, 1, -1);
339 } else { 339 } else {
340 $isoFormat = 'Y-m-dTH:i:s'; 340 $isoFormat = 'Y-m-dTH:i:s';
341 $doyFormat = 'Y z H 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})'; 342 $doyRegex = '(\d{4}) (\d{3}) (\d{2}) (\d{2}) (\d{2})';
344 343
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)) { 344 + if (preg_match('/^' . $doyRegex . ' ' . $doyRegex . '$/', $line)) {
349 $startDate = DateTime::createFromFormat($doyFormat, substr($line, 0, 17))->format($isoFormat); 345 $startDate = DateTime::createFromFormat($doyFormat, substr($line, 0, 17))->format($isoFormat);
350 $stopDate = DateTime::createFromFormat($doyFormat, substr($line, 18))->format($isoFormat); 346 $stopDate = DateTime::createFromFormat($doyFormat, substr($line, 18))->format($isoFormat);
351 } else { 347 } else {
352 - $description = $description . PHP_EOL . $line;  
353 - $descNumber++;  
354 - continue; 348 + $dateLength = round((strlen($line)-1) / 2);
  349 +
  350 + $startTime = strtotime(substr($line, 0, $dateLength));
  351 + $stopTime = strtotime(substr($line, $dateLength + 1));
  352 + if (is_numeric($startTime) && is_numeric($stopTime)) {
  353 + $startDate = date($isoFormat, $startTime);
  354 + $stopDate = date($isoFormat, $stopTime);
  355 + } else {
  356 + $description = $description . PHP_EOL . $line;
  357 + $descNumber++;
  358 + continue;
  359 + }
355 } 360 }
356 361
357 if (!$onlyDescription) { 362 if (!$onlyDescription) {