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!