function test_getTimeFormatGuessed() { appendTestMessage(NEW_LINE_LOG . " >> Tests guess time stamp formats..." . NEW_LINE_LOG); appendTestMessage("time=1364225224111"); $ret = getTimeFormatGuessed("1364225224"); $expected = "2013-03-25T15:27:04Z"; if ($ret == $expected) { appendTestMessage("- ok"); } else { appendTestMessage("- failed"); return false; } appendTestMessage("time=1364225189643"); $ret = getTimeFormatGuessed("1364225189643"); $expected = "2013-03-25T15:26:29Z"; if ($ret == $expected) { appendTestMessage("- ok"); } else { appendTestMessage("- failed"); return false; } appendTestMessage("time=2013-03-25_14:43:01"); $ret = getTimeFormatGuessed("2013-03-25_14:43:01"); $expected = "2013-03-25T14:43:01"; if ($ret == $expected) { appendTestMessage("- ok"); } else { appendTestMessage("- failed"); return false; } appendTestMessage("time=2013-03-25_14:43:01Z+01"); $ret = getTimeFormatGuessed("2013-03-25_14:43:01Z+01"); $expected = "2013-03-25T14:43:01Z+01"; if ($ret == $expected) { appendTestMessage("- ok"); } else { appendTestMessage("- failed"); return false; } appendTestMessage("time=2013-03-25_14:43"); $ret = getTimeFormatGuessed("2013-03-25_14:43"); $expected = "2013-03-25_14:43"; if ($ret == $expected) { appendTestMessage("- ok"); } else { appendTestMessage("- failed"); return false; } return true; }
/** * Can store more than one postions at once. * * @param type $user * @param type $group * @param type $positions multiline CSV, each line containing one postion * Example * lat=43.50457163540115;lon=11.071390274487026;bearing=171.61432;speed=0.7065948;altitude=1067.652498529502;accuracy=6.0;time=2012-03-03T15:48:47.484' * lat=44.50457163540115;lon=11.071390274487026;bearing=171.61432;speed=0.7065948;altitude=1067.652498529502;accuracy=6.0;time=2012-03-03T15:48:47.484' * @param type $storeTrack store the track to * @return boolean true for success */ function writePostions($user, $group, $positions, $storeTrack) { $storeTrack = checkConfigIsWritingTrack($user, $storeTrack); // Write the last request (does not write the password) if (!writeLastRequest($user)) { logMessage("Failed to write the request file for user {$user}."); return false; } if (isNullOrEmptyString($positions)) { logMessage("Missing parameter positions."); return false; } if (!isNullOrEmptyString($group)) { if (!writeGroupFile($user, $group)) { return false; } } // Try to guess the timestamp format $acceptedValues = '/(.*time=)([^;]+)(.*)/i'; $dateString = ''; $lines = explode(PHP_EOL, $positions); $count = count($lines); $lastLine = $lines[$count - 1]; $lineBuffer = ''; for ($i = 0; $i < $count; $i++) { $lastLine = trim($lines[$i]); if (preg_match($acceptedValues, $lastLine, $matches)) { $timeStringReceived = trim($matches[2]); $timeStringGuessed = getTimeFormatGuessed($timeStringReceived); $lastLine = $matches[1] . $timeStringGuessed . $matches[3]; // Check wether the day (local user time) changed) > to split the track files later on $newDateString = getDateForTimezoneOffset($user, $timeStringReceived, true); if ($newDateString == '') { // take today $newDateString = getDateForTimezoneOffset($user, "", true); } if ($dateString == '') { $dateString = $newDateString; } else { if ($newDateString != $dateString) { if (!isNullOrEmptyString($storeTrack)) { // the date of the positions time changed > write the track writePositionsCSVfile($user, $dateString, $lineBuffer); } // start a new track (line buffer) $lineBuffer = ''; $dateString = $newDateString; } } } if ($lineBuffer != '') { $lineBuffer .= PHP_EOL; } $lineBuffer .= $lastLine; } $acceptedValues = '/true/i'; if (preg_match($acceptedValues, $storeTrack)) { // Write CSV writePositionsCSVfile($user, $dateString, $lineBuffer); } // Write PGX // $gpxFile = USER_DIR . DIRECTORY_SEPARATOR . $user . DIRECTORY_SEPARATOR . $dateString . ".gpx"; // if (!writeGPXfile(true, $gpxFile, $positions)) { // return false; // } // // write last position to position.csv return writePositionFile($user, $lastLine); }