changeset 9909:f769e77b4dd3 octave-forge

- If the datefmt contains spaces, then concatenate more than one field.
author cdemills
date Thu, 29 Mar 2012 15:09:25 +0000
parents 56ae3eaf8fde
children 26c9dfb4a501
files extra/dataframe/inst/@dataframe/dataframe.m
diffstat 1 files changed, 37 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/extra/dataframe/inst/@dataframe/dataframe.m	Thu Mar 29 15:08:03 2012 +0000
+++ b/extra/dataframe/inst/@dataframe/dataframe.m	Thu Mar 29 15:09:25 2012 +0000
@@ -159,6 +159,14 @@
   endwhile
 endif
 
+if (~isempty (datefmt))
+  %# replace consecutive spaces by one
+  datefmt =  regexprep (datefmt, '[ ]+', ' ');
+  datefields = 1 + length (regexp (datefmt, ' '));
+else
+  datefields = 1;
+endif
+
 if (~isempty (seeked) && ~isempty (trigger))
   error ('seeked and trigger are mutuallly incompatible arguments');
 endif
@@ -283,45 +291,62 @@
                                   'UniformOutput', false);
             endif
 
-            for indk = (1:size (the_line, 2))
+            indk = 1; indm = 1;
+            while (indk <= size (the_line, 2))
               if (isempty (the_line{indk}) || any (size (the_line{indk}) > 1)) 
                 %#if indi > 1 && indk > 1, disp('line 117 '); keyboard; %#endif
                 if (unquot)
                   try
                     %# remove quotes and leading space(s)
-                    x(indj, indk) = regexp (dummy{indk}, '[^'' ].*[^'']', 'match'){1};
+                    x(indj, indm) = regexp (dummy{indk}, '[^'' ].*[^'']', 'match'){1};
                   catch
                     %# if the previous test fails, try a simpler one
                     in = regexp (dummy{indk}, '[^'' ]+', 'match');
                     if (~isempty (in))
-                      x(indj, indk) = in{1};
+                      x(indj, indm) = in{1};
                       %# else
                       %#    x(indj, indk) = [];
                     endif
                   end_try_catch
                 else
                   %# no conversion possible, store and remove leading space(s)
-                  x(indj, indk) = regexp (dummy{indk}, '[^ ].*', 'match');
+                  x(indj, indm) = regexp (dummy{indk}, '[^ ].*', 'match');
                 endif
               elseif (~isempty (regexp (dummy{indk}, '[/:-]')) && ...
                       ~isempty (datefmt))
+                %# does it look like a date ?
+                datetime = dummy{indk}; 
+                
+                if (datefields > 1)             
+                  %# concatenate the required number of fields 
+                  indc = 1;
+                  for indc = (2:datefields)
+                    datetime = cstrcat(datetime, ' ', dummy{indk+indc-1});
+                  endfor
+                endif
+                
                 try
-                  datetime = datevec (dummy{indk}, datefmt);
+                  datetime = datevec (datetime, datefmt);
                   timeval = struct ("usec", 0, "sec", floor (datetime (6)),
                                     "min", datetime(5), "hour", datetime(4),
                                     "mday", datetime(3), "mon", datetime(2)-1,
                                     "year", datetime(1)-1900);
-                  timeval.usec = 1e6*(datetime(6)-timeval.sec);
-                  x(indj, indk) =  str2num (strftime ([char(37) 's'], timeval)) + ...
+                  timeval.usec = 1e6*(datetime(6) - timeval.sec);
+                  x(indj, indm) =  str2num (strftime ([char(37) 's'], timeval)) + ...
                       timeval.usec * 1e-6;
+                  if (datefields > 1)
+                    %# skip fields successfully converted
+                    indk = indk + (datefields - 1);
+                  endif
                 catch
                   %# store it as is
-                  x(indj, indk) = the_line{indk}; 
+                  x(indj, indm) = the_line{indk}; 
                 end_try_catch
               else
-                x(indj, indk) = the_line{indk}; 
+                x(indj, indm) = the_line{indk}; 
               endif
-            endfor
+              indk = indk + 1; indm = indm + 1;
+            endwhile
             indl = indl + 1; indj = indj + 1;
           endwhile
           
@@ -336,12 +361,12 @@
           endif
           
           clear UTF8_BOM fid in lines indl the_line content empty_lines
-          clear timeval timestr nfields idx
+          clear datetime timeval idx
         
         endif
       end_try_catch
     endif
-  
+
     %# fallback, avoiding a recursive call
     idx.type = '()';
     if (~isa (x, 'char'))