string2date.m 2.72 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
function date = string2date(a) % --*-- Unitary tests --*--
    
% Copyright (C) 2013 Dynare Team
%
% 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/>.

date = struct('freq', NaN, 'time', NaN(1,2));

if ~ischar(a) || ~isdate(a)
    error('dates::string2date: Input must be a string that can be interpreted as a date!');
end

if isyearly(a)
    year = 1:(regexp(a,'[AaYy]')-1);
    date.freq = 1;
    date.time = write_time_field_y(a, year);
    return
end

if isquaterly(a)
    year = 1:(regexp(a,'[Qq]')-1);
    date.freq = 4;
    date.time = write_time_field(a, year);
    return
end

if ismonthly(a)
    year = 1:(regexp(a,'[Mm]')-1);
    date.freq = 12;
    date.time = write_time_field(a, year);
    return
end

if isweekly(a)
    year = 1:(regexp(a,'[Ww]')-1);
    date.freq = 52;
    date.time = write_time_field(a, year);
    return
end


function b = write_time_field(c, d)
    b(1) = str2double(c(d));
    b(2) = str2double(c(d(end)+2:end));

function b = write_time_field_y(c, d)
    b(1) = str2double(c(d));
    b(2) = 1;

%@test:1
%$
%$ % Define some dates
%$ date_1 = '1950Q2';
%$ date_2 = '1950m10';
%$ date_3 = '1950w50';
%$ date_4 = '1950a';
%$ date_5 = '1967y';
%$ date_6 = '2009A';
%$
%$ % Define expected results.
%$ e_date_1 = [1950 2];
%$ e_freq_1 = 4;
%$ e_date_2 = [1950 10];
%$ e_freq_2 = 12;
%$ e_date_3 = [1950 50];
%$ e_freq_3 = 52;
%$ e_date_4 = [1950 1];
%$ e_freq_4 = 1;
%$ e_date_5 = [1967 1];
%$ e_freq_5 = 1;
%$ e_date_6 = [2009 1];
%$ e_freq_6 = 1;
%$
%$ % Call the tested routine.
%$ d1 = string2date(date_1);
%$ d2 = string2date(date_2);
%$ d3 = string2date(date_3);
%$ d4 = string2date(date_4);
%$ d5 = string2date(date_5);
%$ d6 = string2date(date_6);
%$
%$ % Check the results.
%$ t(1) = dassert(d1.time,e_date_1);
%$ t(2) = dassert(d2.time,e_date_2);
%$ t(3) = dassert(d3.time,e_date_3);
%$ t(4) = dassert(d4.time,e_date_4);
%$ t(5) = dassert(d5.time,e_date_5);
%$ t(6) = dassert(d6.time,e_date_6);
%$ t(7) = dassert(d1.freq,e_freq_1);
%$ t(8) = dassert(d2.freq,e_freq_2);
%$ t(9) = dassert(d3.freq,e_freq_3);
%$ t(10) = dassert(d4.freq,e_freq_4);
%$ t(11)= dassert(d5.freq,e_freq_5);
%$ t(12)= dassert(d6.freq,e_freq_6);
%$ T = all(t);
%@eof:1