Mercurial > hg > octave-lyh
annotate scripts/time/addtodate.m @ 14335:ce2b59a6d0e5
maint: periodic merge of stable to default.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 05 Feb 2012 15:32:24 -0800 |
parents | 72c96de7a403 |
children | f3d52523cde1 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13929
diff
changeset
|
1 ## Copyright (C) 2008-2012 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}) | |
13929
9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13856
diff
changeset
|
21 ## Add @var{q} amount of time (with units @var{f}) to the serial datenum, |
9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
13856
diff
changeset
|
22 ## @var{d}. |
7656 | 23 ## |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
24 ## @var{f} must be one of "year", "month", "day", "hour", "minute", "second", |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
25 ## or "millisecond". |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
26 ## @seealso{datenum, datevec, etime} |
7656 | 27 ## @end deftypefn |
28 | |
29 ## Author: Bill Denney <bill@denney.ws> | |
30 | |
31 function d = addtodate (d, q, f) | |
32 | |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
33 persistent mult = struct ("day", 1, "hour", 1/24, "minute", 1/1440, ... |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
34 "second", 1/86400, "millisecond", 1/86400000); |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
35 |
7656 | 36 if (nargin != 3) |
37 print_usage (); | |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
38 elseif (! (ischar (f) && isrow (f))) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
39 error ("addtodate: F must be a single character string"); |
7656 | 40 endif |
41 | |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
42 if (isscalar (d) && ! isscalar (q)) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
43 ## expand d to size of q to make later addition easier. |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
44 d = repmat (d, size (q)); |
7656 | 45 endif |
46 | |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
47 ## in case the user gives f as a plural, remove the 's' |
7656 | 48 if ("s" == f(end)) |
49 f(end) = []; | |
50 endif | |
51 | |
52 if (any (strcmpi ({"year" "month"}, f))) | |
53 dtmp = datevec (d); | |
54 if (strcmpi ("year", f)) | |
55 dtmp(:,1) += q(:); | |
56 elseif (strcmpi ("month", f)) | |
57 dtmp(:,2) += q(:); | |
58 ## adjust the years and months if the date rolls over a year | |
59 dtmp(:,1) += floor ((dtmp(:,2)-1)/12); | |
60 dtmp(:,2) = mod (dtmp(:,2)-1, 12) + 1; | |
61 endif | |
62 dnew = datenum (dtmp); | |
63 ## make the output the right shape | |
64 if (numel (d) == numel (dnew)) | |
65 d = reshape (dnew, size (d)); | |
66 else | |
67 d = reshape (dnew, size (q)); | |
68 endif | |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
69 elseif (any (strcmpi ({"day" "hour" "minute" "second", "millisecond"}, f))) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
70 d += q .* mult.(f); |
7656 | 71 else |
8664 | 72 error ("addtodate: Invalid time unit: %s", f); |
7656 | 73 endif |
74 | |
75 endfunction | |
76 | |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
77 |
7656 | 78 ## tests |
79 %!shared d | |
80 %! d = datenum (2008, 1, 1); | |
81 ## Identity | |
82 %!assert (addtodate (d, 0, "year"), d) | |
83 %!assert (addtodate (d, 0, "month"), d) | |
84 %!assert (addtodate (d, 0, "day"), d) | |
85 %!assert (addtodate (d, 0, "hour"), d) | |
86 %!assert (addtodate (d, 0, "minute"), d) | |
87 %!assert (addtodate (d, 0, "second"), d) | |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
88 %!assert (addtodate (d, 0, "millisecond"), d) |
7656 | 89 ## Add one of each |
90 ## leap year | |
91 %!assert (addtodate (d, 1, "year"), d+366) | |
92 %!assert (addtodate (d, 1, "month"), d+31) | |
93 %!assert (addtodate (d, 1, "day"), d+1) | |
94 %!assert (addtodate (d, 1, "hour"), d+1/24) | |
95 %!assert (addtodate (d, 1, "minute"), d+1/1440) | |
96 %!assert (addtodate (d, 1, "second"), d+1/86400) | |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
97 %!assert (addtodate (d, 1, "millisecond"), d+1/86400000) |
7656 | 98 ## substract one of each |
99 %!assert (addtodate (d, -1, "year"), d-365) | |
100 %!assert (addtodate (d, -1, "month"), d-31) | |
101 %!assert (addtodate (d, -1, "day"), d-1) | |
102 %!assert (addtodate (d, -1, "hour"), d-1/24) | |
103 %!assert (addtodate (d, -1, "minute"), d-1/1440) | |
104 %!assert (addtodate (d, -1, "second"), d-1/86400) | |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
105 %!assert (addtodate (d, -1, "millisecond"), d-1/86400000) |
7656 | 106 ## rollover |
107 %!assert (addtodate (d, 12, "month"), d+366) | |
108 %!assert (addtodate (d, 13, "month"), d+366+31) | |
109 ## multiple inputs and output orientation | |
110 %!assert (addtodate ([d d], [1 13], "month"), [d+31 d+366+31]) | |
111 %!assert (addtodate ([d;d], [1;13], "month"), [d+31;d+366+31]) | |
112 %!assert (addtodate (d, [1;13], "month"), [d+31;d+366+31]) | |
113 %!assert (addtodate (d, [1 13], "month"), [d+31 d+366+31]) | |
114 %!assert (addtodate ([d;d+1], 1, "month"), [d+31;d+1+31]) | |
115 %!assert (addtodate ([d d+1], 1, "month"), [d+31 d+1+31]) | |
13856
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
116 |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
117 %% Test input validation |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
118 %!error addtodate () |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
119 %!error addtodate (1) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
120 %!error addtodate (1,2) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
121 %!error addtodate (1,2,3,4) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
122 %!error <F must be a single character string> addtodate (1,2,3) |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
123 %!error <F must be a single character string> addtodate (1,2,"month"') |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
124 %!error <Invalid time unit> addtodate (1,2,"abc") |
d490ca8ab1a5
Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
125 |