]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/date/locale.js.uncompressed.js
1 define("dojo/date/locale", [
6 "../cldr/supplemental",
9 "../i18n!../cldr/nls/gregorian"
10 ], function(dojo
, lang
, array
, date
, cldr
, regexp
, string
, gregorian
) {
14 // This modules defines dojo.date.locale, localization methods for Date.
16 lang
.getObject("date.locale", true, dojo
);
18 // Localization methods for Date. Honor local customs using locale-dependent dojo.cldr data.
20 // Load the bundles containing localization information for
23 //NOTE: Everything in this module assumes Gregorian calendars.
24 // Other calendars will be implemented in separate modules.
26 // Format a pattern without literals
27 function formatPattern(dateObject
, bundle
, options
, pattern
){
28 return pattern
.replace(/([a-z])\1*/ig, function(match
){
32 widthList
= ["abbr", "wide", "narrow"];
35 s
= bundle
[(l
< 4) ? "eraAbbr" : "eraNames"][dateObject
.getFullYear() < 0 ? 0 : 1];
38 s
= dateObject
.getFullYear();
43 if(!options
.fullYear
){
44 s
= String(s
); s
= s
.substr(s
.length
- 2);
54 s
= Math
.ceil((dateObject
.getMonth()+1)/3);
59 // case 3: case 4: // unimplemented
63 var m
= dateObject
.getMonth();
67 var propM
= ["months", "format", widthList
[l
-3]].join("-");
73 s
= dojo
.date
.locale
._getWeekOfYear(dateObject
, firstDay
); pad
= true;
76 s
= dateObject
.getDate(); pad
= true;
79 s
= dojo
.date
.locale
._getDayOfYear(dateObject
); pad
= true;
82 var d
= dateObject
.getDay();
86 var propD
= ["days", "format", widthList
[l
-3]].join("-");
91 var timePeriod
= (dateObject
.getHours() < 12) ? 'am' : 'pm';
92 s
= options
[timePeriod
] || bundle
['dayPeriods-format-wide-' + timePeriod
];
98 var h
= dateObject
.getHours();
99 // strange choices in the date format make it impossible to write this succinctly
117 s
= dateObject
.getMinutes(); pad
= true;
120 s
= dateObject
.getSeconds(); pad
= true;
123 s
= Math
.round(dateObject
.getMilliseconds() * Math
.pow(10, l
-3)); pad
= true;
125 case 'v': // FIXME: don't know what this is. seems to be same as z?
127 // We only have one timezone to offer; the one from the browser
128 s
= dojo
.date
.locale
._getZone(dateObject
, true, options
);
131 // fallthrough... use GMT if tz not available
133 var offset
= dojo
.date
.locale
._getZone(dateObject
, false, options
);
135 (offset
<=0 ? "+" : "-"),
136 string
.pad(Math
.floor(Math
.abs(offset
)/60), 2),
137 string
.pad(Math
.abs(offset
)% 60, 2)
140 tz
.splice(0, 0, "GMT");
141 tz
.splice(3, 0, ":");
145 // case 'Y': case 'u': case 'W': case 'F': case 'g': case 'A': case 'e':
146 // console.log(match+" modifier unimplemented");
148 throw new Error("dojo.date.locale.format: invalid pattern char: "+pattern
);
150 if(pad
){ s
= string
.pad(s
, l
); }
156 dojo.date.locale.__FormatOptions = function(){
158 // choice of 'time','date' (default: date and time)
159 // formatLength: String
160 // choice of long, short, medium or full (plus any custom additions). Defaults to 'short'
161 // datePattern:String
162 // override pattern with this string
163 // timePattern:String
164 // override pattern with this string
166 // override strings for am in times
168 // override strings for pm in times
170 // override the locale used to determine formatting rules
172 // (format only) use 4 digit years whenever 2 digit years are called for
174 // (parse only) strict parsing, off by default
175 this.selector = selector;
176 this.formatLength = formatLength;
177 this.datePattern = datePattern;
178 this.timePattern = timePattern;
181 this.locale = locale;
182 this.fullYear = fullYear;
183 this.strict = strict;
187 dojo
.date
.locale
._getZone = function(/*Date*/dateObject
, /*boolean*/getName
, /*dojo.date.locale.__FormatOptions?*/options
){
189 // Returns the zone (or offset) for the given date and options. This
190 // is broken out into a separate function so that it can be overridden
191 // by timezone-aware code.
194 // the date and/or time being formatted.
197 // Whether to return the timezone string (if true), or the offset (if false)
200 // The options being used for formatting
202 return date
.getTimezoneName(dateObject
);
204 return dateObject
.getTimezoneOffset();
209 dojo
.date
.locale
.format = function(/*Date*/dateObject
, /*dojo.date.locale.__FormatOptions?*/options
){
211 // Format a Date object as a String, using locale-specific settings.
214 // Create a string from a Date object using a known localized pattern.
215 // By default, this method formats both date and time from dateObject.
216 // Formatting patterns are chosen appropriate to the locale. Different
217 // formatting lengths may be chosen, with "full" used by default.
218 // Custom patterns may be used or registered with translations using
219 // the dojo.date.locale.addCustomFormats method.
220 // Formatting patterns are implemented using [the syntax described at
221 // unicode.org](http://www.unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns)
224 // the date and/or time to be formatted. If a time only is formatted,
225 // the values in the year, month, and day fields are irrelevant. The
226 // opposite is true when formatting only dates.
228 options
= options
|| {};
230 var locale
= dojo
.i18n
.normalizeLocale(options
.locale
),
231 formatLength
= options
.formatLength
|| 'short',
232 bundle
= dojo
.date
.locale
._getGregorianBundle(locale
),
234 sauce
= lang
.hitch(this, formatPattern
, dateObject
, bundle
, options
);
235 if(options
.selector
== "year"){
236 return _processPattern(bundle
["dateFormatItem-yyyy"] || "yyyy", sauce
);
239 if(options
.selector
!= "date"){
240 pattern
= options
.timePattern
|| bundle
["timeFormat-"+formatLength
];
241 if(pattern
){str
.push(_processPattern(pattern
, sauce
));}
243 if(options
.selector
!= "time"){
244 pattern
= options
.datePattern
|| bundle
["dateFormat-"+formatLength
];
245 if(pattern
){str
.push(_processPattern(pattern
, sauce
));}
248 return str
.length
== 1 ? str
[0] : bundle
["dateTimeFormat-"+formatLength
].replace(/\{(\d+)\}/g,
249 function(match
, key
){ return str
[key
]; }); // String
252 dojo
.date
.locale
.regexp = function(/*dojo.date.locale.__FormatOptions?*/options
){
254 // Builds the regular needed to parse a localized date
256 return dojo
.date
.locale
._parseInfo(options
).regexp
; // String
259 dojo
.date
.locale
._parseInfo = function(/*dojo.date.locale.__FormatOptions?*/options
){
260 options
= options
|| {};
261 var locale
= dojo
.i18n
.normalizeLocale(options
.locale
),
262 bundle
= dojo
.date
.locale
._getGregorianBundle(locale
),
263 formatLength
= options
.formatLength
|| 'short',
264 datePattern
= options
.datePattern
|| bundle
["dateFormat-" + formatLength
],
265 timePattern
= options
.timePattern
|| bundle
["timeFormat-" + formatLength
],
267 if(options
.selector
== 'date'){
268 pattern
= datePattern
;
269 }else if(options
.selector
== 'time'){
270 pattern
= timePattern
;
272 pattern
= bundle
["dateTimeFormat-"+formatLength
].replace(/\{(\d+)\}/g,
273 function(match
, key
){ return [timePattern
, datePattern
][key
]; });
277 re
= _processPattern(pattern
, lang
.hitch(this, _buildDateTimeRE
, tokens
, bundle
, options
));
278 return {regexp
: re
, tokens
: tokens
, bundle
: bundle
};
281 dojo
.date
.locale
.parse = function(/*String*/value
, /*dojo.date.locale.__FormatOptions?*/options
){
283 // Convert a properly formatted string to a primitive Date object,
284 // using locale-specific settings.
287 // Create a Date object from a string using a known localized pattern.
288 // By default, this method parses looking for both date and time in the string.
289 // Formatting patterns are chosen appropriate to the locale. Different
290 // formatting lengths may be chosen, with "full" used by default.
291 // Custom patterns may be used or registered with translations using
292 // the dojo.date.locale.addCustomFormats method.
294 // Formatting patterns are implemented using [the syntax described at
295 // unicode.org](http://www.unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns)
296 // When two digit years are used, a century is chosen according to a sliding
297 // window of 80 years before and 20 years after present year, for both `yy` and `yyyy` patterns.
298 // year < 100CE requires strict mode.
301 // A string representation of a date
303 // remove non-printing bidi control chars from input and pattern
304 var controlChars
= /[\u200E\u200F\u202A\u202E]/g,
305 info
= dojo
.date
.locale
._parseInfo(options
),
306 tokens
= info
.tokens
, bundle
= info
.bundle
,
307 re
= new RegExp("^" + info
.regexp
.replace(controlChars
, "") + "$",
308 info
.strict
? "" : "i"),
309 match
= re
.exec(value
&& value
.replace(controlChars
, ""));
311 if(!match
){ return null; } // null
313 var widthList
= ['abbr', 'wide', 'narrow'],
314 result
= [1970,0,1,0,0,0,0], // will get converted to a Date at the end
316 valid
= dojo
.every(match
, function(v
, i
){
318 var token
=tokens
[i
-1];
320 switch(token
.charAt(0)){
322 if(l
!= 2 && options
.strict
){
323 //interpret year literally, so '5' would be 5 A.D.
328 //choose century to apply, according to a sliding window
329 //of 80 years before and 20 years after present year
330 var year
= '' + new Date().getFullYear(),
331 century
= year
.substring(0, 2) * 100,
332 cutoff
= Math
.min(Number(year
.substring(2, 4)) + 20, 99);
333 result
[0] = (v
< cutoff
) ? century
+ v
: century
- 100 + v
;
335 //we expected 2 digits and got more...
339 //interpret literally, so '150' would be 150 A.D.
340 //also tolerate '1950', if 'yyyy' input passed to 'yy' format
347 var months
= bundle
['months-format-' + widthList
[l
-3]].concat();
349 //Tolerate abbreviating period in month part
350 //Case-insensitive comparison
351 v
= v
.replace(".","").toLowerCase();
352 months
= dojo
.map(months
, function(s
){ return s
.replace(".","").toLowerCase(); } );
354 v
= dojo
.indexOf(months
, v
);
356 // console.log("dojo.date.locale.parse: Could not parse month name: '" + v + "'.");
366 var days
= bundle
['days-format-' + widthList
[l
-3]].concat();
368 //Case-insensitive comparison
370 days
= dojo
.map(days
, function(d
){return d
.toLowerCase();});
372 v
= dojo
.indexOf(days
, v
);
374 // console.log("dojo.date.locale.parse: Could not parse weekday name: '" + v + "'.");
378 //TODO: not sure what to actually do with this input,
379 //in terms of setting something on the Date obj...?
380 //without more context, can't affect the actual date
381 //TODO: just validate?
390 var am
= options
.am
|| bundle
['dayPeriods-format-wide-am'],
391 pm
= options
.pm
|| bundle
['dayPeriods-format-wide-pm'];
394 v
= v
.replace(period
,'').toLowerCase();
395 am
= am
.replace(period
,'').toLowerCase();
396 pm
= pm
.replace(period
,'').toLowerCase();
398 if(options
.strict
&& v
!= am
&& v
!= pm
){
399 // console.log("dojo.date.locale.parse: Could not parse am/pm part.");
403 // we might not have seen the hours field yet, so store the state and apply hour change later
404 amPm
= (v
== pm
) ? 'p' : (v
== am
) ? 'a' : '';
406 case 'K': //hour (1-24)
407 if(v
== 24){ v
= 0; }
409 case 'h': //hour (1-12)
410 case 'H': //hour (0-23)
411 case 'k': //hour (0-11)
412 //TODO: strict bounds checking, padding
414 // console.log("dojo.date.locale.parse: Illegal hours value");
418 //in the 12-hour case, adjusting for am/pm requires the 'a' part
419 //which could come before or after the hour, so we will adjust later
428 case 'S': //milliseconds
432 //TODO var firstDay = 0;
435 // console.log("dojo.date.locale.parse: unsupported pattern char=" + token.charAt(0));
440 var hours
= +result
[3];
441 if(amPm
=== 'p' && hours
< 12){
442 result
[3] = hours
+ 12; //e.g., 3pm -> 15
443 }else if(amPm
=== 'a' && hours
== 12){
444 result
[3] = 0; //12am -> 0
447 //TODO: implement a getWeekday() method in order to test
448 //validity of input strings containing 'EEE' or 'EEEE'...
450 var dateObject
= new Date(result
[0], result
[1], result
[2], result
[3], result
[4], result
[5], result
[6]); // Date
452 dateObject
.setFullYear(result
[0]);
455 // Check for overflow. The Date() constructor normalizes things like April 32nd...
456 //TODO: why isn't this done for times as well?
457 var allTokens
= tokens
.join(""),
458 dateToken
= allTokens
.indexOf('d') != -1,
459 monthToken
= allTokens
.indexOf('M') != -1;
462 (monthToken
&& dateObject
.getMonth() > result
[1]) ||
463 (dateToken
&& dateObject
.getDate() > result
[2])){
467 // Check for underflow, due to DST shifts. See #9366
468 // This assumes a 1 hour dst shift correction at midnight
469 // We could compare the timezone offset after the shift and add the difference instead.
470 if((monthToken
&& dateObject
.getMonth() < result
[1]) ||
471 (dateToken
&& dateObject
.getDate() < result
[2])){
472 dateObject
= date
.add(dateObject
, "hour", 1);
475 return dateObject
; // Date
478 function _processPattern(pattern
, applyPattern
, applyLiteral
, applyAll
){
479 //summary: Process a pattern with literals in it
481 // Break up on single quotes, treat every other one as a literal, except '' which becomes '
482 var identity = function(x
){return x
;};
483 applyPattern
= applyPattern
|| identity
;
484 applyLiteral
= applyLiteral
|| identity
;
485 applyAll
= applyAll
|| identity
;
487 //split on single quotes (which escape literals in date format strings)
488 //but preserve escaped single quotes (e.g., o''clock)
489 var chunks
= pattern
.match(/(''|[^'])+/g),
490 literal
= pattern
.charAt(0) == "'";
492 dojo
.forEach(chunks
, function(chunk
, i
){
496 chunks
[i
]=(literal
? applyLiteral
: applyPattern
)(chunk
.replace(/''/g, "'"));
500 return applyAll(chunks
.join(''));
503 function _buildDateTimeRE(tokens
, bundle
, options
, pattern
){
504 pattern
= regexp
.escapeString(pattern
);
505 if(!options
.strict
){ pattern
= pattern
.replace(" a", " ?a"); } // kludge to tolerate no space before am/pm
506 return pattern
.replace(/([a-z])\1*/ig, function(match
){
507 // Build a simple regexp. Avoid captures, which would ruin the tokens list
513 if(l
> 1){ p2
= '0' + '{'+(l
-1)+'}'; }
514 if(l
> 2){ p3
= '0' + '{'+(l
-2)+'}'; }
516 p2
= '0?'; p3
= '0{0,2}';
523 s
= (l
>2) ? '\\S+?' : '1[0-2]|'+p2
+'[1-9]';
526 s
= '[12][0-9][0-9]|3[0-5][0-9]|36[0-6]|'+p2
+'[1-9][0-9]|'+p3
+'[1-9]';
529 s
= '3[01]|[12]\\d|'+p2
+'[1-9]';
532 s
= '[1-4][0-9]|5[0-3]|'+p2
+'[1-9]';
537 case 'h': //hour (1-12)
538 s
= '1[0-2]|'+p2
+'[1-9]';
540 case 'k': //hour (0-11)
541 s
= '1[01]|'+p2
+'\\d';
543 case 'H': //hour (0-23)
544 s
= '1\\d|2[0-3]|'+p2
+'\\d';
546 case 'K': //hour (1-24)
547 s
= '1\\d|2[0-4]|'+p2
+'[1-9]';
557 var am
= options
.am
|| bundle
['dayPeriods-format-wide-am'],
558 pm
= options
.pm
|| bundle
['dayPeriods-format-wide-pm'];
561 if(am
!= am
.toLowerCase()){ s
+= '|' + am
.toLowerCase(); }
562 if(pm
!= pm
.toLowerCase()){ s
+= '|' + pm
.toLowerCase(); }
563 if(s
.indexOf('.') != -1){ s
+= '|' + s
.replace(/\./g, ""); }
565 s
= s
.replace(/\./g, "\\.");
572 // console.log("parse of date format, pattern=" + pattern);
575 if(tokens
){ tokens
.push(match
); }
577 return "(" + s
+ ")"; // add capture
578 }).replace(/[\xa0 ]/g, "[\\s\\xa0]"); // normalize whitespace. Need explicit handling of \xa0 for IE.
581 var _customFormats
= [];
582 dojo
.date
.locale
.addCustomFormats = function(/*String*/packageName
, /*String*/bundleName
){
584 // Add a reference to a bundle containing localized custom formats to be
585 // used by date/time formatting and parsing routines.
588 // The user may add custom localized formats where the bundle has properties following the
589 // same naming convention used by dojo.cldr: `dateFormat-xxxx` / `timeFormat-xxxx`
590 // The pattern string should match the format used by the CLDR.
591 // See dojo.date.locale.format() for details.
592 // The resources must be loaded by dojo.requireLocalization() prior to use
594 _customFormats
.push({pkg
:packageName
,name
:bundleName
});
597 dojo
.date
.locale
._getGregorianBundle = function(/*String*/locale
){
599 dojo
.forEach(_customFormats
, function(desc
){
600 var bundle
= dojo
.i18n
.getLocalization(desc
.pkg
, desc
.name
, locale
);
601 gregorian
= lang
.mixin(gregorian
, bundle
);
603 return gregorian
; /*Object*/
606 dojo
.date
.locale
.addCustomFormats("dojo.cldr","gregorian");
608 dojo
.date
.locale
.getNames = function(/*String*/item
, /*String*/type
, /*String?*/context
, /*String?*/locale
){
610 // Used to get localized strings from dojo.cldr for day or month names.
613 // 'months' || 'days'
615 // 'wide' || 'abbr' || 'narrow' (e.g. "Monday", "Mon", or "M" respectively, in English)
617 // 'standAlone' || 'format' (default)
619 // override locale used to find the names
622 lookup
= dojo
.date
.locale
._getGregorianBundle(locale
),
623 props
= [item
, context
, type
];
624 if(context
== 'standAlone'){
625 var key
= props
.join('-');
627 // Fall back to 'format' flavor of name
628 if(label
[0] == 1){ label
= undefined; } // kludge, in the absence of real aliasing support in dojo.cldr
632 // return by copy so changes won't be made accidentally to the in-memory model
633 return (label
|| lookup
[props
.join('-')]).concat(); /*Array*/
636 dojo
.date
.locale
.isWeekend = function(/*Date?*/dateObject
, /*String?*/locale
){
638 // Determines if the date falls on a weekend, according to local custom.
640 var weekend
= cldr
.getWeekend(locale
),
641 day
= (dateObject
|| new Date()).getDay();
642 if(weekend
.end
< weekend
.start
){
644 if(day
< weekend
.start
){ day
+= 7; }
646 return day
>= weekend
.start
&& day
<= weekend
.end
; // Boolean
649 // These are used only by format and strftime. Do they need to be public? Which module should they go in?
651 dojo
.date
.locale
._getDayOfYear = function(/*Date*/dateObject
){
652 // summary: gets the day of the year as represented by dateObject
653 return date
.difference(new Date(dateObject
.getFullYear(), 0, 1, dateObject
.getHours()), dateObject
) + 1; // Number
656 dojo
.date
.locale
._getWeekOfYear = function(/*Date*/dateObject
, /*Number*/firstDayOfWeek
){
657 if(arguments
.length
== 1){ firstDayOfWeek
= 0; } // Sunday
659 var firstDayOfYear
= new Date(dateObject
.getFullYear(), 0, 1).getDay(),
660 adj
= (firstDayOfYear
- firstDayOfWeek
+ 7) % 7,
661 week
= Math
.floor((dojo
.date
.locale
._getDayOfYear(dateObject
) + adj
- 1) / 7);
663 // if year starts on the specified day, start counting weeks at 1
664 if(firstDayOfYear
== firstDayOfWeek
){ week
++; }
666 return week
; // Number
669 return dojo
.date
.locale
;