在Matlab中将纪元转换为日期 [英] Converting Epoch to Date in Matlab

查看:96
本文介绍了在Matlab中将纪元转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Matlab中有一个历元数组(数字数组).我想将它们转换为UTC日期时间格式,例如DD-MM-YYYY HH:MM.

I have an array of Epoch milliseconds (array of numbers) in Matlab. I would like to convert these into UTC date-time format, such as DD-MM-YYYY HH:MM.

是否有预定义的Matlab方法可以做到这一点?还是我必须编写自己的函数?

Is there a pre-defined Matlab way to do this or will I have to write my own function?

推荐答案

假设您从向量time_unix开始,然后:

Suppose, you start with a vector time_unix, then:

>> time_unix = 1339116554872; % example time
>> time_reference = datenum('1970', 'yyyy'); 
>> time_matlab = time_reference + time_unix / 8.64e7;
>> time_matlab_string = datestr(time_matlab, 'yyyymmdd HH:MM:SS.FFF')

    time_matlab_string =

    20120608 00:49:14.872

注意:

1)参见matlab时间的定义.

1) See the definition of matlab's time.

2)8.64e7是一天中的毫秒数.

2) 8.64e7 is number of milliseconds in a day.

3)Matlab不应用任何时区偏移,因此结果是相同的UTC时间.

3) Matlab does not apply any time-zone shifts, so the result is the same UTC time.

4)向后转换示例:

>> matlab_time = now;
>> unix_time = round(8.64e7 * (matlab_time - datenum('1970', 'yyyy')))

unix_time =

             1339118367664

总结一下,这是两个功能:

To summarize, here are two functions:

function tm = unix2matlab(tu)
    tm = datenum('1970', 'yyyy') + tu / 864e5;
end
function tu = matlab2unix(tm)
    tu = round(864e5 * (tm - datenum('1970', 'yyyy')));
end

此处的Matlab时间为数字.您始终可以使用datestr()将其转换为字符串

The matlab time here is numeric. You can always convert it to string using datestr()

更新纳秒

time_unix_nanos = 1339116554872666666;
millis = round(time_unix_nanos / 1e6);
nanos = time_unix_nanos - 1e6 * millis;
time_matlab = unix2matlab(millis);
s = [datestr(time_matlab, 'yyyymmdd HH:MM:SS.FFF'), num2str(nanos)];

        s =
        20120608 00:49:14.872666666

这篇关于在Matlab中将纪元转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆