Get time/date online

23 ビュー (過去 30 日間)
Vincent
Vincent 2016 年 12 月 5 日
編集済み: Jan 2022 年 7 月 29 日
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

採用された回答

Jan
Jan 2016 年 12 月 5 日
編集済み: Jan 2022 年 7 月 29 日
I'd take one of the many C-codes from the net and write a small C-Mex-wrapper.
  3 件のコメント
Jan
Jan 2016 年 12 月 9 日
Your solution does not depend on the platform. The C-code for Windows and Linux network stacks differ often.
Chiqueti
Chiqueti 2018 年 10 月 1 日
For people trying to do it with matlab 2016a. Change "urlread" to "webread"

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

その他の回答 (2 件)

Rik
Rik 2019 年 5 月 14 日
編集済み: Rik 2020 年 7 月 28 日
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 件のコメント
mohammad ibrahim
mohammad ibrahim 2020 年 7 月 28 日
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 2020 年 7 月 28 日
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.

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


Peter Perkins
Peter Perkins 2019 年 5 月 14 日
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 件のコメント
Ali Afruzi
Ali Afruzi 2022 年 4 月 12 日
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 2022 年 4 月 12 日
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:

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

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by