~openerp-mongolian/openerp-mongolization/7.0

« back to all changes in this revision

Viewing changes to l10n_mn_base/static/src/js/.svn/text-base/dates.js.svn-base

  • Committer: Unurjartal Ts
  • Date: 2013-06-17 09:33:42 UTC
  • mfrom: (1.1.2 openerp-mongolization)
  • Revision ID: unuruu25@gmail.com-20130617093342-w2o2hbtz5qg8ioli
ShineERP custom modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
openerp.l10n_mn_base = function(openerp) {
 
3
/**
 
4
 * Converts a string to a Date javascript object using OpenERP's
 
5
 * datetime string format (exemple: '2011-12-01 15:12:35').
 
6
 * 
 
7
 * The time zone is assumed to be UTC (standard for OpenERP 6.1)
 
8
 * and will be converted to the browser's time zone.
 
9
 * 
 
10
 * @param {String} str A string representing a datetime.
 
11
 * @returns {Date}
 
12
 */
 
13
openerp.web.str_to_datetime = function(str) {
 
14
    if(!str) {
 
15
        return str;
 
16
    }
 
17
    var regex = /^(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(?:\.\d+)?$/;
 
18
    var res = regex.exec(str);
 
19
    if ( !res ) {
 
20
        throw new Error("'" + str + "' is not a valid datetime");
 
21
    }
 
22
    /*
 
23
    return new Date(Date.UTC(
 
24
        parseInt(res[1], 10),
 
25
        parseInt(res[2], 10) - 1,
 
26
        parseInt(res[3], 10),
 
27
        parseInt(res[4], 10),
 
28
        parseInt(res[5], 10),
 
29
        parseInt(res[6], 10)
 
30
    ));
 
31
    */
 
32
    return new Date(
 
33
            parseInt(res[1], 10),
 
34
            parseInt(res[2], 10) - 1,
 
35
            parseInt(res[3], 10),
 
36
            parseInt(res[4], 10),
 
37
            parseInt(res[5], 10),
 
38
            parseInt(res[6], 10)
 
39
        );
 
40
};
 
41
 
 
42
/**
 
43
 * Converts a string to a Date javascript object using OpenERP's
 
44
 * date string format (exemple: '2011-12-01').
 
45
 * 
 
46
 * As a date is not subject to time zones, we assume it should be
 
47
 * represented as a Date javascript object at 00:00:00 in the
 
48
 * time zone of the browser.
 
49
 * 
 
50
 * @param {String} str A string representing a date.
 
51
 * @returns {Date}
 
52
 */
 
53
openerp.web.str_to_date = function(str) {
 
54
    if(!str) {
 
55
        return str;
 
56
    }
 
57
    var regex = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
 
58
    var res = regex.exec(str);
 
59
    if ( !res ) {
 
60
        throw new Error("'" + str + "' is not a valid date");
 
61
    }
 
62
    return new Date(
 
63
        parseInt(res[1], 10),
 
64
        parseInt(res[2], 10) - 1,
 
65
        parseInt(res[3], 10)
 
66
    );
 
67
};
 
68
 
 
69
/**
 
70
 * Converts a string to a Date javascript object using OpenERP's
 
71
 * time string format (exemple: '15:12:35').
 
72
 * 
 
73
 * The OpenERP times are supposed to always be naive times. We assume it is
 
74
 * represented using a javascript Date with a date 1 of January 1970 and a
 
75
 * time corresponding to the meant time in the browser's time zone.
 
76
 * 
 
77
 * @param {String} str A string representing a time.
 
78
 * @returns {Date}
 
79
 */
 
80
openerp.web.str_to_time = function(str) {
 
81
    if(!str) {
 
82
        return str;
 
83
    }
 
84
    var regex = /^(\d\d):(\d\d):(\d\d)(?:\.\d+)?$/;
 
85
    var res = regex.exec(str);
 
86
    if ( !res ) {
 
87
        throw new Error("'" + str + "' is not a valid time");
 
88
    }
 
89
    return new Date(
 
90
        1970, 0, 1, parseInt(res[1], 10), parseInt(res[2], 10), parseInt(res[3], 10));
 
91
};
 
92
 
 
93
/*
 
94
 * Left-pad provided arg 1 with zeroes until reaching size provided by second
 
95
 * argument.
 
96
 *
 
97
 * @param {Number|String} str value to pad
 
98
 * @param {Number} size size to reach on the final padded value
 
99
 * @returns {String} padded string
 
100
 */
 
101
var zpad = function(str, size) {
 
102
    str = "" + str;
 
103
    return new Array(size - str.length + 1).join('0') + str;
 
104
};
 
105
 
 
106
/**
 
107
 * Converts a Date javascript object to a string using OpenERP's
 
108
 * datetime string format (exemple: '2011-12-01 15:12:35').
 
109
 * 
 
110
 * The time zone of the Date object is assumed to be the one of the
 
111
 * browser and it will be converted to UTC (standard for OpenERP 6.1).
 
112
 * 
 
113
 * @param {Date} obj
 
114
 * @returns {String} A string representing a datetime.
 
115
 */
 
116
openerp.web.datetime_to_str = function(obj) {
 
117
    if (!obj) {
 
118
        return false;
 
119
    }
 
120
    /*
 
121
    return zpad(obj.getUTCFullYear(),4) + "-" + zpad(obj.getUTCMonth() + 1,2) + "-"
 
122
         + zpad(obj.getUTCDate(),2) + " " + zpad(obj.getUTCHours(),2) + ":"
 
123
         + zpad(obj.getUTCMinutes(),2) + ":" + zpad(obj.getUTCSeconds(),2);
 
124
    */
 
125
    return zpad(obj.getFullYear(),4) + "-" + zpad(obj.getMonth() + 1,2) + "-"
 
126
        + zpad(obj.getDate(),2) + " " + zpad(obj.getHours(),2) + ":"
 
127
        + zpad(obj.getMinutes(),2) + ":" + zpad(obj.getSeconds(),2);
 
128
};
 
129
 
 
130
/**
 
131
 * Converts a Date javascript object to a string using OpenERP's
 
132
 * date string format (exemple: '2011-12-01').
 
133
 * 
 
134
 * As a date is not subject to time zones, we assume it should be
 
135
 * represented as a Date javascript object at 00:00:00 in the
 
136
 * time zone of the browser.
 
137
 * 
 
138
 * @param {Date} obj
 
139
 * @returns {String} A string representing a date.
 
140
 */
 
141
openerp.web.date_to_str = function(obj) {
 
142
    if (!obj) {
 
143
        return false;
 
144
    }
 
145
    return zpad(obj.getFullYear(),4) + "-" + zpad(obj.getMonth() + 1,2) + "-"
 
146
         + zpad(obj.getDate(),2);
 
147
};
 
148
 
 
149
/**
 
150
 * Converts a Date javascript object to a string using OpenERP's
 
151
 * time string format (exemple: '15:12:35').
 
152
 * 
 
153
 * The OpenERP times are supposed to always be naive times. We assume it is
 
154
 * represented using a javascript Date with a date 1 of January 1970 and a
 
155
 * time corresponding to the meant time in the browser's time zone.
 
156
 * 
 
157
 * @param {Date} obj
 
158
 * @returns {String} A string representing a time.
 
159
 */
 
160
openerp.web.time_to_str = function(obj) {
 
161
    if (!obj) {
 
162
        return false;
 
163
    }
 
164
    return zpad(obj.getHours(),2) + ":" + zpad(obj.getMinutes(),2) + ":"
 
165
         + zpad(obj.getSeconds(),2);
 
166
};
 
167
    
 
168
};