{"id":799,"date":"2016-02-10T16:43:25","date_gmt":"2016-02-10T07:43:25","guid":{"rendered":"http:\/\/nantoka.filmm.info\/blog\/?p=799"},"modified":"2016-02-10T16:44:49","modified_gmt":"2016-02-10T07:44:49","slug":"ntp%e3%82%b5%e3%83%bc%e3%83%90%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%ef%bc%8c%e3%83%87%e3%83%90%e3%82%a4%e3%82%b9%e9%96%93%e3%81%ae%e6%99%82%e5%88%bb%e4%bf%ae%e6%ad%a3","status":"publish","type":"post","link":"https:\/\/nantoka.filmm.info\/blog\/?p=799","title":{"rendered":"ntp\u30b5\u30fc\u30d0\u3092\u4f7f\u3063\u3066\uff0c\u30c7\u30d0\u30a4\u30b9\u9593\u306e\u6642\u523b\u4fee\u6b63"},"content":{"rendered":"<p>\u30ab\u30a6\u30f3\u30c8\u30c0\u30a6\u30f3\u30a2\u30d7\u30ea\u3092\u3064\u304f\u3063\u3066\u3044\u307e\u3059\u304c\uff0c\u5404\u81ea\u306e\u30b9\u30de\u30db\u306e\u6642\u9593\u304c\u30d0\u30e9\u30d0\u30e9\u306a\u306e\u3067\uff0cntp\u304b\u3089\u5dee\u5206\u3068\u3063\u305f\u6642\u523b\u3067\u6b63\u78ba\u306a\u6642\u9593\u3092\u51fa\u3059\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3057\u305f\uff0e<\/p>\n<p>\u4fee\u6b63\u4e2d\u3067\u3059\u304c\uff0c\u6982\u306d\u3053\u308c\u3067\u4f5c\u52d5\u3057\u307e\u3059\uff0e<\/p>\n<p>\u5143\u30cd\u30bf\u306f\u3053\u308c\u2192\u3000http:\/\/ftvoid.com\/blog\/post\/847<\/p>\n<p><a href=\"http:\/\/nantoka.filmm.info\/blog\/?p=558\" target=\"_blank\">HTML5\u306a\u3089\u3082\u3046\u5c11\u3057\u7c21\u5358<\/a>\u3060\u3063\u305f\u306e\u3067\u3059\u304c\uff0c\uff0c<\/p>\n<pre class=\"lang:default decode:true \" title=\"NtpDate\">using UnityEngine;\r\nusing System;\r\nusing System.Collections;\r\nusing System.Net;\r\nusing System.Net.Sockets;\r\nusing System.Threading;\r\n\r\nusing UnityEngine.UI;\/\/use unity gui\r\n\r\n\/\/ NTP\u540c\u671f\u6642\u523b\u3092\u7ba1\u7406\u3059\u308b\u30af\u30e9\u30b9\r\npublic class NtpDate : MonoBehaviour {\r\n\tprivate DateTime ntpDate;   \/\/ NTP\u540c\u671f\u6642\u523b\r\n\tprivate float rcvAppDate;   \/\/ NTP\u901a\u4fe1\u6642\u306e\u30a2\u30d7\u30ea\u6642\u523b\r\n\r\n\tprivate IPEndPoint ipAny;\r\n\tprivate UdpClient sock;\r\n\tprivate Thread thread;\r\n\tprivate volatile bool threadRunning = false;\r\n\tprivate byte[] rcvData;\r\n\r\n\tpublic string myString; \/\/for time display\r\n\tprivate DateTime endDate; \/\/for end date\r\n\tpublic Transform myButton; \/\/get button object\r\n\tpublic Text countText; \/\/Text\u7528\u5909\u6570\r\n\tprivate TimeSpan zure; \r\n\r\n\t\/\/ \u521d\u671f\u5316\r\n\tvoid Start() {\r\n\t\t\/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u5b9f\u884c\r\n\t\tSyncDate();\r\n\t\t\/\/ \u6642\u523b\u8868\u793a(\u30c7\u30d0\u30c3\u30b0\u7528)\r\n\t\tStartCoroutine(ShowSyncDate());\r\n\r\n\t\t\/\/Debug.Log (System.DateTime.Now);\r\n\t}\r\n\r\n\t\/\/ \u540c\u671f\u6642\u523b\u306e\u8868\u793a\r\n\tprivate IEnumerator ShowSyncDate() {\r\n\t\twhile ( true ) {\r\n\t\t\tyield return new WaitForSeconds(0.1f);\/\/0.5f\r\n\r\n\t\t\ttimeCheck ();\/\/ do timeCheck() every 1 second\r\n\r\n\t\t\t\/\/if ( Date == false ) {\r\n\t\t\tif ( Date == null ) {\r\n\t\t\t\tDebug.Log(\"Time is not received.\");\r\n\t\t\t} else {\r\n\t\t\t\/\/Debug.Log(\"Receive date : \" + Date.ToString());\r\n\t\t\t\t\/\/\tDebug.Log (DateTime.Now);\/\/  DateTime.Now is confiured time\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\r\n\t\/\/\/ for GUI my add\r\n\tpublic void timeCheck(){\r\n\r\n\tendDate = new DateTime(2016, 4, 2, 18,30,00); \/\/\u672c\u756a\u7528\r\n\r\n\t\/\/\tendDate = new DateTime(2016, 2, 4, 12,30,00);\/\/for test\r\n\t\tTimeSpan sabun = endDate - DateTime.Now; \/\/compare now time and end time\r\n\r\n\/\/\t\tDebug.Log (\"before\" + sabun);\r\n\/\/\t\tDebug.Log (\"after\" + (sabun - zure));\r\n\r\n\r\n\t\t\/\/int result = TimeSpan.Compare(endDate, DateTime.Now);\/\/ cant use\r\n\t\tsabun = sabun - zure;\/\/zure naosu\r\n\t\tif (sabun.Days &lt;= 0 &amp;&amp; sabun.Hours &lt;= 0 &amp;&amp; sabun.Minutes &lt;= 0 &amp;&amp; sabun.Seconds &lt;= 0) { \/\/sabun is all zero and minus\r\n\t\t\tmyString = \"Light It Up Blue!\"; \/\/message of button\r\n\t\t\tcountText.text = myString; \/\/put text to GUItext\r\n\t\t\tmyButton.GetComponent&lt;Toggle&gt;().interactable = true; \/\/unlock the button\r\n\t\t} else {\r\n\t\t\tmyString = string.Format (\"{0:00}D{1:00}H{2:00}M{3:00}S\", sabun.Days, sabun.Hours, sabun.Minutes, sabun.Seconds);\/\/string format\r\n\t\t\tcountText.text = myString; \/\/time remaining\r\n\t\t\tmyButton.GetComponent&lt;Toggle&gt;().interactable = false; \/\/lock button before last time\r\n\t\t}\r\n\r\n\r\n\r\n\t}\r\n\r\n\tpublic void Update(){\r\n\t\/\/\ttimeCheck ();\r\n\r\n\t}\r\n\r\n\r\n\t\/\/ \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u7d42\u4e86\u6642\u51e6\u7406\r\n\tvoid OnApplicationQuit() {\r\n\t\tif ( thread != null ) {\r\n\t\t\tthread.Abort();\r\n\t\t}\r\n\t\tif ( sock != null ) {\r\n\t\t\tsock.Close();\r\n\t\t}\r\n\t}\r\n\r\n\r\n\r\n\t\/\/ \u6642\u523b\u540c\u671f\u3092\u884c\u3046\r\n\tpublic void SyncDate() {\r\n\t\t\/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u5b9f\u884c\r\n\t\tthreadRunning = true;\r\n\t\tthread = new Thread(new ThreadStart(Request));\r\n\t\tthread.Start();\r\n\r\n\t\t\/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u5f85\u6a5f\u30b3\u30eb\u30fc\u30c1\u30f3\u5b9f\u884c\r\n\t\tStartCoroutine(WaitForRequest());\r\n\r\n\t\tDebug.Log(\"Thread is started.\");\r\n\t}\r\n\r\n\t\/\/ NTP\u30b5\u30fc\u30d0\u306b\u5bfe\u3057\u3066\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u5b9f\u884c\u3059\u308b\r\n\tprivate void Request() {\r\n\t\t\/\/ \u30bd\u30b1\u30c3\u30c8\u3092\u958b\u304f\r\n\t\tipAny = new IPEndPoint(IPAddress.Any, 123);\r\n\t\/\/\tsock = new UdpClient(ipAny);\r\n\t\tsock = new UdpClient();\r\n\r\n\t\t\/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u9001\u4fe1\r\n\t\tbyte[] sndData = new byte[48];\r\n\t\tsndData[0] = 0xB;\r\n\t\tsock.Send(sndData, sndData.Length, \"ntp.jst.mfeed.ad.jp\", 123);\r\n\r\n\t\t\/\/ \u30c7\u30fc\u30bf\u53d7\u4fe1\r\n\t\trcvData = sock.Receive(ref ipAny);\r\n\t\t\/\/ \u5b9f\u884c\u4e2d\u30d5\u30e9\u30b0\u30af\u30ea\u30a2\r\n\t\tthreadRunning = false;\r\n\t}\r\n\r\n\t\/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u5f85\u6a5f\u30b3\u30eb\u30fc\u30c1\u30f3\r\n\tprivate IEnumerator WaitForRequest() {\r\n\t\t\/\/ \u30ea\u30af\u30a8\u30b9\u30c8\u7d42\u4e86\u307e\u3067\u5f85\u6a5f\r\n\t\twhile ( threadRunning ) {\r\n\t\t\tyield return 0;\r\n\t\t}\r\n\r\n\t\t\/\/ \u30a2\u30d7\u30ea\u6642\u523b\u4fdd\u5b58\r\n\t\trcvAppDate = Time.realtimeSinceStartup;\r\n\t\tDebug.Log (rcvAppDate);\r\n\t\t\/\/ \u53d7\u4fe1\u3057\u305f\u30d0\u30a4\u30ca\u30ea\u30c7\u30fc\u30bf\u3092DateTime\u578b\u306b\u5909\u63db\r\n\t\tntpDate = new DateTime(1900, 1, 1);\r\n\t\tvar high = (double)BitConverter.ToUInt32(new byte[] { rcvData[43], rcvData[42], rcvData[41], rcvData[40] }, 0);\r\n\t\tvar low = (double)BitConverter.ToUInt32(new byte[] { rcvData[47], rcvData[46], rcvData[45], rcvData[44] }, 0);\r\n\t\tntpDate = ntpDate.AddSeconds(high + low \/ UInt32.MaxValue);\r\n\r\n\t\t\/\/ UTC\u2192\u30ed\u30fc\u30ab\u30eb\u65e5\u6642\u306b\u5909\u63db\r\n\t\tntpDate = ntpDate.ToLocalTime();\r\n\t\tzure = Date - DateTime.Now;\r\n\t\t\tDebug.Log (\"zure\" + zure);\r\n\t}\r\n\r\n\t\/\/ NTP\u540c\u671f\u6642\u523b\r\n\tpublic DateTime Date {\r\n\t\tget {\r\n\t\t\t\r\n\t\t\treturn ntpDate.AddSeconds(Time.realtimeSinceStartup - rcvAppDate);\r\n\t\t}\r\n\t}\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30ab\u30a6\u30f3\u30c8\u30c0\u30a6\u30f3\u30a2\u30d7\u30ea\u3092\u3064\u304f\u3063\u3066\u3044\u307e\u3059\u304c\uff0c\u5404\u81ea\u306e\u30b9\u30de\u30db\u306e\u6642\u9593\u304c\u30d0\u30e9\u30d0\u30e9\u306a\u306e\u3067\uff0cntp\u304b\u3089\u5dee\u5206\u3068\u3063\u305f\u6642\u523b\u3067\u6b63\u78ba\u306a\u6642\u9593\u3092\u51fa\u3059\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3057\u305f\uff0e \u4fee\u6b63\u4e2d\u3067\u3059\u304c\uff0c\u6982\u306d\u3053\u308c\u3067\u4f5c\u52d5\u3057\u307e\u3059\uff0e \u5143\u30cd\u30bf\u306f\u3053\u308c\u2192\u3000http:\/\/ftvoid &hellip; <a href=\"https:\/\/nantoka.filmm.info\/blog\/?p=799\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">ntp\u30b5\u30fc\u30d0\u3092\u4f7f\u3063\u3066\uff0c\u30c7\u30d0\u30a4\u30b9\u9593\u306e\u6642\u523b\u4fee\u6b63<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,2],"tags":[],"class_list":["post-799","post","type-post","status-publish","format-standard","hentry","category-c","category-unity"],"_links":{"self":[{"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=799"}],"version-history":[{"count":2,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/799\/revisions"}],"predecessor-version":[{"id":801,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/799\/revisions\/801"}],"wp:attachment":[{"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}