Get time/date online

23 views (last 30 days)
Vincent
Vincent on 5 Dec 2016
Edited: Jan on 29 Jul 2022
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
I'd take one of the many C-codes from the net and write a small C-Mex-wrapper.
  3 Comments
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.
Chiqueti
Chiqueti on 1 Oct 2018
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
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
mohammad ibrahim
mohammad ibrahim on 28 Jul 2020
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.


Peter Perkins
Peter Perkins on 14 May 2019
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
Ali Afruzi
Ali Afruzi on 12 Apr 2022
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);
Peter Perkins
Peter Perkins on 12 Apr 2022
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 Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!