Last active
August 29, 2015 14:08
-
-
Save amigrave/6882ef9a924029894648 to your computer and use it in GitHub Desktop.
date_js_to_moment_js.diff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git addons/web/static/src/js/formats.js addons/web/static/src/js/formats.js | |
index f98e7ca..34eb185 100644 | |
--- addons/web/static/src/js/formats.js | |
+++ 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,59 @@ 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) { | |
+ 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; | |
} | |
- moment_format += unformatted + (replacement ? replacement : format[directive_index]); | |
- format = format.substr(directive_index+1); | |
- } | |
- if (format.length){ | |
- moment_format += '['+format+']'; | |
+ if (isletter.test(character)) { | |
+ if (value[index-1] === '%' && table[character] !== undefined) { | |
+ character = table[character]; | |
+ } else { | |
+ character = '\\' + character; // moment.js escape | |
+ } | |
+ } | |
+ output.push(character); | |
+ inToken = false; | |
} | |
- return moment_format; | |
+ return output.join(''); | |
+}; | |
+instance.web.normalize_format_table = { | |
+ // Table token from https://github.com/uruz/moment-datetime/blob/master/moment-datetime.js | |
+ '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': 'z', - moment.js does not support timezone names | |
+ 'z': 'ZZ' | |
}; | |
})(); | |
diff --git addons/web/static/test/formats.js addons/web/static/test/formats.js | |
index 2d0504c..45cbca1 100644 | |
--- addons/web/static/test/formats.js | |
+++ addons/web/static/test/formats.js | |
@@ -167,22 +167,24 @@ openerp.testing.section('web-formats', { | |
dependencies: ['web.formats'] | |
}, function (test) { | |
test('ES date format', function (instance) { | |
+ var old_format = instance.web._t.database.parameters.date_format; | |
instance.web._t.database.parameters.date_format = '%a, %Y %b %d'; | |
var date = instance.web.str_to_date("2009-05-04"); | |
strictEqual(instance.web.format_value(date, {type:"date"}), | |
'Mon, 2009 May 04'); | |
strictEqual(instance.web.parse_value('Mon, 2009 May 04', {type: 'date'}), | |
'2009-05-04'); | |
- instance.web._t.database.parameters.date_format = '%m/%d/%Y'; | |
+ instance.web._t.database.parameters.date_format = old_format; | |
}); | |
test('extended ES date format', function (instance) { | |
- instance.web._t.database.parameters.date_format = '%a, %Y.eko %b ren %da'; | |
+ var old_format = instance.web._t.database.parameters.date_format; | |
+ instance.web._t.database.parameters.date_format = '%aquarium, %Y.eko %brenDDDA'; | |
var date = instance.web.str_to_date("2009-05-04"); | |
strictEqual(instance.web.format_value(date, {type:"date"}), | |
- 'Mon, 2009.eko May ren 04'); | |
- strictEqual(instance.web.parse_value('Mon, 2009.eko May ren 04', {type: 'date'}), | |
+ "Monquarium, 2009.eko MayrenDDDA"); | |
+ strictEqual(instance.web.parse_value("Monquarium, 2009.eko MayrenDDDA", {type: 'date'}), | |
'2009-05-04'); | |
- instance.web._t.database.parameters.date_format = '%m/%d/%Y'; | |
+ instance.web._t.database.parameters.date_format = old_format; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment