File Exchange

image thumbnail

Get UTC Time, Convert a UTC time to Datenum (supports vectors, Win/Linux)

version (6.36 KB) by Abraham Cohn
Get a UTC time using standard C time function, convert UTC time to MATLAB serial date ...


Updated 28 Jun 2006

No License

UTC time (universal time convention) is used in ANSI-C standard for defining time. This is measured in seconds since 1970, at the Prime Meridian. By definition, UTC does not depend on Daylight Savings Time. The UTC_TIME function uses a C-MEX function to get the System Time in UTC format.

Another function UTC2DATENUM will convert any UTC time to the correct MATLAB serial date number in local time, accounting for Daylight Savings. This is done with the help of the C-function gmtime. Vectors are supported. The difference of the local time from UTC is also provided as an output.

Cite As

Abraham Cohn (2020). Get UTC Time, Convert a UTC time to Datenum (supports vectors, Win/Linux) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (9)

Jerry Wang

This segfaults on R2018b

Distributor ID: Ubuntu
Description: Ubuntu 18.04.1 LTS
Release: 18.04
Codename: bionic

Segmentation violation detected at Tue Sep 25 20:39:28 2018 -0400

Crash Decoding : Disabled - No sandbox or build area path
Crash Mode : continue (default)
Default Encoding : UTF-8
Deployed : false
Desktop Environment : GNOME
GNU C Library : 2.27 stable
Graphics Driver : Unknown hardware
Java Version : Java 1.8.0_152-b16 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
MATLAB Architecture : glnxa64
MATLAB Entitlement ID : 1550280
MATLAB Root : /usr/local/MATLAB/R2018b
MATLAB Version : (R2018b)
OpenGL : hardware
Operating System : Ubuntu 18.04.1 LTS
Process ID : 21676
Processor ID : x86 Family 6 Model 158 Stepping 10, GenuineIntel
Session Key : 4398f19c-e429-4400-a58f-68064ce17832
Static TLS mitigation : Enabled: Full
Window System : The X.Org Foundation (11906000), display :0

Fault Count: 1

Hello everybody
I would recommend using directly java.lang.System.currentTimeMillis
Example: utc_time = java.lang.System.currentTimeMillis;
(assuming you have JAVA installed)

One advantage is also, that it is in milli seconds as required.
Best wishes, Albert

Abraham Cohn

Hello, in response to Klaps Jos:
I searched the code for "datepart", and it is not part of the submission ... this must be part of your calling code. So to debug your error, please check the syntax of your function "datepart". Thanks, and good luck.

Klaps Jos


I downloaded the file didn't work, see comments of Matlab Command Window below..if u can help plz send mail to

Command Window:
??? Input argument "din" is undefined.

Error in ==> datepart at 43


ralph habib

i want to convert real time to GPs time..can u help?and i tried to download didn't work..if u can help plz send mail to

Richard Sonnenfeld

Worked through the issues of how to use this in Linux. If .mex these
C-functions. (In Linux do e.g.
mex -lm -o utc_time.mexglx utc_time.c) ,
then you don't need the DLL at all. Just use the two m-files and two c-files provided and it just works! Thank you Dr. Cohn!

Richard Sonnenfeld

Just what I was looking for, but the .dll is for Windows, not Linux. Ah well. This could
be coded native to Matlab -- It's just a pain in the neck. Was hoping someone else did it!

K Reese

Great idea. Clearly mathworks forgot about time zone conversions. However, your function does not handle arrays of dates, and it displays an unneeded message in the command window.

Bulent Bayraktar

Great, Abe! :)


By Request:
* Supports VECTORS
* No message to console (time zone diff is provided in 2nd output argument)
* Better help text for troubleshooting local time
* Includes compile script for Linux (mexCompile), courtesy of Nicholas Bercher

MATLAB Release Compatibility
Created with R2006a
Compatible with any release
Platform Compatibility
Windows macOS Linux