|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | var browserSettings = require('./browser-settings'); |
| 4 | +var moment = require('moment-timezone'); |
| 5 | +var timeformat = require('./timeformat'); |
4 | 6 | var client = {}; |
5 | 7 | var latestProperties = {}; |
| 8 | +var profileTimezone = null; |
6 | 9 |
|
7 | 10 | client.query = function query () { |
8 | 11 | var parts = (location.search || '?').substring(1).split('&'); |
@@ -35,6 +38,39 @@ client.query = function query () { |
35 | 38 | }); |
36 | 39 | }; |
37 | 40 |
|
| 41 | +client.queryProfile = function queryProfile () { |
| 42 | + var parts = (location.search || '?').substring(1).split('&'); |
| 43 | + var token = ''; |
| 44 | + parts.forEach(function(val) { |
| 45 | + if (val.startsWith('token=')) { |
| 46 | + token = val.substring('token='.length); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + var secret = localStorage.getItem('apisecrethash'); |
| 51 | + var src = '/api/v1/profile.json'; |
| 52 | + |
| 53 | + if (secret) { |
| 54 | + var s = '?secret=' + secret; |
| 55 | + src += s; |
| 56 | + } else if (token) { |
| 57 | + var s2 = '?token=' + token; |
| 58 | + src += s2; |
| 59 | + } |
| 60 | + |
| 61 | + $.ajax(src, { |
| 62 | + error: function gotError (err) { |
| 63 | + console.error('Error fetching profile:', err); |
| 64 | + } |
| 65 | + , success: function gotData (data) { |
| 66 | + if (data && data.length > 0 && data[0].store && data[0].store.Default && data[0].store.Default.timezone) { |
| 67 | + profileTimezone = data[0].store.Default.timezone; |
| 68 | + console.log('Profile timezone:', profileTimezone); |
| 69 | + } |
| 70 | + } |
| 71 | + }); |
| 72 | +}; |
| 73 | + |
38 | 74 | client.render = function render () { |
39 | 75 |
|
40 | 76 | if (!latestProperties.bgnow && !latestProperties.bgnow.sgvs) { |
@@ -189,23 +225,72 @@ client.render = function render () { |
189 | 225 |
|
190 | 226 | function updateClock () { |
191 | 227 | let timeDivisor = parseInt(client.settings.timeFormat ? client.settings.timeFormat : 12, 10); |
192 | | - let today = new Date() |
193 | | - , h = today.getHours() % timeDivisor; |
| 228 | + |
| 229 | + // Get device time and offset using shared utilities |
| 230 | + let deviceTime = timeformat.getDeviceTime(profileTimezone, moment); |
| 231 | + let deviceOffsetSeconds = timeformat.getTimezoneOffsetSeconds(profileTimezone, moment); |
| 232 | + |
| 233 | + // Fall back to browser time if no device timezone |
| 234 | + if (!deviceTime) { |
| 235 | + deviceTime = new Date(); |
| 236 | + } |
| 237 | + |
| 238 | + // Format device time |
| 239 | + let h = deviceTime.getHours() % timeDivisor; |
194 | 240 | if (timeDivisor === 12) { |
195 | 241 | h = (h === 0) ? 12 : h; // In the case of 00:xx, change to 12:xx for 12h time |
196 | 242 | } |
197 | 243 | if (timeDivisor === 24) { |
198 | 244 | h = (h < 10) ? ("0" + h) : h; // Pad the hours with a 0 in 24h time |
199 | 245 | } |
200 | | - let m = today.getMinutes(); |
| 246 | + let m = deviceTime.getMinutes(); |
201 | 247 | if (m < 10) m = "0" + m; |
202 | | - $('.tm').html(h + ":" + m); |
| 248 | + |
| 249 | + // Check if we need to show both times (different UTC offsets) |
| 250 | + let showBothTimes = timeformat.shouldShowBothTimes(deviceOffsetSeconds); |
| 251 | + |
| 252 | + if (showBothTimes) { |
| 253 | + // Format browser local time |
| 254 | + let browserTime = new Date(); |
| 255 | + let browserH = browserTime.getHours() % timeDivisor; |
| 256 | + if (timeDivisor === 12) { |
| 257 | + browserH = (browserH === 0) ? 12 : browserH; |
| 258 | + } |
| 259 | + if (timeDivisor === 24) { |
| 260 | + browserH = (browserH < 10) ? ("0" + browserH) : browserH; |
| 261 | + } |
| 262 | + let browserM = browserTime.getMinutes(); |
| 263 | + if (browserM < 10) browserM = "0" + browserM; |
| 264 | + |
| 265 | + // Display both times with separate spans for styling |
| 266 | + $('.tm').html( |
| 267 | + h + ":" + m + " <span class=\"device-label\">(Device)</span><br>" + |
| 268 | + "<span class=\"local-time\">" + browserH + ":" + browserM + " (Local)</span>" |
| 269 | + ); |
| 270 | + |
| 271 | + // Apply styles via CSS |
| 272 | + $('.tm .device-label').css({ |
| 273 | + 'font-size': '0.6em', |
| 274 | + 'opacity': '0.7' |
| 275 | + }); |
| 276 | + $('.tm .local-time').css({ |
| 277 | + 'font-size': '0.8em', |
| 278 | + 'opacity': '0.6' |
| 279 | + }); |
| 280 | + } else { |
| 281 | + // Display only device time (or browser time if no device data) |
| 282 | + $('.tm').html(h + ":" + m); |
| 283 | + } |
203 | 284 | } |
204 | 285 |
|
205 | 286 | client.init = function init () { |
206 | 287 |
|
207 | 288 | console.log('Initializing clock'); |
208 | 289 | client.settings = browserSettings(client, window.serverSettings, $); |
| 290 | + |
| 291 | + // Fetch profile timezone once on initialization |
| 292 | + client.queryProfile(); |
| 293 | + |
209 | 294 | client.query(); |
210 | 295 | setInterval(client.query, 20 * 1000); // update every 20 seconds |
211 | 296 |
|
|
0 commit comments