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
http-clock-script.inc@14:c3c43c8faf0e, 2018-12-29 (annotated)
- Committer:
- andrewboyson
- Date:
- Sat Dec 29 19:01:39 2018 +0000
- Revision:
- 14:c3c43c8faf0e
- Child:
- 15:2cea2bbd5046
Simplified and tidied. Most things now called HTTP rather than HTML.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| andrewboyson | 14:c3c43c8faf0e | 1 | "'use strict';\n" |
| andrewboyson | 14:c3c43c8faf0e | 2 | "\n" |
| andrewboyson | 14:c3c43c8faf0e | 3 | "var response = '';\n" |
| andrewboyson | 14:c3c43c8faf0e | 4 | "var headers = '';\n" |
| andrewboyson | 14:c3c43c8faf0e | 5 | "var msRtc = 0; //nibbles 0 to 3: 16 bits\n" |
| andrewboyson | 14:c3c43c8faf0e | 6 | "var msCountAtRtcSet = 0;\n" |
| andrewboyson | 14:c3c43c8faf0e | 7 | "var msDiff = 0;\n" |
| andrewboyson | 14:c3c43c8faf0e | 8 | "var ppsIsStable = false; //nibble 4: bit 0\n" |
| andrewboyson | 14:c3c43c8faf0e | 9 | "var rateIsLocked = false; //nibble 4: bit 1\n" |
| andrewboyson | 14:c3c43c8faf0e | 10 | "var timeIsLocked = false; //nibble 4: bit 2\n" |
| andrewboyson | 14:c3c43c8faf0e | 11 | "var leapEnable = false; //nibble 5: bit 0\n" |
| andrewboyson | 14:c3c43c8faf0e | 12 | "var leapBackward = false; //nibble 5: bit 1\n" |
| andrewboyson | 14:c3c43c8faf0e | 13 | "var leapmonths1970 = 0; //nibbles 6 to 8: 12 bits\n" |
| andrewboyson | 14:c3c43c8faf0e | 14 | "var leapmonth = 0;\n" |
| andrewboyson | 14:c3c43c8faf0e | 15 | "var leapyear = 0;\n" |
| andrewboyson | 14:c3c43c8faf0e | 16 | "var leaps = 0; //nibbles 9 to 12: 16 bits\n" |
| andrewboyson | 14:c3c43c8faf0e | 17 | "var msCount = 0;\n" |
| andrewboyson | 14:c3c43c8faf0e | 18 | "\n" |
| andrewboyson | 14:c3c43c8faf0e | 19 | "const TICK_MS = 100;\n" |
| andrewboyson | 14:c3c43c8faf0e | 20 | "const AJAX_REFRESH_MS = 10000;\n" |
| andrewboyson | 14:c3c43c8faf0e | 21 | "const AJAX_QUIET_MS = 3000;\n" |
| andrewboyson | 14:c3c43c8faf0e | 22 | "\n" |
| andrewboyson | 14:c3c43c8faf0e | 23 | "function hexToBit(iChar, iBit)\n" |
| andrewboyson | 14:c3c43c8faf0e | 24 | "{\n" |
| andrewboyson | 14:c3c43c8faf0e | 25 | " var value = parseInt(response.charAt(iChar), 16);\n" |
| andrewboyson | 14:c3c43c8faf0e | 26 | " value >>= iBit;\n" |
| andrewboyson | 14:c3c43c8faf0e | 27 | " return value & 1;\n" |
| andrewboyson | 14:c3c43c8faf0e | 28 | "}\n" |
| andrewboyson | 14:c3c43c8faf0e | 29 | "function parseAjax()\n" |
| andrewboyson | 14:c3c43c8faf0e | 30 | "{\n" |
| andrewboyson | 14:c3c43c8faf0e | 31 | " var iDateStart = headers.indexOf('Date:');\n" |
| andrewboyson | 14:c3c43c8faf0e | 32 | " var iDateEnd = headers.indexOf('\\r', iDateStart);\n" |
| andrewboyson | 14:c3c43c8faf0e | 33 | " var rtcDate = new Date(headers.slice(iDateStart + 5, iDateEnd));\n" |
| andrewboyson | 14:c3c43c8faf0e | 34 | " msRtc = parseInt(response.substr(0, 4), 16);\n" |
| andrewboyson | 14:c3c43c8faf0e | 35 | " msRtc += rtcDate.getTime();\n" |
| andrewboyson | 14:c3c43c8faf0e | 36 | " msDiff = msRtc - Date.now();\n" |
| andrewboyson | 14:c3c43c8faf0e | 37 | " msCountAtRtcSet = msCount;\n" |
| andrewboyson | 14:c3c43c8faf0e | 38 | " ppsIsStable = hexToBit(4, 0);\n" |
| andrewboyson | 14:c3c43c8faf0e | 39 | " rateIsLocked = hexToBit(4, 1);\n" |
| andrewboyson | 14:c3c43c8faf0e | 40 | " timeIsLocked = hexToBit(4, 2);\n" |
| andrewboyson | 14:c3c43c8faf0e | 41 | " leapEnable = hexToBit(5, 0);\n" |
| andrewboyson | 14:c3c43c8faf0e | 42 | " leapBackward = hexToBit(5, 1);\n" |
| andrewboyson | 14:c3c43c8faf0e | 43 | " leapmonths1970 = parseInt(response.substr(6, 3), 16);\n" |
| andrewboyson | 14:c3c43c8faf0e | 44 | " leapmonth = leapmonths1970 % 12;\n" |
| andrewboyson | 14:c3c43c8faf0e | 45 | " leapyear = (leapmonths1970 - leapmonth) / 12;\n" |
| andrewboyson | 14:c3c43c8faf0e | 46 | " leapmonth += 1;\n" |
| andrewboyson | 14:c3c43c8faf0e | 47 | " leapyear += 1970;\n" |
| andrewboyson | 14:c3c43c8faf0e | 48 | " leaps = parseInt(response.substr(9, 4), 16);\n" |
| andrewboyson | 14:c3c43c8faf0e | 49 | "}\n" |
| andrewboyson | 14:c3c43c8faf0e | 50 | "function displayGeneral()\n" |
| andrewboyson | 14:c3c43c8faf0e | 51 | "{\n" |
| andrewboyson | 14:c3c43c8faf0e | 52 | " var elem;\n" |
| andrewboyson | 14:c3c43c8faf0e | 53 | " elem = document.getElementById('ajax-pps-stable' ); if (elem) elem.setAttribute('dir', ppsIsStable ? 'rtl' : 'ltr');\n" |
| andrewboyson | 14:c3c43c8faf0e | 54 | " elem = document.getElementById('ajax-rate-locked' ); if (elem) elem.setAttribute('dir', rateIsLocked ? 'rtl' : 'ltr');\n" |
| andrewboyson | 14:c3c43c8faf0e | 55 | " elem = document.getElementById('ajax-time-locked' ); if (elem) elem.setAttribute('dir', timeIsLocked ? 'rtl' : 'ltr');\n" |
| andrewboyson | 14:c3c43c8faf0e | 56 | "\n" |
| andrewboyson | 14:c3c43c8faf0e | 57 | " elem = document.getElementById('ajax-leap-enable' ); if (elem) elem.setAttribute('dir', leapEnable ? 'rtl' : 'ltr');\n" |
| andrewboyson | 14:c3c43c8faf0e | 58 | " elem = document.getElementById('ajax-leap-backward'); if (elem) elem.setAttribute('dir', leapBackward ? 'rtl' : 'ltr');\n" |
| andrewboyson | 14:c3c43c8faf0e | 59 | "\n" |
| andrewboyson | 14:c3c43c8faf0e | 60 | " elem = document.getElementById('ajax-leap-year' ); if (elem) elem.value = leapmonths1970 ? leapyear : '';\n" |
| andrewboyson | 14:c3c43c8faf0e | 61 | " elem = document.getElementById('ajax-leap-month' ); if (elem) elem.value = leapmonths1970 ? leapmonth : '';\n" |
| andrewboyson | 14:c3c43c8faf0e | 62 | "\n" |
| andrewboyson | 14:c3c43c8faf0e | 63 | " elem = document.getElementById('ajax-leap-count' ); if (elem) elem.value = leaps;\n" |
| andrewboyson | 14:c3c43c8faf0e | 64 | "\n" |
| andrewboyson | 14:c3c43c8faf0e | 65 | " elem = document.getElementById('ajax-response' ); if (elem) elem.innerHTML = response;\n" |
| andrewboyson | 14:c3c43c8faf0e | 66 | " elem = document.getElementById('ajax-headers' ); if (elem) elem.innerHTML = headers;\n" |
| andrewboyson | 14:c3c43c8faf0e | 67 | " \n" |
| andrewboyson | 14:c3c43c8faf0e | 68 | " elem = document.getElementById('date-diff' ); if (elem) elem.innerHTML = msDiff;\n" |
| andrewboyson | 14:c3c43c8faf0e | 69 | "}\n" |
| andrewboyson | 14:c3c43c8faf0e | 70 | "\n" |
| andrewboyson | 14:c3c43c8faf0e | 71 | "function formatNumbers00(i)\n" |
| andrewboyson | 14:c3c43c8faf0e | 72 | "{\n" |
| andrewboyson | 14:c3c43c8faf0e | 73 | " if (i<10) i='0' + i;\n" |
| andrewboyson | 14:c3c43c8faf0e | 74 | " return i;\n" |
| andrewboyson | 14:c3c43c8faf0e | 75 | "}\n" |
| andrewboyson | 14:c3c43c8faf0e | 76 | "function formatDayOfWeek(wday)\n" |
| andrewboyson | 14:c3c43c8faf0e | 77 | "{\n" |
| andrewboyson | 14:c3c43c8faf0e | 78 | " switch(wday)\n" |
| andrewboyson | 14:c3c43c8faf0e | 79 | " {\n" |
| andrewboyson | 14:c3c43c8faf0e | 80 | " case 0: return 'Sun';\n" |
| andrewboyson | 14:c3c43c8faf0e | 81 | " case 1: return 'Mon';\n" |
| andrewboyson | 14:c3c43c8faf0e | 82 | " case 2: return 'Tue';\n" |
| andrewboyson | 14:c3c43c8faf0e | 83 | " case 3: return 'Wed';\n" |
| andrewboyson | 14:c3c43c8faf0e | 84 | " case 4: return 'Thu';\n" |
| andrewboyson | 14:c3c43c8faf0e | 85 | " case 5: return 'Fri';\n" |
| andrewboyson | 14:c3c43c8faf0e | 86 | " case 6: return 'Sat';\n" |
| andrewboyson | 14:c3c43c8faf0e | 87 | " default: return '---';\n" |
| andrewboyson | 14:c3c43c8faf0e | 88 | " }\n" |
| andrewboyson | 14:c3c43c8faf0e | 89 | "}\n" |
| andrewboyson | 14:c3c43c8faf0e | 90 | "function adjustLeap()\n" |
| andrewboyson | 14:c3c43c8faf0e | 91 | "{\n" |
| andrewboyson | 14:c3c43c8faf0e | 92 | " if (msRtc == 0) return; //Don't attempt to adjust an invalid time\n" |
| andrewboyson | 14:c3c43c8faf0e | 93 | " \n" |
| andrewboyson | 14:c3c43c8faf0e | 94 | " if (!leapEnable) return; // Adjustment disabled\n" |
| andrewboyson | 14:c3c43c8faf0e | 95 | " \n" |
| andrewboyson | 14:c3c43c8faf0e | 96 | " //Get the calander date and time from the ms\n" |
| andrewboyson | 14:c3c43c8faf0e | 97 | " var now = new Date(msCount - msCountAtRtcSet + msRtc);\n" |
| andrewboyson | 14:c3c43c8faf0e | 98 | " var y = now.getUTCFullYear();\n" |
| andrewboyson | 14:c3c43c8faf0e | 99 | " var n = now.getUTCMonth () + 1;\n" |
| andrewboyson | 14:c3c43c8faf0e | 100 | " var d = now.getUTCDate ();\n" |
| andrewboyson | 14:c3c43c8faf0e | 101 | " var h = now.getUTCHours ();\n" |
| andrewboyson | 14:c3c43c8faf0e | 102 | " var m = now.getUTCMinutes ();\n" |
| andrewboyson | 14:c3c43c8faf0e | 103 | " var s = now.getUTCSeconds ();\n" |
| andrewboyson | 14:c3c43c8faf0e | 104 | " \n" |
| andrewboyson | 14:c3c43c8faf0e | 105 | " //Check for leap\n" |
| andrewboyson | 14:c3c43c8faf0e | 106 | " if (y == leapyear && n == leapmonth && d == 1 && m == 0 && s == 0)\n" |
| andrewboyson | 14:c3c43c8faf0e | 107 | " {\n" |
| andrewboyson | 14:c3c43c8faf0e | 108 | " leapEnable = false;\n" |
| andrewboyson | 14:c3c43c8faf0e | 109 | " if (leapBackward) { msRtc += 1000; leaps -= 1; }\n" |
| andrewboyson | 14:c3c43c8faf0e | 110 | " else { msRtc -= 1000; leaps += 1; }\n" |
| andrewboyson | 14:c3c43c8faf0e | 111 | " } \n" |
| andrewboyson | 14:c3c43c8faf0e | 112 | "}\n" |
| andrewboyson | 14:c3c43c8faf0e | 113 | "function displayTime()\n" |
| andrewboyson | 14:c3c43c8faf0e | 114 | "{\n" |
| andrewboyson | 14:c3c43c8faf0e | 115 | " if (msRtc == 0) return; //Don't attempt to display an invalid time\n" |
| andrewboyson | 14:c3c43c8faf0e | 116 | " \n" |
| andrewboyson | 14:c3c43c8faf0e | 117 | " //Get the calander date and time from the ms\n" |
| andrewboyson | 14:c3c43c8faf0e | 118 | " var now = new Date(msCount - msCountAtRtcSet + msRtc);\n" |
| andrewboyson | 14:c3c43c8faf0e | 119 | " var y = now.getUTCFullYear();\n" |
| andrewboyson | 14:c3c43c8faf0e | 120 | " var n = now.getUTCMonth () + 1;\n" |
| andrewboyson | 14:c3c43c8faf0e | 121 | " var d = now.getUTCDate ();\n" |
| andrewboyson | 14:c3c43c8faf0e | 122 | " var w = now.getUTCDay (); // 0 == Sunday\n" |
| andrewboyson | 14:c3c43c8faf0e | 123 | " var h = now.getUTCHours ();\n" |
| andrewboyson | 14:c3c43c8faf0e | 124 | " var m = now.getUTCMinutes ();\n" |
| andrewboyson | 14:c3c43c8faf0e | 125 | " var s = now.getUTCSeconds ();\n" |
| andrewboyson | 14:c3c43c8faf0e | 126 | " \n" |
| andrewboyson | 14:c3c43c8faf0e | 127 | " //Format time\n" |
| andrewboyson | 14:c3c43c8faf0e | 128 | " n = formatNumbers00(n);\n" |
| andrewboyson | 14:c3c43c8faf0e | 129 | " d = formatNumbers00(d);\n" |
| andrewboyson | 14:c3c43c8faf0e | 130 | " h = formatNumbers00(h);\n" |
| andrewboyson | 14:c3c43c8faf0e | 131 | " m = formatNumbers00(m);\n" |
| andrewboyson | 14:c3c43c8faf0e | 132 | " s = formatNumbers00(s);\n" |
| andrewboyson | 14:c3c43c8faf0e | 133 | " w = formatDayOfWeek(w);\n" |
| andrewboyson | 14:c3c43c8faf0e | 134 | " \n" |
| andrewboyson | 14:c3c43c8faf0e | 135 | " //Display time\n" |
| andrewboyson | 14:c3c43c8faf0e | 136 | " var elem;\n" |
| andrewboyson | 14:c3c43c8faf0e | 137 | " \n" |
| andrewboyson | 14:c3c43c8faf0e | 138 | " elem = document.getElementById('date-utc');\n" |
| andrewboyson | 14:c3c43c8faf0e | 139 | " if (elem) elem.innerHTML = y + '-' + n + '-' + d + ' ' + w + ' ' + h + ':' + m + ':' + s + ' TAI-UTC=' + leaps;\n" |
| andrewboyson | 14:c3c43c8faf0e | 140 | "\n" |
| andrewboyson | 14:c3c43c8faf0e | 141 | " elem = document.getElementById('date-pc');\n" |
| andrewboyson | 14:c3c43c8faf0e | 142 | " //if (elem) elem.innerHTML = now.toString();\n" |
| andrewboyson | 14:c3c43c8faf0e | 143 | " var options = \n" |
| andrewboyson | 14:c3c43c8faf0e | 144 | " {\n" |
| andrewboyson | 14:c3c43c8faf0e | 145 | " year: 'numeric',\n" |
| andrewboyson | 14:c3c43c8faf0e | 146 | " month: 'short',\n" |
| andrewboyson | 14:c3c43c8faf0e | 147 | " day: '2-digit',\n" |
| andrewboyson | 14:c3c43c8faf0e | 148 | " weekday: 'short',\n" |
| andrewboyson | 14:c3c43c8faf0e | 149 | " hour: '2-digit',\n" |
| andrewboyson | 14:c3c43c8faf0e | 150 | " minute: '2-digit',\n" |
| andrewboyson | 14:c3c43c8faf0e | 151 | " second: '2-digit',\n" |
| andrewboyson | 14:c3c43c8faf0e | 152 | " timeZoneName: 'short'\n" |
| andrewboyson | 14:c3c43c8faf0e | 153 | " };\n" |
| andrewboyson | 14:c3c43c8faf0e | 154 | " if (elem) elem.innerHTML = now.toLocaleString(undefined, options);\n" |
| andrewboyson | 14:c3c43c8faf0e | 155 | "}\n" |
| andrewboyson | 14:c3c43c8faf0e | 156 | "\n" |
| andrewboyson | 14:c3c43c8faf0e | 157 | "var ajax;\n" |
| andrewboyson | 14:c3c43c8faf0e | 158 | "var msCountAtAjaxSend = 0;\n" |
| andrewboyson | 14:c3c43c8faf0e | 159 | "function AjaxRequest(request) //Used by this script and from HTML page\n" |
| andrewboyson | 14:c3c43c8faf0e | 160 | "{\n" |
| andrewboyson | 14:c3c43c8faf0e | 161 | " ajax=new XMLHttpRequest();\n" |
| andrewboyson | 14:c3c43c8faf0e | 162 | " ajax.onreadystatechange=handleAjaxResponse;\n" |
| andrewboyson | 14:c3c43c8faf0e | 163 | " if (request) ajax.open('GET', '/ajax-clock' + '?' + request, true);\n" |
| andrewboyson | 14:c3c43c8faf0e | 164 | " else ajax.open('GET', '/ajax-clock' , true);\n" |
| andrewboyson | 14:c3c43c8faf0e | 165 | " ajax.send();\n" |
| andrewboyson | 14:c3c43c8faf0e | 166 | "}\n" |
| andrewboyson | 14:c3c43c8faf0e | 167 | "function requestAjax() //Used in this script\n" |
| andrewboyson | 14:c3c43c8faf0e | 168 | "{\n" |
| andrewboyson | 14:c3c43c8faf0e | 169 | " \n" |
| andrewboyson | 14:c3c43c8faf0e | 170 | " AjaxRequest('');\n" |
| andrewboyson | 14:c3c43c8faf0e | 171 | " msCountAtAjaxSend = msCount;\n" |
| andrewboyson | 14:c3c43c8faf0e | 172 | "}\n" |
| andrewboyson | 14:c3c43c8faf0e | 173 | "\n" |
| andrewboyson | 14:c3c43c8faf0e | 174 | "function counter()\n" |
| andrewboyson | 14:c3c43c8faf0e | 175 | "{\n" |
| andrewboyson | 14:c3c43c8faf0e | 176 | " msCount += TICK_MS; //Don't use Date.now() as we don't know when its clock will be updated around a leap second\n" |
| andrewboyson | 14:c3c43c8faf0e | 177 | " adjustLeap();\n" |
| andrewboyson | 14:c3c43c8faf0e | 178 | " displayTime();\n" |
| andrewboyson | 14:c3c43c8faf0e | 179 | " if (msCount >= msCountAtRtcSet + AJAX_REFRESH_MS && //Wait until time to refresh\n" |
| andrewboyson | 14:c3c43c8faf0e | 180 | " msCount >= msCountAtAjaxSend + AJAX_QUIET_MS) //Don't repeat during quiet period\n" |
| andrewboyson | 14:c3c43c8faf0e | 181 | " {\n" |
| andrewboyson | 14:c3c43c8faf0e | 182 | " requestAjax(); //RequestAjax will set msRtc and reset msCount\n" |
| andrewboyson | 14:c3c43c8faf0e | 183 | " }\n" |
| andrewboyson | 14:c3c43c8faf0e | 184 | "}\n" |
| andrewboyson | 14:c3c43c8faf0e | 185 | "\n" |
| andrewboyson | 14:c3c43c8faf0e | 186 | "function handleAjaxResponse()\n" |
| andrewboyson | 14:c3c43c8faf0e | 187 | "{\n" |
| andrewboyson | 14:c3c43c8faf0e | 188 | " if (ajax.readyState==4 && ajax.status==200)\n" |
| andrewboyson | 14:c3c43c8faf0e | 189 | " {\n" |
| andrewboyson | 14:c3c43c8faf0e | 190 | " response = ajax.responseText;\n" |
| andrewboyson | 14:c3c43c8faf0e | 191 | " headers = ajax.getAllResponseHeaders();\n" |
| andrewboyson | 14:c3c43c8faf0e | 192 | " parseAjax();\n" |
| andrewboyson | 14:c3c43c8faf0e | 193 | " displayGeneral();\n" |
| andrewboyson | 14:c3c43c8faf0e | 194 | " }\n" |
| andrewboyson | 14:c3c43c8faf0e | 195 | "}\n" |
| andrewboyson | 14:c3c43c8faf0e | 196 | "\n" |
| andrewboyson | 14:c3c43c8faf0e | 197 | "function DisplayLeap() //Called by display leap button in HTML\n" |
| andrewboyson | 14:c3c43c8faf0e | 198 | "{\n" |
| andrewboyson | 14:c3c43c8faf0e | 199 | " leapEnable = true;\n" |
| andrewboyson | 14:c3c43c8faf0e | 200 | " msRtc = Date.UTC(leapyear, leapmonth - 1, 1) - AJAX_REFRESH_MS / 2; //displays the refresh period around the latest leap second\n" |
| andrewboyson | 14:c3c43c8faf0e | 201 | " msCountAtRtcSet = msCount;\n" |
| andrewboyson | 14:c3c43c8faf0e | 202 | "}\n" |
| andrewboyson | 14:c3c43c8faf0e | 203 | "\n" |
| andrewboyson | 14:c3c43c8faf0e | 204 | "function init()\n" |
| andrewboyson | 14:c3c43c8faf0e | 205 | "{\n" |
| andrewboyson | 14:c3c43c8faf0e | 206 | " setInterval(counter, TICK_MS);\n" |
| andrewboyson | 14:c3c43c8faf0e | 207 | " requestAjax();\n" |
| andrewboyson | 14:c3c43c8faf0e | 208 | "}\n" |
| andrewboyson | 14:c3c43c8faf0e | 209 | "if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init ); // Loading hasn't finished yet\n" |
| andrewboyson | 14:c3c43c8faf0e | 210 | "else init(); //`DOMContentLoaded` has already fired\n" |
| andrewboyson | 14:c3c43c8faf0e | 211 | "" |