[ADD] translatable name to views
[odoo/odoo.git] / addons / web / static / src / js / dates.js
1
2 openerp.web.dates = function(openerp) {
3
4 /**
5  * Converts a string to a Date javascript object using OpenERP's
6  * datetime string format (exemple: '2011-12-01 15:12:35').
7  * 
8  * The timezone is assumed to be UTC (standard for OpenERP 6.1)
9  * and will be converted to the browser's timezone.
10  * 
11  * @param {String} str A string representing a datetime.
12  * @returns {Date}
13  */
14 openerp.web.str_to_datetime = function(str) {
15     if(!str) {
16         return str;
17     }
18     var regex = /^(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d)(?:\.\d+)?$/;
19     var res = regex.exec(str);
20     if ( !res ) {
21         throw new Error("'" + str + "' is not a valid datetime");
22     }
23     var obj = Date.parseExact(res[1] + " UTC", 'yyyy-MM-dd HH:mm:ss zzz');
24     if (! obj) {
25         throw new Error("'" + str + "' is not a valid datetime");
26     }
27     return obj;
28 };
29
30 /**
31  * Converts a string to a Date javascript object using OpenERP's
32  * date string format (exemple: '2011-12-01').
33  * 
34  * @param {String} str A string representing a date.
35  * @returns {Date}
36  */
37 openerp.web.str_to_date = function(str) {
38     if(!str) {
39         return str;
40     }
41     var regex = /^\d\d\d\d-\d\d-\d\d$/;
42     var res = regex.exec(str);
43     if ( !res ) {
44         throw new Error("'" + str + "' is not a valid date");
45     }
46     var obj = Date.parseExact(str + ' UTC', 'yyyy-MM-dd zzz');
47     if (! obj) {
48         throw new Error("'" + str + "' is not a valid date");
49     }
50     return obj;
51 };
52
53 /**
54  * Converts a string to a Date javascript object using OpenERP's
55  * time string format (exemple: '15:12:35').
56  * 
57  * @param {String} str A string representing a time.
58  * @returns {Date}
59  */
60 openerp.web.str_to_time = function(str) {
61     if(!str) {
62         return str;
63     }
64     var regex = /^(\d\d:\d\d:\d\d)(?:\.\d+)?$/;
65     var res = regex.exec(str);
66     if ( !res ) {
67         throw new Error("'" + str + "' is not a valid time");
68     }
69     var obj = Date.parseExact(res[1] + ' UTC', 'HH:mm:ss zzz');
70     if (! obj) {
71         throw new Error("'" + str + "' is not a valid time");
72     }
73     return obj;
74 };
75
76 /*
77  * Left-pad provided arg 1 with zeroes until reaching size provided by second
78  * argument.
79  *
80  * @param {Number|String} str value to pad
81  * @param {Number} size size to reach on the final padded value
82  * @returns {String} padded string
83  */
84 var zpad = function(str, size) {
85     str = "" + str;
86     return new Array(size - str.length + 1).join('0') + str;
87 };
88
89 /**
90  * Converts a Date javascript object to a string using OpenERP's
91  * datetime string format (exemple: '2011-12-01 15:12:35').
92  * 
93  * The timezone of the Date object is assumed to be the one of the
94  * browser and it will be converted to UTC (standard for OpenERP 6.1).
95  * 
96  * @param {Date} obj
97  * @returns {String} A string representing a datetime.
98  */
99 openerp.web.datetime_to_str = function(obj) {
100     if (!obj) {
101         return false;
102     }
103     return zpad(obj.getUTCFullYear(),4) + "-" + zpad(obj.getUTCMonth() + 1,2) + "-"
104          + zpad(obj.getUTCDate(),2) + " " + zpad(obj.getUTCHours(),2) + ":"
105          + zpad(obj.getUTCMinutes(),2) + ":" + zpad(obj.getUTCSeconds(),2);
106 };
107
108 /**
109  * Converts a Date javascript object to a string using OpenERP's
110  * date string format (exemple: '2011-12-01').
111  * 
112  * @param {Date} obj
113  * @returns {String} A string representing a date.
114  */
115 openerp.web.date_to_str = function(obj) {
116     if (!obj) {
117         return false;
118     }
119     return zpad(obj.getFullYear(),4) + "-" + zpad(obj.getMonth() + 1,2) + "-"
120          + zpad(obj.getDate(),2);
121 };
122
123 /**
124  * Converts a Date javascript object to a string using OpenERP's
125  * time string format (exemple: '15:12:35').
126  * 
127  * @param {Date} obj
128  * @returns {String} A string representing a time.
129  */
130 openerp.web.time_to_str = function(obj) {
131     if (!obj) {
132         return false;
133     }
134     return zpad(obj.getHours(),2) + ":" + zpad(obj.getMinutes(),2) + ":"
135          + zpad(obj.getSeconds(),2);
136 };
137     
138 };