versatile date conversion

1 回表示 (過去 30 日間)
Brian
Brian 2011 年 7 月 26 日
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

回答 (1 件)

Oleg Komarov
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
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?
Brian
Brian 2011 年 7 月 27 日
The try/catch with datenum solution might be okay, I'll work on it. As for number of different format cases, I guess I'm not sure. I'm parsing RSS feeds, so anything that would be acceptable in the RSS2.0 spec I guess. I found one spec doc that I worked off of, then added a new RSS channel to parse, and it had a different format (not conforming to the expected <lastBuild> channel element, and instead using something like <dc:date> and the above example I posted.
For the most part, my code is compatible, but then I came across the other format for which you suggested a custom parsing. I guess I'm a bit surprised that matlab doesn't keep up with all the web or iso accepted formats. I'm not too experienced with reading ISO or other spec documents, or for that matter, even in general what web feeds have to conform with etc, so I'm probing for a generalized solution.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by