2011年06月23日

(ブログパーツ)時計

NICTの時刻サーバーと通信して現在時刻を表示します。

NICTサーバーと通信しています。


ソースコードは以下になります。
<p><span id="res20110623">NICTサーバーと通信しています。</span><span id="json20110623"></span></p><script type="text/javascript">json20110623 = [];function jsont(json) {if (json.st && json.it && json.leap && json.next) {json.rt = new Date().getTime();json.it = Math.round(json.it * 1000);json.st = Math.round(json.st * 1000);json.offset = json.st - (json.it + json.rt) / 2;json20110623.push(json);if (json20110623.length == 1) {var script = document.createElement("script");script.type = "text/javascript";script.src = "http://ntp-b1.nict.go.jp/cgi-bin/jsont?" + (new Date().getTime() / 1000);document.getElementById("json20110623").appendChild(script);} else if (json20110623.length == 2) {if (Math.abs(json20110623[0].offset - json20110623[1].offset) < 200) {json20110623[2] = {offset : Math.round((json20110623[0].offset + json20110623[1].offset) / 2)};} else {json20110623[2] = {offset : Math.round(Math.min(json20110623[0].offset, json20110623[1].offset))};}var now = new Date().getTime() + json20110623[2].offset;setTimeout("clock20110623()", Math.ceil(now / 1000) * 1000 - now);}} else {document.getElementById("res20110623").innerHTML = 'NICTサーバーとの通信に失敗しました。';}}function clock20110623() {var obj = new Date(new Date().getTime() + json20110623[2].offset);document.getElementById("res20110623").innerHTML = obj.getFullYear() + '年' + (obj.getMonth() + 1) + '月' + obj.getDate() + '日' + ['日曜日', '月曜日', '火曜日', '水曜日', '木曜日', '金曜日', '土曜日'][obj.getDay()] + obj.getHours() + '時' + obj.getMinutes() + '分' + obj.getSeconds() + '秒<br>あなたのパソコンの時計は' + (json20110623[2].offset < 0 ? -json20110623[2].offset / 1000 + '秒進んでいます。' : json20110623[2].offset / 1000 + '秒遅れています。');var now = new Date().getTime() + json20110623[2].offset;setTimeout("clock20110623()", Math.ceil(now / 1000) * 1000 - now);}(function() {var script = document.createElement("script");script.type = "text/javascript";script.src = "http://ntp-a1.nict.go.jp/cgi-bin/jsont?" + (new Date().getTime() / 1000);document.getElementById("json20110623").appendChild(script);})();</script>


Posted by W少年 at 13:54│Comments(0)
※会員のみコメントを受け付けております、ログインが必要です。
上の画像に書かれている文字を入力して下さい
 
<ご注意>
書き込まれた内容は公開され、ブログの持ち主だけが削除できます。