Dear All,
I am wondering if it is pissible to get the UTC time in my program. Do I need the internet avaibale if I want to get UTC time?
Thanks a lot.
Benson

 Akzeptierte Antwort

Star Strider
Star Strider am 12 Jul. 2020

1 Stimme

Try this:
dtLCL = datetime('now', 'TimeZone','local') % Current Local Time
dtUTC = datetime(dtLCL, 'TimeZone','Z') % Current UTC Time
That will give you both the local time and the corresponding UTC time.
.

5 Kommentare

Benson Gou
Benson Gou am 13 Jul. 2020
Hi, Star,
Thanks for your answer.
My computer time is Not accurate, but I want to obtain the accurate UTC time. How can I do it?
Thanks.
Bensen
Star Strider
Star Strider am 13 Jul. 2020
My pleasure.
I have not done this for many years (likely since the early 1990s). However NIST Internet time service , NIST Internet time servers, and NIST TIme Widget are three options.
Back in the 1980s and early 1990s, I had my computer (MS-DOS, early Windows) access those and set my computer clock with them every time I booted it. I wrote my own FORTRAN code to do that (long before I was introduced to MATLAB.) You can probably use the ftp or webread functions (neither of which I have used in the past few years) to get the time. Setting the time may be more of a problem, since I never had to do that in Windows 10 or recent earlier Windows versions. That might require an operating system call.
That is the best I can do.
Benson Gou
Benson Gou am 13 Jul. 2020
Hi, Star,
Thanks a lot for your great help.
Benson
Star Strider
Star Strider am 13 Jul. 2020
As always, my pleasure!
This gives you a simple network time, which does not rely on local computer time:
url_time = webread('http://worldtimeapi.org/api/timezone/Etc/UTC')

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Vini Lima
Vini Lima am 25 Mär. 2022

2 Stimmen

utc_time = datetime('now', 'timezone', 'utc')

1 Kommentar

This just converts your local computer time to UTC. So if your local computer time is incorrect for any reason, the utc_time will be also incorrect.

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by