LoginSignup
3
2

More than 5 years have passed since last update.

Octave/Matlab で Date文字列をUnix timestampに変換する

Last updated at Posted at 2015-11-09

octaveで日付を扱いたい時に、浮動小数点形式のmatlab timestamp(?)がやっていることの都合で不便だったのでUnix timestampに変換する関数を作った.
調べてもすぐには見つけられなかったので、ここにメモしておく.

function timestamps = conv_datestr_timestamp(date_strs)
    if ischar(date_strs) == 1
        date_strs = cellstr(date_strs);
    end

    date_strs  = date_strs(:);
    sz         = size(date_strs, 1);
    timestamps = zeros(sz, 1);

    for n = 1:sz
        timestamps(n) = str2num(['int32(' strftime("%s", strptime(date_strs{n}, '%Y/%m/%d %T')) ')']);
    end
end


% Test code
date_strs = {"2015/10/31 14:46:59"; "2015/1/1 14:46:59"; "2018/1/1 14:46:59"};
expects = [1446270419; 1420091219; 1514785619];
assert(conv_datestr_timestamp(date_strs), expects);

assert(conv_datestr_timestamp("2015/10/31 14:46:59"), 1446270419);

書いたあとに気づいたがmktime関数でも似たようなことができるっぽい?

参照リンク

In GNU Octave, get the date and time

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2