How can I get date and time from Nist server?

I want to get the actual date and time from Nist.gov servers in MatLab using tcp connection to their server "time.nist.gov".
I've already tried another method using url https://nist.time.gov/actualtime.cgi?lzbc=siqm9b but it doesn't work anymore as it says "Use of this script is explicitly forbidden".
I coded this, but the return variable is a "uint8" 1x51 array and I cannot get what I need:
clc;
Nist_Time = tcpclient('time.nist.gov',13);
pause(0.5);
tcpdata = read(Nist_Time);
disp(tcpdata)
I expect to get a string with the 'tcpclient' formula where I can convert it into "YYYY-MM-DD HH:MM:SS" format

 Akzeptierte Antwort

J Chen
J Chen am 23 Aug. 2019
Bearbeitet: J Chen am 23 Aug. 2019

2 Stimmen

str = char(tcpdata)
will convert the ascii code to a string. str(8:15) will give you the date. str(17:24) will give you the time.

3 Kommentare

Thanks, it's working! ;)
I made a function to convert to serial date! :)
function [outdate] = GetNist2019()
Nist_Time = tcpclient('time.nist.gov',13);
pause(0.5);
tcpdata = read(Nist_Time);
str = char(tcpdata);
str = str(8:15);
outdate = datenum(str,'yy-mm-dd');
clear str tcpdata Nist_Time;
end
Benson Gou
Benson Gou am 13 Jul. 2020
Thanks.
Benson
If I run
tcpdata = read(Nist_Time)
two times, only for the first time I receive data, does anyone know why that is? I always have to run
Nist_Time = tcpclient('time.nist.gov',13);
again. I assume the connection get lost after the first read, but how can I keep the connection open?
Thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2018a

Gefragt:

am 23 Aug. 2019

Kommentiert:

am 4 Apr. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by