plus.m 2.57 KB
Newer Older
Stéphane Adjemian's avatar
Stéphane Adjemian committed
1
2
3
4
function c = plus(a,b)

%@info:
%! @deftypefn {Function File} {@var{c} =} plus (@var{a},@var{b})
5
%! @anchor{@dynDate/plus}
Stéphane Adjemian's avatar
Stéphane Adjemian committed
6
%! @sp 1
7
%! Overloads the plus (addition) operator for the Dynare dates class (@ref{dynDate}). Given an initial date @var{a},
Stéphane Adjemian's avatar
Stéphane Adjemian committed
8
9
10
11
12
13
%! computes a new date @var{c} by adding the number of periods @var{b}.
%! @sp 2
%! @strong{Inputs}
%! @sp 1
%! @table @ @var
%! @item a
14
%! Dynare date object instantiated by @ref{dynDate}.
Stéphane Adjemian's avatar
Stéphane Adjemian committed
15
16
17
18
19
20
21
22
%! @item b
%! Positive scalar integer, the number of periods
%! @end table
%! @sp 2
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item c
23
%! Dynare date object instantiated by @ref{dynDate}.
Stéphane Adjemian's avatar
Stéphane Adjemian committed
24
25
26
27
28
%! @end table
%! @sp 2
%! @strong{This function is called by:}
%! @sp 2
%! @strong{This function calls:}
29
%! @ref{@@dynDate/eq}
Stéphane Adjemian's avatar
Stéphane Adjemian committed
30
31
32
33
%!
%! @end deftypefn
%@eod:

Sébastien Villemot's avatar
Sébastien Villemot committed
34
% Copyright (C) 2011-2013 Dynare Team
Stéphane Adjemian's avatar
Stéphane Adjemian committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

51
52
if ~isa(a,'dynDate')
    error(['dynDate::plus: Input argument ' inputname(1) ' must be a dynDate object!'])
Stéphane Adjemian's avatar
Stéphane Adjemian committed
53
54
end

55
if b<0 || ~isint(b)
56
    error(['dynDate::plus: Input argument ' inputname(2) ' must be a positive integer'])
Stéphane Adjemian's avatar
Stéphane Adjemian committed
57
58
59
60
61
62
63
64
65
end

if b==0
    c = a;
    return
end

switch a.freq
  case 1
66
67
    c = a;
    c.time(1) = a.time(1) + b - 1;
Stéphane Adjemian's avatar
Stéphane Adjemian committed
68
  case {4,12,52}
69
70
71
72
73
74
    c = a;
    n1 = b;
    n2 = floor(n1/a.freq);
    n3 = mod(n1,a.freq);
    c.time(2) = c.time(2)+n3-1;
    c.time(1) = c.time(1)+n2;
Stéphane Adjemian's avatar
Stéphane Adjemian committed
75
  otherwise
76
    error('dynDate::plus: Unknown frequency!')
Stéphane Adjemian's avatar
Stéphane Adjemian committed
77
78
79
80
end

%@test:1
%$ % Define some dates
81
82
83
%$ date_1 = 1950;
%$ date_2 = '1950Q4';
%$ date_3 = '2000M3';
Stéphane Adjemian's avatar
Stéphane Adjemian committed
84
85
%$
%$ % Call the tested routine.
86
87
88
%$ d_1 = dynDate(date_1);
%$ d_2 = dynDate(date_2);
%$ d_3 = dynDate(date_3);
89
90
91
92
93
94
95
%$
%$ d1 = d_1+3;
%$ d2 = d_2+5;
%$ d3 = d_3+15;
%$ d4 = d_3+10;
%$
%$ % Expected results.
96
97
98
99
%$ e1 = dynDate(1952);
%$ e2 = dynDate('1951Q4');
%$ e3 = dynDate('2001M5');
%$ e4 = dynDate('2000M12');
Stéphane Adjemian's avatar
Stéphane Adjemian committed
100
101
%$
%$ % Check the results.
102
103
104
105
%$ t(1) = dyn_assert(e1.time,d1.time);
%$ t(2) = dyn_assert(e2.time,d2.time);
%$ t(3) = dyn_assert(e3.time,d3.time);
%$ t(4) = dyn_assert(e4.time,d4.time);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
106
%$ T = all(t);
Sébastien Villemot's avatar
Sébastien Villemot committed
107
%@eof:1