summaryrefslogtreecommitdiff
path: root/addons/web/static/src
diff options
context:
space:
mode:
authorFabien Meghazi <fme@openerp.com>2014-11-05 13:42:06 +0100
committerFabien Meghazi <fme@openerp.com>2014-11-05 15:28:00 +0100
commita29c44be3161a9f051306f6889972ca233cc9260 (patch)
tree954e7d250ecb6fabea9e7e9f64f9334d884ce431 /addons/web/static/src
parenteb1f4ea5d2c7e3f782095bfb093694842205ef6e (diff)
[FIX] python strftime to moment.js format conversion
Diffstat (limited to 'addons/web/static/src')
-rw-r--r--addons/web/static/src/js/formats.js103
1 files changed, 56 insertions, 47 deletions
diff --git a/addons/web/static/src/js/formats.js b/addons/web/static/src/js/formats.js
index f98e7ca3ecc..0ec366d44ce 100644
--- a/addons/web/static/src/js/formats.js
+++ b/addons/web/static/src/js/formats.js
@@ -114,7 +114,7 @@ instance.web.human_size = function(size) {
instance.web.format_value = function (value, descriptor, value_if_empty) {
var l10n = _t.database.parameters;
var date_format = instance.web.normalize_format(l10n.date_format);
- var time_format = instance.web.normalize_format(l10n.time_format)
+ var time_format = instance.web.normalize_format(l10n.time_format);
// If NaN value, display as with a `false` (empty cell)
if (typeof value === 'number' && isNaN(value)) {
value = false;
@@ -331,54 +331,63 @@ instance.web.round_decimals = function(value, decimals){
};
/**
-+ *
-+ * convert python.strftime format into moment.js format
-+ * inspired from : https://github.com/uruz/moment-datetime/blob/master/moment-datetime.js
-+*/
-instance.web.normalize_format = function(format){
- if (!format)
- return false;
- var replacements = {
- 'a': 'ddd',
- 'A': 'dddd',
- 'b': 'MMM',
- 'B': 'MMMM',
- //'c': //%c is defined too vaguely
- 'd': 'DD',
- //'f': JS have no support for microseconds and moment.js have no support for milliseconds
- 'H': 'HH',
- 'I': 'hh',
- 'j': 'DDDD',
- 'm': 'MM',
- 'M': 'mm',
- 'p': 'A',
- 'S': 'ss',
- 'U': 'ww',//ww is for Sunday-based week
- 'w': 'd',
- //'W': 'ww',//%W is weeknumber for weeks starting from Monday and it is not implemented in moment.js
- //'x':
- //'X': //%x and %X are defined too vaguely to be implemented
- 'y': 'YY',
- 'Y': 'YYYY',
- 'z': 'ZZ',
- //'Z': 'z', - moment.js does not support timezone names
- '%': '%'
- }
- var moment_format = '', directive_index = 0, replacement, unformatted;
- while (format.indexOf('%') !== -1){
- directive_index = format.indexOf('%') + 1;
- replacement = replacements[format[directive_index]];
- unformatted = format.substr(0, directive_index-1);
- if (unformatted.length){
- unformatted = '[' + unformatted.replace(/(\[|\])/g, '\\$&') +']';
+ * Convert Python strftime to escaped moment.js format.
+ *
+ * @param {String} value original format
+ */
+instance.web.normalize_format = function (value) {
+ if (_normalize_format_cache[value] === undefined) {
+ var isletter = /[a-zA-Z]/,
+ output = [],
+ inToken = false,
+ table = instance.web.normalize_format_table;
+
+ for (var index=0; index < value.length; ++index) {
+ var character = value[index];
+ if (character === '%' && !inToken) {
+ inToken = true;
+ continue;
+ }
+ if (isletter.test(character)) {
+ if (inToken && table[character] !== undefined) {
+ character = table[character];
+ } else {
+ character = '[' + character + ']'; // moment.js escape
+ }
+ }
+ output.push(character);
+ inToken = false;
}
- moment_format += unformatted + (replacement ? replacement : format[directive_index]);
- format = format.substr(directive_index+1);
+ _normalize_format_cache[value] = output.join('');
}
- if (format.length){
- moment_format += '['+format+']';
- }
- return moment_format;
+ return _normalize_format_cache[value];
+};
+instance.web.normalize_format_table = {
+ // Python strftime to moment.js conversion table
+ // See openerp/addons/base/res/res_lang_view.xml
+ // for details about supported directives
+ 'a': 'ddd',
+ 'A': 'dddd',
+ 'b': 'MMM',
+ 'B': 'MMMM',
+ 'd': 'DD',
+ 'H': 'HH',
+ 'I': 'hh',
+ 'j': 'DDDD',
+ 'm': 'MM',
+ 'M': 'mm',
+ 'p': 'A',
+ 'S': 'ss',
+ 'U': 'ww',
+ 'W': 'WW',
+ 'w': 'd',
+ 'y': 'YY',
+ 'Y': 'YYYY',
+ // unsupported directives
+ 'c': 'ddd MMM D HH:mm:ss YYYY',
+ 'x': 'MM/DD/YY',
+ 'X': 'HH:mm:ss'
};
+var _normalize_format_cache = {};
})();