root / trunk / web / dojo / dojox / date / timezone.js @ 9
History | View | Annotate | Download (11.3 KB)
1 |
/*
|
---|---|
2 |
Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
3 |
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
4 |
see: http://dojotoolkit.org/license for details
|
5 |
*/
|
6 |
|
7 |
|
8 |
if(!dojo._hasResource["dojox.date.timezone"]){ |
9 |
dojo._hasResource["dojox.date.timezone"]=true; |
10 |
dojo.experimental("dojox.date.timezone");
|
11 |
dojo.provide("dojox.date.timezone");
|
12 |
dojo.require("dojo.date.locale");
|
13 |
(function(_1){
|
14 |
var _2=_1.config;
|
15 |
var _3=["africa","antarctica","asia","australasia","backward","etcetera","europe","northamerica","pacificnew","southamerica"]; |
16 |
var _4=1835,_5=2038; |
17 |
var _6={},_7={},_8={},_9={};
|
18 |
var _a=_2.timezoneFileBasePath||_1.moduleUrl("dojox.date","zoneinfo"); |
19 |
var _b=_2.timezoneLoadingScheme||"preloadAll"; |
20 |
var _c=_2.defaultZoneFile||((_b=="preloadAll")?_3:"northamerica"); |
21 |
_1._contentHandlers["olson-zoneinfo"]=function(_d){ |
22 |
var _e=_1._contentHandlers["text"](_d); |
23 |
var s=""; |
24 |
var _f=_e.split("\n"); |
25 |
var arr=[];
|
26 |
var _10=""; |
27 |
var _11=null; |
28 |
var _12=null; |
29 |
var ret={zones:{},rules:{}}; |
30 |
for(var i=0;i<_f.length;i++){ |
31 |
var l=_f[i];
|
32 |
if(l.match(/^\s/)){ |
33 |
l="Zone "+_11+l;
|
34 |
} |
35 |
l=l.split("#")[0]; |
36 |
if(l.length>3){ |
37 |
arr=l.split(/\s+/);
|
38 |
_10=arr.shift(); |
39 |
switch(_10){
|
40 |
case "Zone": |
41 |
_11=arr.shift(); |
42 |
if(arr[0]){ |
43 |
if(!ret.zones[_11]){
|
44 |
ret.zones[_11]=[]; |
45 |
} |
46 |
ret.zones[_11].push(arr); |
47 |
} |
48 |
break;
|
49 |
case "Rule": |
50 |
_12=arr.shift(); |
51 |
if(!ret.rules[_12]){
|
52 |
ret.rules[_12]=[]; |
53 |
} |
54 |
ret.rules[_12].push(arr); |
55 |
break;
|
56 |
case "Link": |
57 |
if(ret.zones[arr[1]]){ |
58 |
throw new Error("Error with Link "+arr[1]); |
59 |
} |
60 |
ret.zones[arr[1]]=arr[0]; |
61 |
break;
|
62 |
case "Leap": |
63 |
break;
|
64 |
default:
|
65 |
break;
|
66 |
} |
67 |
} |
68 |
} |
69 |
return ret;
|
70 |
}; |
71 |
function _13(_14){ |
72 |
_14=_14||{}; |
73 |
_7=_1.mixin(_7,_14.zones||{}); |
74 |
_9=_1.mixin(_9,_14.rules||{}); |
75 |
}; |
76 |
function _15(e){ |
77 |
console.error("Error loading zone file:",e);
|
78 |
throw e;
|
79 |
}; |
80 |
function _16(_17){ |
81 |
_6[_17]=true;
|
82 |
_1.xhrGet({url:_a+"/"+_17,sync:true,handleAs:"olson-zoneinfo",load:_13,error:_15}); |
83 |
}; |
84 |
var _18={"jan":0,"feb":1,"mar":2,"apr":3,"may":4,"jun":5,"jul":6,"aug":7,"sep":8,"oct":9,"nov":10,"dec":11},_19={"sun":0,"mon":1,"tue":2,"wed":3,"thu":4,"fri":5,"sat":6},_1a={"EST":"northamerica","MST":"northamerica","HST":"northamerica","EST5EDT":"northamerica","CST6CDT":"northamerica","MST7MDT":"northamerica","PST8PDT":"northamerica","America":"northamerica","Pacific":"australasia","Atlantic":"europe","Africa":"africa","Indian":"africa","Antarctica":"antarctica","Asia":"asia","Australia":"australasia","Europe":"europe","WET":"europe","CET":"europe","MET":"europe","EET":"europe"},_1b={"Pacific/Honolulu":"northamerica","Atlantic/Bermuda":"northamerica","Atlantic/Cape_Verde":"africa","Atlantic/St_Helena":"africa","Indian/Kerguelen":"antarctica","Indian/Chagos":"asia","Indian/Maldives":"asia","Indian/Christmas":"australasia","Indian/Cocos":"australasia","America/Danmarkshavn":"europe","America/Scoresbysund":"europe","America/Godthab":"europe","America/Thule":"europe","Asia/Yekaterinburg":"europe","Asia/Omsk":"europe","Asia/Novosibirsk":"europe","Asia/Krasnoyarsk":"europe","Asia/Irkutsk":"europe","Asia/Yakutsk":"europe","Asia/Vladivostok":"europe","Asia/Sakhalin":"europe","Asia/Magadan":"europe","Asia/Kamchatka":"europe","Asia/Anadyr":"europe","Africa/Ceuta":"europe","America/Argentina/Buenos_Aires":"southamerica","America/Argentina/Cordoba":"southamerica","America/Argentina/Tucuman":"southamerica","America/Argentina/La_Rioja":"southamerica","America/Argentina/San_Juan":"southamerica","America/Argentina/Jujuy":"southamerica","America/Argentina/Catamarca":"southamerica","America/Argentina/Mendoza":"southamerica","America/Argentina/Rio_Gallegos":"southamerica","America/Argentina/Ushuaia":"southamerica","America/Aruba":"southamerica","America/La_Paz":"southamerica","America/Noronha":"southamerica","America/Belem":"southamerica","America/Fortaleza":"southamerica","America/Recife":"southamerica","America/Araguaina":"southamerica","America/Maceio":"southamerica","America/Bahia":"southamerica","America/Sao_Paulo":"southamerica","America/Campo_Grande":"southamerica","America/Cuiaba":"southamerica","America/Porto_Velho":"southamerica","America/Boa_Vista":"southamerica","America/Manaus":"southamerica","America/Eirunepe":"southamerica","America/Rio_Branco":"southamerica","America/Santiago":"southamerica","Pacific/Easter":"southamerica","America/Bogota":"southamerica","America/Curacao":"southamerica","America/Guayaquil":"southamerica","Pacific/Galapagos":"southamerica","Atlantic/Stanley":"southamerica","America/Cayenne":"southamerica","America/Guyana":"southamerica","America/Asuncion":"southamerica","America/Lima":"southamerica","Atlantic/South_Georgia":"southamerica","America/Paramaribo":"southamerica","America/Port_of_Spain":"southamerica","America/Montevideo":"southamerica","America/Caracas":"southamerica"},_1c={"US":"S","Chatham":"S","NZ":"S","NT_YK":"S","Edm":"S","Salv":"S","Canada":"S","StJohns":"S","TC":"S","Guat":"S","Mexico":"S","Haiti":"S","Barb":"S","Belize":"S","CR":"S","Moncton":"S","Swift":"S","Hond":"S","Thule":"S","NZAQ":"S","Zion":"S","ROK":"S","PRC":"S","Taiwan":"S","Ghana":"GMT","SL":"WAT","Chicago":"S","Detroit":"S","Vanc":"S","Denver":"S","Halifax":"S","Cuba":"S","Indianapolis":"S","Starke":"S","Marengo":"S","Pike":"S","Perry":"S","Vincennes":"S","Pulaski":"S","Louisville":"S","CA":"S","Nic":"S","Menominee":"S","Mont":"S","Bahamas":"S","NYC":"S","Regina":"S","Resolute":"ES","DR":"S","Toronto":"S","Winn":"S"}; |
85 |
function _1d(t){ |
86 |
throw new Error("Timezone \""+t+"\" is either incorrect, or not loaded in the timezone registry."); |
87 |
}; |
88 |
function _1e(tz){ |
89 |
var ret=_1b[tz];
|
90 |
if(!ret){
|
91 |
var reg=tz.split("/")[0]; |
92 |
ret=_1a[reg]; |
93 |
if(!ret){
|
94 |
var _1f=_7[tz];
|
95 |
if(typeof _1f=="string"){ |
96 |
return _1e(_1f);
|
97 |
}else{
|
98 |
if(!_6.backward){
|
99 |
_16("backward");
|
100 |
return _1e(tz);
|
101 |
}else{
|
102 |
_1d(tz); |
103 |
} |
104 |
} |
105 |
} |
106 |
} |
107 |
return ret;
|
108 |
}; |
109 |
function _20(str){ |
110 |
var pat=/(\d+)(?::0*(\d*))?(?::0*(\d*))?([su])?$/; |
111 |
var hms=str.match(pat);
|
112 |
if(!hms){
|
113 |
return null; |
114 |
} |
115 |
hms[1]=parseInt(hms[1],10); |
116 |
hms[2]=hms[2]?parseInt(hms[2],10):0; |
117 |
hms[3]=hms[3]?parseInt(hms[3],10):0; |
118 |
return hms;
|
119 |
}; |
120 |
function _21(y,m,d,h,mn,s,off){ |
121 |
return Date.UTC(y,m,d,h,mn,s)+((off||0)*60*1000); |
122 |
}; |
123 |
function _22(m){ |
124 |
return _18[m.substr(0,3).toLowerCase()]; |
125 |
}; |
126 |
function _23(str){ |
127 |
var off=_20(str);
|
128 |
if(off===null){ |
129 |
return 0; |
130 |
} |
131 |
var adj=str.indexOf("-")===0?-1:1; |
132 |
off=adj*(((off[1]*60+off[2])*60+off[3])*1000); |
133 |
return -off/60/1000; |
134 |
}; |
135 |
function _24(_25,_26,off){ |
136 |
var _27=_22(_25[3]),day=_25[4],_28=_20(_25[5]); |
137 |
if(_28[4]=="u"){ |
138 |
off=0;
|
139 |
} |
140 |
var d,_29,_2a;
|
141 |
if(isNaN(day)){
|
142 |
if(day.substr(0,4)=="last"){ |
143 |
day=_19[day.substr(4,3).toLowerCase()]; |
144 |
d=new Date(_21(_26,_27+1,1,_28[1]-24,_28[2],_28[3],off)); |
145 |
_29=_1.date.add(d,"minute",-off).getUTCDay();
|
146 |
_2a=(day>_29)?(day-_29-7):(day-_29);
|
147 |
if(_2a!==0){ |
148 |
d=_1.date.add(d,"hour",_2a*24); |
149 |
} |
150 |
return d;
|
151 |
}else{
|
152 |
day=_19[day.substr(0,3).toLowerCase()]; |
153 |
if(day!="undefined"){ |
154 |
if(_25[4].substr(3,2)==">="){ |
155 |
d=new Date(_21(_26,_27,parseInt(_25[4].substr(5),10),_28[1],_28[2],_28[3],off)); |
156 |
_29=_1.date.add(d,"minute",-off).getUTCDay();
|
157 |
_2a=(day<_29)?(day-_29+7):(day-_29);
|
158 |
if(_2a!==0){ |
159 |
d=_1.date.add(d,"hour",_2a*24); |
160 |
} |
161 |
return d;
|
162 |
}else{
|
163 |
if(day.substr(3,2)=="<="){ |
164 |
d=new Date(_21(_26,_27,parseInt(_25[4].substr(5),10),_28[1],_28[2],_28[3],off)); |
165 |
_29=_1.date.add(d,"minute",-off).getUTCDay();
|
166 |
_2a=(day>_29)?(day-_29-7):(day-_29);
|
167 |
if(_2a!==0){ |
168 |
d=_1.date.add(d,"hour",_2a*24); |
169 |
} |
170 |
return d;
|
171 |
} |
172 |
} |
173 |
} |
174 |
} |
175 |
}else{
|
176 |
d=new Date(_21(_26,_27,parseInt(day,10),_28[1],_28[2],_28[3],off)); |
177 |
return d;
|
178 |
} |
179 |
return null; |
180 |
}; |
181 |
function _2b(_2c,_2d){ |
182 |
var _2e=[];
|
183 |
_1.forEach(_9[_2c[1]]||[],function(r){ |
184 |
for(var i=0;i<2;i++){ |
185 |
switch(r[i]){
|
186 |
case "min": |
187 |
r[i]=_4; |
188 |
break;
|
189 |
case "max": |
190 |
r[i]=_5; |
191 |
break;
|
192 |
case "only": |
193 |
break;
|
194 |
default:
|
195 |
r[i]=parseInt(r[i],10);
|
196 |
if(isNaN(r[i])){
|
197 |
throw new Error("Invalid year found on rule"); |
198 |
} |
199 |
break;
|
200 |
} |
201 |
} |
202 |
if(typeof r[6]=="string"){ |
203 |
r[6]=_23(r[6]); |
204 |
} |
205 |
if((r[0]<=_2d&&r[1]>=_2d)||(r[0]==_2d&&r[1]=="only")){ |
206 |
_2e.push({r:r,d:_24(r,_2d,_2c[0])}); |
207 |
} |
208 |
}); |
209 |
return _2e;
|
210 |
}; |
211 |
function _2f(tz,_30){ |
212 |
var zr=_8[tz]=[];
|
213 |
for(var i=0;i<_30.length;i++){ |
214 |
var z=_30[i];
|
215 |
var r=zr[i]=[];
|
216 |
var _31=null; |
217 |
var _32=null; |
218 |
var _33=[];
|
219 |
if(typeof z[0]=="string"){ |
220 |
z[0]=_23(z[0]); |
221 |
} |
222 |
if(i===0){ |
223 |
r[0]=Date.UTC(_4,0,1,0,0,0,0); |
224 |
}else{
|
225 |
r[0]=zr[i-1][1]; |
226 |
_31=_30[i-1];
|
227 |
_32=zr[i-1];
|
228 |
_33=_32[2];
|
229 |
} |
230 |
var _34=new Date(r[0]).getUTCFullYear(); |
231 |
var _35=z[3]?parseInt(z[3],10):_5; |
232 |
var rlz=[];
|
233 |
var j;
|
234 |
for(j=_34;j<=_35;j++){
|
235 |
rlz=rlz.concat(_2b(z,j)); |
236 |
} |
237 |
rlz.sort(function(a,b){
|
238 |
return _1.date.compare(a.d,b.d);
|
239 |
}); |
240 |
var rl;
|
241 |
for(j=0,rl;(rl=rlz[j]);j++){ |
242 |
var _36=j>0?rlz[j-1]:null; |
243 |
if(rl.r[5].indexOf("u")<0&&rl.r[5].indexOf("s")<0){ |
244 |
if(j===0&&i>0){ |
245 |
if(_33.length){
|
246 |
rl.d=_1.date.add(rl.d,"minute",_33[_33.length-1].r[6]); |
247 |
}else{
|
248 |
if(_1.date.compare(new Date(_32[1]),rl.d,"date")===0){ |
249 |
rl.d=new Date(_32[1]); |
250 |
}else{
|
251 |
rl.d=_1.date.add(rl.d,"minute",_23(_31[1])); |
252 |
} |
253 |
} |
254 |
}else{
|
255 |
if(j>0){ |
256 |
rl.d=_1.date.add(rl.d,"minute",_36.r[6]); |
257 |
} |
258 |
} |
259 |
} |
260 |
} |
261 |
r[2]=rlz;
|
262 |
if(!z[3]){ |
263 |
r[1]=Date.UTC(_5,11,31,23,59,59,999); |
264 |
}else{
|
265 |
var _37=parseInt(z[3],10),_38=_22(z[4]||"Jan"),day=parseInt(z[5]||"1",10),_39=_20(z[6]||"0"); |
266 |
var _3a=r[1]=_21(_37,_38,day,_39[1],_39[2],_39[3],((_39[4]=="u")?0:z[0])); |
267 |
if(isNaN(_3a)){
|
268 |
_3a=r[1]=_24([0,0,0,z[4],z[5],z[6]||"0"],_37,((_39[4]=="u")?0:z[0])).getTime(); |
269 |
} |
270 |
var _3b=_1.filter(rlz,function(rl,idx){ |
271 |
var o=idx>0?rlz[idx-1].r[6]*60*1000:0; |
272 |
return (rl.d.getTime()<_3a+o);
|
273 |
}); |
274 |
if(_39[4]!="u"&&_39[4]!="s"){ |
275 |
if(_3b.length){
|
276 |
r[1]+=_3b[_3b.length-1].r[6]*60*1000; |
277 |
}else{
|
278 |
r[1]+=_23(z[1])*60*1000; |
279 |
} |
280 |
} |
281 |
} |
282 |
} |
283 |
}; |
284 |
function _3c(dt,tz){ |
285 |
var t=tz;
|
286 |
var _3d=_7[t];
|
287 |
while(typeof _3d=="string"){ |
288 |
t=_3d; |
289 |
_3d=_7[t]; |
290 |
} |
291 |
if(!_3d){
|
292 |
if(!_6.backward){
|
293 |
var _3e=_16("backward",true); |
294 |
return _3c(dt,tz);
|
295 |
} |
296 |
_1d(t); |
297 |
} |
298 |
if(!_8[tz]){
|
299 |
_2f(tz,_3d); |
300 |
} |
301 |
var _3f=_8[tz];
|
302 |
var tm=dt.getTime();
|
303 |
for(var i=0,r;(r=_3f[i]);i++){ |
304 |
if(tm>=r[0]&&tm<r[1]){ |
305 |
return {zone:_3d[i],range:_3f[i],idx:i}; |
306 |
} |
307 |
} |
308 |
throw new Error("No Zone found for \""+tz+"\" on "+dt); |
309 |
}; |
310 |
function _40(dt,_41){ |
311 |
var _42=-1; |
312 |
var _43=_41.range[2]||[]; |
313 |
var tsp=dt.getTime();
|
314 |
var zr=_41.range;
|
315 |
for(var i=0,r;(r=_43[i]);i++){ |
316 |
if(tsp>=r.d.getTime()){
|
317 |
_42=i; |
318 |
} |
319 |
} |
320 |
if(_42>=0){ |
321 |
return _43[_42].r;
|
322 |
} |
323 |
return null; |
324 |
}; |
325 |
function _44(tz,_45,_46){ |
326 |
var res;
|
327 |
var _47=_45.zone;
|
328 |
var _48=_47[2]; |
329 |
if(_48.indexOf("%s")>-1){ |
330 |
var _49;
|
331 |
if(_46){
|
332 |
_49=_46[7];
|
333 |
if(_49=="-"){ |
334 |
_49="";
|
335 |
} |
336 |
}else{
|
337 |
if(_47[1] in _1c){ |
338 |
_49=_1c[_47[1]];
|
339 |
}else{
|
340 |
if(_45.idx>0){ |
341 |
var pz=_7[tz][_45.idx-1]; |
342 |
var pb=pz[2]; |
343 |
if(pb.indexOf("%s")<0){ |
344 |
if(_48.replace("%s","S")==pb){ |
345 |
_49="S";
|
346 |
}else{
|
347 |
_49="";
|
348 |
} |
349 |
}else{
|
350 |
_49="";
|
351 |
} |
352 |
}else{
|
353 |
_49="";
|
354 |
} |
355 |
} |
356 |
} |
357 |
res=_48.replace("%s",_49);
|
358 |
}else{
|
359 |
if(_48.indexOf("/")>-1){ |
360 |
var bs=_48.split("/"); |
361 |
if(_46){
|
362 |
res=bs[_46[6]===0?0:1]; |
363 |
}else{
|
364 |
res=bs[0];
|
365 |
} |
366 |
}else{
|
367 |
res=_48; |
368 |
} |
369 |
} |
370 |
return res;
|
371 |
}; |
372 |
_1.setObject("dojox.date.timezone",{getTzInfo:function(dt,tz){ |
373 |
if(_b=="lazyLoad"){ |
374 |
var _4a=_1e(tz);
|
375 |
if(!_4a){
|
376 |
throw new Error("Not a valid timezone ID."); |
377 |
}else{
|
378 |
if(!_6[_4a]){
|
379 |
_16(_4a); |
380 |
} |
381 |
} |
382 |
} |
383 |
var _4b=_3c(dt,tz);
|
384 |
var off=_4b.zone[0]; |
385 |
var _4c=_40(dt,_4b);
|
386 |
if(_4c){
|
387 |
off+=_4c[6];
|
388 |
}else{
|
389 |
if(_9[_4b.zone[1]]&&_4b.idx>0){ |
390 |
off+=_23(_7[tz][_4b.idx-1][1]); |
391 |
}else{
|
392 |
off+=_23(_4b.zone[1]);
|
393 |
} |
394 |
} |
395 |
var _4d=_44(tz,_4b,_4c);
|
396 |
return {tzOffset:off,tzAbbr:_4d}; |
397 |
},getAllZones:function(){ |
398 |
var arr=[];
|
399 |
for(var z in _7){ |
400 |
arr.push(z); |
401 |
} |
402 |
arr.sort(); |
403 |
return arr;
|
404 |
}}); |
405 |
if(typeof _c=="string"&&_c){ |
406 |
_c=[_c]; |
407 |
} |
408 |
if(_1.isArray(_c)){
|
409 |
_1.forEach(_c,function(f){
|
410 |
_16(f); |
411 |
}); |
412 |
} |
413 |
var _4e=_1.date.locale.format,_4f=_1.date.locale._getZone;
|
414 |
_1.date.locale.format=function(_50,_51){ |
415 |
_51=_51||{}; |
416 |
if(_51.timezone&&!_51._tzInfo){
|
417 |
_51._tzInfo=dojox.date.timezone.getTzInfo(_50,_51.timezone); |
418 |
} |
419 |
if(_51._tzInfo){
|
420 |
var _52=_50.getTimezoneOffset()-_51._tzInfo.tzOffset;
|
421 |
_50=new Date(_50.getTime()+(_52*60*1000)); |
422 |
} |
423 |
return _4e.call(this,_50,_51); |
424 |
}; |
425 |
_1.date.locale._getZone=function(_53,_54,_55){ |
426 |
if(_55._tzInfo){
|
427 |
return _54?_55._tzInfo.tzAbbr:_55._tzInfo.tzOffset;
|
428 |
} |
429 |
return _4f.call(this,_53,_54,_55); |
430 |
}; |
431 |
})(dojo); |
432 |
} |