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!