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