converte UNIX time to Human readable format.

Hello,
Work is almost done, but there came one more problem: how can I converte the UNIX time to human readable format, like ISO086..
My UNIX time is in int64 format, for example: 1352434077. So when I tried to converte it with this code:
unix_epoch = datenum(1970,1,1,0,0,0);
for i=1:1:size(data_mat)
matlab_time(i,1) = data_mat(i,1)./86400 + unix_epoch;
end
It gave 735087..not something like YYYY-MM-DD: hh:mm:ss ...can anyone gave me a hint? Thanks a lot!

 Respuesta aceptada

Bjorn Gustavsson
Bjorn Gustavsson el 19 de Dic. de 2014
Take a look at datestr (or some of the other functions in the date-family):
datestr(matlab_time,'yyyymmddTHH:MM:SS')
or whatever format you might choose.
HTH

8 comentarios

Thanks for answering...In fact, I tried this function many times..
datestr(1352434077/86400+datenum('1/1/1970'))
09-Nov-2012 04:07:57
So when I input only one number, it works fine. But my time series are stored in 13892780 * 2 int64 format..so when I tried this function in for loop, it always gave errors:
Error using dateformverify (line 28)
DATESTR failed converting date number to date vector.
Error in datestr (line 195)
S = dateformverify(dtnumber, dateformstr, islocal);
Caused by:
Error using datevecmx
The datevecmx function only accepts double arrays.
That is why I am thinking is it related to int64 format..
very interesting..
datestr(1352435088/86400 + datenum('1/1/1970'))
ans =
09-Nov-2012 04:24:48
if only one number. works fine. but with a mat file does not work
Well this seems to work just fine:
datestr([1;1;1]*matlab_time,'yyyymmddTHH:MM:SS')
...so you'll have to cast the time-array to double as the error message indicates...
HTH
Yes, Bjorn...I also tried this way..This is my data:
Then I do
xx=typecast(Data(:,1),'double');
Then the result is like this:
How can it be like this...I need to converte them to date time...please check it for me...I have spend quite few days already....thanks
Hi,
I think you miss the most obvious way:
>> time64 = int64(now); % Not the best way to handle a time...
>> datestr([1;1;1]*double(time64),'yyyymmddTHH:MM:SS')
ans =
20141222T00:00:00
20141222T00:00:00
20141222T00:00:00
Should be fine if you convert your dates into doubles - if not then you might have something faulty either in the date format or in the actual date-numbers?
If the above doesn't work check your implementation with some dates that you set yourself, both the conversion to doubles and then to date-strings, and saveing to the date-format your trying to read...
HTH
Thanks Bjorn,
I tried with
x=datestr([1]*double(Data(1:10,1)./86400+datenum(1970,1,1,0,0,0)),'yyyymmddTHH:MM:SS');
The result is
20121109T00:00:00
20121109T00:00:00
20121109T00:00:00
20121109T00:00:00
20121109T00:00:00
20121109T00:00:00
20121109T00:00:00
20121109T00:00:00
20121109T00:00:00
20121109T00:00:00
Why the hour, minute and second are all 0? they should be different...
Bjorn Gustavsson
Bjorn Gustavsson el 23 de Dic. de 2014
I guess you should do the typecasting to double ASAP, that is on the Data(1:10,1) before dividing by 86400 - as it is now that division will give you an int - and that corresponds to midnight since the time of day is in fractions of a day...
HTH
buer
buer el 30 de Dic. de 2014
Thanks Bjorn....it is really about the typecasting..thanks a lot

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 19 de Dic. de 2014

Comentada:

el 30 de Dic. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by