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