From 0e2982b18dec4b1c21fd6b7b07a5ea46e76a1e5e Mon Sep 17 00:00:00 2001 From: Dale Davies Date: Wed, 20 Apr 2022 09:26:51 +0100 Subject: [PATCH] Issue #27: Use client timezone when OWM API is not used --- jumpapp/assets/js/index.bundle.js | 2 +- jumpapp/assets/js/src/classes/Clock.js | 15 +++++++++++++-- jumpapp/assets/js/src/classes/Main.js | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/jumpapp/assets/js/index.bundle.js b/jumpapp/assets/js/index.bundle.js index 0447efc..34c8221 100644 --- a/jumpapp/assets/js/index.bundle.js +++ b/jumpapp/assets/js/index.bundle.js @@ -1 +1 @@ -(()=>{"use strict";var t={729:t=>{var e=Object.prototype.hasOwnProperty,n="~";function s(){}function i(t,e,n){this.fn=t,this.context=e,this.once=n||!1}function r(t,e,s,r,o){if("function"!=typeof s)throw new TypeError("The listener must be a function");var h=new i(s,r||t,o),c=n?n+e:e;return t._events[c]?t._events[c].fn?t._events[c]=[t._events[c],h]:t._events[c].push(h):(t._events[c]=h,t._eventsCount++),t}function o(t,e){0==--t._eventsCount?t._events=new s:delete t._events[e]}function h(){this._events=new s,this._eventsCount=0}Object.create&&(s.prototype=Object.create(null),(new s).__proto__||(n=!1)),h.prototype.eventNames=function(){var t,s,i=[];if(0===this._eventsCount)return i;for(s in t=this._events)e.call(t,s)&&i.push(n?s.slice(1):s);return Object.getOwnPropertySymbols?i.concat(Object.getOwnPropertySymbols(t)):i},h.prototype.listeners=function(t){var e=n?n+t:t,s=this._events[e];if(!s)return[];if(s.fn)return[s.fn];for(var i=0,r=s.length,o=new Array(r);i{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},n.d=(t,e)=>{for(var s in e)n.o(e,s)&&!n.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:e[s]})},n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),(()=>{class t{constructor(t,e=!1){this.set_utc_shift(),this.contentintervalid=null,this.eventemitter=t,this.ampm=e}set_utc_shift(t=0){this.utcshift=t,this.shiftedtimestamp=(new Date).getTime()+this.utcshift,this.shifteddate=new Date(this.shiftedtimestamp)}get_formatted_time(){let t=this.shifteddate.getUTCHours();const e=String(this.shifteddate.getUTCMinutes()).padStart(2,"0");if(!this.ampm)return String(t).padStart(2,"0")+":"+e;const n=t<=12?"AM":"PM";return t=(t+11)%12+1,t+":"+e+""+n+""}get_hour(){return this.shifteddate.getUTCHours()}update_time(){this.set_utc_shift(this.utcshift),this.eventemitter.emit("clock-updated",{formatted_time:this.get_formatted_time(),hour:this.get_hour(),utcshift:this.utcshift})}run(t){this.contentintervalid&&clearInterval(this.contentintervalid),this.update_time(),this.contentintervalid=setInterval((()=>{this.update_time()}),t)}}var e=n(729),s=n.n(e);class i{constructor(t){this.hour=t,this.greetings={0:"morning",12:"afternoon",16:"evening",19:"night"}}get_greeting(){let t=Object.keys(this.greetings).reverse();for(let e of t)if(this.hour>=e)return this.greetings[e]}}class r{constructor(t){this.eventemitter=t}fetch_owm_data(t){let e="/api/weatherdata.php?token="+JUMP.token;t.length&&(e+="&lat="+t[0]+"&lon="+t[1]),fetch(e).then((t=>t.json())).then((t=>{if(t.error)console.error("JUMP ERROR: There was an issue with the OWM API... "+t.error);else if(401!==t.cod){var e="night";t.dt>t.sys.sunrise&&t.dt{this.weather.fetch_owm_data(this.latlong)}),this.weatherfrequency)):this.eventemitter.emit("show-content")}add_event_listeners(){this.eventemitter.on("weather-loaded",(t=>{this.timezoneshift=t.timezoneshift,this.weatherelm.href="https://openweathermap.org/city/"+t.locationcode,this.weathericonelm.classList.add(t.iconclass),this.clientlocationelm.innerHTML=t.locationname,this.tempelm.innerHTML=t.temp,this.weatherdescelm.innerHTML=t.description,this.clientlocationelm.classList.add("enable"),this.eventemitter.emit("show-content")})),this.eventemitter.on("clock-updated",(t=>{if(null!=this.timeelm&&(this.timeelm.innerHTML=t.formatted_time),null!=this.greetingelm){let e=new i(t.hour);this.greetingelm.innerHTML=e.get_greeting()}})),this.eventemitter.on("show-content",(()=>{this.set_clock(),this.show_content()})),this.clientlocationelm.addEventListener("click",(t=>{navigator.geolocation.getCurrentPosition((t=>{this.latlong=[t.coords.latitude,t.coords.longitude],this.storage.setItem("lastrequestedlocation",JSON.stringify(this.latlong)),this.weather.fetch_owm_data(this.latlong)}),(t=>{console.error(t.message)}),{enableHighAccuracy:!0})})),this.showtagsbuttonelm&&this.showtagsbuttonelm.addEventListener("click",(t=>{this.tagselectorelm.classList.add("enable"),t.preventDefault()})),this.tagsselectorclosebuttonelm&&this.tagsselectorclosebuttonelm.addEventListener("click",(t=>{this.tagselectorelm.classList.remove("enable")}))}show_content(){document.querySelectorAll(".hidden").forEach((function(t){t.classList.remove("hidden")}))}set_clock(){this.clock.set_utc_shift(this.timezoneshift),this.clock.run(this.clockfrequency)}}).init()})()})(); \ No newline at end of file +(()=>{"use strict";var e={729:e=>{var t=Object.prototype.hasOwnProperty,n="~";function i(){}function s(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function r(e,t,i,r,o){if("function"!=typeof i)throw new TypeError("The listener must be a function");var h=new s(i,r||e,o),c=n?n+t:t;return e._events[c]?e._events[c].fn?e._events[c]=[e._events[c],h]:e._events[c].push(h):(e._events[c]=h,e._eventsCount++),e}function o(e,t){0==--e._eventsCount?e._events=new i:delete e._events[t]}function h(){this._events=new i,this._eventsCount=0}Object.create&&(i.prototype=Object.create(null),(new i).__proto__||(n=!1)),h.prototype.eventNames=function(){var e,i,s=[];if(0===this._eventsCount)return s;for(i in e=this._events)t.call(e,i)&&s.push(n?i.slice(1):i);return Object.getOwnPropertySymbols?s.concat(Object.getOwnPropertySymbols(e)):s},h.prototype.listeners=function(e){var t=n?n+e:e,i=this._events[t];if(!i)return[];if(i.fn)return[i.fn];for(var s=0,r=i.length,o=new Array(r);s{var t=e&&e.__esModule?()=>e.default:()=>e;return n.d(t,{a:t}),t},n.d=(e,t)=>{for(var i in t)n.o(t,i)&&!n.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{class e{constructor(e,t=!1,n=!1){this.set_utc_shift(),this.contentintervalid=null,this.eventemitter=e,this.ampm=t,this.forcelocaltime=n}set_utc_shift(e=0){this.utcshift=e,this.shiftedtimestamp=(new Date).getTime()+this.utcshift,this.shifteddate=new Date(this.shiftedtimestamp)}get_formatted_time(){let e=this.shifteddate.getUTCHours(),t=String(this.shifteddate.getUTCMinutes()).padStart(2,"0");if(this.forcelocaltime&&(e=(new Date).getHours(),t=String((new Date).getMinutes()).padStart(2,"0")),!this.ampm)return String(e).padStart(2,"0")+":"+t;const n=e<=12?"AM":"PM";return e=(e+11)%12+1,e+":"+t+""+n+""}get_hour(){return this.forcelocaltime?(new Date).getHours():this.shifteddate.getUTCHours()}update_time(){this.set_utc_shift(this.utcshift),this.eventemitter.emit("clock-updated",{formatted_time:this.get_formatted_time(),hour:this.get_hour(),utcshift:this.utcshift})}run(e){this.contentintervalid&&clearInterval(this.contentintervalid),this.update_time(),this.contentintervalid=setInterval((()=>{this.update_time()}),e)}}var t=n(729),i=n.n(t);class s{constructor(e){this.hour=e,this.greetings={0:"morning",12:"afternoon",16:"evening",19:"night"}}get_greeting(){let e=Object.keys(this.greetings).reverse();for(let t of e)if(this.hour>=t)return this.greetings[t]}}class r{constructor(e){this.eventemitter=e}fetch_owm_data(e){let t="/api/weatherdata.php?token="+JUMP.token;e.length&&(t+="&lat="+e[0]+"&lon="+e[1]),fetch(t).then((e=>e.json())).then((e=>{if(e.error)console.error("JUMP ERROR: There was an issue with the OWM API... "+e.error);else if(401!==e.cod){var t="night";e.dt>e.sys.sunrise&&e.dt{this.weather.fetch_owm_data(this.latlong)}),this.weatherfrequency)):this.eventemitter.emit("show-content")}add_event_listeners(){this.eventemitter.on("weather-loaded",(e=>{this.timezoneshift=e.timezoneshift,this.weatherelm.href="https://openweathermap.org/city/"+e.locationcode,this.weathericonelm.classList.add(e.iconclass),this.clientlocationelm.innerHTML=e.locationname,this.tempelm.innerHTML=e.temp,this.weatherdescelm.innerHTML=e.description,this.clientlocationelm.classList.add("enable"),this.eventemitter.emit("show-content")})),this.eventemitter.on("clock-updated",(e=>{if(null!=this.timeelm&&(this.timeelm.innerHTML=e.formatted_time),null!=this.greetingelm){let t=new s(e.hour);this.greetingelm.innerHTML=t.get_greeting()}})),this.eventemitter.on("show-content",(()=>{this.set_clock(),this.show_content()})),this.clientlocationelm.addEventListener("click",(e=>{navigator.geolocation.getCurrentPosition((e=>{this.latlong=[e.coords.latitude,e.coords.longitude],this.storage.setItem("lastrequestedlocation",JSON.stringify(this.latlong)),this.weather.fetch_owm_data(this.latlong)}),(e=>{console.error(e.message)}),{enableHighAccuracy:!0})})),this.showtagsbuttonelm&&this.showtagsbuttonelm.addEventListener("click",(e=>{this.tagselectorelm.classList.add("enable"),e.preventDefault()})),this.tagsselectorclosebuttonelm&&this.tagsselectorclosebuttonelm.addEventListener("click",(e=>{this.tagselectorelm.classList.remove("enable")}))}show_content(){document.querySelectorAll(".hidden").forEach((function(e){e.classList.remove("hidden")}))}set_clock(){this.clock.set_utc_shift(this.timezoneshift),this.clock.run(this.clockfrequency)}}).init()})()})(); \ No newline at end of file diff --git a/jumpapp/assets/js/src/classes/Clock.js b/jumpapp/assets/js/src/classes/Clock.js index f389747..22d5e5a 100644 --- a/jumpapp/assets/js/src/classes/Clock.js +++ b/jumpapp/assets/js/src/classes/Clock.js @@ -14,11 +14,12 @@ export default class Clock { * @param boolean ampm Return 12 hour format if true. * @param number utcshift Number of seconds to shift time from UTC. */ - constructor(eventemitter, ampm = false) { + constructor(eventemitter, ampm = false, forcelocaltime = false) { this.set_utc_shift(); this.contentintervalid = null; this.eventemitter = eventemitter; this.ampm = ampm; + this.forcelocaltime = forcelocaltime; } set_utc_shift(newutcshift = 0) { @@ -37,7 +38,14 @@ export default class Clock { // the Date() object adjusting the returned time relative to the // browser's local timezone. let hour = this.shifteddate.getUTCHours(); - const minutes = String(this.shifteddate.getUTCMinutes()).padStart(2, '0'); + let minutes = String(this.shifteddate.getUTCMinutes()).padStart(2, '0'); + + // Completely ignore the shifted date and just return whatever happens to be + // in the local timezone. + if (this.forcelocaltime) { + hour = new Date().getHours(); + minutes = String(new Date().getMinutes()).padStart(2, '0'); + } if (!this.ampm) { return String(hour).padStart(2, '0') + ":" + minutes; @@ -54,6 +62,9 @@ export default class Clock { * @returns number The hour. */ get_hour() { + if (this.forcelocaltime) { + return new Date().getHours(); + } return this.shifteddate.getUTCHours(); } diff --git a/jumpapp/assets/js/src/classes/Main.js b/jumpapp/assets/js/src/classes/Main.js index 3d2b4ab..7d587d5 100644 --- a/jumpapp/assets/js/src/classes/Main.js +++ b/jumpapp/assets/js/src/classes/Main.js @@ -30,7 +30,7 @@ export default class Main { } // Finally create instances of the classes we'll be using. this.eventemitter = new EventEmitter(); - this.clock = new Clock(this.eventemitter, !!JUMP.ampmclock); + this.clock = new Clock(this.eventemitter, !!JUMP.ampmclock, !JUMP.owmapikey); this.weather = new Weather(this.eventemitter); }