Hi all,
I would like to get the date from a web service using MATLAB (not the system date). I'm using MATLAB R2014b.
I've found this topic which addresses the same issue and might give a solution: https://nl.mathworks.com/matlabcentral/answers/263511-get-today-s-date-online-via-matlab-command Unfortunately, I cannot implement the NTP protocol in MATLAB. Could somebody give me a hint?
Thanks in advance, Vincent

 Accepted Answer

Jan
Jan on 5 Dec 2016
Edited: Jan on 29 Jul 2022

0 votes

I'd take one of the many C-codes from the net and write a small C-Mex-wrapper.

3 Comments

Ok, thanks for the suggestion!
Btw, for those looking for a fast (but possibly not robust and certainly not the best) solution:
URL = 'http://tycho.usno.navy.mil/cgi-bin/timer.pl';
temp_URL_read = urlread(URL);
temp_regexp = regexp(temp_URL_read, '<BR>(.*)\sUTC','tokens','once');
atomTime = datenum(temp_regexp,'mmm. dd, HH:MM:SS');
or:
temp_info = urlread('http://nist.time.gov/actualtime.cgi?lzbc=siqm9b');
ind_hashtag = strfind(temp_info, '"');
number_milliseconds = str2num(temp_info(ind_hashtag(1)+1:ind_hashtag(2)-1));
ctime = number_milliseconds/1000000;
atomTime = datenum(1970,1,1) + ctime/86400;
Taking one of the many C-codes on the net and writing a wrapper gives a more robust solution.
Jan
Jan on 9 Dec 2016
Your solution does not depend on the platform. The C-code for Windows and Linux network stacks differ often.
For people trying to do it with matlab 2016a. Change "urlread" to "webread"

Sign in to comment.

More Answers (2)

Rik
Rik on 14 May 2019
Edited: Rik on 28 Jul 2020

0 votes

Slightly expanding the above solution by Vincent, the attached code should work on all Matlab releases, as well as GNU Octave. It should stay working as long as the NIST keeps their servers up. The issue not described by Chiqueti in the comment above can be solved by explicitly using the https version.
Edit: as the NIST is now blocking API access, this method stopped working. In the updated attachment there is a method that relies on C. It will automatically attempt to build the mex file.
Edit 2: I removed the attachment, because I made it available on the FEX. That way I only need to keep one version updated.

4 Comments

fsgeek
fsgeek on 14 May 2020
Edited: fsgeek on 14 May 2020
Sorry to revive an old thread, but this is exactly what I'm looking for. Unfortunately, this script does not work because the URL no longer seems to be valid. Does anybody know of a current URL from NIST which provides the same information?
Rik
Rik on 14 May 2020
When it started failing I discovered that NIST had decided to block API access. I'm preparing an update to my Wayback Machine API function that will contain it. I'll update the attachment with the function I have now.
hello Rik
thank you for your file. actually this is what I need but when I run your script I have the following error
Error using mex
No supported compiler was found. You can install the freely available MinGW-w64 C/C++ compiler; see Install MinGW-w64 Compiler.
is there any solution without the necessity to use mex files ?
many thanks
Rik
Rik on 28 Jul 2020
What is your issue with installing the compiler? It is free for your version of Matlab, so you can get it through the add-on manager, or on the file exchange.
There is an option without mex (that update is implemented here in my FEX submission), but there is no telling when that will stop working. If you add this function to Matlab through the add-on manager, you should get a notification when I post an updated version.

Sign in to comment.

I've taken the liberty of updating Vincent's soln's with soemthing more current (and in the second case, more straight-forward and accurate): datetime.
>> s1 = webread('https://tycho.usno.navy.mil/cgi-bin/timer.pl');
>> s2 = regexp(s1, '<BR>(.*)\sUTC','tokens','once');
>> t = datetime(s2,'InputFormat','MMM. dd, HH:mm:ss')
t =
datetime
14-May-2019 18:44:26
>> s1 = webread('https://nist.time.gov/actualtime.cgi?lzbc=siqm9b');
>> s2 = regexp(s1,'\<timestamp time\=\"(\d*)\"','tokens','once');
>> n = str2double(s2);
>> t = datetime(n,'ConvertFrom','EpochTime','TicksPerSecond',1e6);
>> t.Format = 'dd-MMM-yyyy HH:mm:ss.SSSSSS'
t =
datetime
14-May-2019 18:44:29.203458

4 Comments

Marcel
Marcel on 26 Jan 2021
is there a URL that's still working to get the current time?
Rik
Rik on 26 Jan 2021
You can check out my getUTC function on the FEX.
You can use http://worldtimeapi.org/ that gives an api for the current local time for a given timezone.
For example:
url_time = 'http://worldtimeapi.org/api/timezone/Asia/Tehran';
url_read = webread(url_time);
Yes, that works. Here's how to turn that into a (zoned) datetime:
>> url_time = webread('http://worldtimeapi.org/api/timezone/Asia/Tehran')
url_time =
struct with fields:
abbreviation: '+0430'
client_ip: '144.212.3.4'
datetime: '2022-04-12T23:44:22.975380+04:30'
day_of_week: 2
day_of_year: 102
dst: 1
dst_from: '2022-03-21T20:30:00+00:00'
dst_offset: 3600
dst_until: '2022-09-21T19:30:00+00:00'
raw_offset: 12600
timezone: 'Asia/Tehran'
unixtime: 1.6498e+09
utc_datetime: '2022-04-12T19:14:22.975380+00:00'
utc_offset: '+04:30'
week_number: 15
>> t = datetime(url_time.datetime,"Format","uuuu-MM-dd'T'HH:mm:ss.SSSSSSxxx","TimeZone","Asia/Tehran")
t =
datetime
2022-04-12T23:41:13.023675+04:30
At the risk of sounding like a broken record, datenum is not the right thing to use for dates and times:

Sign in to comment.

Categories

Find more on MATLAB Compiler SDK in Help Center and File Exchange

Asked:

on 5 Dec 2016

Edited:

Jan
on 29 Jul 2022

Community Treasure Hunt

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

Start Hunting!