annotate scripts/time/addtodate.m @ 8664:e07e93c04080

style fixes
author John W. Eaton <jwe@octave.org>
date Wed, 04 Feb 2009 10:56:23 -0500
parents d448ac8a4874
children eb63fbe60fab
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7656
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
1 ## Copyright (C) 2008 Bill Denney
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
2 ##
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
3 ## This file is part of Octave.
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
4 ##
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
8 ## your option) any later version.
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
9 ##
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
13 ## General Public License for more details.
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
14 ##
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
18
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
19 ## -*- texinfo -*-
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
20 ## @deftypefn {Function File} {@var{d} =} addtodate (@var{d}, @var{q}, @var{f})
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
21 ## Add @var{q} amount of time (with units @var{f}) to the datenum, @var{d}.
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
22 ##
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
23 ## @var{f} must be one of "year", "month", "day", "hour", "minute", or
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
24 ## "second".
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
25 ## @seealso{datenum, datevec}
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
26 ## @end deftypefn
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
27
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
28 ## Author: Bill Denney <bill@denney.ws>
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
29
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
30 function d = addtodate (d, q, f)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
31
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
32 if (nargin != 3)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
33 print_usage ();
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
34 elseif (! (ischar (f) && rows (f) == 1))
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
35 ## FIXME: enhance the function so that it works with cellstrs of the
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
36 ## same size as the output.
8664
e07e93c04080 style fixes
John W. Eaton <jwe@octave.org>
parents: 7656
diff changeset
37 error ("addtodate: f must be a single row character string");
7656
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
38 endif
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
39
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
40 if (numel (d) == 1 && numel (q) > 1)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
41 ## expand d to the size of q if d only has one element to make
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
42 ## addition later eaiser.
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
43 d = d.*ones (size (q));
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
44 endif
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
45
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
46 ## in case the user gives f as a plural, remove the s
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
47 if ("s" == f(end))
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
48 f(end) = [];
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
49 endif
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
50
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
51 if (any (strcmpi ({"year" "month"}, f)))
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
52 dtmp = datevec (d);
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
53 if (strcmpi ("year", f))
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
54 dtmp(:,1) += q(:);
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
55 elseif (strcmpi ("month", f))
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
56 dtmp(:,2) += q(:);
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
57 ## adjust the years and months if the date rolls over a year
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
58 dtmp(:,1) += floor ((dtmp(:,2)-1)/12);
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
59 dtmp(:,2) = mod (dtmp(:,2)-1, 12) + 1;
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
60 endif
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
61 dnew = datenum (dtmp);
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
62 ## make the output the right shape
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
63 if (numel (d) == numel (dnew))
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
64 d = reshape (dnew, size (d));
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
65 else
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
66 d = reshape (dnew, size (q));
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
67 endif
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
68 elseif (any (strcmpi ({"day" "hour" "minute" "second"}, f)))
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
69 mult = struct ("day", 1, "hour", 1/24, "minute", 1/1440, "second", 1/86400);
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
70 d += q.*mult.(f);
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
71 else
8664
e07e93c04080 style fixes
John W. Eaton <jwe@octave.org>
parents: 7656
diff changeset
72 error ("addtodate: Invalid time unit: %s", f);
7656
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
73 endif
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
74
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
75 endfunction
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
76
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
77 ## tests
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
78 %!shared d
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
79 %! d = datenum (2008, 1, 1);
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
80 ## Identity
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
81 %!assert (addtodate (d, 0, "year"), d)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
82 %!assert (addtodate (d, 0, "month"), d)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
83 %!assert (addtodate (d, 0, "day"), d)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
84 %!assert (addtodate (d, 0, "hour"), d)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
85 %!assert (addtodate (d, 0, "minute"), d)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
86 %!assert (addtodate (d, 0, "second"), d)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
87 ## Add one of each
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
88 ## leap year
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
89 %!assert (addtodate (d, 1, "year"), d+366)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
90 %!assert (addtodate (d, 1, "month"), d+31)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
91 %!assert (addtodate (d, 1, "day"), d+1)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
92 %!assert (addtodate (d, 1, "hour"), d+1/24)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
93 %!assert (addtodate (d, 1, "minute"), d+1/1440)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
94 %!assert (addtodate (d, 1, "second"), d+1/86400)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
95 ## substract one of each
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
96 %!assert (addtodate (d, -1, "year"), d-365)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
97 %!assert (addtodate (d, -1, "month"), d-31)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
98 %!assert (addtodate (d, -1, "day"), d-1)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
99 %!assert (addtodate (d, -1, "hour"), d-1/24)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
100 %!assert (addtodate (d, -1, "minute"), d-1/1440)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
101 %!assert (addtodate (d, -1, "second"), d-1/86400)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
102 ## rollover
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
103 %!assert (addtodate (d, 12, "month"), d+366)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
104 %!assert (addtodate (d, 13, "month"), d+366+31)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
105 ## multiple inputs and output orientation
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
106 %!assert (addtodate ([d d], [1 13], "month"), [d+31 d+366+31])
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
107 %!assert (addtodate ([d;d], [1;13], "month"), [d+31;d+366+31])
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
108 %!assert (addtodate (d, [1;13], "month"), [d+31;d+366+31])
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
109 %!assert (addtodate (d, [1 13], "month"), [d+31 d+366+31])
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
110 %!assert (addtodate ([d;d+1], 1, "month"), [d+31;d+1+31])
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
111 %!assert (addtodate ([d d+1], 1, "month"), [d+31 d+1+31])