Better handling of time zones

This commit is contained in:
Bartek Fabiszewski 2017-05-22 13:39:40 +02:00
parent 0ef57926dd
commit f04b229644
9 changed files with 62 additions and 44 deletions

View File

@ -80,7 +80,7 @@ switch ($action) {
case "addpos":
$lat = isset($_REQUEST["lat"]) ? $_REQUEST["lat"] : NULL;
$lon = isset($_REQUEST["lon"]) ? $_REQUEST["lon"] : NULL;
$time = isset($_REQUEST["time"]) ? $_REQUEST["time"] : NULL;
$timestamp = isset($_REQUEST["time"]) ? $_REQUEST["time"] : NULL;
$altitude = isset($_REQUEST["altitude"]) ? $_REQUEST["altitude"] : NULL;
$speed = isset($_REQUEST["speed"]) ? $_REQUEST["speed"] : NULL;
$bearing = isset($_REQUEST["bearing"]) ? $_REQUEST["bearing"] : NULL;
@ -90,7 +90,7 @@ switch ($action) {
$imageId = isset($_REQUEST["imageid"]) ? $_REQUEST["imageid"] : NULL;
$trackId = isset($_REQUEST["trackid"]) ? $_REQUEST["trackid"] : NULL;
if (is_null($lat) || is_null($lon) || is_null($time) || is_null($trackId)) {
if (is_null($lat) || is_null($lon) || is_null($timestamp) || is_null($trackId)) {
setError($response, "missing required parameter");
break;
}
@ -98,7 +98,7 @@ switch ($action) {
require_once(ROOT_DIR . "/helpers/position.php");
$position = new uPosition();
$positionId = $position->add($user->id, $trackId,
$time, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId);
$timestamp, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId);
if ($positionId === false) {
setError($response, "Server error");

View File

@ -109,11 +109,10 @@ select {
#user, #track, #summary, #export, #import, #other, #units {
padding-bottom: 10px;
}
#summary span {
display: block;
#summary div {
padding-top: .3em;
}
#summary span img {
#summary div img {
margin-bottom: -2px;
}
#login {
@ -170,6 +169,10 @@ select {
padding-top: 5px;
padding-right: 20px;
}
#pbody .smaller {
color: gray;
font-size: .9em;
}
#pfooter {
font-size: .6rem;
padding-top: 20px;

View File

@ -25,7 +25,7 @@
*/
class uPosition {
public $id;
public $time;
public $timestamp;
public $userId;
public $userLogin;
public $trackId;
@ -53,7 +53,7 @@
self::$db = uDb::getInstance();
if (!empty($positionId)) {
$query = "SELECT p.id, p.time, p.user_id, p.track_id,
$query = "SELECT p.id, UNIX_TIMESTAMP(p.time) AS tstamp, p.user_id, p.track_id,
p.latitude, p.longitude, p.altitude, p.speed, p.bearing, p.accuracy, p.provider,
p.comment, p.image_id, u.login, t.name
FROM `" . self::$db->table('positions') . "` p
@ -82,9 +82,9 @@
* @param int $imageId
* @return int|bool New position id in database, false on error
*/
public function add($userId, $trackId, $time, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId) {
public function add($userId, $trackId, $timestamp, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId) {
$positionId = false;
if (!is_null($lat) && !is_null($lon) && !is_null($time) && !empty($userId) && !empty($trackId)) {
if (!is_null($lat) && !is_null($lon) && !is_null($timestamp) && !empty($userId) && !empty($trackId)) {
$track = new uTrack($trackId);
if ($track->isValid && $track->userId == $userId) {
$query = "INSERT INTO `" . self::$db->table('positions') . "`
@ -94,7 +94,7 @@
$stmt = self::$db->prepare($query);
$stmt->bind_param('iisddddddssi',
$userId, $trackId,
$time, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId);
$timestamp, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageId);
$stmt->execute();
if (!self::$db->error && !$stmt->errno) {
$positionId = self::$db->insert_id;
@ -151,7 +151,7 @@
$where = "";
$params = NULL;
}
$query = "SELECT p.id, p.time, p.user_id, p.track_id,
$query = "SELECT p.id, UNIX_TIMESTAMP(p.time) AS tstamp, p.user_id, p.track_id,
p.latitude, p.longitude, p.altitude, p.speed, p.bearing, p.accuracy, p.provider,
p.comment, p.image_id, u.login, t.name
FROM `" . self::$db->table('positions') . "` p
@ -183,7 +183,7 @@
} else {
$where = "";
}
$query = "SELECT p.id, p.time, p.user_id, p.track_id,
$query = "SELECT p.id, UNIX_TIMESTAMP(p.time) AS tstamp, p.user_id, p.track_id,
p.latitude, p.longitude, p.altitude, p.speed, p.bearing, p.accuracy, p.provider,
p.comment, p.image_id, u.login, t.name
FROM `" . self::$db->table('positions') . "` p
@ -227,7 +227,7 @@
* @return int Number of seconds
*/
public function secondsTo($target) {
return strtotime($this->time) - strtotime($target->time);
return $this->timestamp - $target->timestamp;
}
/**
@ -239,7 +239,7 @@
private function rowToObject($row) {
$position = new uPosition();
$position->id = $row['id'];
$position->time = $row['time'];
$position->timestamp = $row['tstamp'];
$position->userId = $row['user_id'];
$position->userLogin = $row['login'];
$position->trackId = $row['track_id'];
@ -272,7 +272,7 @@
);
}
if ($stmt->execute()) {
$stmt->bind_result($this->id, $this->time, $this->userId, $this->trackId,
$stmt->bind_result($this->id, $this->timestamp, $this->userId, $this->trackId,
$this->latitude, $this->longitude, $this->altitude, $this->speed,
$this->bearing, $this->accuracy, $this->provider,
$this->comment, $this->imageId, $this->userLogin, $this->trackName);

View File

@ -81,10 +81,9 @@ function displayTrack(xml, update) {
setTimeout(function () { google.maps.event.removeListener(zListener) }, 2000);
}
}
latestTime = p.dateoccured;
polies.push(poly);
updateSummary(p.dateoccured, totalMeters, totalSeconds);
updateSummary(p.timestamp, totalMeters, totalSeconds);
if (p.tid != trackid) {
trackid = p.tid;
setTrack(trackid);
@ -120,7 +119,7 @@ function setMarker(p, i, posLen) {
var marker = new google.maps.Marker({
map: map,
position: p.coordinates,
title: p.dateoccured
title: (new Date(p.timestamp * 1000)).toLocaleString()
});
if (latest == 1) { marker.setIcon('//maps.google.com/mapfiles/dd-end.png') }
else if (i == 0) { marker.setIcon('//maps.google.com/mapfiles/marker_greenA.png') }

View File

@ -119,10 +119,8 @@ function displayTrack(xml, update) {
map.zoomOut();
}
}
latestTime = p.dateoccured;
//polies.push(poly);
updateSummary(p.dateoccured, totalMeters, totalSeconds);
updateSummary(p.timestamp, totalMeters, totalSeconds);
if (p.tid != trackid) {
trackid = p.tid;
setTrack(trackid);

View File

@ -33,7 +33,6 @@ if (units == 'imperial') {
unit_km = 'km';
}
var latest = 0;
var latestTime = 0;
var live = 0;
var chart;
var altitudes = new Array();
@ -154,7 +153,7 @@ function parsePosition(p) {
var username = getNode(p, 'username');
var trackname = getNode(p, 'trackname');
var tid = getNode(p, 'trackid');
var dateoccured = getNode(p, 'dateoccured');
var timestamp = getNode(p, 'timestamp');
var distance = parseInt(getNode(p, 'distance'));
var seconds = parseInt(getNode(p, 'seconds'));
return {
@ -169,16 +168,24 @@ function parsePosition(p) {
'username': username,
'trackname': trackname,
'tid': tid,
'dateoccured': dateoccured,
'timestamp': timestamp,
'distance': distance,
'seconds': seconds
};
}
function getPopupHtml(p, i, count) {
var dateTime = p.dateoccured.split(" ");
var date = dateTime[0];
var time = dateTime[1];
var date = '';
var time = '';
if (p.timestamp > 0) {
var d = new Date(p.timestamp * 1000);
date = d.getFullYear() + '-' + ('0' + (d.getMonth() + 1)).slice(-2) + '-' + ('0' + d.getDate()).slice(-2);
time = d.toTimeString();
var offset;
if ((offset = time.indexOf(' ')) >= 0) {
time = time.substr(0, offset) + ' <span class="smaller">' + time.substr(offset + 1) + '</span>';
}
}
var provider = '';
if (p.provider == 'gps') {
provider = ' (<img class="icon" alt="' + lang['gps'] + '" title="' + lang['gps'] + '" src="images/gps_dark.svg">)';
@ -283,15 +290,26 @@ function removeLoader(el) {
el.innerHTML = el.textContent || el.innerText;
}
function updateSummary(l, d, s) {
function updateSummary(timestamp, d, s) {
var t = document.getElementById('summary');
if (latest == 0) {
t.innerHTML = '<div class="menutitle u">' + lang['summary'] + '</div>' +
'<span><img class="icon" alt="' + lang['tdistance'] + '" title="' + lang['tdistance'] + '" src="images/distance.svg"> ' + (d.toKm() * factor_km).toFixed(2) + ' ' + unit_km + '</span>' +
'<span><img class="icon" alt="' + lang['ttime'] + '" title="' + lang['ttime'] + '" src="images/time.svg"> ' + s.toHMS() + '</span>';
'<div><img class="icon" alt="' + lang['tdistance'] + '" title="' + lang['tdistance'] + '" src="images/distance.svg"> ' + (d.toKm() * factor_km).toFixed(2) + ' ' + unit_km + '</div>' +
'<div><img class="icon" alt="' + lang['ttime'] + '" title="' + lang['ttime'] + '" src="images/time.svg"> ' + s.toHMS() + '</div>';
} else {
var today = new Date();
var d = new Date(timestamp * 1000);
var dateString = '';
if (d.toDateString() != today.toDateString()) {
dateString += d.getFullYear() + '-' + ('0' + (d.getMonth() + 1)).slice(-2) + '-' + ('0' + d.getDate()).slice(-2);
dateString += '<br>';
}
else {
t.innerHTML = '<div class="menutitle u">' + lang['latest'] + ':</div>' + l;
var timeString = d.toTimeString();
var offset;
if ((offset = timeString.indexOf(' ')) >= 0) {
timeString = timeString.substr(0, offset) + ' <span style="font-weight:normal">' + timeString.substr(offset + 1) + '</span>';
}
t.innerHTML = '<div class="menutitle u">' + lang['latest'] + ':</div>' + dateString + timeString;
}
}

View File

@ -176,7 +176,7 @@ function process_user_tracks($user_id) {
*/
function process_track($user_id, $old_id, $new_id) {
global $pt_mysqli, $mysqli;
$sql = "SELECT Latitude, Longitude, Altitude, Speed, Angle, DateOccurred, Comments FROM pt_positions WHERE FK_Users_ID = ? AND FK_Trips_ID = ? ORDER BY DateOccurred";
$sql = "SELECT Latitude, Longitude, Altitude, Speed, Angle, UNIX_TIMESTAMP(DateOccurred), Comments FROM pt_positions WHERE FK_Users_ID = ? AND FK_Trips_ID = ? ORDER BY DateOccurred, ID";
if (!($pos_select = $pt_mysqli->prepare($sql))) {
echo "Prepare failed: (" . $pt_mysqli->errno . ") " . $pt_mysqli->error . "\n";
exit(1);
@ -185,7 +185,7 @@ function process_track($user_id, $old_id, $new_id) {
echo "Binding parameters failed: (" . $pos_select->errno . ") " . $pos_select->error . "\n";
exit(1);
}
if (!$pos_select->bind_result($lat, $lon, $altitude, $speed, $bearing, $time, $comment)) {
if (!$pos_select->bind_result($lat, $lon, $altitude, $speed, $bearing, $timestamp, $comment)) {
echo "Binding parameters failed: (" . $pos_select->errno . ") " . $pos_select->error . "\n";
exit(1);
}
@ -194,17 +194,17 @@ function process_track($user_id, $old_id, $new_id) {
exit(1);
}
$pos_select->store_result();
if (!($pos_insert = $mysqli->prepare("INSERT INTO `$tPositions` (time, user_id, track_id, latitude, longitude, altitude, speed, bearing, accuracy, provider, comment, image_id)
if (!($pos_insert = $mysqli->prepare("INSERT INTO `$tPositions` (FROM_UNIXTIME(time), user_id, track_id, latitude, longitude, altitude, speed, bearing, accuracy, provider, comment, image_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error . "\n";
exit(1);
}
$provider = $comment = $time = $imageid = null;
$provider = $comment = $timestamp = $imageid = null;
$lat = $lon = 0;
$altitude = $speed = $bearing = $accuracy = null;
if (!$pos_insert->bind_param('siiddddddssi',
$time, $user_id, $new_id, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageid)) {
$timestamp, $user_id, $new_id, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $imageid)) {
echo "Binding parameters failed: (" . $pos_insert->errno . ") " . $pos_insert->error . "\n";
exit(1);
}

View File

@ -132,7 +132,7 @@ if ($trackId && $userId) {
"{$lang["user"]}: " . htmlspecialchars($position->userLogin) . "<br>{$lang["track"]}: " . htmlspecialchars($position->trackName) .
"</div>" .
"<div>" .
"<div style=\"padding-top: 10px;\"><b>{$lang["time"]}:</b> {$position->time}<br>" .
"<div style=\"padding-top: 10px;\"><b>{$lang["time"]}:</b> " . date("Y-m-d H:i:s (e)", $position->timestamp) . "<br>" .
(!is_null($position->speed) ? "<b>{$lang["speed"]}:</b> " . round($position->speed * 3.6 * $factor_kmh, 2) . " {$unit_kmh}<br>" : "") .
(!is_null($position->altitude) ? "<b>{$lang["altitude"]}:</b> " . round($position->altitude * $factor_m) . " {$unit_m}<br>" : "") .
"<b>{$lang["ttime"]}:</b> " . toHMS($totalSeconds) . "<br>" .
@ -182,7 +182,7 @@ if ($trackId && $userId) {
$xml->writeAttribute("version", "1.1");
$xml->startElement("metadata");
$xml->writeElement("name", $positionsArr[0]->trackName);
$xml->writeElement("time", gmdate("Y-m-d\TH:i:s\Z", strtotime($positionsArr[0]->time)));
$xml->writeElement("time", gmdate("Y-m-d\TH:i:s\Z", $positionsArr[0]->timestamp));
$xml->endElement();
$xml->startElement("trk");
$xml->writeElement("name", $positionsArr[0]->trackName);
@ -200,11 +200,11 @@ if ($trackId && $userId) {
$xml->writeAttribute("lat", $position->latitude);
$xml->writeAttribute("lon", $position->longitude);
if (!is_null($position->altitude)) { $xml->writeElement("ele", $position->altitude); }
$xml->writeElement("time", gmdate("Y-m-d\TH:i:s\Z", strtotime($position->time)));
$xml->writeElement("time", gmdate("Y-m-d\TH:i:s\Z", $position->timestamp));
$xml->writeElement("name", ++$i);
$xml->startElement("desc");
$description =
"{$lang["user"]}: {$position->userLogin} {$lang["track"]}: {$position->trackName} {$lang["time"]}: {$position->time}" .
"{$lang["user"]}: {$position->userLogin} {$lang["track"]}: {$position->trackName} {$lang["time"]}: " . date("Y-m-d H:i:s (e)", $position->timestamp) .
(!is_null($position->speed) ? " {$lang["speed"]}: " . round($position->speed * 3.6 * $factor_kmh, 2) . " {$unit_kmh}" : "") .
(!is_null($position->altitude) ? " {$lang["altitude"]}: " . round($position->altitude * $factor_m) . " {$unit_m}" : "") .
" {$lang["ttime"]}: " . toHMS($totalSeconds) .

View File

@ -56,7 +56,7 @@ if ($userId) {
$xml->writeElement("altitude", ($position->altitude) ? round($position->altitude) : $position->altitude);
$xml->writeElement("speed", $position->speed);
$xml->writeElement("bearing", $position->bearing);
$xml->writeElement("dateoccured", $position->time);
$xml->writeElement("timestamp", $position->timestamp);
$xml->writeElement("accuracy", $position->accuracy);
$xml->writeElement("provider", $position->provider);
$xml->writeElement("comments", $position->comment);