From e26a5e2805a794cd2945b381cc02d2761e92b1ff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Fri, 15 Jan 2021 17:41:35 +0100
Subject: [PATCH] NLS: compatibility fix for Octave and MATLAB < R2014b

---
 matlab/+pac/+estimate/nls.m        |  8 ++++----
 matlab/dynare_config.m             |  5 +++++
 matlab/missing/datetime/datetime.m | 28 ++++++++++++++++++++++++++++
 3 files changed, 37 insertions(+), 4 deletions(-)
 create mode 100644 matlab/missing/datetime/datetime.m

diff --git a/matlab/+pac/+estimate/nls.m b/matlab/+pac/+estimate/nls.m
index f85867e94f..77f2180ab1 100644
--- a/matlab/+pac/+estimate/nls.m
+++ b/matlab/+pac/+estimate/nls.m
@@ -42,7 +42,7 @@ function nls(eqname, params, data, range, optimizer, varargin)
 % is available only if the matylab optimization toolbox is installed), the
 % remaining inputs are the options (key/value) passed to the optimizers.
 
-% Copyright (C) 2018-2019 Dynare Team
+% Copyright (C) 2018-2021 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -130,7 +130,7 @@ fid = fopen(['+' M_.fname filesep() fun '.m'], 'w');
 fprintf(fid, 'function r = %s(params, data, DynareModel, DynareOutput)\n', fun);
 fprintf(fid, '\n');
 fprintf(fid, '%% Evaluates the residuals for equation %s.\n', eqname);
-fprintf(fid, '%% File created by Dynare (%s).\n', datestr(datetime));
+fprintf(fid, '%% File created by Dynare (%s).\n', datetime);
 fprintf(fid, '\n');
 for i=1:length(ipnames_)
     fprintf(fid, 'DynareModel.params(%u) = params(%u);\n', ipnames_(i), i);
@@ -147,7 +147,7 @@ fid = fopen(['+' M_.fname filesep() fun '.m'], 'w');
 fprintf(fid, 'function [s, fake1, fake2, fake3, fake4] = %s(params, data, DynareModel, DynareOutput)\n', fun);
 fprintf(fid, '\n');
 fprintf(fid, '%% Evaluates the sum of square residuals for equation %s.\n', eqname);
-fprintf(fid, '%% File created by Dynare (%s).\n', datestr(datetime));
+fprintf(fid, '%% File created by Dynare (%s).\n', datetime);
 fprintf(fid, '\n');
 fprintf(fid, 'fake1 = 0;\n');
 fprintf(fid, 'fake2 = [];\n');
@@ -335,4 +335,4 @@ oo_.pac.(pacmodl).equations.(eqtag).student = params1./(sqrt(diag(C)));
 
 % Also save estimated parameters in M_
 M_.params(ipnames_) = params1;
-M_ = pac.update.parameters(pacmodl, M_, oo_, false);
\ No newline at end of file
+M_ = pac.update.parameters(pacmodl, M_, oo_, false);
diff --git a/matlab/dynare_config.m b/matlab/dynare_config.m
index 1ee4177037..a91d99bcbf 100644
--- a/matlab/dynare_config.m
+++ b/matlab/dynare_config.m
@@ -126,6 +126,11 @@ if isoctave || matlab_ver_less_than('9.1')
     p{end+1} = '/missing/splitlines';
 end
 
+% datetime doesn't exist in Octave and in MATLAB < R2014b
+if isoctave || matlab_ver_less_than('8.4')
+    p{end+1} = '/missing/datetime';
+end
+
 P = cellfun(@(c)[dynareroot(1:end-1) c], p, 'uni',false);
 
 % Get mex files folder(s)
diff --git a/matlab/missing/datetime/datetime.m b/matlab/missing/datetime/datetime.m
new file mode 100644
index 0000000000..beace5272a
--- /dev/null
+++ b/matlab/missing/datetime/datetime.m
@@ -0,0 +1,28 @@
+% Stub implementation of datetime.
+% It just returns a string, which is the equivalent of what one would
+% get under MATLAB ≥ R2014b and a European locale with:
+%  sprintf('%s', datetime)
+%
+% Hence, it only works as a substitute for the real datetime in the context of
+% formatted output.
+
+% Copyright (C) 2021 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/>.
+
+function d = datetime()
+    d = datestr(now, 'dd-mmm-yyyy HH:MM:SS');
+end
-- 
GitLab