diff --git a/addons/web/static/src/js/framework/pyeval.js b/addons/web/static/src/js/framework/pyeval.js index bcab0b0717f..c1b823ed48f 100644 --- a/addons/web/static/src/js/framework/pyeval.js +++ b/addons/web/static/src/js/framework/pyeval.js @@ -436,6 +436,22 @@ datetime.datetime = py.type('datetime', null, { this.second, this.microsecond / 1000); }, + __add__: function (other) { + if (!py.PY_isInstance(other, datetime.timedelta)) { + return py.NotImplemented; + } + var s = tmxxx(this.year, this.month, this.day + other.days, this.hour, this.minute, this.second + other.seconds); + return datetime.datetime.fromJSON(s.year, s.month, s.day, s.hour, s.minute, s.second); + }, + __sub__: function (other) { + if (py.PY_isInstance(other, datetime.timedelta)) { + return py.PY_add(this, py.PY_negative(other)); + } + return py.NotImplemented; + }, + fromJSON: function (year, month, day, hour, minute, second) { + return py.PY_call(datetime.datetime, [year, month, day, hour, minute, second]); + }, }); datetime.date = py.type('date', null, { diff --git a/addons/web/static/test/evals.js b/addons/web/static/test/evals.js index b6a3b4177e8..bb5b5564ba4 100644 --- a/addons/web/static/test/evals.js +++ b/addons/web/static/test/evals.js @@ -302,6 +302,25 @@ odoo.define_section('eval.types', ['web.pyeval'], function (test, mock) { '2015-02-02'); }); + test('timedelta', function (assert, pyeval) { + assert.strictEqual( + py.eval("(datetime.datetime(2017, 2, 15, 1, 7, 31) + datetime.timedelta(days=1)).strftime('%Y-%m-%d %H:%M:%S')", + pyeval.context()), + "2017-02-16 01:07:31"); + strictEqual( + py.eval("(datetime.datetime(2012, 2, 15, 1, 7, 31) - datetime.timedelta(hours=1)).strftime('%Y-%m-%d %H:%M:%S')", + pyeval.context()), + "2012-02-15 00:07:31"); + strictEqual( + py.eval("(datetime.datetime(2012, 2, 15, 1, 7, 31) + datetime.timedelta(hours=-1)).strftime('%Y-%m-%d %H:%M:%S')", + pyeval.context()), + "2012-02-15 00:07:31"); + strictEqual( + py.eval("(datetime.datetime(2012, 2, 15, 1, 7, 31) + datetime.timedelta(minutes=100)).strftime('%Y-%m-%d %H:%M:%S')", + pyeval.context()), + "2012-02-15 02:47:31"); + }); + test('datetime.tojson', function (assert, pyeval) { var result = py.eval( 'datetime.datetime(2012, 2, 15, 1, 7, 31)',