comparison scripts/time/datevec.m @ 18781:9eb72fa5f8b5

datevec.m: Return value with same shape as input (bug #42334). datevec.m: If input is not a row vector, record size of input and restore with reshape at end of function. Add input validation tests.
author Sergey Plotnikov <seryozha.plotnikov@gmail.com>
date Sun, 11 May 2014 17:14:10 -0700
parents d63878346099
children 38925538ec14
comparison
equal deleted inserted replaced
18780:56bff71de2ca 18781:9eb72fa5f8b5
110 endif 110 endif
111 111
112 if (isempty (p)) 112 if (isempty (p))
113 p = (localtime (time ())).year + 1900 - 50; 113 p = (localtime (time ())).year + 1900 - 50;
114 endif 114 endif
115
116 do_resize = false;
115 117
116 if (iscell (date)) 118 if (iscell (date))
117 119
118 nd = numel (date); 120 nd = numel (date);
119 121
144 endfor 146 endfor
145 endif 147 endif
146 148
147 else # datenum input 149 else # datenum input
148 150
151 if (! isrow (date))
152 date_sz = size (date);
153 do_resize = true;
154 endif
149 date = date(:); 155 date = date(:);
150 156
151 ## Move day 0 from midnight -0001-12-31 to midnight 0000-3-1 157 ## Move day 0 from midnight -0001-12-31 to midnight 0000-3-1
152 z = floor (date) - 60; 158 z = floor (date) - 60;
153 ## Calculate number of centuries; K1 = 0.25 is to avoid rounding problems. 159 ## Calculate number of centuries; K1 = 0.25 is to avoid rounding problems.
180 186
181 endif 187 endif
182 188
183 if (nargout <= 1) 189 if (nargout <= 1)
184 y = [y, m, d, h, mi, s]; 190 y = [y, m, d, h, mi, s];
191 elseif (do_resize)
192 y = reshape (y, date_sz);
193 m = reshape (m, date_sz);
194 d = reshape (d, date_sz);
195 h = reshape (h, date_sz);
196 mi = reshape (mi, date_sz);
197 s = reshape (s, date_sz);
185 endif 198 endif
186 199
187 endfunction 200 endfunction
188 201
189 function [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (f) 202 function [f, rY, ry, fy, fm, fd, fh, fmi, fs] = __date_vfmt2sfmt__ (f)
304 %!assert (datevec ("3:38:09 PM"), [yr,1,1,15,38,9]) 317 %!assert (datevec ("3:38:09 PM"), [yr,1,1,15,38,9])
305 %!assert (datevec ("15:38"), [yr,1,1,15,38,0]) 318 %!assert (datevec ("15:38"), [yr,1,1,15,38,0])
306 %!assert (datevec ("03:38 PM"), [yr,1,1,15,38,0]) 319 %!assert (datevec ("03:38 PM"), [yr,1,1,15,38,0])
307 %!assert (datevec ("03/13/1962"), [1962,3,13,0,0,0]) 320 %!assert (datevec ("03/13/1962"), [1962,3,13,0,0,0])
308 321
309 %% Test millisecond format FFF 322 ## Test millisecond format FFF
310 %!assert (datevec ("15:38:21.25", "HH:MM:SS.FFF"), [yr,1,1,15,38,21.025]) 323 %!assert (datevec ("15:38:21.25", "HH:MM:SS.FFF"), [yr,1,1,15,38,21.025])
311 324
312 # Other tests 325 ## Test structure of return value (bug #42334)
326 %!test
327 %! [~, ~, d] = datevec ([1 2; 3 4]);
328 %! assert (d, [1 2; 3 4]);
329
330 ## Other tests
313 %!assert (datenum (datevec ([-1e4:1e4])), [-1e4:1e4]'); 331 %!assert (datenum (datevec ([-1e4:1e4])), [-1e4:1e4]');
314 %!test 332 %!test
315 %! t = linspace (-2e5, 2e5, 10993); 333 %! t = linspace (-2e5, 2e5, 10993);
316 %! assert (all (abs (datenum (datevec (t)) - t') < 1e-5)); 334 %! assert (all (abs (datenum (datevec (t)) - t') < 1e-5));
317 335
336 %% Test input validation
337 %!error datevec ()
338 %!error datevec (1,2,3,4)
339 %!error <none of the standard formats match> datevec ("foobar")
340 %!error <DATE not parsed correctly with given format> datevec ("foobar", "%d")
341