如何在 Matlab 中使用 Unix 时间戳? [英] How to work with Unix timestamps in Matlab?

查看:75
本文介绍了如何在 Matlab 中使用 Unix 时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些带有 Unix 时间戳的数据文件(在这种情况下,自 1970 年 1 月 1 日 00:00 UTC 以来的毫秒数).我想在 Matlab 中将这些转换为人类友好的日期/时间字符串(例如 31-Aug-2012 11:36:24).在 Matlab 中是否有一种简单的方法可以做到这一点,还是我最好使用外部库(例如 java.text.SimpleDateFormat)?

I have some data files with Unix timestamps (in this case, number of milliseconds since Jan 1, 1970 00:00 UTC). I would like to convert these to human-friendly date/time strings (e.g. 31-Aug-2012 11:36:24) in Matlab. Is there an easy way to do this in Matlab, or am I better off using an external library (e.g. java.text.SimpleDateFormat)?

推荐答案

怎么样

date = datestr(unix_time/86400 + datenum(1970,1,1))

如果 unix_time 以秒为单位给出,unix_time/86400 将给出自 1970 年 1 月 1 日以来的天数.加上 Matlab 的 使用的偏移量datenum (datenum(0000,1,1) == 1),并且您拥有自 0000 年 1 月 1 日以来的天数.这可以轻松转换为人类可读的形式通过 Matlab 的 datestr.

if unix_time is given in seconds, unix_time/86400 will give the number of days since Jan. 1st 1970. Add to that the offset used by Matlab's datenum (datenum(0000,1,1) == 1), and you have the amount of days since Jan. 1st, 0000. This can be easily converted to human-readable form by Matlab's datestr.

如果你有毫秒,就用

date = datestr(unix_time/86400/1000 + datenum(1970,1,1))

封装在函数中,这些将是

Wrapped in functions, these would be

function dn = unixtime_to_datenum( unix_time )
    dn = unix_time/86400 + 719529;         %# == datenum(1970,1,1)
end

function dn = unixtime_in_ms_to_datenum( unix_time_ms )
    dn = unix_time_ms/86400000 + 719529;   %# == datenum(1970,1,1)
end

datestr( unixtime_to_datenum( unix_time ) )

这篇关于如何在 Matlab 中使用 Unix 时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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