comparison scripts/time/datenum.m @ 7782:bfd8d804e6d3

datenum\: fixed combination of scalar and vector/matrix input bug
author bill@denney.ws
date Sun, 18 May 2008 00:09:08 -0400
parents 4ab2488ab2b4
children d1978e7364ad
comparison
equal deleted inserted replaced
7781:02a7fe6907d2 7782:bfd8d804e6d3
97 ## Set start of year to March by moving Jan. and Feb. to previous year. 97 ## Set start of year to March by moving Jan. and Feb. to previous year.
98 ## Correct for months > 12 by moving to subsequent years. 98 ## Correct for months > 12 by moving to subsequent years.
99 Y += fix ((M-14)/12); 99 Y += fix ((M-14)/12);
100 100
101 ## Lookup number of days since start of the current year. 101 ## Lookup number of days since start of the current year.
102 D += reshape (monthstart (mod (M-1,12) + 1), size (D)) + 60; 102 if (numel (M) == 1 || numel (D) == 1)
103 ## Allow M or D to be scalar while other values may be vectors or
104 ## matrices.
105 D += monthstart (mod (M-1,12) + 1) + 60;
106 if (numel (M) > 1)
107 D = reshape (D, size (M));
108 endif
109 else
110 D += reshape (monthstart (mod (M-1,12) + 1), size (D)) + 60;
111 endif
103 112
104 ## Add number of days to the start of the current year. Correct 113 ## Add number of days to the start of the current year. Correct
105 ## for leap year every 4 years except centuries not divisible by 400. 114 ## for leap year every 4 years except centuries not divisible by 400.
106 D += 365*Y + floor (Y/4) - floor (Y/100) + floor (Y/400); 115 D += 365*Y + floor (Y/4) - floor (Y/100) + floor (Y/400);
107 116
128 ## Make sure that the vectors can have either orientation 137 ## Make sure that the vectors can have either orientation
129 %!test 138 %!test
130 %! t = [2001,5,19,12,21,3.5; 1417,6,12,12,21,3.5]'; 139 %! t = [2001,5,19,12,21,3.5; 1417,6,12,12,21,3.5]';
131 %! n = [730990 517712] + part; 140 %! n = [730990 517712] + part;
132 %! assert(datenum(t(1,:), t(2,:), t(3,:), t(4,:), t(5,:), t(6,:)), n, 2*eps); 141 %! assert(datenum(t(1,:), t(2,:), t(3,:), t(4,:), t(5,:), t(6,:)), n, 2*eps);
142
143 ## Test mixed vectors and scalars
144 %!assert (datenum([2008;2009], 1, 1), [datenum(2008, 1, 1);datenum(2009, 1, 1)]);
145 %!assert (datenum(2008, [1;2], 1), [datenum(2008, 1, 1);datenum(2008, 2, 1)]);
146 %!assert (datenum(2008, 1, [1;2]), [datenum(2008, 1, 1);datenum(2008, 1, 2)]);
147 %!assert (datenum([2008;2009], [1;2], 1), [datenum(2008, 1, 1);datenum(2009, 2, 1)]);
148 %!assert (datenum([2008;2009], 1, [1;2]), [datenum(2008, 1, 1);datenum(2009, 1, 2)]);
149 %!assert (datenum(2008, [1;2], [1;2]), [datenum(2008, 1, 1);datenum(2008, 2, 2)]);
150 ## And the other orientation
151 %!assert (datenum([2008 2009], 1, 1), [datenum(2008, 1, 1) datenum(2009, 1, 1)]);
152 %!assert (datenum(2008, [1 2], 1), [datenum(2008, 1, 1) datenum(2008, 2, 1)]);
153 %!assert (datenum(2008, 1, [1 2]), [datenum(2008, 1, 1) datenum(2008, 1, 2)]);
154 %!assert (datenum([2008 2009], [1 2], 1), [datenum(2008, 1, 1) datenum(2009, 2, 1)]);
155 %!assert (datenum([2008 2009], 1, [1 2]), [datenum(2008, 1, 1) datenum(2009, 1, 2)]);
156 %!assert (datenum(2008, [1 2], [1 2]), [datenum(2008, 1, 1) datenum(2008, 2, 2)]);