Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
Diff: http-clock-script.inc
- Revision:
- 14:c3c43c8faf0e
- Child:
- 15:2cea2bbd5046
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/http-clock-script.inc Sat Dec 29 19:01:39 2018 +0000
@@ -0,0 +1,211 @@
+"'use strict';\n"
+"\n"
+"var response = '';\n"
+"var headers = '';\n"
+"var msRtc = 0; //nibbles 0 to 3: 16 bits\n"
+"var msCountAtRtcSet = 0;\n"
+"var msDiff = 0;\n"
+"var ppsIsStable = false; //nibble 4: bit 0\n"
+"var rateIsLocked = false; //nibble 4: bit 1\n"
+"var timeIsLocked = false; //nibble 4: bit 2\n"
+"var leapEnable = false; //nibble 5: bit 0\n"
+"var leapBackward = false; //nibble 5: bit 1\n"
+"var leapmonths1970 = 0; //nibbles 6 to 8: 12 bits\n"
+"var leapmonth = 0;\n"
+"var leapyear = 0;\n"
+"var leaps = 0; //nibbles 9 to 12: 16 bits\n"
+"var msCount = 0;\n"
+"\n"
+"const TICK_MS = 100;\n"
+"const AJAX_REFRESH_MS = 10000;\n"
+"const AJAX_QUIET_MS = 3000;\n"
+"\n"
+"function hexToBit(iChar, iBit)\n"
+"{\n"
+" var value = parseInt(response.charAt(iChar), 16);\n"
+" value >>= iBit;\n"
+" return value & 1;\n"
+"}\n"
+"function parseAjax()\n"
+"{\n"
+" var iDateStart = headers.indexOf('Date:');\n"
+" var iDateEnd = headers.indexOf('\\r', iDateStart);\n"
+" var rtcDate = new Date(headers.slice(iDateStart + 5, iDateEnd));\n"
+" msRtc = parseInt(response.substr(0, 4), 16);\n"
+" msRtc += rtcDate.getTime();\n"
+" msDiff = msRtc - Date.now();\n"
+" msCountAtRtcSet = msCount;\n"
+" ppsIsStable = hexToBit(4, 0);\n"
+" rateIsLocked = hexToBit(4, 1);\n"
+" timeIsLocked = hexToBit(4, 2);\n"
+" leapEnable = hexToBit(5, 0);\n"
+" leapBackward = hexToBit(5, 1);\n"
+" leapmonths1970 = parseInt(response.substr(6, 3), 16);\n"
+" leapmonth = leapmonths1970 % 12;\n"
+" leapyear = (leapmonths1970 - leapmonth) / 12;\n"
+" leapmonth += 1;\n"
+" leapyear += 1970;\n"
+" leaps = parseInt(response.substr(9, 4), 16);\n"
+"}\n"
+"function displayGeneral()\n"
+"{\n"
+" var elem;\n"
+" elem = document.getElementById('ajax-pps-stable' ); if (elem) elem.setAttribute('dir', ppsIsStable ? 'rtl' : 'ltr');\n"
+" elem = document.getElementById('ajax-rate-locked' ); if (elem) elem.setAttribute('dir', rateIsLocked ? 'rtl' : 'ltr');\n"
+" elem = document.getElementById('ajax-time-locked' ); if (elem) elem.setAttribute('dir', timeIsLocked ? 'rtl' : 'ltr');\n"
+"\n"
+" elem = document.getElementById('ajax-leap-enable' ); if (elem) elem.setAttribute('dir', leapEnable ? 'rtl' : 'ltr');\n"
+" elem = document.getElementById('ajax-leap-backward'); if (elem) elem.setAttribute('dir', leapBackward ? 'rtl' : 'ltr');\n"
+"\n"
+" elem = document.getElementById('ajax-leap-year' ); if (elem) elem.value = leapmonths1970 ? leapyear : '';\n"
+" elem = document.getElementById('ajax-leap-month' ); if (elem) elem.value = leapmonths1970 ? leapmonth : '';\n"
+"\n"
+" elem = document.getElementById('ajax-leap-count' ); if (elem) elem.value = leaps;\n"
+"\n"
+" elem = document.getElementById('ajax-response' ); if (elem) elem.innerHTML = response;\n"
+" elem = document.getElementById('ajax-headers' ); if (elem) elem.innerHTML = headers;\n"
+" \n"
+" elem = document.getElementById('date-diff' ); if (elem) elem.innerHTML = msDiff;\n"
+"}\n"
+"\n"
+"function formatNumbers00(i)\n"
+"{\n"
+" if (i<10) i='0' + i;\n"
+" return i;\n"
+"}\n"
+"function formatDayOfWeek(wday)\n"
+"{\n"
+" switch(wday)\n"
+" {\n"
+" case 0: return 'Sun';\n"
+" case 1: return 'Mon';\n"
+" case 2: return 'Tue';\n"
+" case 3: return 'Wed';\n"
+" case 4: return 'Thu';\n"
+" case 5: return 'Fri';\n"
+" case 6: return 'Sat';\n"
+" default: return '---';\n"
+" }\n"
+"}\n"
+"function adjustLeap()\n"
+"{\n"
+" if (msRtc == 0) return; //Don't attempt to adjust an invalid time\n"
+" \n"
+" if (!leapEnable) return; // Adjustment disabled\n"
+" \n"
+" //Get the calander date and time from the ms\n"
+" var now = new Date(msCount - msCountAtRtcSet + msRtc);\n"
+" var y = now.getUTCFullYear();\n"
+" var n = now.getUTCMonth () + 1;\n"
+" var d = now.getUTCDate ();\n"
+" var h = now.getUTCHours ();\n"
+" var m = now.getUTCMinutes ();\n"
+" var s = now.getUTCSeconds ();\n"
+" \n"
+" //Check for leap\n"
+" if (y == leapyear && n == leapmonth && d == 1 && m == 0 && s == 0)\n"
+" {\n"
+" leapEnable = false;\n"
+" if (leapBackward) { msRtc += 1000; leaps -= 1; }\n"
+" else { msRtc -= 1000; leaps += 1; }\n"
+" } \n"
+"}\n"
+"function displayTime()\n"
+"{\n"
+" if (msRtc == 0) return; //Don't attempt to display an invalid time\n"
+" \n"
+" //Get the calander date and time from the ms\n"
+" var now = new Date(msCount - msCountAtRtcSet + msRtc);\n"
+" var y = now.getUTCFullYear();\n"
+" var n = now.getUTCMonth () + 1;\n"
+" var d = now.getUTCDate ();\n"
+" var w = now.getUTCDay (); // 0 == Sunday\n"
+" var h = now.getUTCHours ();\n"
+" var m = now.getUTCMinutes ();\n"
+" var s = now.getUTCSeconds ();\n"
+" \n"
+" //Format time\n"
+" n = formatNumbers00(n);\n"
+" d = formatNumbers00(d);\n"
+" h = formatNumbers00(h);\n"
+" m = formatNumbers00(m);\n"
+" s = formatNumbers00(s);\n"
+" w = formatDayOfWeek(w);\n"
+" \n"
+" //Display time\n"
+" var elem;\n"
+" \n"
+" elem = document.getElementById('date-utc');\n"
+" if (elem) elem.innerHTML = y + '-' + n + '-' + d + ' ' + w + ' ' + h + ':' + m + ':' + s + ' TAI-UTC=' + leaps;\n"
+"\n"
+" elem = document.getElementById('date-pc');\n"
+" //if (elem) elem.innerHTML = now.toString();\n"
+" var options = \n"
+" {\n"
+" year: 'numeric',\n"
+" month: 'short',\n"
+" day: '2-digit',\n"
+" weekday: 'short',\n"
+" hour: '2-digit',\n"
+" minute: '2-digit',\n"
+" second: '2-digit',\n"
+" timeZoneName: 'short'\n"
+" };\n"
+" if (elem) elem.innerHTML = now.toLocaleString(undefined, options);\n"
+"}\n"
+"\n"
+"var ajax;\n"
+"var msCountAtAjaxSend = 0;\n"
+"function AjaxRequest(request) //Used by this script and from HTML page\n"
+"{\n"
+" ajax=new XMLHttpRequest();\n"
+" ajax.onreadystatechange=handleAjaxResponse;\n"
+" if (request) ajax.open('GET', '/ajax-clock' + '?' + request, true);\n"
+" else ajax.open('GET', '/ajax-clock' , true);\n"
+" ajax.send();\n"
+"}\n"
+"function requestAjax() //Used in this script\n"
+"{\n"
+" \n"
+" AjaxRequest('');\n"
+" msCountAtAjaxSend = msCount;\n"
+"}\n"
+"\n"
+"function counter()\n"
+"{\n"
+" msCount += TICK_MS; //Don't use Date.now() as we don't know when its clock will be updated around a leap second\n"
+" adjustLeap();\n"
+" displayTime();\n"
+" if (msCount >= msCountAtRtcSet + AJAX_REFRESH_MS && //Wait until time to refresh\n"
+" msCount >= msCountAtAjaxSend + AJAX_QUIET_MS) //Don't repeat during quiet period\n"
+" {\n"
+" requestAjax(); //RequestAjax will set msRtc and reset msCount\n"
+" }\n"
+"}\n"
+"\n"
+"function handleAjaxResponse()\n"
+"{\n"
+" if (ajax.readyState==4 && ajax.status==200)\n"
+" {\n"
+" response = ajax.responseText;\n"
+" headers = ajax.getAllResponseHeaders();\n"
+" parseAjax();\n"
+" displayGeneral();\n"
+" }\n"
+"}\n"
+"\n"
+"function DisplayLeap() //Called by display leap button in HTML\n"
+"{\n"
+" leapEnable = true;\n"
+" msRtc = Date.UTC(leapyear, leapmonth - 1, 1) - AJAX_REFRESH_MS / 2; //displays the refresh period around the latest leap second\n"
+" msCountAtRtcSet = msCount;\n"
+"}\n"
+"\n"
+"function init()\n"
+"{\n"
+" setInterval(counter, TICK_MS);\n"
+" requestAjax();\n"
+"}\n"
+"if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init ); // Loading hasn't finished yet\n"
+"else init(); //`DOMContentLoaded` has already fired\n"
+""
\ No newline at end of file