From 4adba04737ed38eab851c5337b1e46dafa4227c7 Mon Sep 17 00:00:00 2001 From: Franzz Date: Mon, 18 Apr 2022 18:50:51 +0200 Subject: [PATCH] Relative local time --- inc/Spot.php | 18 +++++++++++++++--- masks/project.html | 10 ++++++++-- script/spot.js | 2 +- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/inc/Spot.php b/inc/Spot.php index 6efdee6..35d8df7 100755 --- a/inc/Spot.php +++ b/inc/Spot.php @@ -117,7 +117,7 @@ class Spot extends Main 'site_time' => "TIMESTAMP DEFAULT 0", //DEFAULT 0 removes auto-set to current time 'status' => "VARCHAR(10)", 'taken_on' => "TIMESTAMP DEFAULT 0", - 'timezone' => "CHAR(64)", //see mysql.time_zone_name + 'timezone' => "CHAR(64) NOT NULL", //see mysql.time_zone_name 'token' => "VARCHAR(4096)", 'type' => "VARCHAR(20)", 'unix_time' => "INT", @@ -428,7 +428,11 @@ class Spot extends Main $asData['unix_time'] = (int) $iTime; $asData['relative_time'] = Toolbox::getDateTimeDesc($iTime, $this->oLang->getLanguage()); $asData['formatted_time'] = $this->getTimeFormat($iTime); - if($sTimeZone != '') $asData['formatted_time_local'] = $this->getTimeFormat($iTime, $sTimeZone); + + if($sTimeZone != '') { + $asData['formatted_time_local'] = $this->getTimeFormat($iTime, $sTimeZone); + $asData['day_offset'] = self::getTimeZoneDayOffset($iTime, $sTimeZone); + } } private function getFeedConstraints($sType, $sTimeField='site_time', $sReturnFormat='array') { @@ -763,7 +767,7 @@ class Spot extends Main } public function getTimeFormat($iTime, $sTimeZone='') { - if($sTimeZone=='') $sTimeZone = date_default_timezone_get(); + if($sTimeZone == '') $sTimeZone = date_default_timezone_get(); $oDate = new \DateTime('@'.$iTime); $oDate->setTimezone(new \DateTimeZone($sTimeZone)); @@ -773,6 +777,14 @@ class Spot extends Main return $this->oLang->getTranslation('date_time', array($sDate, $sTime)); } + public static function getTimeZoneDayOffset($iTime, $sLocalTimeZone) { + $sSiteTimeZone = date_default_timezone_get(); + $iLocalDate = (int) (new \DateTime('@'.$iTime))->setTimezone(new \DateTimeZone($sLocalTimeZone))->format('Ymd'); + $iSiteDate = (int) (new \DateTime('@'.$iTime))->setTimezone(new \DateTimeZone($sSiteTimeZone ))->format('Ymd'); + + return ($iLocalDate == $iSiteDate)?'0':(($iLocalDate > $iSiteDate)?'+1':'-1'); + } + public static function getTimeZoneFromDate($sDate) { $sTimeZone = null; diff --git a/masks/project.html b/masks/project.html index 000401c..9606b22 100644 --- a/masks/project.html +++ b/masks/project.html @@ -669,7 +669,7 @@ function addSpotMessages(aoMessages) { if(oMsg.formatted_time_local != oMsg.formatted_time) { $Tooltip.append($('

', {'class':'timezone'}) .addIcon('fa-timezone fa-fw fa-lg', true) - .append(oSpot.lang('local_time', oMsg.formatted_time_local))); + .append(oSpot.lang('local_time', getRelativeTime(oMsg.formatted_time_local, oMsg.day_offset)))); } //Weather @@ -853,6 +853,12 @@ function focusOnPost(oFocusPost) { } } +function getRelativeTime(sLocalTime, iOffset) { + let $Time = $('').text(sLocalTime.substr(-5)); + if(iOffset != '0') $Time.append($('', {'title': iOffset+' '+oSpot.lang('unit_day')+' ('+sLocalTime.substr(0, 5)+')'}).text(' '+iOffset)); + return $Time.html(); +} + function getPost(asPost) { asPost.headerless = asPost.headerless || false; var bLink = false; @@ -877,7 +883,7 @@ function getPost(asPost) { .data('clicked', false) .append($('

').addIcon('fa-coords', true).append(getGoogleMapsLink(asPost))) .append($('

').addIcon('fa-time', true).append(sAbsTime)) - .append(bTimeDiff?$('

').addIcon('fa-timezone', true).append(oSpot.lang('local_time', sAbsTimeLocal)):'') + .append(bTimeDiff?$('

').addIcon('fa-timezone', true).append(oSpot.lang('local_time', getRelativeTime(sAbsTimeLocal, asPost.day_offset))):'') .append($('', {'class':'drill'}) .append((!asPost.weather_icon || asPost.weather_icon=='unknown')?'':$('', {'class':'weather', 'title':oSpot.lang(asPost.weather_cond)}).addIcon('fa-'+asPost.weather_icon).append($('').text(asPost.weather_temp+'°C'))) .append($('', {'class':'staticmap', title: oSpot.lang('click_zoom'), src: asPost.static_img_url})) diff --git a/script/spot.js b/script/spot.js index a4da119..45b2c99 100755 --- a/script/spot.js +++ b/script/spot.js @@ -264,7 +264,7 @@ function Spot(asGlobals) this.checkClearance = function(sClearance) { return (self.vars(['user', 'clearance']) >= sClearance); - } + }; } /* Common Functions */