2 openerp.web.dates = function(instance) {
5 * Converts a string to a Date javascript object using OpenERP's
6 * datetime string format (exemple: '2011-12-01 15:12:35').
8 * The time zone is assumed to be UTC (standard for OpenERP 6.1)
9 * and will be converted to the browser's time zone.
11 * @param {String} str A string representing a datetime.
14 instance.web.str_to_datetime = function(str) {
18 var regex = /^(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d)(?:\.\d+)?$/;
19 var res = regex.exec(str);
21 throw new Error("'" + str + "' is not a valid datetime");
23 var obj = Date.parseExact(res[1] + " UTC", 'yyyy-MM-dd HH:mm:ss zzz');
25 throw new Error("'" + str + "' is not a valid datetime");
31 * Converts a string to a Date javascript object using OpenERP's
32 * date string format (exemple: '2011-12-01').
34 * As a date is not subject to time zones, we assume it should be
35 * represented as a Date javascript object at 00:00:00 in the
36 * time zone of the browser.
38 * @param {String} str A string representing a date.
41 instance.web.str_to_date = function(str) {
45 var regex = /^\d\d\d\d-\d\d-\d\d$/;
46 var res = regex.exec(str);
48 throw new Error("'" + str + "' is not a valid date");
50 var obj = Date.parseExact(str, 'yyyy-MM-dd');
52 throw new Error("'" + str + "' is not a valid date");
58 * Converts a string to a Date javascript object using OpenERP's
59 * time string format (exemple: '15:12:35').
61 * The OpenERP times are supposed to always be naive times. We assume it is
62 * represented using a javascript Date with a date 1 of January 1970 and a
63 * time corresponding to the meant time in the browser's time zone.
65 * @param {String} str A string representing a time.
68 instance.web.str_to_time = function(str) {
72 var regex = /^(\d\d:\d\d:\d\d)(?:\.\d+)?$/;
73 var res = regex.exec(str);
75 throw new Error("'" + str + "' is not a valid time");
77 var obj = Date.parseExact("1970-01-01 " + res[1], 'yyyy-MM-dd HH:mm:ss');
79 throw new Error("'" + str + "' is not a valid time");
85 * Left-pad provided arg 1 with zeroes until reaching size provided by second
88 * @param {Number|String} str value to pad
89 * @param {Number} size size to reach on the final padded value
90 * @returns {String} padded string
92 var zpad = function(str, size) {
94 return new Array(size - str.length + 1).join('0') + str;
98 * Converts a Date javascript object to a string using OpenERP's
99 * datetime string format (exemple: '2011-12-01 15:12:35').
101 * The time zone of the Date object is assumed to be the one of the
102 * browser and it will be converted to UTC (standard for OpenERP 6.1).
105 * @returns {String} A string representing a datetime.
107 instance.web.datetime_to_str = function(obj) {
111 return zpad(obj.getUTCFullYear(),4) + "-" + zpad(obj.getUTCMonth() + 1,2) + "-"
112 + zpad(obj.getUTCDate(),2) + " " + zpad(obj.getUTCHours(),2) + ":"
113 + zpad(obj.getUTCMinutes(),2) + ":" + zpad(obj.getUTCSeconds(),2);
117 * Converts a Date javascript object to a string using OpenERP's
118 * date string format (exemple: '2011-12-01').
120 * As a date is not subject to time zones, we assume it should be
121 * represented as a Date javascript object at 00:00:00 in the
122 * time zone of the browser.
125 * @returns {String} A string representing a date.
127 instance.web.date_to_str = function(obj) {
131 return zpad(obj.getFullYear(),4) + "-" + zpad(obj.getMonth() + 1,2) + "-"
132 + zpad(obj.getDate(),2);
136 * Converts a Date javascript object to a string using OpenERP's
137 * time string format (exemple: '15:12:35').
139 * The OpenERP times are supposed to always be naive times. We assume it is
140 * represented using a javascript Date with a date 1 of January 1970 and a
141 * time corresponding to the meant time in the browser's time zone.
144 * @returns {String} A string representing a time.
146 instance.web.time_to_str = function(obj) {
150 return zpad(obj.getHours(),2) + ":" + zpad(obj.getMinutes(),2) + ":"
151 + zpad(obj.getSeconds(),2);