Debian Codegraph @

dojox/date/buddhist/locale.js

raw symbols SCIP
1 define(["dojox/main", "dojo/_base/lang", "dojo/_base/array", "dojo/date", "dojo/i18n", "dojo/regexp", "dojo/string", "./Date", "dojo/i18n!dojo/cldr/nls/buddhist"],
2 function(dojox, lang, arr, dd, i18n, regexp, string, BDate){
3
4 var blocale = lang.getObject("date.buddhist.locale", true, dojox);
5
6 // Format a pattern without literals
7 function formatPattern(dateObject, bundle, locale, fullYear, pattern){
8
9 return pattern.replace(/([a-z])\1*/ig, function(match){
10 var s, pad;
11 var c = match.charAt(0);
12 var l = match.length;
13 var widthList = ["abbr", "wide", "narrow"];
14
15 switch(c){
16 case 'G':
17 s = bundle["eraAbbr"][0];
18 break;
19 case 'y':
20 s = String(dateObject.getFullYear());
21 break;
22 case 'M':
23 var m = dateObject.getMonth();
24 if(l<3){
25 s = m+1; pad = true;
26 }else{
27 var propM = ["months", "format", widthList[l-3]].join("-");
28 s = bundle[propM][m];
29 }
30 break;
31 case 'd':
32 s = dateObject.getDate(true); pad = true;
33 break;
34 case 'E':
35 var d = dateObject.getDay();
36 if(l<3){
37 s = d+1; pad = true;
38 }else{
39 var propD = ["days", "format", widthList[l-3]].join("-");
40 s = bundle[propD][d];
41 }
42 break;
43 case 'a':
44 var timePeriod = (dateObject.getHours() < 12) ? 'am' : 'pm';
45 s = bundle['dayPeriods-format-wide-' + timePeriod];
46 break;
47 case 'h':
48 case 'H':
49 case 'K':
50 case 'k':
51 var h = dateObject.getHours();
52 switch (c){
53 case 'h': // 1-12
54 s = (h % 12) || 12;
55 break;
56 case 'H': // 0-23
57 s = h;
58 break;
59 case 'K': // 0-11
60 s = (h % 12);
61 break;
62 case 'k': // 1-24
63 s = h || 24;
64 break;
65 }
66 pad = true;
67 break;
68 case 'm':
69 s = dateObject.getMinutes(); pad = true;
70 break;
71 case 's':
72 s = dateObject.getSeconds(); pad = true;
73 break;
74 case 'S':
75 s = Math.round(dateObject.getMilliseconds() * Math.pow(10, l-3)); pad = true;
76 break;
77 case 'z':
78 // We only have one timezone to offer; the one from the browser
79 s = dd.getTimezoneName(dateObject.toGregorian());
80 if(s){ break; }
81 l = 4;
82 // fallthrough... use GMT if tz not available
83 case 'Z':
84 var offset = dateObject.toGregorian().getTimezoneOffset();
85 var tz = [
86 (offset <= 0 ? "+" : "-"),
87 string.pad(Math.floor(Math.abs(offset) / 60), 2),
88 string.pad(Math.abs(offset) % 60, 2)
89 ];
90 if(l == 4){
91 tz.splice(0, 0, "GMT");
92 tz.splice(3, 0, ":");
93 }
94 s = tz.join("");
95 break;
96 default:
97 throw new Error("dojox.date.buddhist.locale.formatPattern: invalid pattern char: "+pattern);
98 }
99 if(pad){ s = string.pad(s, l); }
100 return s;
101 });
102 }
103
104 blocale.format = function(/*dojox/date/buddhist/Date*/dateObject, /*object?*/options){
105 // summary:
106 // Format a Date object as a String, using settings.
107
108 // based on and similar to dojo.date.locale.format
109
110 options = options || {};
111
112 var locale = i18n.normalizeLocale(options.locale);
113 var formatLength = options.formatLength || 'short';
114 var bundle = blocale._getBuddhistBundle(locale);
115 var str = [];
116
117 var sauce = lang.hitch(this, formatPattern, dateObject, bundle, locale, options.fullYear);
118 if(options.selector == "year"){
119 var year = dateObject.getFullYear();
120 return year;
121 }
122 if(options.selector != "time"){
123 var datePattern = options.datePattern || bundle["dateFormat-"+formatLength];
124 if(datePattern){str.push(_processPattern(datePattern, sauce));}
125 }
126 if(options.selector != "date"){
127 var timePattern = options.timePattern || bundle["timeFormat-"+formatLength];
128 if(timePattern){str.push(_processPattern(timePattern, sauce));}
129 }
130 var result = str.join(" "); //TODO: use locale-specific pattern to assemble date + time
131
132 return result; // String
133 };
134
135 blocale.regexp = function(/*Object?*/options){
136 // summary:
137 // Builds the regular needed to parse a buddhist.Date
138
139 // based on and similar to dojo.date.locale.regexp
140
141 return blocale._parseInfo(options).regexp; // String
142 };
143
144 blocale._parseInfo = function(/*Object?*/options){
145 /* based on and similar to dojo.date.locale._parseInfo */
146
147 options = options || {};
148 var locale = i18n.normalizeLocale(options.locale);
149 var bundle = blocale._getBuddhistBundle(locale);
150 var formatLength = options.formatLength || 'short';
151 var datePattern = options.datePattern || bundle["dateFormat-" + formatLength];
152 var timePattern = options.timePattern || bundle["timeFormat-" + formatLength];
153
154 var pattern;
155 if(options.selector == 'date'){
156 pattern = datePattern;
157 }else if(options.selector == 'time'){
158 pattern = timePattern;
159 }else{
160 pattern = (typeof (timePattern) == "undefined") ? datePattern : datePattern + ' ' + timePattern;
161 }
162
163 var tokens = [];
164
165 var re = _processPattern(pattern, lang.hitch(this, _buildDateTimeRE, tokens, bundle, options));
166 return {regexp: re, tokens: tokens, bundle: bundle};
167 };
168
169 blocale.parse = function(/*String*/value, /*Object?*/options){
170 // summary:
171 // This function parses string date value according to options
172
173 // based on and similar to dojo.date.locale.parse
174
175 value = value.replace(/[\u200E\u200F\u202A-\u202E]/g, ""); //remove special chars
176
177 if(!options){options={};}
178 var info = blocale._parseInfo(options);
179
180 var tokens = info.tokens, bundle = info.bundle;
181 var re = new RegExp("^" + info.regexp + "$");
182
183 var match = re.exec(value);
184
185 var locale = i18n.normalizeLocale(options.locale);
186
187 if(!match){ return null; } // null
188
189 var date, date1;
190
191 var result = [2513,0,1,0,0,0,0]; // buddhist date for [1970,0,1,0,0,0,0] used in gregorian locale
192 var amPm = "";
193 var mLength = 0;
194 var widthList = ["abbr", "wide", "narrow"];
195 var valid = arr.every(match, function(v, i){
196 if(!i){return true;}
197 var token=tokens[i-1];
198 var l=token.length;
199 switch(token.charAt(0)){
200 case 'y':
201 result[0] = Number(v);
202 break;
203 case 'M':
204 if(l>2){
205 var months = bundle['months-format-' + widthList[l-3]].concat();
206 if(!options.strict){
207 //Tolerate abbreviating period in month part
208 //Case-insensitive comparison
209 v = v.replace(".","").toLowerCase();
210 months = arr.map(months, function(s){ return s ? s.replace(".","").toLowerCase() : s; } );
211 }
212 v = arr.indexOf(months, v);
213 if(v == -1){
214 return false;
215 }
216 mLength = l;
217 }else{
218 v--;
219 }
220 result[1] = Number(v);
221 break;
222 case 'D':
223 result[1] = 0;
224 // fallthrough...
225 case 'd':
226 result[2] = Number(v);
227 break;
228 case 'a': //am/pm
229 var am = options.am || bundle['dayPeriods-format-wide-am'],
230 pm = options.pm || bundle['dayPeriods-format-wide-pm'];
231 if(!options.strict){
232 var period = /\./g;
233 v = v.replace(period,'').toLowerCase();
234 am = am.replace(period,'').toLowerCase();
235 pm = pm.replace(period,'').toLowerCase();
236 }
237 if(options.strict && v != am && v != pm){
238 return false;
239 }
240
241 // we might not have seen the hours field yet, so store the state and apply hour change later
242 amPm = (v == pm) ? 'p' : (v == am) ? 'a' : '';
243 break;
244 case 'K': //hour (1-24)
245 if(v == 24){ v = 0; }
246 // fallthrough...
247 case 'h': //hour (1-12)
248 case 'H': //hour (0-23)
249 case 'k': //hour (0-11)
250 //in the 12-hour case, adjusting for am/pm requires the 'a' part
251 //which could come before or after the hour, so we will adjust later
252 result[3] = Number(v);
253 break;
254 case 'm': //minutes
255 result[4] = Number(v);
256 break;
257 case 's': //seconds
258 result[5] = Number(v);
259 break;
260 case 'S': //milliseconds
261 result[6] = Number(v);
262 }
263 return true;
264 });
265
266 var hours = +result[3];
267 if(amPm === 'p' && hours < 12){
268 result[3] = hours + 12; //e.g., 3pm -> 15
269 }else if(amPm === 'a' && hours == 12){
270 result[3] = 0; //12am -> 0
271 }
272 var dateObject = new BDate(result[0], result[1], result[2], result[3], result[4], result[5], result[6]);
273 return dateObject;
274 };
275
276
277 function _processPattern(pattern, applyPattern, applyLiteral, applyAll){
278 // summary:
279 // Process a pattern with literals in it
280
281 // Break up on single quotes, treat every other one as a literal, except '' which becomes '
282 var identity = function(x){return x;};
283 applyPattern = applyPattern || identity;
284 applyLiteral = applyLiteral || identity;
285 applyAll = applyAll || identity;
286
287 //split on single quotes (which escape literals in date format strings)
288 //but preserve escaped single quotes (e.g., o''clock)
289 var chunks = pattern.match(/(''|[^'])+/g);
290 var literal = pattern.charAt(0) == "'";
291
292 arr.forEach(chunks, function(chunk, i){
293 if(!chunk){
294 chunks[i]='';
295 }else{
296 chunks[i]=(literal ? applyLiteral : applyPattern)(chunk);
297 literal = !literal;
298 }
299 });
300 return applyAll(chunks.join(''));
301 }
302
303 function _buildDateTimeRE (tokens, bundle, options, pattern){
304 // based on and similar to dojo.date.locale._buildDateTimeRE
305 //
306
307 pattern = regexp.escapeString(pattern);
308 var locale = i18n.normalizeLocale(options.locale);
309
310 return pattern.replace(/([a-z])\1*/ig, function(match){
311
312 // Build a simple regexp. Avoid captures, which would ruin the tokens list
313 var s;
314 var c = match.charAt(0);
315 var l = match.length;
316 var p2 = '', p3 = '';
317 if(options.strict){
318 if(l > 1){ p2 = '0' + '{'+(l-1)+'}'; }
319 if(l > 2){ p3 = '0' + '{'+(l-2)+'}'; }
320 }else{
321 p2 = '0?'; p3 = '0{0,2}';
322 }
323 switch(c){
324 case 'y':
325 s = '\\d+';
326 break;
327 case 'M':
328 s = (l>2) ? '\\S+' : p2+'[1-9]|1[0-2]';
329 break;
330 case 'd':
331 s = '[12]\\d|'+p2+'[1-9]|3[01]';
332 break;
333 case 'E':
334 s = '\\S+';
335 break;
336 case 'h': //hour (1-12)
337 s = p2+'[1-9]|1[0-2]';
338 break;
339 case 'k': //hour (0-11)
340 s = p2+'\\d|1[01]';
341 break;
342 case 'H': //hour (0-23)
343 s = p2+'\\d|1\\d|2[0-3]';
344 break;
345 case 'K': //hour (1-24)
346 s = p2+'[1-9]|1\\d|2[0-4]';
347 break;
348 case 'm':
349 case 's':
350 s = p2+'\\d|[0-5]\\d';
351 break;
352 case 'S':
353 s = '\\d{'+l+'}';
354 break;
355 case 'a':
356 var am = options.am || bundle['dayPeriods-format-wide-am'],
357 pm = options.pm || bundle['dayPeriods-format-wide-pm'];
358 if(options.strict){
359 s = am + '|' + pm;
360 }else{
361 s = am + '|' + pm;
362 if(am != am.toLowerCase()){ s += '|' + am.toLowerCase(); }
363 if(pm != pm.toLowerCase()){ s += '|' + pm.toLowerCase(); }
364 }
365 break;
366 default:
367 s = ".*";
368 }
369 if(tokens){ tokens.push(match); }
370 return "(" + s + ")"; // add capture
371 }).replace(/[\xa0 ]/g, "[\\s\\xa0]"); // normalize whitespace. Need explicit handling of \xa0 for IE. */
372 }
373
374 var _customFormats = [];
375 blocale.addCustomFormats = function(/*String*/packageName, /*String*/bundleName){
376 // summary:
377 // Add a reference to a bundle containing localized custom formats to be
378 // used by date/time formatting and parsing routines.
379 _customFormats.push({pkg:packageName,name:bundleName});
380 };
381
382 blocale._getBuddhistBundle = function(/*String*/locale){
383 var buddhist = {};
384 arr.forEach(_customFormats, function(desc){
385 var bundle = i18n.getLocalization(desc.pkg, desc.name, locale);
386 buddhist = lang.mixin(buddhist, bundle);
387 }, this);
388 return buddhist; /*Object*/
389 };
390
391 blocale.addCustomFormats("dojo.cldr","buddhist");
392
393 blocale.getNames = function(/*String*/item, /*String*/type, /*String?*/context, /*String?*/locale, /*dojox/date/buddhist/Date?*/date){
394 // summary:
395 // Used to get localized strings from dojo.cldr for day or month names.
396 var label;
397 var lookup = blocale._getBuddhistBundle(locale);
398 var props = [item, context, type];
399 if(context == 'standAlone'){
400 var key = props.join('-');
401 label = lookup[key];
402 // Fall back to 'format' flavor of name
403 if(label[0] == 1){ label = undefined; } // kludge, in the absence of real aliasing support in dojo.cldr
404 }
405 props[1] = 'format';
406
407 // return by copy so changes won't be made accidentally to the in-memory model
408 return (label || lookup[props.join('-')]).concat(); /*Array*/
409 };
410
411 return blocale;
412 });