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");
5 // Localization methods for Date. Honor local customs using locale-dependent dojo.cldr data.
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");
13 // Load the bundles containing localization information for
15 dojo.requireLocalization("dojo.cldr", "gregorian", null, "zh-cn,zh,en-ca,ko-kr,pt,pt-br,it-it,ROOT,en-gb,de,ja,en,en-au,fr,es,ko,zh-tw,it,es-es");
17 //NOTE: Everything in this module assumes Gregorian calendars.
18 // Other calendars will be implemented in separate modules.
21 // Format a pattern without literals
22 function formatPattern(dateObject, bundle, fullYear, pattern){
23 return pattern.replace(/([a-z])\1*/ig, function(match){
25 var c = match.charAt(0);
27 var widthList = ["abbr", "wide", "narrow"];
30 s = bundle[(l < 4) ? "eraAbbr" : "eraNames"][dateObject.getFullYear() < 0 ? 0 : 1];
33 s = dateObject.getFullYear();
39 s = String(s); s = s.substr(s.length - 2);
49 s = Math.ceil((dateObject.getMonth()+1)/3);
54 // case 3: case 4: // unimplemented
59 var m = dateObject.getMonth();
65 case 3: case 4: case 5:
66 widthM = widthList[l-3];
70 var typeM = (c == "L") ? "standalone" : "format";
71 var propM = ["months", typeM, widthM].join("-");
77 s = dojo.date.locale._getWeekOfYear(dateObject, firstDay); pad = true;
80 s = dateObject.getDate(); pad = true;
83 s = dojo.date.locale._getDayOfYear(dateObject); pad = true;
87 case 'c': // REVIEW: don't see this in the spec?
88 var d = dateObject.getDay();
93 var first = dojo.cldr.supplemental.getFirstDayOfWeek(options.locale);
100 // else fallthrough...
101 case 3: case 4: case 5:
102 widthD = widthList[l-3];
106 var typeD = (c == "c") ? "standalone" : "format";
107 var propD = ["days", typeD, widthD].join("-");
108 s = bundle[propD][d];
112 var timePeriod = (dateObject.getHours() < 12) ? 'am' : 'pm';
113 s = bundle[timePeriod];
119 var h = dateObject.getHours();
120 // strange choices in the date format make it impossible to write this succinctly
138 s = dateObject.getMinutes(); pad = true;
141 s = dateObject.getSeconds(); pad = true;
144 s = Math.round(dateObject.getMilliseconds() * Math.pow(10, l-3)); pad = true;
146 case 'v': // FIXME: don't know what this is. seems to be same as z?
148 // We only have one timezone to offer; the one from the browser
149 s = dojo.date.getTimezoneName(dateObject);
152 // fallthrough... use GMT if tz not available
154 var offset = dateObject.getTimezoneOffset();
156 (offset<=0 ? "+" : "-"),
157 dojo.string.pad(Math.floor(Math.abs(offset)/60), 2),
158 dojo.string.pad(Math.abs(offset)% 60, 2)
161 tz.splice(0, 0, "GMT");
162 tz.splice(3, 0, ":");
166 // case 'Y': case 'u': case 'W': case 'F': case 'g': case 'A':
167 // console.debug(match+" modifier unimplemented");
169 throw new Error("dojo.date.locale.format: invalid pattern char: "+pattern);
171 if(pad){ s = dojo.string.pad(s, l); }
177 dojo.date.locale.__FormatOptions = function(){
179 // choice of 'time','date' (default: date and time)
180 // formatLength: String
181 // choice of long, short, medium or full (plus any custom additions). Defaults to 'short'
182 // datePattern:String
183 // override pattern with this string
184 // timePattern:String
185 // override pattern with this string
187 // override strings for am in times
189 // override strings for pm in times
191 // override the locale used to determine formatting rules
193 // (format only) use 4 digit years whenever 2 digit years are called for
195 // (parse only) strict parsing, off by default
196 this.selector = selector;
197 this.formatLength = formatLength;
198 this.datePattern = datePattern;
199 this.timePattern = timePattern;
202 this.locale = locale;
203 this.fullYear = fullYear;
204 this.strict = strict;
208 dojo.date.locale.format = function(/*Date*/dateObject, /*dojo.date.locale.__FormatOptions?*/options){
210 // Format a Date object as a String, using locale-specific settings.
213 // Create a string from a Date object using a known localized pattern.
214 // By default, this method formats both date and time from dateObject.
215 // Formatting patterns are chosen appropriate to the locale. Different
216 // formatting lengths may be chosen, with "full" used by default.
217 // Custom patterns may be used or registered with translations using
218 // the dojo.date.locale.addCustomFormats method.
219 // Formatting patterns are implemented using [the syntax described at
220 // unicode.org](http://www.unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns)
223 // the date and/or time to be formatted. If a time only is formatted,
224 // the values in the year, month, and day fields are irrelevant. The
225 // opposite is true when formatting only dates.
227 options = options || {};
229 var locale = dojo.i18n.normalizeLocale(options.locale);
230 var formatLength = options.formatLength || 'short';
231 var bundle = dojo.date.locale._getGregorianBundle(locale);
233 var sauce = dojo.hitch(this, formatPattern, dateObject, bundle, options.fullYear);
234 if(options.selector == "year"){
235 // Special case as this is not yet driven by CLDR data
236 var year = dateObject.getFullYear();
237 if(locale.match(/^zh|^ja/)){
242 if(options.selector != "time"){
243 var datePattern = options.datePattern || bundle["dateFormat-"+formatLength];
244 if(datePattern){str.push(_processPattern(datePattern, sauce));}
246 if(options.selector != "date"){
247 var timePattern = options.timePattern || bundle["timeFormat-"+formatLength];
248 if(timePattern){str.push(_processPattern(timePattern, sauce));}
250 var result = str.join(" "); //TODO: use locale-specific pattern to assemble date + time
251 return result; // String
254 dojo.date.locale.regexp = function(/*dojo.date.locale.__FormatOptions?*/options){
256 // Builds the regular needed to parse a localized date
258 return dojo.date.locale._parseInfo(options).regexp; // String
261 dojo.date.locale._parseInfo = function(/*dojo.date.locale.__FormatOptions?*/options){
262 options = options || {};
263 var locale = dojo.i18n.normalizeLocale(options.locale);
264 var bundle = dojo.date.locale._getGregorianBundle(locale);
265 var formatLength = options.formatLength || 'short';
266 var datePattern = options.datePattern || bundle["dateFormat-" + formatLength];
267 var timePattern = options.timePattern || bundle["timeFormat-" + formatLength];
269 if(options.selector == 'date'){
270 pattern = datePattern;
271 }else if(options.selector == 'time'){
272 pattern = timePattern;
274 pattern = datePattern + ' ' + timePattern; //TODO: use locale-specific pattern to assemble date + time
278 var re = _processPattern(pattern, dojo.hitch(this, _buildDateTimeRE, tokens, bundle, options));
279 return {regexp: re, tokens: tokens, bundle: bundle};
282 dojo.date.locale.parse = function(/*String*/value, /*dojo.date.locale.__FormatOptions?*/options){
284 // Convert a properly formatted string to a primitive Date object,
285 // using locale-specific settings.
288 // Create a Date object from a string using a known localized pattern.
289 // By default, this method parses looking for both date and time in the string.
290 // Formatting patterns are chosen appropriate to the locale. Different
291 // formatting lengths may be chosen, with "full" used by default.
292 // Custom patterns may be used or registered with translations using
293 // the dojo.date.locale.addCustomFormats method.
295 // Formatting patterns are implemented using [the syntax described at
296 // unicode.org](http://www.unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns)
297 // When two digit years are used, a century is chosen according to a sliding
298 // window of 80 years before and 20 years after present year, for both `yy` and `yyyy` patterns.
299 // year < 100CE requires strict mode.
302 // A string representation of a date
304 var info = dojo.date.locale._parseInfo(options);
305 var tokens = info.tokens, bundle = info.bundle;
306 var re = new RegExp("^" + info.regexp + "$");
307 var match = re.exec(value);
308 if(!match){ return null; } // null
310 var widthList = ['abbr', 'wide', 'narrow'];
311 var result = [1970,0,1,0,0,0,0]; // will get converted to a Date at the end
313 var valid = dojo.every(match, function(v, i){
315 var token=tokens[i-1];
317 switch(token.charAt(0)){
319 if(l != 2 && options.strict){
320 //interpret year literally, so '5' would be 5 A.D.
325 //choose century to apply, according to a sliding window
326 //of 80 years before and 20 years after present year
327 var year = '' + new Date().getFullYear();
328 var century = year.substring(0, 2) * 100;
329 var cutoff = Math.min(Number(year.substring(2, 4)) + 20, 99);
330 var num = (v < cutoff) ? century + v : century - 100 + v;
333 //we expected 2 digits and got more...
337 //interpret literally, so '150' would be 150 A.D.
338 //also tolerate '1950', if 'yyyy' input passed to 'yy' format
345 var months = bundle['months-format-' + widthList[l-3]].concat();
347 //Tolerate abbreviating period in month part
348 //Case-insensitive comparison
349 v = v.replace(".","").toLowerCase();
350 months = dojo.map(months, function(s){ return s.replace(".","").toLowerCase(); } );
352 v = dojo.indexOf(months, v);
354 // console.debug("dojo.date.locale.parse: Could not parse month name: '" + v + "'.");
364 var days = bundle['days-format-' + widthList[l-3]].concat();
366 //Case-insensitive comparison
368 days = dojo.map(days, function(d){return d.toLowerCase();});
370 v = dojo.indexOf(days, v);
372 // console.debug("dojo.date.locale.parse: Could not parse weekday name: '" + v + "'.");
376 //TODO: not sure what to actually do with this input,
377 //in terms of setting something on the Date obj...?
378 //without more context, can't affect the actual date
379 //TODO: just validate?
388 var am = options.am || bundle.am;
389 var pm = options.pm || bundle.pm;
392 v = v.replace(period,'').toLowerCase();
393 am = am.replace(period,'').toLowerCase();
394 pm = pm.replace(period,'').toLowerCase();
396 if(options.strict && v != am && v != pm){
397 // console.debug("dojo.date.locale.parse: Could not parse am/pm part.");
401 // we might not have seen the hours field yet, so store the state and apply hour change later
402 amPm = (v == pm) ? 'p' : (v == am) ? 'a' : '';
404 case 'K': //hour (1-24)
405 if(v == 24){ v = 0; }
407 case 'h': //hour (1-12)
408 case 'H': //hour (0-23)
409 case 'k': //hour (0-11)
410 //TODO: strict bounds checking, padding
412 // console.debug("dojo.date.locale.parse: Illegal hours value");
416 //in the 12-hour case, adjusting for am/pm requires the 'a' part
417 //which could come before or after the hour, so we will adjust later
426 case 'S': //milliseconds
430 //TODO var firstDay = 0;
433 // console.debug("dojo.date.locale.parse: unsupported pattern char=" + token.charAt(0));
438 var hours = +result[3];
439 if(amPm === 'p' && hours < 12){
440 result[3] = hours + 12; //e.g., 3pm -> 15
441 }else if(amPm === 'a' && hours == 12){
442 result[3] = 0; //12am -> 0
445 //TODO: implement a getWeekday() method in order to test
446 //validity of input strings containing 'EEE' or 'EEEE'...
448 var dateObject = new Date(result[0], result[1], result[2], result[3], result[4], result[5], result[6]); // Date
450 dateObject.setFullYear(result[0]);
453 // Check for overflow. The Date() constructor normalizes things like April 32nd...
454 //TODO: why isn't this done for times as well?
455 var allTokens = tokens.join("");
457 (allTokens.indexOf('M') != -1 && dateObject.getMonth() != result[1]) ||
458 (allTokens.indexOf('d') != -1 && dateObject.getDate() != result[2])){
462 return dateObject; // Date
465 function _processPattern(pattern, applyPattern, applyLiteral, applyAll){
466 //summary: Process a pattern with literals in it
468 // Break up on single quotes, treat every other one as a literal, except '' which becomes '
469 var identity = function(x){return x;};
470 applyPattern = applyPattern || identity;
471 applyLiteral = applyLiteral || identity;
472 applyAll = applyAll || identity;
474 //split on single quotes (which escape literals in date format strings)
475 //but preserve escaped single quotes (e.g., o''clock)
476 var chunks = pattern.match(/(''|[^'])+/g);
479 dojo.forEach(chunks, function(chunk, i){
483 chunks[i]=(literal ? applyLiteral : applyPattern)(chunk);
487 return applyAll(chunks.join(''));
490 function _buildDateTimeRE(tokens, bundle, options, pattern){
491 pattern = dojo.regexp.escapeString(pattern);
492 if(!options.strict){ pattern = pattern.replace(" a", " ?a"); } // kludge to tolerate no space before am/pm
493 return pattern.replace(/([a-z])\1*/ig, function(match){
494 // Build a simple regexp. Avoid captures, which would ruin the tokens list
496 var c = match.charAt(0);
497 var l = match.length;
498 var p2 = '', p3 = '';
500 if(l > 1){ p2 = '0' + '{'+(l-1)+'}'; }
501 if(l > 2){ p3 = '0' + '{'+(l-2)+'}'; }
503 p2 = '0?'; p3 = '0{0,2}';
510 s = (l>2) ? '\\S+' : p2+'[1-9]|1[0-2]';
513 s = p2+'[1-9]|'+p3+'[1-9][0-9]|[12][0-9][0-9]|3[0-5][0-9]|36[0-6]';
516 s = p2+'[1-9]|[12]\\d|3[01]';
519 s = p2+'[1-9]|[1-4][0-9]|5[0-3]';
524 case 'h': //hour (1-12)
525 s = p2+'[1-9]|1[0-2]';
527 case 'k': //hour (0-11)
530 case 'H': //hour (0-23)
531 s = p2+'\\d|1\\d|2[0-3]';
533 case 'K': //hour (1-24)
534 s = p2+'[1-9]|1\\d|2[0-4]';
544 var am = options.am || bundle.am || 'AM';
545 var pm = options.pm || bundle.pm || 'PM';
550 if(am != am.toLowerCase()){ s += '|' + am.toLowerCase(); }
551 if(pm != pm.toLowerCase()){ s += '|' + pm.toLowerCase(); }
559 // console.debug("parse of date format, pattern=" + pattern);
562 if(tokens){ tokens.push(match); }
564 return "(" + s + ")"; // add capture
565 }).replace(/[\xa0 ]/g, "[\\s\\xa0]"); // normalize whitespace. Need explicit handling of \xa0 for IE.
570 var _customFormats = [];
571 dojo.date.locale.addCustomFormats = function(/*String*/packageName, /*String*/bundleName){
573 // Add a reference to a bundle containing localized custom formats to be
574 // used by date/time formatting and parsing routines.
577 // The user may add custom localized formats where the bundle has properties following the
578 // same naming convention used by dojo.cldr: `dateFormat-xxxx` / `timeFormat-xxxx`
579 // The pattern string should match the format used by the CLDR.
580 // See dojo.date.locale.format() for details.
581 // The resources must be loaded by dojo.requireLocalization() prior to use
583 _customFormats.push({pkg:packageName,name:bundleName});
586 dojo.date.locale._getGregorianBundle = function(/*String*/locale){
588 dojo.forEach(_customFormats, function(desc){
589 var bundle = dojo.i18n.getLocalization(desc.pkg, desc.name, locale);
590 gregorian = dojo.mixin(gregorian, bundle);
592 return gregorian; /*Object*/
596 dojo.date.locale.addCustomFormats("dojo.cldr","gregorian");
598 dojo.date.locale.getNames = function(/*String*/item, /*String*/type, /*String?*/use, /*String?*/locale){
600 // Used to get localized strings from dojo.cldr for day or month names.
603 // 'months' || 'days'
605 // 'wide' || 'narrow' || 'abbr' (e.g. "Monday", "Mon", or "M" respectively, in English)
607 // 'standAlone' || 'format' (default)
609 // override locale used to find the names
612 var lookup = dojo.date.locale._getGregorianBundle(locale);
613 var props = [item, use, type];
614 if(use == 'standAlone'){
615 label = lookup[props.join('-')];
619 // return by copy so changes won't be made accidentally to the in-memory model
620 return (label || lookup[props.join('-')]).concat(); /*Array*/
623 dojo.date.locale.isWeekend = function(/*Date?*/dateObject, /*String?*/locale){
625 // Determines if the date falls on a weekend, according to local custom.
627 var weekend = dojo.cldr.supplemental.getWeekend(locale);
628 var day = (dateObject || new Date()).getDay();
629 if(weekend.end < weekend.start){
631 if(day < weekend.start){ day += 7; }
633 return day >= weekend.start && day <= weekend.end; // Boolean
636 // These are used only by format and strftime. Do they need to be public? Which module should they go in?
638 dojo.date.locale._getDayOfYear = function(/*Date*/dateObject){
639 // summary: gets the day of the year as represented by dateObject
640 return dojo.date.difference(new Date(dateObject.getFullYear(), 0, 1), dateObject) + 1; // Number
643 dojo.date.locale._getWeekOfYear = function(/*Date*/dateObject, /*Number*/firstDayOfWeek){
644 if(arguments.length == 1){ firstDayOfWeek = 0; } // Sunday
646 var firstDayOfYear = new Date(dateObject.getFullYear(), 0, 1).getDay();
647 var adj = (firstDayOfYear - firstDayOfWeek + 7) % 7;
648 var week = Math.floor((dojo.date.locale._getDayOfYear(dateObject) + adj - 1) / 7);
650 // if year starts on the specified day, start counting weeks at 1
651 if(firstDayOfYear == firstDayOfWeek){ week++; }
653 return week; // Number