[WIP] Breadcrumb
[odoo/odoo.git] / addons / web / static / src / js / dates.js
1
2 openerp.web.dates = function(instance) {
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 time zone is assumed to be UTC (standard for OpenERP 6.1)
9  * and will be converted to the browser's time zone.
10  * 
11  * @param {String} str A string representing a datetime.
12  * @returns {Date}
13  */
14 instance.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  * 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.
37  * 
38  * @param {String} str A string representing a date.
39  * @returns {Date}
40  */
41 instance.web.str_to_date = function(str) {
42     if(!str) {
43         return str;
44     }
45     var regex = /^\d\d\d\d-\d\d-\d\d$/;
46     var res = regex.exec(str);
47     if ( !res ) {
48         throw new Error("'" + str + "' is not a valid date");
49     }
50     var obj = Date.parseExact(str, 'yyyy-MM-dd');
51     if (! obj) {
52         throw new Error("'" + str + "' is not a valid date");
53     }
54     return obj;
55 };
56
57 /**
58  * Converts a string to a Date javascript object using OpenERP's
59  * time string format (exemple: '15:12:35').
60  * 
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.
64  * 
65  * @param {String} str A string representing a time.
66  * @returns {Date}
67  */
68 instance.web.str_to_time = function(str) {
69     if(!str) {
70         return str;
71     }
72     var regex = /^(\d\d:\d\d:\d\d)(?:\.\d+)?$/;
73     var res = regex.exec(str);
74     if ( !res ) {
75         throw new Error("'" + str + "' is not a valid time");
76     }
77     var obj = Date.parseExact("1970-01-01 " + res[1], 'yyyy-MM-dd HH:mm:ss');
78     if (! obj) {
79         throw new Error("'" + str + "' is not a valid time");
80     }
81     return obj;
82 };
83
84 /*
85  * Left-pad provided arg 1 with zeroes until reaching size provided by second
86  * argument.
87  *
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
91  */
92 var zpad = function(str, size) {
93     str = "" + str;
94     return new Array(size - str.length + 1).join('0') + str;
95 };
96
97 /**
98  * Converts a Date javascript object to a string using OpenERP's
99  * datetime string format (exemple: '2011-12-01 15:12:35').
100  * 
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).
103  * 
104  * @param {Date} obj
105  * @returns {String} A string representing a datetime.
106  */
107 instance.web.datetime_to_str = function(obj) {
108     if (!obj) {
109         return false;
110     }
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);
114 };
115
116 /**
117  * Converts a Date javascript object to a string using OpenERP's
118  * date string format (exemple: '2011-12-01').
119  * 
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.
123  * 
124  * @param {Date} obj
125  * @returns {String} A string representing a date.
126  */
127 instance.web.date_to_str = function(obj) {
128     if (!obj) {
129         return false;
130     }
131     return zpad(obj.getFullYear(),4) + "-" + zpad(obj.getMonth() + 1,2) + "-"
132          + zpad(obj.getDate(),2);
133 };
134
135 /**
136  * Converts a Date javascript object to a string using OpenERP's
137  * time string format (exemple: '15:12:35').
138  * 
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.
142  * 
143  * @param {Date} obj
144  * @returns {String} A string representing a time.
145  */
146 instance.web.time_to_str = function(obj) {
147     if (!obj) {
148         return false;
149     }
150     return zpad(obj.getHours(),2) + ":" + zpad(obj.getMinutes(),2) + ":"
151          + zpad(obj.getSeconds(),2);
152 };
153     
154 };