1
dojo.provide("dojo.date.locale");
3
// Localization methods for Date. Honor local customs using locale-dependent dojo.cldr data.
5
dojo.require("dojo.date");
6
dojo.require("dojo.cldr.supplemental");
7
dojo.require("dojo.regexp");
8
dojo.require("dojo.string");
9
dojo.require("dojo.i18n");
11
// Load the bundles containing localization information for
13
dojo.requireLocalization("dojo.cldr", "gregorian");
15
//NOTE: Everything in this module assumes Gregorian calendars.
16
// Other calendars will be implemented in separate modules.
19
// Format a pattern without literals
20
function formatPattern(dateObject, bundle, fullYear, pattern){
21
return pattern.replace(/([a-z])\1*/ig, function(match){
23
var c = match.charAt(0);
25
var widthList = ["abbr", "wide", "narrow"];
28
s = bundle[(l < 4) ? "eraAbbr" : "eraNames"][dateObject.getFullYear() < 0 ? 0 : 1];
31
s = dateObject.getFullYear();
37
s = String(s); s = s.substr(s.length - 2);
47
s = Math.ceil((dateObject.getMonth()+1)/3);
52
// case 3: case 4: // unimplemented
56
var m = dateObject.getMonth();
60
var propM = ["months", "format", widthList[l-3]].join("-");
66
s = dojo.date.locale._getWeekOfYear(dateObject, firstDay); pad = true;
69
s = dateObject.getDate(); pad = true;
72
s = dojo.date.locale._getDayOfYear(dateObject); pad = true;
75
var d = dateObject.getDay();
79
var propD = ["days", "format", widthList[l-3]].join("-");
84
var timePeriod = (dateObject.getHours() < 12) ? 'am' : 'pm';
85
s = bundle[timePeriod];
91
var h = dateObject.getHours();
92
// strange choices in the date format make it impossible to write this succinctly
110
s = dateObject.getMinutes(); pad = true;
113
s = dateObject.getSeconds(); pad = true;
116
s = Math.round(dateObject.getMilliseconds() * Math.pow(10, l-3)); pad = true;
118
case 'v': // FIXME: don't know what this is. seems to be same as z?
120
// We only have one timezone to offer; the one from the browser
121
s = dojo.date.getTimezoneName(dateObject);
124
// fallthrough... use GMT if tz not available
126
var offset = dateObject.getTimezoneOffset();
128
(offset<=0 ? "+" : "-"),
129
dojo.string.pad(Math.floor(Math.abs(offset)/60), 2),
130
dojo.string.pad(Math.abs(offset)% 60, 2)
133
tz.splice(0, 0, "GMT");
134
tz.splice(3, 0, ":");
138
// case 'Y': case 'u': case 'W': case 'F': case 'g': case 'A': case 'e':
139
// console.log(match+" modifier unimplemented");
141
throw new Error("dojo.date.locale.format: invalid pattern char: "+pattern);
143
if(pad){ s = dojo.string.pad(s, l); }
149
dojo.date.locale.__FormatOptions = function(){
151
// choice of 'time','date' (default: date and time)
152
// formatLength: String
153
// choice of long, short, medium or full (plus any custom additions). Defaults to 'short'
154
// datePattern:String
155
// override pattern with this string
156
// timePattern:String
157
// override pattern with this string
159
// override strings for am in times
161
// override strings for pm in times
163
// override the locale used to determine formatting rules
165
// (format only) use 4 digit years whenever 2 digit years are called for
167
// (parse only) strict parsing, off by default
168
this.selector = selector;
169
this.formatLength = formatLength;
170
this.datePattern = datePattern;
171
this.timePattern = timePattern;
174
this.locale = locale;
175
this.fullYear = fullYear;
176
this.strict = strict;
180
dojo.date.locale.format = function(/*Date*/dateObject, /*dojo.date.locale.__FormatOptions?*/options){
182
// Format a Date object as a String, using locale-specific settings.
185
// Create a string from a Date object using a known localized pattern.
186
// By default, this method formats both date and time from dateObject.
187
// Formatting patterns are chosen appropriate to the locale. Different
188
// formatting lengths may be chosen, with "full" used by default.
189
// Custom patterns may be used or registered with translations using
190
// the dojo.date.locale.addCustomFormats method.
191
// Formatting patterns are implemented using [the syntax described at
192
// unicode.org](http://www.unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns)
195
// the date and/or time to be formatted. If a time only is formatted,
196
// the values in the year, month, and day fields are irrelevant. The
197
// opposite is true when formatting only dates.
199
options = options || {};
201
var locale = dojo.i18n.normalizeLocale(options.locale);
202
var formatLength = options.formatLength || 'short';
203
var bundle = dojo.date.locale._getGregorianBundle(locale);
205
var sauce = dojo.hitch(this, formatPattern, dateObject, bundle, options.fullYear);
206
if(options.selector == "year"){
207
// Special case as this is not yet driven by CLDR data
208
var year = dateObject.getFullYear();
209
if(locale.match(/^zh|^ja/)){
214
if(options.selector != "time"){
215
var datePattern = options.datePattern || bundle["dateFormat-"+formatLength];
216
if(datePattern){str.push(_processPattern(datePattern, sauce));}
218
if(options.selector != "date"){
219
var timePattern = options.timePattern || bundle["timeFormat-"+formatLength];
220
if(timePattern){str.push(_processPattern(timePattern, sauce));}
222
var result = str.join(" "); //TODO: use locale-specific pattern to assemble date + time
223
return result; // String
226
dojo.date.locale.regexp = function(/*dojo.date.locale.__FormatOptions?*/options){
228
// Builds the regular needed to parse a localized date
230
return dojo.date.locale._parseInfo(options).regexp; // String
233
dojo.date.locale._parseInfo = function(/*dojo.date.locale.__FormatOptions?*/options){
234
options = options || {};
235
var locale = dojo.i18n.normalizeLocale(options.locale);
236
var bundle = dojo.date.locale._getGregorianBundle(locale);
237
var formatLength = options.formatLength || 'short';
238
var datePattern = options.datePattern || bundle["dateFormat-" + formatLength];
239
var timePattern = options.timePattern || bundle["timeFormat-" + formatLength];
241
if(options.selector == 'date'){
242
pattern = datePattern;
243
}else if(options.selector == 'time'){
244
pattern = timePattern;
246
pattern = datePattern + ' ' + timePattern; //TODO: use locale-specific pattern to assemble date + time
250
var re = _processPattern(pattern, dojo.hitch(this, _buildDateTimeRE, tokens, bundle, options));
251
return {regexp: re, tokens: tokens, bundle: bundle};
254
dojo.date.locale.parse = function(/*String*/value, /*dojo.date.locale.__FormatOptions?*/options){
256
// Convert a properly formatted string to a primitive Date object,
257
// using locale-specific settings.
260
// Create a Date object from a string using a known localized pattern.
261
// By default, this method parses looking for both date and time in the string.
262
// Formatting patterns are chosen appropriate to the locale. Different
263
// formatting lengths may be chosen, with "full" used by default.
264
// Custom patterns may be used or registered with translations using
265
// the dojo.date.locale.addCustomFormats method.
267
// Formatting patterns are implemented using [the syntax described at
268
// unicode.org](http://www.unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns)
269
// When two digit years are used, a century is chosen according to a sliding
270
// window of 80 years before and 20 years after present year, for both `yy` and `yyyy` patterns.
271
// year < 100CE requires strict mode.
274
// A string representation of a date
276
var info = dojo.date.locale._parseInfo(options);
277
var tokens = info.tokens, bundle = info.bundle;
278
var re = new RegExp("^" + info.regexp + "$", info.strict ? "" : "i");
279
var match = re.exec(value);
280
if(!match){ return null; } // null
282
var widthList = ['abbr', 'wide', 'narrow'];
283
var result = [1970,0,1,0,0,0,0]; // will get converted to a Date at the end
285
var valid = dojo.every(match, function(v, i){
287
var token=tokens[i-1];
289
switch(token.charAt(0)){
291
if(l != 2 && options.strict){
292
//interpret year literally, so '5' would be 5 A.D.
297
//choose century to apply, according to a sliding window
298
//of 80 years before and 20 years after present year
299
var year = '' + new Date().getFullYear();
300
var century = year.substring(0, 2) * 100;
301
var cutoff = Math.min(Number(year.substring(2, 4)) + 20, 99);
302
var num = (v < cutoff) ? century + v : century - 100 + v;
305
//we expected 2 digits and got more...
309
//interpret literally, so '150' would be 150 A.D.
310
//also tolerate '1950', if 'yyyy' input passed to 'yy' format
317
var months = bundle['months-format-' + widthList[l-3]].concat();
319
//Tolerate abbreviating period in month part
320
//Case-insensitive comparison
321
v = v.replace(".","").toLowerCase();
322
months = dojo.map(months, function(s){ return s.replace(".","").toLowerCase(); } );
324
v = dojo.indexOf(months, v);
326
// console.log("dojo.date.locale.parse: Could not parse month name: '" + v + "'.");
336
var days = bundle['days-format-' + widthList[l-3]].concat();
338
//Case-insensitive comparison
340
days = dojo.map(days, function(d){return d.toLowerCase();});
342
v = dojo.indexOf(days, v);
344
// console.log("dojo.date.locale.parse: Could not parse weekday name: '" + v + "'.");
348
//TODO: not sure what to actually do with this input,
349
//in terms of setting something on the Date obj...?
350
//without more context, can't affect the actual date
351
//TODO: just validate?
360
var am = options.am || bundle.am;
361
var pm = options.pm || bundle.pm;
364
v = v.replace(period,'').toLowerCase();
365
am = am.replace(period,'').toLowerCase();
366
pm = pm.replace(period,'').toLowerCase();
368
if(options.strict && v != am && v != pm){
369
// console.log("dojo.date.locale.parse: Could not parse am/pm part.");
373
// we might not have seen the hours field yet, so store the state and apply hour change later
374
amPm = (v == pm) ? 'p' : (v == am) ? 'a' : '';
376
case 'K': //hour (1-24)
377
if(v == 24){ v = 0; }
379
case 'h': //hour (1-12)
380
case 'H': //hour (0-23)
381
case 'k': //hour (0-11)
382
//TODO: strict bounds checking, padding
384
// console.log("dojo.date.locale.parse: Illegal hours value");
388
//in the 12-hour case, adjusting for am/pm requires the 'a' part
389
//which could come before or after the hour, so we will adjust later
398
case 'S': //milliseconds
402
//TODO var firstDay = 0;
405
// console.log("dojo.date.locale.parse: unsupported pattern char=" + token.charAt(0));
410
var hours = +result[3];
411
if(amPm === 'p' && hours < 12){
412
result[3] = hours + 12; //e.g., 3pm -> 15
413
}else if(amPm === 'a' && hours == 12){
414
result[3] = 0; //12am -> 0
417
//TODO: implement a getWeekday() method in order to test
418
//validity of input strings containing 'EEE' or 'EEEE'...
420
var dateObject = new Date(result[0], result[1], result[2], result[3], result[4], result[5], result[6]); // Date
422
dateObject.setFullYear(result[0]);
425
// Check for overflow. The Date() constructor normalizes things like April 32nd...
426
//TODO: why isn't this done for times as well?
427
var allTokens = tokens.join("");
429
(allTokens.indexOf('M') != -1 && dateObject.getMonth() != result[1]) ||
430
(allTokens.indexOf('d') != -1 && dateObject.getDate() != result[2])){
434
return dateObject; // Date
437
function _processPattern(pattern, applyPattern, applyLiteral, applyAll){
438
//summary: Process a pattern with literals in it
440
// Break up on single quotes, treat every other one as a literal, except '' which becomes '
441
var identity = function(x){return x;};
442
applyPattern = applyPattern || identity;
443
applyLiteral = applyLiteral || identity;
444
applyAll = applyAll || identity;
446
//split on single quotes (which escape literals in date format strings)
447
//but preserve escaped single quotes (e.g., o''clock)
448
var chunks = pattern.match(/(''|[^'])+/g);
449
var literal = pattern.charAt(0) == "'";
451
dojo.forEach(chunks, function(chunk, i){
455
chunks[i]=(literal ? applyLiteral : applyPattern)(chunk);
459
return applyAll(chunks.join(''));
462
function _buildDateTimeRE(tokens, bundle, options, pattern){
463
pattern = dojo.regexp.escapeString(pattern);
464
if(!options.strict){ pattern = pattern.replace(" a", " ?a"); } // kludge to tolerate no space before am/pm
465
return pattern.replace(/([a-z])\1*/ig, function(match){
466
// Build a simple regexp. Avoid captures, which would ruin the tokens list
468
var c = match.charAt(0);
469
var l = match.length;
470
var p2 = '', p3 = '';
472
if(l > 1){ p2 = '0' + '{'+(l-1)+'}'; }
473
if(l > 2){ p3 = '0' + '{'+(l-2)+'}'; }
475
p2 = '0?'; p3 = '0{0,2}';
482
s = (l>2) ? '\\S+?' : p2+'[1-9]|1[0-2]';
485
s = p2+'[1-9]|'+p3+'[1-9][0-9]|[12][0-9][0-9]|3[0-5][0-9]|36[0-6]';
488
s = '[12]\\d|'+p2+'[1-9]|3[01]';
491
s = p2+'[1-9]|[1-4][0-9]|5[0-3]';
496
case 'h': //hour (1-12)
497
s = p2+'[1-9]|1[0-2]';
499
case 'k': //hour (0-11)
502
case 'H': //hour (0-23)
503
s = p2+'\\d|1\\d|2[0-3]';
505
case 'K': //hour (1-24)
506
s = p2+'[1-9]|1\\d|2[0-4]';
516
var am = options.am || bundle.am || 'AM';
517
var pm = options.pm || bundle.pm || 'PM';
522
if(am != am.toLowerCase()){ s += '|' + am.toLowerCase(); }
523
if(pm != pm.toLowerCase()){ s += '|' + pm.toLowerCase(); }
524
if(s.indexOf('.') != -1){ s += '|' + s.replace(/\./g, ""); }
526
s = s.replace(/\./g, "\\.");
533
// console.log("parse of date format, pattern=" + pattern);
536
if(tokens){ tokens.push(match); }
538
return "(" + s + ")"; // add capture
539
}).replace(/[\xa0 ]/g, "[\\s\\xa0]"); // normalize whitespace. Need explicit handling of \xa0 for IE.
544
var _customFormats = [];
545
dojo.date.locale.addCustomFormats = function(/*String*/packageName, /*String*/bundleName){
547
// Add a reference to a bundle containing localized custom formats to be
548
// used by date/time formatting and parsing routines.
551
// The user may add custom localized formats where the bundle has properties following the
552
// same naming convention used by dojo.cldr: `dateFormat-xxxx` / `timeFormat-xxxx`
553
// The pattern string should match the format used by the CLDR.
554
// See dojo.date.locale.format() for details.
555
// The resources must be loaded by dojo.requireLocalization() prior to use
557
_customFormats.push({pkg:packageName,name:bundleName});
560
dojo.date.locale._getGregorianBundle = function(/*String*/locale){
562
dojo.forEach(_customFormats, function(desc){
563
var bundle = dojo.i18n.getLocalization(desc.pkg, desc.name, locale);
564
gregorian = dojo.mixin(gregorian, bundle);
566
return gregorian; /*Object*/
570
dojo.date.locale.addCustomFormats("dojo.cldr","gregorian");
572
dojo.date.locale.getNames = function(/*String*/item, /*String*/type, /*String?*/context, /*String?*/locale){
574
// Used to get localized strings from dojo.cldr for day or month names.
577
// 'months' || 'days'
579
// 'wide' || 'narrow' || 'abbr' (e.g. "Monday", "Mon", or "M" respectively, in English)
581
// 'standAlone' || 'format' (default)
583
// override locale used to find the names
586
var lookup = dojo.date.locale._getGregorianBundle(locale);
587
var props = [item, context, type];
588
if(context == 'standAlone'){
589
var key = props.join('-');
591
// Fall back to 'format' flavor of name
592
if(label[0] == 1){ label = undefined; } // kludge, in the absense of real aliasing support in dojo.cldr
596
// return by copy so changes won't be made accidentally to the in-memory model
597
return (label || lookup[props.join('-')]).concat(); /*Array*/
600
dojo.date.locale.displayPattern = function(/*String*/fixedPattern, /*String?*/locale){
602
// Provides a localized representation of a date/time pattern string
605
// Takes a date/time pattern string like "MM/dd/yyyy" and substitutes
606
// the letters appropriate to show a user in a particular locale, as
607
// defined in [the CLDR specification](http://www.unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns)
609
// A date string using symbols from this set: "GyMdkHmsSEDFwWahKzYeugAZvcL"
611
// use a special locale, otherwise takes the default
613
var fixed = "GyMdkHmsSEDFwWahKzYeugAZvcL",
614
local = dojo.date.locale._getGregorianBundle(locale).patternChars;
615
return dojo.map(fixedPattern, function(c){
616
var i = fixed.indexOf(c);
617
return i < 0 ? c : local.charAt(i);
618
}).join(""); // String
621
dojo.date.locale.isWeekend = function(/*Date?*/dateObject, /*String?*/locale){
623
// Determines if the date falls on a weekend, according to local custom.
625
var weekend = dojo.cldr.supplemental.getWeekend(locale);
626
var day = (dateObject || new Date()).getDay();
627
if(weekend.end < weekend.start){
629
if(day < weekend.start){ day += 7; }
631
return day >= weekend.start && day <= weekend.end; // Boolean
634
// These are used only by format and strftime. Do they need to be public? Which module should they go in?
636
dojo.date.locale._getDayOfYear = function(/*Date*/dateObject){
637
// summary: gets the day of the year as represented by dateObject
638
return dojo.date.difference(new Date(dateObject.getFullYear(), 0, 1, dateObject.getHours()), dateObject) + 1; // Number
641
dojo.date.locale._getWeekOfYear = function(/*Date*/dateObject, /*Number*/firstDayOfWeek){
642
if(arguments.length == 1){ firstDayOfWeek = 0; } // Sunday
644
var firstDayOfYear = new Date(dateObject.getFullYear(), 0, 1).getDay();
645
var adj = (firstDayOfYear - firstDayOfWeek + 7) % 7;
646
var week = Math.floor((dojo.date.locale._getDayOfYear(dateObject) + adj - 1) / 7);
648
// if year starts on the specified day, start counting weeks at 1
649
if(firstDayOfYear == firstDayOfWeek){ week++; }
651
return week; // Number