versatile date conversion
1 回表示 (過去 30 日間)
古いコメントを表示
Hello,
I'm having some trouble dealing with string dates, which comply with XML specs in RSS feeds I've downloaded but don't agree with MATLAB's datenum function. Some examples of the formats are:
4 Jun 2010 17:42:23 PDT
23 Mar 2009 03:21 GMT
2011-07-25T16:00:12-08:00
the built in matlab date-functions deal fine with the first two, but not with the last. Are there any updated or third-party matlab functions that automatically deal with a wider variety of date formats? All of my inputted data is downloaded from the web and follow well defined formats (usually parsed from XML).
Thanks, Brian
edit: I'd like a function that adjusts for timezone differences too
0 件のコメント
回答 (1 件)
Oleg Komarov
2011 年 7 月 26 日
% Suppose you have a cellstring with dates in your third format:
s = {'2011-07-25T16:00:12-08:00'
'2011-07-25T16:00:18+07:00'};
% Separate the date from the timezone catching the + or - sign
[s,op] = regexp(s,'(?<=\d{2}:\d{2})-|+','split','match');
s = cat(1,s{:});
% Index the minus sign
idx = strcmp('-',cat(1,op{:}));
% To keep only the fraction of the day we offset by today date
t = fix(now);
% Dates with time zone conversion
out1 = datenum(s( idx,1),'yyyy-mm-ddTHH:MM:SS') - datenum(s( idx,2),'HH:MM') + t;
out2 = datenum(s(~idx,1),'yyyy-mm-ddTHH:MM:SS') + datenum(s(~idx,2),'HH:MM') - t;
3 件のコメント
Oleg Komarov
2011 年 7 月 26 日
You can write a routine tries to call datenum and if fails reverts to a parsing mechanism to identify possible cases as I did with regexp and then calls datenum.
How many cases are you going to have roughly?
参考
カテゴリ
Help Center および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!