From d9ca5363961e465ce43ac75f7ba26c977880005d Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Thu, 31 Oct 2013 11:33:33 +0100 Subject: [PATCH] [FIX] web: correct date and datetime parsing at end of month. As `new Date()` return the current date, when we are the 31, setting the month to a month with less than 31 days will change the date to next day (1st of next month). This result to a date(time) object one month ahead of the wanted date(time). bzr revid: chs@openerp.com-20131031103333-vt68132ptj9sbr04 --- addons/web/static/src/js/openerpframework.js | 6 ++-- addons/web/static/test/framework.js | 37 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/addons/web/static/src/js/openerpframework.js b/addons/web/static/src/js/openerpframework.js index 027452b42fb..1d688505a06 100644 --- a/addons/web/static/src/js/openerpframework.js +++ b/addons/web/static/src/js/openerpframework.js @@ -1265,7 +1265,7 @@ openerp.str_to_datetime = function(str) { if ( !res ) { throw new Error("'" + str + "' is not a valid datetime"); } - var tmp = new Date(); + var tmp = new Date(0); tmp.setUTCFullYear(parseFloat(res[1])); tmp.setUTCMonth(parseFloat(res[2]) - 1); tmp.setUTCDate(parseFloat(res[3])); @@ -1297,7 +1297,7 @@ openerp.str_to_date = function(str) { if ( !res ) { throw new Error("'" + str + "' is not a valid date"); } - var tmp = new Date(); + var tmp = new Date(0); tmp.setFullYear(parseFloat(res[1])); tmp.setMonth(parseFloat(res[2]) - 1); tmp.setDate(parseFloat(res[3])); @@ -1424,4 +1424,4 @@ if (typeof(define) !== "undefined") { // amd window.openerp = declare($, _, QWeb2); } -})(); \ No newline at end of file +})(); diff --git a/addons/web/static/test/framework.js b/addons/web/static/test/framework.js index 3619ab87e65..88bc413a279 100644 --- a/addons/web/static/test/framework.js +++ b/addons/web/static/test/framework.js @@ -427,12 +427,49 @@ ropenerp.testing.section('server-formats', { date3.getUTCHours(), date3.getUTCMinutes(), date3.getUTCSeconds(), date3.getUTCMilliseconds()], [2009, 5 - 1, 4, 12, 34, 23, 845]); }); + test('Parse server datetime on 31', function() { + var wDate = window.Date; + var s = openerp.testing.Stack(); + return s.push(function() { + window.Date = function() { + return wDate('2013-10-31 12:34:56'); + }; + }, function() { + window.Date = wDate; + }).execute(function() { + return openerp.str_to_datetime('2013-11-11 02:45:21'); + }).then(function(date) { + deepEqual( + [date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), + date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()], + [2013, 11 - 1, 11, 2, 45, 21]); + }); + + }); test('Parse server date', function () { var date = openerp.str_to_date("2009-05-04"); deepEqual( [date.getFullYear(), date.getMonth(), date.getDate()], [2009, 5 - 1, 4]); }); + test('Parse server date on 31', function() { + var wDate = window.Date; + var s = openerp.testing.Stack(); + return s.push(function() { + window.Date = function() { + return wDate('2013-10-31 12:34:56'); + }; + }, function() { + window.Date = wDate; + }).execute(function() { + return openerp.str_to_date('2013-11-11'); + }).then(function(date) { + deepEqual( + [date.getFullYear(), date.getMonth(), date.getDate()], + [2013, 11 - 1, 11]); + }); + + }); test('Parse server time', function () { var date = openerp.str_to_time("12:34:23"); deepEqual(