testing/mochitest/tests/MochiKit-1.4.2/MochiKit/DateTime.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:51bf8e9c3a31
1 /***
2
3 MochiKit.DateTime 1.4.2
4
5 See <http://mochikit.com/> for documentation, downloads, license, etc.
6
7 (c) 2005 Bob Ippolito. All rights Reserved.
8
9 ***/
10
11 MochiKit.Base._deps('DateTime', ['Base']);
12
13 MochiKit.DateTime.NAME = "MochiKit.DateTime";
14 MochiKit.DateTime.VERSION = "1.4.2";
15 MochiKit.DateTime.__repr__ = function () {
16 return "[" + this.NAME + " " + this.VERSION + "]";
17 };
18 MochiKit.DateTime.toString = function () {
19 return this.__repr__();
20 };
21
22 /** @id MochiKit.DateTime.isoDate */
23 MochiKit.DateTime.isoDate = function (str) {
24 str = str + "";
25 if (typeof(str) != "string" || str.length === 0) {
26 return null;
27 }
28 var iso = str.split('-');
29 if (iso.length === 0) {
30 return null;
31 }
32 var date = new Date(iso[0], iso[1] - 1, iso[2]);
33 date.setFullYear(iso[0]);
34 date.setMonth(iso[1] - 1);
35 date.setDate(iso[2]);
36 return date;
37 };
38
39 MochiKit.DateTime._isoRegexp = /(\d{4,})(?:-(\d{1,2})(?:-(\d{1,2})(?:[T ](\d{1,2}):(\d{1,2})(?::(\d{1,2})(?:\.(\d+))?)?(?:(Z)|([+-])(\d{1,2})(?::(\d{1,2}))?)?)?)?)?/;
40
41 /** @id MochiKit.DateTime.isoTimestamp */
42 MochiKit.DateTime.isoTimestamp = function (str) {
43 str = str + "";
44 if (typeof(str) != "string" || str.length === 0) {
45 return null;
46 }
47 var res = str.match(MochiKit.DateTime._isoRegexp);
48 if (typeof(res) == "undefined" || res === null) {
49 return null;
50 }
51 var year, month, day, hour, min, sec, msec;
52 year = parseInt(res[1], 10);
53 if (typeof(res[2]) == "undefined" || res[2] === '') {
54 return new Date(year);
55 }
56 month = parseInt(res[2], 10) - 1;
57 day = parseInt(res[3], 10);
58 if (typeof(res[4]) == "undefined" || res[4] === '') {
59 return new Date(year, month, day);
60 }
61 hour = parseInt(res[4], 10);
62 min = parseInt(res[5], 10);
63 sec = (typeof(res[6]) != "undefined" && res[6] !== '') ? parseInt(res[6], 10) : 0;
64 if (typeof(res[7]) != "undefined" && res[7] !== '') {
65 msec = Math.round(1000.0 * parseFloat("0." + res[7]));
66 } else {
67 msec = 0;
68 }
69 if ((typeof(res[8]) == "undefined" || res[8] === '') && (typeof(res[9]) == "undefined" || res[9] === '')) {
70 return new Date(year, month, day, hour, min, sec, msec);
71 }
72 var ofs;
73 if (typeof(res[9]) != "undefined" && res[9] !== '') {
74 ofs = parseInt(res[10], 10) * 3600000;
75 if (typeof(res[11]) != "undefined" && res[11] !== '') {
76 ofs += parseInt(res[11], 10) * 60000;
77 }
78 if (res[9] == "-") {
79 ofs = -ofs;
80 }
81 } else {
82 ofs = 0;
83 }
84 return new Date(Date.UTC(year, month, day, hour, min, sec, msec) - ofs);
85 };
86
87 /** @id MochiKit.DateTime.toISOTime */
88 MochiKit.DateTime.toISOTime = function (date, realISO/* = false */) {
89 if (typeof(date) == "undefined" || date === null) {
90 return null;
91 }
92 var hh = date.getHours();
93 var mm = date.getMinutes();
94 var ss = date.getSeconds();
95 var lst = [
96 ((realISO && (hh < 10)) ? "0" + hh : hh),
97 ((mm < 10) ? "0" + mm : mm),
98 ((ss < 10) ? "0" + ss : ss)
99 ];
100 return lst.join(":");
101 };
102
103 /** @id MochiKit.DateTime.toISOTimeStamp */
104 MochiKit.DateTime.toISOTimestamp = function (date, realISO/* = false*/) {
105 if (typeof(date) == "undefined" || date === null) {
106 return null;
107 }
108 var sep = realISO ? "T" : " ";
109 var foot = realISO ? "Z" : "";
110 if (realISO) {
111 date = new Date(date.getTime() + (date.getTimezoneOffset() * 60000));
112 }
113 return MochiKit.DateTime.toISODate(date) + sep + MochiKit.DateTime.toISOTime(date, realISO) + foot;
114 };
115
116 /** @id MochiKit.DateTime.toISODate */
117 MochiKit.DateTime.toISODate = function (date) {
118 if (typeof(date) == "undefined" || date === null) {
119 return null;
120 }
121 var _padTwo = MochiKit.DateTime._padTwo;
122 var _padFour = MochiKit.DateTime._padFour;
123 return [
124 _padFour(date.getFullYear()),
125 _padTwo(date.getMonth() + 1),
126 _padTwo(date.getDate())
127 ].join("-");
128 };
129
130 /** @id MochiKit.DateTime.americanDate */
131 MochiKit.DateTime.americanDate = function (d) {
132 d = d + "";
133 if (typeof(d) != "string" || d.length === 0) {
134 return null;
135 }
136 var a = d.split('/');
137 return new Date(a[2], a[0] - 1, a[1]);
138 };
139
140 MochiKit.DateTime._padTwo = function (n) {
141 return (n > 9) ? n : "0" + n;
142 };
143
144 MochiKit.DateTime._padFour = function(n) {
145 switch(n.toString().length) {
146 case 1: return "000" + n; break;
147 case 2: return "00" + n; break;
148 case 3: return "0" + n; break;
149 case 4:
150 default:
151 return n;
152 }
153 };
154
155 /** @id MochiKit.DateTime.toPaddedAmericanDate */
156 MochiKit.DateTime.toPaddedAmericanDate = function (d) {
157 if (typeof(d) == "undefined" || d === null) {
158 return null;
159 }
160 var _padTwo = MochiKit.DateTime._padTwo;
161 return [
162 _padTwo(d.getMonth() + 1),
163 _padTwo(d.getDate()),
164 d.getFullYear()
165 ].join('/');
166 };
167
168 /** @id MochiKit.DateTime.toAmericanDate */
169 MochiKit.DateTime.toAmericanDate = function (d) {
170 if (typeof(d) == "undefined" || d === null) {
171 return null;
172 }
173 return [d.getMonth() + 1, d.getDate(), d.getFullYear()].join('/');
174 };
175
176 MochiKit.DateTime.EXPORT = [
177 "isoDate",
178 "isoTimestamp",
179 "toISOTime",
180 "toISOTimestamp",
181 "toISODate",
182 "americanDate",
183 "toPaddedAmericanDate",
184 "toAmericanDate"
185 ];
186
187 MochiKit.DateTime.EXPORT_OK = [];
188 MochiKit.DateTime.EXPORT_TAGS = {
189 ":common": MochiKit.DateTime.EXPORT,
190 ":all": MochiKit.DateTime.EXPORT
191 };
192
193 MochiKit.DateTime.__new__ = function () {
194 // MochiKit.Base.nameFunctions(this);
195 var base = this.NAME + ".";
196 for (var k in this) {
197 var o = this[k];
198 if (typeof(o) == 'function' && typeof(o.NAME) == 'undefined') {
199 try {
200 o.NAME = base + k;
201 } catch (e) {
202 // pass
203 }
204 }
205 }
206 };
207
208 MochiKit.DateTime.__new__();
209
210 if (typeof(MochiKit.Base) != "undefined") {
211 MochiKit.Base._exportSymbols(this, MochiKit.DateTime);
212 } else {
213 (function (globals, module) {
214 if ((typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined')
215 || (MochiKit.__export__ === false)) {
216 var all = module.EXPORT_TAGS[":all"];
217 for (var i = 0; i < all.length; i++) {
218 globals[all[i]] = module[all[i]];
219 }
220 }
221 })(this, MochiKit.DateTime);
222 }

mercurial