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

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

问题描述

我有一些带有Unix时间戳的数据文件(在这种情况下,是自1970年1月1日00:00 UTC以来的毫秒数).我想在Matlab中将其转换为对人类友好的日期/时间字符串(例如2012年8月31日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天全站免登陆