versatile date conversion
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
Oleg Komarov
on 26 Jul 2011
% 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 Comments
Oleg Komarov
on 26 Jul 2011
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?
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!