diff options
author | Christian Pointner <equinox@helsinki.at> | 2015-07-17 19:07:27 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2015-07-17 19:07:43 (GMT) |
commit | e3d491b0f97dae84336d5b3c60ceb7206af20c92 (patch) | |
tree | 3e28445c24232c85e058050ea7e41bad84901686 /www/js/utils.js | |
parent | 0a4460d48706e36ca17217d0cdfa6da116299e19 (diff) |
added websocket base clock
Diffstat (limited to 'www/js/utils.js')
-rw-r--r-- | www/js/utils.js | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/www/js/utils.js b/www/js/utils.js index 8436672..fbcf709 100644 --- a/www/js/utils.js +++ b/www/js/utils.js @@ -116,6 +116,44 @@ function get_rd_week() { // var today = new Date(); var sEpoch = ((+today) / 1000) + get_tz_offset(today); - var week = Math.floor((((sEpoch + 259200)/604800) + 2) % 4) + 1 + var week = Math.floor((((sEpoch + 259200)/604800) + 2) % 4) + 1; return week; } + +function Clock(draw_callback) { + this.draw_callback = draw_callback; + this.daynames = new Array('So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'); + + this.last_update = { timestamp: 0, tz_offset: 3600, week: 3 }; + this.clock_offset = 0; + + this.redraw = function() { + var rdtime_ms = (+new Date()) + (this.last_update.tz_offset * 1000) + this.clock_offset; + + var rdtime = new Date(rdtime_ms); + var date_str = this.daynames[rdtime.getUTCDay()] + ', '; + date_str += rdtime.getUTCDate() + '.' + (rdtime.getUTCMonth() + 1) + '.' + rdtime.getUTCFullYear(); + + var time_str = (rdtime.getUTCHours() > 9 ? ' ' : ' 0') + rdtime.getUTCHours(); + time_str += (rdtime.getUTCMinutes() > 9 ? ':' : ':0') + rdtime.getUTCMinutes(); + time_str += (rdtime.getUTCSeconds() > 9 ? ':' : ':0') + rdtime.getUTCSeconds(); + + this.draw_callback(date_str, time_str, this.last_update.week); + } + + this.update = function(event) { + var update = JSON.parse(event.data); + this.last_update = update; + this.clock_offset = (update.timestamp*1000) - (+new Date()) ; + } + + this.start = function() { + this.sock = new WebSocket("wss://import.helsinki.at/time"); + this.sock.onmessage = this.update.bind(this); + this.interval = setInterval(this.redraw.bind(this), 500); + } + this.stop = function() { + clearInterval(this.interval) + delete this.interval + } +} |