annotate scripts/time/addtodate.m @ 27956:2310164737b3 stable

fix many spelling errors (bug #57613)
author John W. Eaton <jwe@octave.org>
date Fri, 17 Jan 2020 13:15:27 -0600
parents 00f796120a6d
children c20b7290c778
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26376
00f796120a6d maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents: 25054
diff changeset
1 ## Copyright (C) 2008-2019 Bill Denney
7656
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 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
5 ## Octave is free software: you can redistribute it and/or modify it
7656
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
7 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
8 ## (at your option) any later version.
7656
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
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## GNU General Public License for more details.
7656
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
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
17 ## <https://www.gnu.org/licenses/>.
7656
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 -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 19833
diff changeset
20 ## @deftypefn {} {@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
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
23 ##
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14363
diff changeset
24 ## @var{f} must be one of @qcode{"year"}, @qcode{"month"}, @qcode{"day"},
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14363
diff changeset
25 ## @qcode{"hour"}, @qcode{"minute"}, @qcode{"second"}, or
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14363
diff changeset
26 ## @qcode{"millisecond"}.
13856
d490ca8ab1a5 Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
27 ## @seealso{datenum, datevec, etime}
7656
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
28 ## @end deftypefn
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
29
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
30 ## Author: Bill Denney <bill@denney.ws>
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
31
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
32 function d = addtodate (d, q, f)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
33
13856
d490ca8ab1a5 Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
34 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
35 "second", 1/86400, "millisecond", 1/86400000);
d490ca8ab1a5 Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
36
7656
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
37 if (nargin != 3)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
38 print_usage ();
13856
d490ca8ab1a5 Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
39 elseif (! (ischar (f) && isrow (f)))
d490ca8ab1a5 Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
40 error ("addtodate: F must be a single character string");
7656
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
41 endif
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
42
13856
d490ca8ab1a5 Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
43 if (isscalar (d) && ! isscalar (q))
d490ca8ab1a5 Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
44 ## 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
45 d = repmat (d, size (q));
7656
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
46 endif
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
47
13856
d490ca8ab1a5 Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
48 ## in case the user gives f as a plural, remove the 's'
7656
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
49 if ("s" == f(end))
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
50 f(end) = [];
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
51 endif
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
52
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
53 if (any (strcmpi ({"year" "month"}, f)))
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
54 dtmp = datevec (d);
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
55 if (strcmpi ("year", f))
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
56 dtmp(:,1) += q(:);
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
57 elseif (strcmpi ("month", f))
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
58 dtmp(:,2) += q(:);
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
59 ## adjust the years and months if the date rolls over a year
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
60 dtmp(:,1) += floor ((dtmp(:,2)-1)/12);
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
61 dtmp(:,2) = mod (dtmp(:,2)-1, 12) + 1;
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
62 endif
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
63 dnew = datenum (dtmp);
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
64 ## make the output the right shape
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
65 if (numel (d) == numel (dnew))
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
66 d = reshape (dnew, size (d));
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
67 else
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
68 d = reshape (dnew, size (q));
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
69 endif
13856
d490ca8ab1a5 Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
70 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
71 d += q .* mult.(f);
7656
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
72 else
8664
e07e93c04080 style fixes
John W. Eaton <jwe@octave.org>
parents: 7656
diff changeset
73 error ("addtodate: Invalid time unit: %s", f);
7656
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
74 endif
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
75
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
76 endfunction
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
77
13856
d490ca8ab1a5 Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
78
7656
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
79 ## tests
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
80 %!shared d
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
81 %! d = datenum (2008, 1, 1);
7656
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
82 ## Identity
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
83 %!assert (addtodate (d, 0, "year"), d)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
84 %!assert (addtodate (d, 0, "month"), d)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
85 %!assert (addtodate (d, 0, "day"), d)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
86 %!assert (addtodate (d, 0, "hour"), d)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
87 %!assert (addtodate (d, 0, "minute"), d)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
88 %!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
89 %!assert (addtodate (d, 0, "millisecond"), d)
7656
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
90 ## Add one of each
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
91 ## leap year
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
92 %!assert (addtodate (d, 1, "year"), d+366)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
93 %!assert (addtodate (d, 1, "month"), d+31)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
94 %!assert (addtodate (d, 1, "day"), d+1)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
95 %!assert (addtodate (d, 1, "hour"), d+1/24)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
96 %!assert (addtodate (d, 1, "minute"), d+1/1440)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
97 %!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
98 %!assert (addtodate (d, 1, "millisecond"), d+1/86400000)
27956
2310164737b3 fix many spelling errors (bug #57613)
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
99 ## subtract one of each
7656
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
100 %!assert (addtodate (d, -1, "year"), d-365)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
101 %!assert (addtodate (d, -1, "month"), d-31)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
102 %!assert (addtodate (d, -1, "day"), d-1)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
103 %!assert (addtodate (d, -1, "hour"), d-1/24)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
104 %!assert (addtodate (d, -1, "minute"), d-1/1440)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
105 %!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
106 %!assert (addtodate (d, -1, "millisecond"), d-1/86400000)
7656
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
107 ## rollover
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
108 %!assert (addtodate (d, 12, "month"), d+366)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
109 %!assert (addtodate (d, 13, "month"), d+366+31)
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
110 ## multiple inputs and output orientation
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
111 %!assert (addtodate ([d d], [1 13], "month"), [d+31 d+366+31])
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
112 %!assert (addtodate ([d;d], [1;13], "month"), [d+31;d+366+31])
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
113 %!assert (addtodate (d, [1;13], "month"), [d+31;d+366+31])
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
114 %!assert (addtodate (d, [1 13], "month"), [d+31 d+366+31])
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
115 %!assert (addtodate ([d;d+1], 1, "month"), [d+31;d+1+31])
d448ac8a4874 addtodate.m: new function
bill@denney.ws
parents:
diff changeset
116 %!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
117
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
118 ## Test input validation
13856
d490ca8ab1a5 Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
119 %!error addtodate ()
d490ca8ab1a5 Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
120 %!error addtodate (1)
d490ca8ab1a5 Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
121 %!error addtodate (1,2)
d490ca8ab1a5 Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
122 %!error addtodate (1,2,3,4)
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,3)
d490ca8ab1a5 Modernize function implementations and docstrings in scripts/time.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
124 %!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
125 %!error <Invalid time unit> addtodate (1,2,"abc")