How to get internet time?

21 ビュー (過去 30 日間)
Shuai Xu
Shuai Xu 2022 年 7 月 29 日
コメント済み: Shuai Xu 2022 年 8 月 1 日
I want to write a simple license funciton for my app, one of the avaliable/simple way is using internet time as time reference.
So my question is how to get internet time in matlab?
  4 件のコメント
Walter Roberson
Walter Roberson 2022 年 7 月 29 日
編集済み: Jan 2022 年 7 月 29 日
Shuai Xu
Shuai Xu 2022 年 7 月 29 日
Thank you, Walter. But this link is written by c#, what I want is matlab code version. maybe webread funciton is the right funciton I need, but I cannot get a internet time with it.
Hi Benjamin, regarding the internet access issue, yes, you are right. but it doesn't matter, since my app is not a public one, it just a beta/trial version for some specified users. I will tell them they must have an internet connection before they can use it.

サインインしてコメントする。

回答 (1 件)

Jan
Jan 2022 年 7 月 29 日
編集済み: Jan 2022 年 7 月 29 日
This java code does not run here in the forum. Maybe it is blocked by a firewall. Please try it in your local Matlab instance:
% [UNTESTED CODE]
import java.net.InetAddress;
import java.util.Date;
import org.apache.commons.net.ntp.NTPUDPClient;
import org.apache.commons.net.ntp.NtpV3Packet;
import org.apache.commons.net.ntp.TimeInfo;
TIME_SERVER = "de.pool.ntp.org"; % [EDITED] New server address
timeClient = NTPUDPClient();
inetAddress = InetAddress.getByName(java.lang.String(TIME_SERVER));
timeInfo = timeClient.getTime(inetAddress);
message = timeInfo.getMessage();
serverTime = message.getTransmitTimeStamp().getTime()
serverTime = 1.6591e+12
Date(serverTime)
ans = Fri Jul 29 18:06:53 GMT 2022
Or download FEX: getUTC
You can parse the output of this also:
data=webread('http://www.utctime.net/utc-timestamp')
data =
'<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style>*{box-sizing:border-box;margin:0;padding:0}header{background-color:#f8f8f8;height:50px;padding:4px 0 0 0;margin-bottom:4px;z-index: 1;position:fixed;top:0;left:0;right:0}main.content{margin-top:51px}label{margin-bottom:.3em}legend{font-size:16px;font-weight:600}label.noblock{padding:4px 6px;margin:.2em 0}.margin10{margin:10px 0}#utctime{font-family:monospace;vertical-align:super;font-size:1.15em;background-color:#fff;border-radius:4px;padding:2px 5px;border:1px solid #ddd}fieldset,legend{border:0}input,select,textarea{color:inherit;font:inherit;padding:.5em;margin:0 5px 5px 0;border:1px solid #ccc;box-shadow:inset 0 1px 3px #ddd;border-radius:4px;display:inline-block;font-size:100%;line-height:normal;max-width:100%}.max300{max-width:300px;margin:10px 0}nav .list-vertical{display:inline-block;overflow:auto}.list-horizontal li a:before,.list-vertical li a:before{content:"\00bb";padding-right:5px;color:#bbb}.toggle{display:none;width:40px;height:40px;cursor:pointer;left:6px;top:12px;position:absolute}.toggle:after{content:'';display:block;width:27px;height:3px;background:#777;margin:5px;box-shadow:0 8px 0 #777,0 16px 0 #777}select{text-transform:none;background-color:#fff;display:inline-block;font-size:100%;height:2.5em}nav{padding:5px 0}.bgwgr{background:-moz-linear-gradient(top, #ffffff 0%, #f3f3f3 50%, #ededed 51%, #ffffff 100%);background:-webkit-linear-gradient(top, #ffffff 0%,#f3f3f3 50%,#ededed 51%,#ffffff 100%);background:linear-gradient(to bottom, #ffffff 0%,#f3f3f3 50%,#ededed 51%,#ffffff 100%);filter:progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#ffffff',GradientType=0 );}.comments{padding:10px 0;margin-top:15px}.comcon{background-color:#fff;overflow:auto;border-radius:4px;padding:10px;margin:5px 0}.comaut{font-weight:600}.comcon .date{color:#555;font-size:85%;float:right}.comcon p{border-top:1px dashed #ccc;clear:left}.comments form{margin:15px 0;background-color:#f1f1f1;padding:10px;border-radius:4px}#message{background-color:#e3f1d4;padding:10px}button,.button{overflow:visible;font:inherit;text-transform:none;letter-spacing:1px;border:0;line-height:normal;white-space:nowrap;padding:.5em;color:#fff;background-color:#1565c0;border-radius:4px;display:inline-block;font-size:100%;text-align:center}.button:focus,.button:hover{opacity:.9}button,html input[type="button"],input[type="submit"]{cursor:pointer}input[type="number"]::-webkit-inner-spin-button,input[type="number"]::-webkit-outer-spin-button{height:auto}.cd-top{overflow:hidden;display:inline-block;height:40px;width:40px;position:fixed;bottom:40px;right:10px;z-index:10;font-size:36px;line-height:55px;color:#fff;background-color:rgba(125,125,125,.5);visibility:hidden;opacity:0;border-radius:4px;text-align:center}.cd-top:hover{opacity:1;color:#eee}.cd-is-visible{visibility:visible;opacity:1}.list-horizontal li{display:inline-block}.list-horizontal li a{padding:2px}#result2,#result{font-size:200%;font-weight:300;padding:12px 0;margin:7px 0;}html{font-size:16px;font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%;overflow-y:scroll}body{line-height:1.6;font-weight:400;background-color:#f5f5f0;color:#333}article,header,aside,main,footer,label{display:block}a{background-color:transparent;color:#3c6bb7;text-decoration:none}a:active,a:hover{outline:0;color:#111}h1 a{color:#436BB1;}img{border:0}.row:after{content:"";clear:both;display:block}[class*=col-]{padding:1px;float:left;width:100%}.breadcrumb{margin-bottom:5px;min-height:27px;font-size:.875em;color:#555;border-bottom:1px solid #ddd}.breadcrumb div{display:inline-block;margin:2px}.bigtime{color:#000;background-color:#fafafa;text-align:center;padding:15px 10px;font-weight:300;margin:16px auto;max-width:320px}.bigtime small{color:#555;font-size:70%}table{width:100%;border-collapse:collapse;border-spacing:0;border:1px solid #ddd;margin-bottom:1em;background-color:#fff}td,th{padding:5px;text-align:left}th{background-color:#e2f0c6}tr{border-bottom:1px solid #ddd}tr:nth-child(odd){background-color:#f7f7f7}h1,h2,h3,h4,h5,h6{font-weight:400;line-height:1.2;margin:.5em 0}h1{font-size:1.6em}h2{font-size:1.5em}h3{font-size:1.4em}h4{font-size:1.3em}h5{font-size:1.2em}p{margin:5px 0 10px 0}ul{padding-left:20px;list-style-type:square}ul li{line-height:1.7em}header ul{padding-left:5px}main,header .row{max-width:1000px;margin:0 auto}main{padding:10px;min-height:320px;background-color:#fff;word-wrap:break-word}footer{font-size:90%;padding:20px 0;text-align:center}footer ul{padding-left:0}.full-width{width:100%}.center{text-align:center}.noblock{display:inline-block}.border{border:1px solid #ddd}.fontsmall{font-size:80%}.fontbig{font-size:250%;line-height:1em;letter-spacing:3px;display:block;margin-bottom:5px}.rounded{border-radius:4px}.bgrs{background-color:#f8f8f8}.divw{background-color:#f8f8f8;padding:10px;margin-bottom:21px}.bgwhite{background-color:#fff;color:green;padding:3px 8px;border:1px solid #ddd}.shadow,table,.comcon,#frmcomment,main{box-shadow:0 1px 4px 0 rgba(0,0,0,.3)}.shadow2{box-shadow:0 0 10px rgba(0,0,0,0.4)}.social{margin:5px 0}.share-on-twitter{background:#0b6e8a}.share-on-facebook{background:#3b5997}.share-on-googleplus{background:#d64937}.social a:hover{color:#fff;text-shadow:none}.social .button{font-size:90%;margin:10px 5px;padding:10px 15px}.social .scount{border:1px solid #aaa;margin-right:4px;padding:7px 4px;font-size:85%;background-color:#f5f5f5;color:#555}@media(min-width:800px){html{font-size:110%}main{padding:1px 10px}form,label{margin:5px 0}.form,button,.button{padding:.5em 1em}.col-6{width:50%;padding:5px}.col-2{width:16.66%}.col-3{width:25%}.col-4{width:33.33%}.col-8{width:66.66%}.col-9{width:75%}.col-10{width:83.33%}}@media (max-width:970px){header .col-3{text-align:center;width:100%}header,nav{position:fixed;left:0}nav .list-vertical{padding-left:5px;padding-top:10px}header [class*=col-]{padding:5px 0}.m2{width:50%}header{top:0;right:0}main{margin-top:52px;-webkit-transition:-webkit-transform .3s ease-out;transition:transform .3s ease-out}header img,header svg{margin:0 auto}.toggle{display:block;z-index:1;position:fixed}nav.open{-webkit-transform:translate(0,0);transform:translate(0,0)}body.open{overflow:hidden}main.open{transform:translate(255px,0)}nav{width:250px;height:100%;top:51px;bottom:0;background-color:#555;-webkit-transform:translate(-250px,0);transform:translate(-250px,0);-webkit-transition:-webkit-transform .3s ease-out;transition:transform .3s ease-out}nav .list-vertical li a{color:#fff}}@media(max-width:480px){html{font-size:100%}table{width:100%;font-size:100%;line-height:1.6em}td{padding:2px}.form{padding:5px 7px}.bigtime{padding:10px;margin:5px auto}.comments,.comcon{padding:5px}h1,h2,h3,h4,h5,h6{line-height:1.4;margin:10px 0 5px 0}h1{font-size:1.3em}h2{font-size:1.25em}h3,h4,h5{font-size:1.2em}main{padding:5px}}@media print{body{color:#111}}.clock{border-radius:50%;background:url(/clock.svg) center no-repeat #fafafa;background-size:88%;height:12em;margin:5px auto;position:relative;width:12em}.clock.simple:after{background:#000;border-radius:50%;content:"";position:absolute;left:50%;top:50%;transform:translate(-50%,-50%);width:5%;height:5%;z-index:10}.hours,.minutes{transform-origin:50% 100%;background:#000;position:absolute}.hours-container,.minutes-container,.seconds-container{position:absolute;top:0;right:0;bottom:0;left:0}.hours{height:20%;left:48.75%;top:30%;width:2.5%}.minutes{height:40%;left:49%;top:10%;width:2%}.seconds{background:#000;height:45%;left:49.5%;position:absolute;top:14%;transform-origin:50% 80%;width:1%;z-index:8}@keyframes rotate{100%{transform:rotateZ(360deg)}}.hours-container{animation:rotate 43200s infinite linear}.minutes-container{transition:transform .3s cubic-bezier(.4,2.08,.55,.44)}.seconds-container{transition:transform .2s cubic-bezier(.4,2.08,.55,.44)}</style> <link rel="shortcut icon" type="image/png" href="/favicon32.png" /> <link rel="icon" sizes="192x192" href="https://www.utctime.net/icon.png" /> <link rel="apple-touch-icon" href="https://www.utctime.net/apple-touch-icon-152x152.png" sizes="152x152" /> <!-- OneTrust Cookies Consent Notice start for www.utctime.net --> <script src="https://cdn.cookielaw.org/scripttemplates/otSDKStub.js" charset="UTF-8" data-domain-script="0c3397d1-0cb9-4397-ac86-32a31eb85490" async></script> <script> function OptanonWrapper() { } </script> <!-- OneTrust Cookies Consent Notice end for www.utctime.net --> <script async src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script> <script> window.googletag = window.googletag || {cmd: []}; googletag.cmd.push(function() { var slot = googletag.defineOutOfPageSlot( '/22247219933/UTC_Interstitial', googletag.enums.OutOfPageFormat.INTERSTITIAL); if (slot) slot.addService(googletag.pubads()); googletag.enableServices(); googletag.display(slot); }); </script> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-159581532-16"></script> <script src="https://cdn.adapex.io/hb/aaw.utc.js" async></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-159581532-16'); </script> <style> main li a { padding: 12px 5px; line-height: 36px; } footer li a { padding: 6px; line-height: 36px; } .stickyAd { position: fixed; left:5px; top: 55px; } .stickyAdMobile { position: sticky; bottom: 0; left:0; right:0; } @media screen and (max-width: 1000px) { .stickyAd { display: none !important; } } @media screen and (min-width: 1000px) and (max-width:1330px) { main { margin: 0 0 10px 162px; } } </style><title>UTC Timestamp Time in Seconds</title> <meta name="description" content="UTC timestamp calculates the total number of seconds from the starting time of UTC to current time in seconds. List of the current UTC timestamp in various date and time formats." /> </head> <body> <header class="shadow2"> <div class="row"> <div class="col-3 center"> <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0" y="0" width="140" height="32" viewBox="0, 0, 112, 24"> <g id="Layer_1" transform="translate(-245, -139.5)"> <text transform="matrix(1, 0, 0, 1, 292.25, 151.5)"> <tspan x="-47.25" y="7" font-family="monospace" font-size="20" fill="#0066cc">UTC Time</tspan> </text> </g> </svg> <span id="utctime">18:06:53</span> </div> <div class="col-9"> <span class="toggle" id="toggle"></span> <nav id="menu"> <ul id="navmenu" class="list-horizontal"> <li>UTC Time Converter</li> <li>UTC Time Offsets</li> <li>Time Zone Abbreviations</li> <li>UTC Timestamp</li> </ul> </nav> </div> </div> </header><main class="content"> <div class="center" style="min-height:100px"> <div data-aaad='true' data-aa-adunit='/22247219933/UTC_728v_1'></div> <div data-aaad='true' data-aa-adunit='/22247219933/UTC_Mob_300v_1'></div> </div> <h1 class="center">UTC Timestamp Time in Seconds</h1> <div class="bigtime shadow rounded bgwgr"><span id="time" class="fontbig">1659118013</span></div> <p>Current UTC timestamp to time is <span id="time2">18:06:53</span>.</p> <p>This is the page for those who want to learn current UTC timestamp. It can be useful information for anyone who needs to spot the <em>current moment of time</em>. It is valid for any UTC time zone and can be used in a few kinds of UTC time formats.</p> <div class="social"> Facebook Twitter </div><h3>UTC Timestamp in Various Formats</h3> <table> <tr><th>Format</th><th>UTC Time</th></tr> <tr><td>ISO-8601</td><td>2022-07-29T18:06:53+00:00</td></tr> <tr><td>RFC 1123</td><td>Fri, 29 Jul 2022 18:06:53 GMT</td></tr> <tr><td>UTC</td><td>2022-07-29T18:06:53Z</td></tr> </table> <div class="margin10 center"> <div data-aaad='true' data-aa-adunit='/22247219933/UTC_728v_2'></div> <div data-aaad='true' data-aa-adunit='/22247219933/UTC_Mob_300v_2'></div> </div> <h4>Helpful Resources</h4> <ul> <li>Unix time (Wikipedia)</li> <li>NTP Timestamp Calculations (University of Delaware)</li> </ul> <div class="margin10 center"> <div data-aaad='true' data-aa-adunit='/22247219933/UTC_728v_3'></div> <div data-aaad='true' data-aa-adunit='/22247219933/UTC_Mob_300v_3'></div> </div> </main> <footer> <p>&copy; 2016-2022 www.UtcTime.net</p> <ul class="list-horizontal"> <li>Privacy Policy and Cookies</li> <li>About Us</li> <li>Contact Us</li> </ul> </footer> <div class="stickyAd"> <div data-aaad='true' data-aa-adunit='/22247219933/UTC_160v_Sticky'></div> </div> <div class="stickyAdMobile" style="text-align:center;background-color:#fff"> <div data-aaad='true' data-aa-adunit='/22247219933/UTC_Mob_300v_sticky'></div> </div> <script> var tf='24h'; var utcsec = 65213; setInterval(function () { utcsec+=1; document.getElementById('utctime').innerHTML= gettimestr(utcsec,tf); }, 1000); function pad(val) { return val > 9 ? val : "0" + val; } function gettimestr(totalsec, tfr) { var hours = parseInt(totalsec / 3600) % 24; var minutes = parseInt(totalsec / 60) % 60; var seconds = totalsec % 60; if(tfr === '24h') { return pad(hours) + ":" + pad(minutes) + ":" + pad(seconds); } else { var ampm = hours >= 12 ? 'PM' : 'AM'; hours = hours % 12; hours = hours ? hours : 12; return hours + ':' + pad(minutes)+ ':' + pad(seconds) +' <small>' + ampm+'</small>'; } } function setTf(tfr) { tf = tfr; } !function(e,t){function n(){t.getElementById("navmenu").classList.toggle("list-vertical"),t.getElementsByTagName("header")[0].classList.toggle("open"),t.getElementsByTagName("body")[0].classList.toggle("open")}function s(){o.classList.contains("open")?setTimeout(n,500):n(),o.classList.toggle("open"),t.getElementsByTagName("main")[0].classList.toggle("open")}function i(){o.classList.contains("open")&&s()}var o=t.getElementById("menu"),a="onorientationchange"in e?"orientationchange":"resize";t.getElementById("toggle").addEventListener("click",function(){s()}),e.addEventListener(a,i)}(this,this.document); </script> <script> var sec = 65213; var timestamp = 1659118013; function pad(val) { return val > 9 ? val : "0" + val; } setInterval(function () { timestamp++; var timestr = pad(parseInt(sec / 3600, 10)) + ":"; timestr += pad(parseInt(sec / 60, 10) % 60) + ":"; timestr += pad(++sec % 60); document.getElementById('time').innerHTML = timestamp; document.getElementById('time2').innerHTML = timestr; }, 1000); </script> </body> </html>'
  4 件のコメント
Jan
Jan 2022 年 7 月 29 日
編集済み: Jan 2022 年 7 月 29 日
@Shuai Xu: If the server does not run anymore, ask Google for another server e.g. using the keywords "public ntp server". I've inserted another one above and it runs.
Come on. This is not magic.
data = urlread('https://www.utctime.net/');
key = '<td>ATOM</td><td>';
index = strfind(data, key);
utcTime = data(index + numel(key) + (0:18))
utcTime = '2022-07-29T18:22:10'
For http://www.utctime.net/utc-timestamp search e.g. for the key '<tr><td>UTC</td><td>'
Shuai Xu
Shuai Xu 2022 年 8 月 1 日
Yes, it runs successfully on my pc now. Thank you, Jan!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWeb Services についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by