From a01c57c31fd591f2428a73608adea82660555af4 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Wed, 15 Feb 2012 16:32:37 +0100 Subject: [PATCH] [FIX] handling of date and time formats with bare/literal characters lp bug: https://launchpad.net/bugs/932448 fixed bzr revid: xmo@openerp.com-20120215153237-4q1y6snebxk6sheh --- addons/web/static/src/js/formats.js | 34 ++++++++++++++++++++++++----- addons/web/static/test/formats.js | 25 +++++++++++++++++++++ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/addons/web/static/src/js/formats.js b/addons/web/static/src/js/formats.js index e8970b65f52..4fb576e4a90 100644 --- a/addons/web/static/src/js/formats.js +++ b/addons/web/static/src/js/formats.js @@ -61,6 +61,28 @@ openerp.web.insert_thousand_seps = function (num) { return (negative ? '-' : '') + openerp.web.intersperse( num, _t.database.parameters.grouping, _t.database.parameters.thousands_sep); }; + +/** + * removes literal (non-format) text from a date or time pattern, as datejs can + * not deal with literal text in format strings (whatever the format), whereas + * strftime allows for literal characters + * + * @param {String} value original format + */ +openerp.web.strip_raw_chars = function (value) { + var isletter = /[a-zA-Z]/, output = []; + for(var index=0; index < value.length; ++index) { + var character = value[index]; + if(isletter.test(character) && (index === 0 || value[index-1] !== '%')) { + continue; + } + output.push(character); + } + return output.join(''); +}; +var normalize_format = function (format) { + return Date.normalizeFormat(openerp.web.strip_raw_chars(format)); +}; /** * Formats a single atomic value based on a field descriptor * @@ -116,16 +138,16 @@ openerp.web.format_value = function (value, descriptor, value_if_empty) { if (typeof(value) == "string") value = openerp.web.auto_str_to_date(value); - return value.format(l10n.date_format - + ' ' + l10n.time_format); + return value.toString(normalize_format(l10n.date_format) + + ' ' + normalize_format(l10n.time_format)); case 'date': if (typeof(value) == "string") value = openerp.web.auto_str_to_date(value); - return value.format(l10n.date_format); + return value.toString(normalize_format(l10n.date_format)); case 'time': if (typeof(value) == "string") value = openerp.web.auto_str_to_date(value); - return value.format(l10n.time_format); + return value.toString(normalize_format(l10n.time_format)); case 'selection': // Each choice is [value, label] if(_.isArray(value)) { @@ -142,8 +164,8 @@ openerp.web.format_value = function (value, descriptor, value_if_empty) { }; openerp.web.parse_value = function (value, descriptor, value_if_empty) { - var date_pattern = Date.normalizeFormat(_t.database.parameters.date_format), - time_pattern = Date.normalizeFormat(_t.database.parameters.time_format); + var date_pattern = normalize_format(_t.database.parameters.date_format), + time_pattern = normalize_format(_t.database.parameters.time_format); switch (value) { case false: case "": diff --git a/addons/web/static/test/formats.js b/addons/web/static/test/formats.js index 2e292af41c8..c0d6798628d 100644 --- a/addons/web/static/test/formats.js +++ b/addons/web/static/test/formats.js @@ -204,4 +204,29 @@ $(document).ready(function () { equal(openerp.web.format_value(6000, {type: 'float'}), '6.000,00'); }); + module('custom-date-formats', { + setup: function () { + openerp = window.openerp.init(); + window.openerp.web.core(openerp); + window.openerp.web.dates(openerp); + window.openerp.web.formats(openerp); + } + }); + test('format stripper', function () { + strictEqual(openerp.web.strip_raw_chars('%a, %Y %b %d'), '%a, %Y %b %d'); + strictEqual(openerp.web.strip_raw_chars('%a, %Y.eko %bren %da'), '%a, %Y. %b %d'); + }); + test('ES date format', function () { + openerp.web._t.database.parameters.date_format = '%a, %Y %b %d'; + var date = openerp.web.str_to_date("2009-05-04"); + strictEqual(openerp.web.format_value(date, {type:"date"}), 'Mon, 2009 May 04'); + strictEqual(openerp.web.parse_value('Mon, 2009 May 04', {type: 'date'}), '2009-05-04'); + }); + test('extended ES date format', function () { + openerp.web._t.database.parameters.date_format = '%a, %Y.eko %bren %da'; + var date = openerp.web.str_to_date("2009-05-04"); + strictEqual(openerp.web.format_value(date, {type:"date"}), 'Mon, 2009. May 04'); + strictEqual(openerp.web.parse_value('Mon, 2009. May 04', {type: 'date'}), '2009-05-04'); + }); + });