2150 |
mathias |
1 |
if(!dojo._hasResource["dojo.date.locale"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojo.date.locale"] = true;
|
|
|
3 |
dojo.provide("dojo.date.locale");
|
|
|
4 |
|
|
|
5 |
// Localization methods for Date. Honor local customs using locale-dependent dojo.cldr data.
|
|
|
6 |
|
|
|
7 |
dojo.require("dojo.date");
|
|
|
8 |
dojo.require("dojo.cldr.supplemental");
|
|
|
9 |
dojo.require("dojo.regexp");
|
|
|
10 |
dojo.require("dojo.string");
|
|
|
11 |
dojo.require("dojo.i18n");
|
|
|
12 |
|
|
|
13 |
// Load the bundles containing localization information for
|
|
|
14 |
// names and formats
|
|
|
15 |
dojo.requireLocalization("dojo.cldr", "gregorian", null, "ko,zh-cn,zh,ja,en,it-it,en-ca,en-au,it,en-gb,es-es,fr,pt,ROOT,ko-kr,es,de,pt-br");
|
|
|
16 |
|
|
|
17 |
//NOTE: Everything in this module assumes Gregorian calendars.
|
|
|
18 |
// Other calendars will be implemented in separate modules.
|
|
|
19 |
|
|
|
20 |
(function(){
|
|
|
21 |
// Format a pattern without literals
|
|
|
22 |
function formatPattern(dateObject, bundle, pattern){
|
|
|
23 |
return pattern.replace(/([a-z])\1*/ig, function(match){
|
|
|
24 |
var s;
|
|
|
25 |
var c = match.charAt(0);
|
|
|
26 |
var l = match.length;
|
|
|
27 |
var pad;
|
|
|
28 |
var widthList = ["abbr", "wide", "narrow"];
|
|
|
29 |
switch(c){
|
|
|
30 |
case 'G':
|
|
|
31 |
s = bundle[(l < 4) ? "eraAbbr" : "eraNames"][dateObject.getFullYear() < 0 ? 0 : 1];
|
|
|
32 |
break;
|
|
|
33 |
case 'y':
|
|
|
34 |
s = dateObject.getFullYear();
|
|
|
35 |
switch(l){
|
|
|
36 |
case 1:
|
|
|
37 |
break;
|
|
|
38 |
case 2:
|
|
|
39 |
s = String(s); s = s.substr(s.length - 2);
|
|
|
40 |
break;
|
|
|
41 |
default:
|
|
|
42 |
pad = true;
|
|
|
43 |
}
|
|
|
44 |
break;
|
|
|
45 |
case 'Q':
|
|
|
46 |
case 'q':
|
|
|
47 |
s = Math.ceil((dateObject.getMonth()+1)/3);
|
|
|
48 |
// switch(l){
|
|
|
49 |
// case 1: case 2:
|
|
|
50 |
pad = true;
|
|
|
51 |
// break;
|
|
|
52 |
// case 3: case 4: // unimplemented
|
|
|
53 |
// }
|
|
|
54 |
break;
|
|
|
55 |
case 'M':
|
|
|
56 |
case 'L':
|
|
|
57 |
var m = dateObject.getMonth();
|
|
|
58 |
var width;
|
|
|
59 |
switch(l){
|
|
|
60 |
case 1: case 2:
|
|
|
61 |
s = m+1; pad = true;
|
|
|
62 |
break;
|
|
|
63 |
case 3: case 4: case 5:
|
|
|
64 |
width = widthList[l-3];
|
|
|
65 |
break;
|
|
|
66 |
}
|
|
|
67 |
if(width){
|
|
|
68 |
var type = (c == "L") ? "standalone" : "format";
|
|
|
69 |
var prop = ["months",type,width].join("-");
|
|
|
70 |
s = bundle[prop][m];
|
|
|
71 |
}
|
|
|
72 |
break;
|
|
|
73 |
case 'w':
|
|
|
74 |
var firstDay = 0;
|
|
|
75 |
s = dojo.date.locale._getWeekOfYear(dateObject, firstDay); pad = true;
|
|
|
76 |
break;
|
|
|
77 |
case 'd':
|
|
|
78 |
s = dateObject.getDate(); pad = true;
|
|
|
79 |
break;
|
|
|
80 |
case 'D':
|
|
|
81 |
s = dojo.date.locale._getDayOfYear(dateObject); pad = true;
|
|
|
82 |
break;
|
|
|
83 |
case 'E':
|
|
|
84 |
case 'e':
|
|
|
85 |
case 'c': // REVIEW: don't see this in the spec?
|
|
|
86 |
var d = dateObject.getDay();
|
|
|
87 |
var width;
|
|
|
88 |
switch(l){
|
|
|
89 |
case 1: case 2:
|
|
|
90 |
if(c == 'e'){
|
|
|
91 |
var first = dojo.cldr.supplemental.getFirstDayOfWeek(options.locale);
|
|
|
92 |
d = (d-first+7)%7;
|
|
|
93 |
}
|
|
|
94 |
if(c != 'c'){
|
|
|
95 |
s = d+1; pad = true;
|
|
|
96 |
break;
|
|
|
97 |
}
|
|
|
98 |
// else fallthrough...
|
|
|
99 |
case 3: case 4: case 5:
|
|
|
100 |
width = widthList[l-3];
|
|
|
101 |
break;
|
|
|
102 |
}
|
|
|
103 |
if(width){
|
|
|
104 |
var type = (c == "c") ? "standalone" : "format";
|
|
|
105 |
var prop = ["days",type,width].join("-");
|
|
|
106 |
s = bundle[prop][d];
|
|
|
107 |
}
|
|
|
108 |
break;
|
|
|
109 |
case 'a':
|
|
|
110 |
var timePeriod = (dateObject.getHours() < 12) ? 'am' : 'pm';
|
|
|
111 |
s = bundle[timePeriod];
|
|
|
112 |
break;
|
|
|
113 |
case 'h':
|
|
|
114 |
case 'H':
|
|
|
115 |
case 'K':
|
|
|
116 |
case 'k':
|
|
|
117 |
var h = dateObject.getHours();
|
|
|
118 |
// strange choices in the date format make it impossible to write this succinctly
|
|
|
119 |
switch (c) {
|
|
|
120 |
case 'h': // 1-12
|
|
|
121 |
s = (h % 12) || 12;
|
|
|
122 |
break;
|
|
|
123 |
case 'H': // 0-23
|
|
|
124 |
s = h;
|
|
|
125 |
break;
|
|
|
126 |
case 'K': // 0-11
|
|
|
127 |
s = (h % 12);
|
|
|
128 |
break;
|
|
|
129 |
case 'k': // 1-24
|
|
|
130 |
s = h || 24;
|
|
|
131 |
break;
|
|
|
132 |
}
|
|
|
133 |
pad = true;
|
|
|
134 |
break;
|
|
|
135 |
case 'm':
|
|
|
136 |
s = dateObject.getMinutes(); pad = true;
|
|
|
137 |
break;
|
|
|
138 |
case 's':
|
|
|
139 |
s = dateObject.getSeconds(); pad = true;
|
|
|
140 |
break;
|
|
|
141 |
case 'S':
|
|
|
142 |
s = Math.round(dateObject.getMilliseconds() * Math.pow(10, l-3));
|
|
|
143 |
break;
|
|
|
144 |
case 'v': // FIXME: don't know what this is. seems to be same as z?
|
|
|
145 |
case 'z':
|
|
|
146 |
// We only have one timezone to offer; the one from the browser
|
|
|
147 |
s = dojo.date.getTimezoneName(dateObject);
|
|
|
148 |
if(s){break;}
|
|
|
149 |
l=4;
|
|
|
150 |
// fallthrough... use GMT if tz not available
|
|
|
151 |
case 'Z':
|
|
|
152 |
var offset = dateObject.getTimezoneOffset();
|
|
|
153 |
var tz = [
|
|
|
154 |
(offset<=0 ? "+" : "-"),
|
|
|
155 |
dojo.string.pad(Math.floor(Math.abs(offset)/60), 2),
|
|
|
156 |
dojo.string.pad(Math.abs(offset)% 60, 2)
|
|
|
157 |
];
|
|
|
158 |
if(l==4){
|
|
|
159 |
tz.splice(0, 0, "GMT");
|
|
|
160 |
tz.splice(3, 0, ":");
|
|
|
161 |
}
|
|
|
162 |
s = tz.join("");
|
|
|
163 |
break;
|
|
|
164 |
// case 'Y': case 'u': case 'W': case 'F': case 'g': case 'A':
|
|
|
165 |
// console.debug(match+" modifier unimplemented");
|
|
|
166 |
default:
|
|
|
167 |
throw new Error("dojo.date.locale.format: invalid pattern char: "+pattern);
|
|
|
168 |
}
|
|
|
169 |
if(pad){ s = dojo.string.pad(s, l); }
|
|
|
170 |
return s;
|
|
|
171 |
});
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
dojo.date.locale.format = function(/*Date*/dateObject, /*Object?*/options){
|
|
|
175 |
// summary:
|
|
|
176 |
// Format a Date object as a String, using locale-specific settings.
|
|
|
177 |
//
|
|
|
178 |
// description:
|
|
|
179 |
// Create a string from a Date object using a known localized pattern.
|
|
|
180 |
// By default, this method formats both date and time from dateObject.
|
|
|
181 |
// Formatting patterns are chosen appropriate to the locale. Different
|
|
|
182 |
// formatting lengths may be chosen, with "full" used by default.
|
|
|
183 |
// Custom patterns may be used or registered with translations using
|
|
|
184 |
// the addCustomFormats method.
|
|
|
185 |
// Formatting patterns are implemented using the syntax described at
|
|
|
186 |
// http://www.unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns
|
|
|
187 |
//
|
|
|
188 |
// dateObject:
|
|
|
189 |
// the date and/or time to be formatted. If a time only is formatted,
|
|
|
190 |
// the values in the year, month, and day fields are irrelevant. The
|
|
|
191 |
// opposite is true when formatting only dates.
|
|
|
192 |
//
|
|
|
193 |
// options: object {selector: string, formatLength: string, datePattern: string, timePattern: string, locale: string}
|
|
|
194 |
// selector- choice of 'time','date' (default: date and time)
|
|
|
195 |
// formatLength- choice of long, short, medium or full (plus any custom additions). Defaults to 'short'
|
|
|
196 |
// datePattern,timePattern- override pattern with this string
|
|
|
197 |
// am,pm- override strings for am/pm in times
|
|
|
198 |
// locale- override the locale used to determine formatting rules
|
|
|
199 |
|
|
|
200 |
options = options || {};
|
|
|
201 |
|
|
|
202 |
var locale = dojo.i18n.normalizeLocale(options.locale);
|
|
|
203 |
var formatLength = options.formatLength || 'short';
|
|
|
204 |
var bundle = dojo.date.locale._getGregorianBundle(locale);
|
|
|
205 |
var str = [];
|
|
|
206 |
var sauce = dojo.hitch(this, formatPattern, dateObject, bundle);
|
|
|
207 |
if(options.selector == "year"){
|
|
|
208 |
// Special case as this is not yet driven by CLDR data
|
|
|
209 |
var year = dateObject.getFullYear();
|
|
|
210 |
if(locale.match(/^zh|^ja/)){
|
|
|
211 |
year += "\u5E74";
|
|
|
212 |
}
|
|
|
213 |
return year;
|
|
|
214 |
}
|
|
|
215 |
if(options.selector != "time"){
|
|
|
216 |
var datePattern = options.datePattern || bundle["dateFormat-"+formatLength];
|
|
|
217 |
if(datePattern){str.push(_processPattern(datePattern, sauce));}
|
|
|
218 |
}
|
|
|
219 |
if(options.selector != "date"){
|
|
|
220 |
var timePattern = options.timePattern || bundle["timeFormat-"+formatLength];
|
|
|
221 |
if(timePattern){str.push(_processPattern(timePattern, sauce));}
|
|
|
222 |
}
|
|
|
223 |
var result = str.join(" "); //TODO: use locale-specific pattern to assemble date + time
|
|
|
224 |
return result; // String
|
|
|
225 |
};
|
|
|
226 |
|
|
|
227 |
dojo.date.locale.regexp = function(/*Object?*/options){
|
|
|
228 |
// summary:
|
|
|
229 |
// Builds the regular needed to parse a localized date
|
|
|
230 |
//
|
|
|
231 |
// options: object {selector: string, formatLength: string, datePattern: string, timePattern: string, locale: string, strict: boolean}
|
|
|
232 |
// selector- choice of 'time', 'date' (default: date and time)
|
|
|
233 |
// formatLength- choice of long, short, medium or full (plus any custom additions). Defaults to 'short'
|
|
|
234 |
// datePattern,timePattern- override pattern with this string
|
|
|
235 |
// locale- override the locale used to determine formatting rules
|
|
|
236 |
|
|
|
237 |
return dojo.date.locale._parseInfo(options).regexp; // String
|
|
|
238 |
};
|
|
|
239 |
|
|
|
240 |
dojo.date.locale._parseInfo = function(/*Object?*/options){
|
|
|
241 |
options = options || {};
|
|
|
242 |
var locale = dojo.i18n.normalizeLocale(options.locale);
|
|
|
243 |
var bundle = dojo.date.locale._getGregorianBundle(locale);
|
|
|
244 |
var formatLength = options.formatLength || 'short';
|
|
|
245 |
var datePattern = options.datePattern || bundle["dateFormat-" + formatLength];
|
|
|
246 |
var timePattern = options.timePattern || bundle["timeFormat-" + formatLength];
|
|
|
247 |
var pattern;
|
|
|
248 |
if(options.selector == 'date'){
|
|
|
249 |
pattern = datePattern;
|
|
|
250 |
}else if(options.selector == 'time'){
|
|
|
251 |
pattern = timePattern;
|
|
|
252 |
}else{
|
|
|
253 |
pattern = datePattern + ' ' + timePattern; //TODO: use locale-specific pattern to assemble date + time
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
var tokens = [];
|
|
|
257 |
var re = _processPattern(pattern, dojo.hitch(this, _buildDateTimeRE, tokens, bundle, options));
|
|
|
258 |
return {regexp: re, tokens: tokens, bundle: bundle};
|
|
|
259 |
};
|
|
|
260 |
|
|
|
261 |
dojo.date.locale.parse = function(/*String*/value, /*Object?*/options){
|
|
|
262 |
// summary:
|
|
|
263 |
// Convert a properly formatted string to a primitive Date object,
|
|
|
264 |
// using locale-specific settings.
|
|
|
265 |
//
|
|
|
266 |
// description:
|
|
|
267 |
// Create a Date object from a string using a known localized pattern.
|
|
|
268 |
// By default, this method parses looking for both date and time in the string.
|
|
|
269 |
// Formatting patterns are chosen appropriate to the locale. Different
|
|
|
270 |
// formatting lengths may be chosen, with "full" used by default.
|
|
|
271 |
// Custom patterns may be used or registered with translations using
|
|
|
272 |
// the addCustomFormats method.
|
|
|
273 |
// Formatting patterns are implemented using the syntax described at
|
|
|
274 |
// http://www.unicode.org/reports/tr35/#Date_Format_Patterns
|
|
|
275 |
//
|
|
|
276 |
// value:
|
|
|
277 |
// A string representation of a date
|
|
|
278 |
//
|
|
|
279 |
// options: object {selector: string, formatLength: string, datePattern: string, timePattern: string, locale: string, strict: boolean}
|
|
|
280 |
// selector- choice of 'time', 'date' (default: date and time)
|
|
|
281 |
// formatLength- choice of long, short, medium or full (plus any custom additions). Defaults to 'short'
|
|
|
282 |
// datePattern,timePattern- override pattern with this string
|
|
|
283 |
// am,pm- override strings for am/pm in times
|
|
|
284 |
// locale- override the locale used to determine formatting rules
|
|
|
285 |
// strict- strict parsing, off by default
|
|
|
286 |
|
|
|
287 |
var info = dojo.date.locale._parseInfo(options);
|
|
|
288 |
var tokens = info.tokens, bundle = info.bundle;
|
|
|
289 |
var re = new RegExp("^" + info.regexp + "$");
|
|
|
290 |
var match = re.exec(value);
|
|
|
291 |
if(!match){ return null; } // null
|
|
|
292 |
|
|
|
293 |
var widthList = ['abbr', 'wide', 'narrow'];
|
|
|
294 |
//1972 is a leap year. We want to avoid Feb 29 rolling over into Mar 1,
|
|
|
295 |
//in the cases where the year is parsed after the month and day.
|
|
|
296 |
var result = new Date(1972, 0);
|
|
|
297 |
var expected = {};
|
|
|
298 |
var amPm = "";
|
|
|
299 |
dojo.forEach(match, function(v, i){
|
|
|
300 |
if(!i){return;}
|
|
|
301 |
var token=tokens[i-1];
|
|
|
302 |
var l=token.length;
|
|
|
303 |
switch(token.charAt(0)){
|
|
|
304 |
case 'y':
|
|
|
305 |
if(l != 2){
|
|
|
306 |
//interpret year literally, so '5' would be 5 A.D.
|
|
|
307 |
result.setFullYear(v);
|
|
|
308 |
expected.year = v;
|
|
|
309 |
}else{
|
|
|
310 |
if(v<100){
|
|
|
311 |
v = Number(v);
|
|
|
312 |
//choose century to apply, according to a sliding window
|
|
|
313 |
//of 80 years before and 20 years after present year
|
|
|
314 |
var year = '' + new Date().getFullYear();
|
|
|
315 |
var century = year.substring(0, 2) * 100;
|
|
|
316 |
var yearPart = Number(year.substring(2, 4));
|
|
|
317 |
var cutoff = Math.min(yearPart + 20, 99);
|
|
|
318 |
var num = (v < cutoff) ? century + v : century - 100 + v;
|
|
|
319 |
result.setFullYear(num);
|
|
|
320 |
expected.year = num;
|
|
|
321 |
}else{
|
|
|
322 |
//we expected 2 digits and got more...
|
|
|
323 |
if(options.strict){
|
|
|
324 |
return null;
|
|
|
325 |
}
|
|
|
326 |
//interpret literally, so '150' would be 150 A.D.
|
|
|
327 |
//also tolerate '1950', if 'yyyy' input passed to 'yy' format
|
|
|
328 |
result.setFullYear(v);
|
|
|
329 |
expected.year = v;
|
|
|
330 |
}
|
|
|
331 |
}
|
|
|
332 |
break;
|
|
|
333 |
case 'M':
|
|
|
334 |
if(l>2){
|
|
|
335 |
var months = bundle['months-format-' + widthList[l-3]].concat();
|
|
|
336 |
if(!options.strict){
|
|
|
337 |
//Tolerate abbreviating period in month part
|
|
|
338 |
//Case-insensitive comparison
|
|
|
339 |
v = v.replace(".","").toLowerCase();
|
|
|
340 |
months = dojo.map(months, function(s){ return s.replace(".","").toLowerCase(); } );
|
|
|
341 |
}
|
|
|
342 |
v = dojo.indexOf(months, v);
|
|
|
343 |
if(v == -1){
|
|
|
344 |
// console.debug("dojo.date.locale.parse: Could not parse month name: '" + v + "'.");
|
|
|
345 |
return null;
|
|
|
346 |
}
|
|
|
347 |
}else{
|
|
|
348 |
v--;
|
|
|
349 |
}
|
|
|
350 |
result.setMonth(v);
|
|
|
351 |
expected.month = v;
|
|
|
352 |
break;
|
|
|
353 |
case 'E':
|
|
|
354 |
case 'e':
|
|
|
355 |
var days = bundle['days-format-' + widthList[l-3]].concat();
|
|
|
356 |
if(!options.strict){
|
|
|
357 |
//Case-insensitive comparison
|
|
|
358 |
v = v.toLowerCase();
|
|
|
359 |
days = dojo.map(days, "".toLowerCase);
|
|
|
360 |
}
|
|
|
361 |
v = dojo.indexOf(days, v);
|
|
|
362 |
if(v == -1){
|
|
|
363 |
// console.debug("dojo.date.locale.parse: Could not parse weekday name: '" + v + "'.");
|
|
|
364 |
return null;
|
|
|
365 |
}
|
|
|
366 |
|
|
|
367 |
//TODO: not sure what to actually do with this input,
|
|
|
368 |
//in terms of setting something on the Date obj...?
|
|
|
369 |
//without more context, can't affect the actual date
|
|
|
370 |
//TODO: just validate?
|
|
|
371 |
break;
|
|
|
372 |
case 'd':
|
|
|
373 |
result.setDate(v);
|
|
|
374 |
expected.date = v;
|
|
|
375 |
break;
|
|
|
376 |
case 'D':
|
|
|
377 |
//FIXME: need to defer this until after the year is set for leap-year?
|
|
|
378 |
result.setMonth(0);
|
|
|
379 |
result.setDate(v);
|
|
|
380 |
break;
|
|
|
381 |
case 'a': //am/pm
|
|
|
382 |
var am = options.am || bundle.am;
|
|
|
383 |
var pm = options.pm || bundle.pm;
|
|
|
384 |
if(!options.strict){
|
|
|
385 |
var period = /\./g;
|
|
|
386 |
v = v.replace(period,'').toLowerCase();
|
|
|
387 |
am = am.replace(period,'').toLowerCase();
|
|
|
388 |
pm = pm.replace(period,'').toLowerCase();
|
|
|
389 |
}
|
|
|
390 |
if(options.strict && v != am && v != pm){
|
|
|
391 |
// console.debug("dojo.date.locale.parse: Could not parse am/pm part.");
|
|
|
392 |
return null;
|
|
|
393 |
}
|
|
|
394 |
|
|
|
395 |
// we might not have seen the hours field yet, so store the state and apply hour change later
|
|
|
396 |
amPm = (v == pm) ? 'p' : (v == am) ? 'a' : '';
|
|
|
397 |
break;
|
|
|
398 |
case 'K': //hour (1-24)
|
|
|
399 |
if(v==24){v=0;}
|
|
|
400 |
// fallthrough...
|
|
|
401 |
case 'h': //hour (1-12)
|
|
|
402 |
case 'H': //hour (0-23)
|
|
|
403 |
case 'k': //hour (0-11)
|
|
|
404 |
//TODO: strict bounds checking, padding
|
|
|
405 |
if(v > 23){
|
|
|
406 |
// console.debug("dojo.date.locale.parse: Illegal hours value");
|
|
|
407 |
return null;
|
|
|
408 |
}
|
|
|
409 |
|
|
|
410 |
//in the 12-hour case, adjusting for am/pm requires the 'a' part
|
|
|
411 |
//which could come before or after the hour, so we will adjust later
|
|
|
412 |
result.setHours(v);
|
|
|
413 |
break;
|
|
|
414 |
case 'm': //minutes
|
|
|
415 |
result.setMinutes(v);
|
|
|
416 |
break;
|
|
|
417 |
case 's': //seconds
|
|
|
418 |
result.setSeconds(v);
|
|
|
419 |
break;
|
|
|
420 |
case 'S': //milliseconds
|
|
|
421 |
result.setMilliseconds(v);
|
|
|
422 |
// break;
|
|
|
423 |
// case 'w':
|
|
|
424 |
//TODO var firstDay = 0;
|
|
|
425 |
// default:
|
|
|
426 |
//TODO: throw?
|
|
|
427 |
// console.debug("dojo.date.locale.parse: unsupported pattern char=" + token.charAt(0));
|
|
|
428 |
}
|
|
|
429 |
});
|
|
|
430 |
|
|
|
431 |
var hours = result.getHours();
|
|
|
432 |
if(amPm === 'p' && hours < 12){
|
|
|
433 |
result.setHours(hours + 12); //e.g., 3pm -> 15
|
|
|
434 |
}else if(amPm === 'a' && hours == 12){
|
|
|
435 |
result.setHours(0); //12am -> 0
|
|
|
436 |
}
|
|
|
437 |
|
|
|
438 |
//validate parse date fields versus input date fields
|
|
|
439 |
if(expected.year && result.getFullYear() != expected.year){
|
|
|
440 |
// console.debug("dojo.date.locale.parse: Parsed year: '" + result.getFullYear() + "' did not match input year: '" + expected.year + "'.");
|
|
|
441 |
return null;
|
|
|
442 |
}
|
|
|
443 |
if(expected.month && result.getMonth() != expected.month){
|
|
|
444 |
// console.debug("dojo.date.locale.parse: Parsed month: '" + result.getMonth() + "' did not match input month: '" + expected.month + "'.");
|
|
|
445 |
return null;
|
|
|
446 |
}
|
|
|
447 |
if(expected.date && result.getDate() != expected.date){
|
|
|
448 |
// console.debug("dojo.date.locale.parse: Parsed day of month: '" + result.getDate() + "' did not match input day of month: '" + expected.date + "'.");
|
|
|
449 |
return null;
|
|
|
450 |
}
|
|
|
451 |
|
|
|
452 |
//TODO: implement a getWeekday() method in order to test
|
|
|
453 |
//validity of input strings containing 'EEE' or 'EEEE'...
|
|
|
454 |
return result; // Date
|
|
|
455 |
};
|
|
|
456 |
|
|
|
457 |
function _processPattern(pattern, applyPattern, applyLiteral, applyAll){
|
|
|
458 |
//summary: Process a pattern with literals in it
|
|
|
459 |
|
|
|
460 |
// Break up on single quotes, treat every other one as a literal, except '' which becomes '
|
|
|
461 |
var identity = function(x){return x;};
|
|
|
462 |
applyPattern = applyPattern || identity;
|
|
|
463 |
applyLiteral = applyLiteral || identity;
|
|
|
464 |
applyAll = applyAll || identity;
|
|
|
465 |
|
|
|
466 |
//split on single quotes (which escape literals in date format strings)
|
|
|
467 |
//but preserve escaped single quotes (e.g., o''clock)
|
|
|
468 |
var chunks = pattern.match(/(''|[^'])+/g);
|
|
|
469 |
var literal = false;
|
|
|
470 |
|
|
|
471 |
dojo.forEach(chunks, function(chunk, i){
|
|
|
472 |
if(!chunk){
|
|
|
473 |
chunks[i]='';
|
|
|
474 |
}else{
|
|
|
475 |
chunks[i]=(literal ? applyLiteral : applyPattern)(chunk);
|
|
|
476 |
literal = !literal;
|
|
|
477 |
}
|
|
|
478 |
});
|
|
|
479 |
return applyAll(chunks.join(''));
|
|
|
480 |
}
|
|
|
481 |
|
|
|
482 |
function _buildDateTimeRE(tokens, bundle, options, pattern){
|
|
|
483 |
pattern = dojo.regexp.escapeString(pattern);
|
|
|
484 |
if(!options.strict){ pattern = pattern.replace(" a", " ?a"); } // kludge to tolerate no space before am/pm
|
|
|
485 |
return pattern.replace(/([a-z])\1*/ig, function(match){
|
|
|
486 |
// Build a simple regexp. Avoid captures, which would ruin the tokens list
|
|
|
487 |
var s;
|
|
|
488 |
var c = match.charAt(0);
|
|
|
489 |
var l = match.length;
|
|
|
490 |
var p2 = '', p3 = '';
|
|
|
491 |
if(options.strict){
|
|
|
492 |
if(l > 1){ p2 = '0' + '{'+(l-1)+'}'; }
|
|
|
493 |
if(l > 2){ p3 = '0' + '{'+(l-2)+'}'; }
|
|
|
494 |
}else{
|
|
|
495 |
p2 = '0?'; p3 = '0{0,2}';
|
|
|
496 |
}
|
|
|
497 |
switch(c){
|
|
|
498 |
case 'y':
|
|
|
499 |
s = '\\d{2,4}';
|
|
|
500 |
break;
|
|
|
501 |
case 'M':
|
|
|
502 |
s = (l>2) ? '\\S+' : p2+'[1-9]|1[0-2]';
|
|
|
503 |
break;
|
|
|
504 |
case 'D':
|
|
|
505 |
s = p2+'[1-9]|'+p3+'[1-9][0-9]|[12][0-9][0-9]|3[0-5][0-9]|36[0-6]';
|
|
|
506 |
break;
|
|
|
507 |
case 'd':
|
|
|
508 |
s = p2+'[1-9]|[12]\\d|3[01]';
|
|
|
509 |
break;
|
|
|
510 |
case 'w':
|
|
|
511 |
s = p2+'[1-9]|[1-4][0-9]|5[0-3]';
|
|
|
512 |
break;
|
|
|
513 |
case 'E':
|
|
|
514 |
s = '\\S+';
|
|
|
515 |
break;
|
|
|
516 |
case 'h': //hour (1-12)
|
|
|
517 |
s = p2+'[1-9]|1[0-2]';
|
|
|
518 |
break;
|
|
|
519 |
case 'k': //hour (0-11)
|
|
|
520 |
s = p2+'\\d|1[01]';
|
|
|
521 |
break;
|
|
|
522 |
case 'H': //hour (0-23)
|
|
|
523 |
s = p2+'\\d|1\\d|2[0-3]';
|
|
|
524 |
break;
|
|
|
525 |
case 'K': //hour (1-24)
|
|
|
526 |
s = p2+'[1-9]|1\\d|2[0-4]';
|
|
|
527 |
break;
|
|
|
528 |
case 'm':
|
|
|
529 |
case 's':
|
|
|
530 |
s = '[0-5]\\d';
|
|
|
531 |
break;
|
|
|
532 |
case 'S':
|
|
|
533 |
s = '\\d{'+l+'}';
|
|
|
534 |
break;
|
|
|
535 |
case 'a':
|
|
|
536 |
var am = options.am || bundle.am || 'AM';
|
|
|
537 |
var pm = options.pm || bundle.pm || 'PM';
|
|
|
538 |
if(options.strict){
|
|
|
539 |
s = am + '|' + pm;
|
|
|
540 |
}else{
|
|
|
541 |
s = am + '|' + pm;
|
|
|
542 |
if(am != am.toLowerCase()){ s += '|' + am.toLowerCase(); }
|
|
|
543 |
if(pm != pm.toLowerCase()){ s += '|' + pm.toLowerCase(); }
|
|
|
544 |
}
|
|
|
545 |
break;
|
|
|
546 |
default:
|
|
|
547 |
// case 'v':
|
|
|
548 |
// case 'z':
|
|
|
549 |
// case 'Z':
|
|
|
550 |
s = ".*";
|
|
|
551 |
// console.debug("parse of date format, pattern=" + pattern);
|
|
|
552 |
}
|
|
|
553 |
|
|
|
554 |
if(tokens){ tokens.push(match); }
|
|
|
555 |
|
|
|
556 |
return "(" + s + ")"; // add capture
|
|
|
557 |
}).replace(/[\xa0 ]/g, "[\\s\\xa0]"); // normalize whitespace. Need explicit handling of \xa0 for IE.
|
|
|
558 |
}
|
|
|
559 |
})();
|
|
|
560 |
|
|
|
561 |
(function(){
|
|
|
562 |
var _customFormats = [];
|
|
|
563 |
dojo.date.locale.addCustomFormats = function(/*String*/packageName, /*String*/bundleName){
|
|
|
564 |
// summary:
|
|
|
565 |
// Add a reference to a bundle containing localized custom formats to be
|
|
|
566 |
// used by date/time formatting and parsing routines.
|
|
|
567 |
//
|
|
|
568 |
// description:
|
|
|
569 |
// The user may add custom localized formats where the bundle has properties following the
|
|
|
570 |
// same naming convention used by dojo for the CLDR data: dateFormat-xxxx / timeFormat-xxxx
|
|
|
571 |
// The pattern string should match the format used by the CLDR.
|
|
|
572 |
// See dojo.date.format for details.
|
|
|
573 |
// The resources must be loaded by dojo.requireLocalization() prior to use
|
|
|
574 |
|
|
|
575 |
_customFormats.push({pkg:packageName,name:bundleName});
|
|
|
576 |
};
|
|
|
577 |
|
|
|
578 |
dojo.date.locale._getGregorianBundle = function(/*String*/locale){
|
|
|
579 |
var gregorian = {};
|
|
|
580 |
dojo.forEach(_customFormats, function(desc){
|
|
|
581 |
var bundle = dojo.i18n.getLocalization(desc.pkg, desc.name, locale);
|
|
|
582 |
gregorian = dojo.mixin(gregorian, bundle);
|
|
|
583 |
}, this);
|
|
|
584 |
return gregorian; /*Object*/
|
|
|
585 |
};
|
|
|
586 |
})();
|
|
|
587 |
|
|
|
588 |
dojo.date.locale.addCustomFormats("dojo.cldr","gregorian");
|
|
|
589 |
|
|
|
590 |
dojo.date.locale.getNames = function(/*String*/item, /*String*/type, /*String?*/use, /*String?*/locale){
|
|
|
591 |
// summary:
|
|
|
592 |
// Used to get localized strings from dojo.cldr for day or month names.
|
|
|
593 |
//
|
|
|
594 |
// item: 'months' || 'days'
|
|
|
595 |
// type: 'wide' || 'narrow' || 'abbr' (e.g. "Monday", "Mon", or "M" respectively, in English)
|
|
|
596 |
// use: 'standAlone' || 'format' (default)
|
|
|
597 |
// locale: override locale used to find the names
|
|
|
598 |
|
|
|
599 |
var label;
|
|
|
600 |
var lookup = dojo.date.locale._getGregorianBundle(locale);
|
|
|
601 |
var props = [item, use, type];
|
|
|
602 |
if(use == 'standAlone'){
|
|
|
603 |
label = lookup[props.join('-')];
|
|
|
604 |
}
|
|
|
605 |
props[1] = 'format';
|
|
|
606 |
|
|
|
607 |
// return by copy so changes won't be made accidentally to the in-memory model
|
|
|
608 |
return (label || lookup[props.join('-')]).concat(); /*Array*/
|
|
|
609 |
};
|
|
|
610 |
|
|
|
611 |
dojo.date.locale.isWeekend = function(/*Date?*/dateObject, /*String?*/locale){
|
|
|
612 |
// summary:
|
|
|
613 |
// Determines if the date falls on a weekend, according to local custom.
|
|
|
614 |
|
|
|
615 |
var weekend = dojo.cldr.supplemental.getWeekend(locale);
|
|
|
616 |
var day = (dateObject || new Date()).getDay();
|
|
|
617 |
if(weekend.end < weekend.start){
|
|
|
618 |
weekend.end += 7;
|
|
|
619 |
if(day < weekend.start){ day += 7; }
|
|
|
620 |
}
|
|
|
621 |
return day >= weekend.start && day <= weekend.end; // Boolean
|
|
|
622 |
};
|
|
|
623 |
|
|
|
624 |
// These are used only by format and strftime. Do they need to be public? Which module should they go in?
|
|
|
625 |
|
|
|
626 |
dojo.date.locale._getDayOfYear = function(/*Date*/dateObject){
|
|
|
627 |
// summary: gets the day of the year as represented by dateObject
|
|
|
628 |
return dojo.date.difference(new Date(dateObject.getFullYear(), 0, 1), dateObject) + 1; // Number
|
|
|
629 |
};
|
|
|
630 |
|
|
|
631 |
dojo.date.locale._getWeekOfYear = function(/*Date*/dateObject, /*Number*/firstDayOfWeek){
|
|
|
632 |
if(arguments.length == 1){ firstDayOfWeek = 0; } // Sunday
|
|
|
633 |
|
|
|
634 |
var firstDayOfYear = new Date(dateObject.getFullYear(), 0, 1).getDay();
|
|
|
635 |
var adj = (firstDayOfYear - firstDayOfWeek + 7) % 7;
|
|
|
636 |
var week = Math.floor((dojo.date.locale._getDayOfYear(dateObject) + adj - 1) / 7);
|
|
|
637 |
|
|
|
638 |
// if year starts on the specified day, start counting weeks at 1
|
|
|
639 |
if(firstDayOfYear == firstDayOfWeek){ week++; }
|
|
|
640 |
|
|
|
641 |
return week; // Number
|
|
|
642 |
};
|
|
|
643 |
|
|
|
644 |
}
|