locale.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojo.date.locale"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojo.date.locale"] = true;
  8. dojo.provide("dojo.date.locale");
  9. dojo.require("dojo.date");
  10. dojo.require("dojo.cldr.supplemental");
  11. dojo.require("dojo.regexp");
  12. dojo.require("dojo.string");
  13. dojo.require("dojo.i18n");
  14. dojo.requireLocalization("dojo.cldr", "gregorian", null, "ROOT,aa,af,ak,am,ar,ar-dz,ar-jo,ar-lb,ar-ma,ar-qa,ar-sa,ar-sy,ar-tn,ar-ye,as,asa,az,az-cyrl,be,bem,bez,bg,bm,bn,bn-in,bo,br,brx,bs,byn,ca,cgg,chr,cs,cy,da,dav,de,de-at,de-be,dz,ebu,ee,el,el-polyton,en,en-au,en-be,en-bw,en-bz,en-ca,en-dsrt,en-gb,en-hk,en-ie,en-in,en-mt,en-nz,en-pk,en-sg,en-shaw,en-za,en-zw,eo,es,es-ar,es-cl,es-co,es-ec,es-gt,es-hn,es-pa,es-pe,es-pr,es-us,et,eu,fa,fa-af,ff,fi,fil,fo,fr,fr-be,fr-ca,fr-ch,fur,ga,gl,gsw,gu,guz,gv,ha,haw,he,hi,hr,hu,hy,id,ig,ii,in,is,it,it-ch,iw,ja,jmc,ka,kab,kam,kde,kea,khq,ki,kk,kl,kln,km,kn,ko,kok,ksb,ksh,kw,lag,lg,ln,lt,luo,luy,lv,mas,mer,mfe,mg,mk,ml,mo,mr,ms,ms-bn,mt,my,naq,nb,nd,ne,ne-in,nl,nl-be,nn,no,nyn,oc,om,or,pa,pa-arab,pa-pk,pl,ps,pt,pt-pt,rm,ro,rof,ru,ru-ua,rw,rwk,saq,se,seh,ses,sg,sh,shi,shi-tfng,si,sk,sl,sn,so,sq,sr,sr-ba,sr-cyrl-ba,sr-latn,sr-latn-me,sr-me,ssy,sv,sv-fi,sw,syr,ta,te,teo,th,ti,ti-er,tl,to,tr,trv,tzm,uk,ur,vi,vun,xog,yo,zh,zh-hans-sg,zh-hant,zh-hant-hk,zh-hant-mo,zh-hk,zh-mo,zh-sg,zh-tw,zu");
  15. dojo.getObject("date.locale", true, dojo);
  16. // Localization methods for Date. Honor local customs using locale-dependent dojo.cldr data.
  17. // Load the bundles containing localization information for
  18. // names and formats
  19. //NOTE: Everything in this module assumes Gregorian calendars.
  20. // Other calendars will be implemented in separate modules.
  21. (function(){
  22. // Format a pattern without literals
  23. function formatPattern(dateObject, bundle, options, pattern){
  24. return pattern.replace(/([a-z])\1*/ig, function(match){
  25. var s, pad,
  26. c = match.charAt(0),
  27. l = match.length,
  28. 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. if(!options.fullYear){
  40. s = String(s); s = s.substr(s.length - 2);
  41. break;
  42. }
  43. // fallthrough
  44. default:
  45. pad = true;
  46. }
  47. break;
  48. case 'Q':
  49. case 'q':
  50. s = Math.ceil((dateObject.getMonth()+1)/3);
  51. // switch(l){
  52. // case 1: case 2:
  53. pad = true;
  54. // break;
  55. // case 3: case 4: // unimplemented
  56. // }
  57. break;
  58. case 'M':
  59. var m = dateObject.getMonth();
  60. if(l<3){
  61. s = m+1; pad = true;
  62. }else{
  63. var propM = ["months", "format", widthList[l-3]].join("-");
  64. s = bundle[propM][m];
  65. }
  66. break;
  67. case 'w':
  68. var firstDay = 0;
  69. s = dojo.date.locale._getWeekOfYear(dateObject, firstDay); pad = true;
  70. break;
  71. case 'd':
  72. s = dateObject.getDate(); pad = true;
  73. break;
  74. case 'D':
  75. s = dojo.date.locale._getDayOfYear(dateObject); pad = true;
  76. break;
  77. case 'E':
  78. var d = dateObject.getDay();
  79. if(l<3){
  80. s = d+1; pad = true;
  81. }else{
  82. var propD = ["days", "format", widthList[l-3]].join("-");
  83. s = bundle[propD][d];
  84. }
  85. break;
  86. case 'a':
  87. var timePeriod = (dateObject.getHours() < 12) ? 'am' : 'pm';
  88. s = options[timePeriod] || bundle['dayPeriods-format-wide-' + timePeriod];
  89. break;
  90. case 'h':
  91. case 'H':
  92. case 'K':
  93. case 'k':
  94. var h = dateObject.getHours();
  95. // strange choices in the date format make it impossible to write this succinctly
  96. switch (c){
  97. case 'h': // 1-12
  98. s = (h % 12) || 12;
  99. break;
  100. case 'H': // 0-23
  101. s = h;
  102. break;
  103. case 'K': // 0-11
  104. s = (h % 12);
  105. break;
  106. case 'k': // 1-24
  107. s = h || 24;
  108. break;
  109. }
  110. pad = true;
  111. break;
  112. case 'm':
  113. s = dateObject.getMinutes(); pad = true;
  114. break;
  115. case 's':
  116. s = dateObject.getSeconds(); pad = true;
  117. break;
  118. case 'S':
  119. s = Math.round(dateObject.getMilliseconds() * Math.pow(10, l-3)); pad = true;
  120. break;
  121. case 'v': // FIXME: don't know what this is. seems to be same as z?
  122. case 'z':
  123. // We only have one timezone to offer; the one from the browser
  124. s = dojo.date.locale._getZone(dateObject, true, options);
  125. if(s){break;}
  126. l=4;
  127. // fallthrough... use GMT if tz not available
  128. case 'Z':
  129. var offset = dojo.date.locale._getZone(dateObject, false, options);
  130. var tz = [
  131. (offset<=0 ? "+" : "-"),
  132. dojo.string.pad(Math.floor(Math.abs(offset)/60), 2),
  133. dojo.string.pad(Math.abs(offset)% 60, 2)
  134. ];
  135. if(l==4){
  136. tz.splice(0, 0, "GMT");
  137. tz.splice(3, 0, ":");
  138. }
  139. s = tz.join("");
  140. break;
  141. // case 'Y': case 'u': case 'W': case 'F': case 'g': case 'A': case 'e':
  142. // console.log(match+" modifier unimplemented");
  143. default:
  144. throw new Error("dojo.date.locale.format: invalid pattern char: "+pattern);
  145. }
  146. if(pad){ s = dojo.string.pad(s, l); }
  147. return s;
  148. });
  149. }
  150. /*=====
  151. dojo.date.locale.__FormatOptions = function(){
  152. // selector: String
  153. // choice of 'time','date' (default: date and time)
  154. // formatLength: String
  155. // choice of long, short, medium or full (plus any custom additions). Defaults to 'short'
  156. // datePattern:String
  157. // override pattern with this string
  158. // timePattern:String
  159. // override pattern with this string
  160. // am: String
  161. // override strings for am in times
  162. // pm: String
  163. // override strings for pm in times
  164. // locale: String
  165. // override the locale used to determine formatting rules
  166. // fullYear: Boolean
  167. // (format only) use 4 digit years whenever 2 digit years are called for
  168. // strict: Boolean
  169. // (parse only) strict parsing, off by default
  170. this.selector = selector;
  171. this.formatLength = formatLength;
  172. this.datePattern = datePattern;
  173. this.timePattern = timePattern;
  174. this.am = am;
  175. this.pm = pm;
  176. this.locale = locale;
  177. this.fullYear = fullYear;
  178. this.strict = strict;
  179. }
  180. =====*/
  181. dojo.date.locale._getZone = function(/*Date*/dateObject, /*boolean*/getName, /*dojo.date.locale.__FormatOptions?*/options){
  182. // summary:
  183. // Returns the zone (or offset) for the given date and options. This
  184. // is broken out into a separate function so that it can be overridden
  185. // by timezone-aware code.
  186. //
  187. // dateObject:
  188. // the date and/or time being formatted.
  189. //
  190. // getName:
  191. // Whether to return the timezone string (if true), or the offset (if false)
  192. //
  193. // options:
  194. // The options being used for formatting
  195. if(getName){
  196. return dojo.date.getTimezoneName(dateObject);
  197. }else{
  198. return dateObject.getTimezoneOffset();
  199. }
  200. };
  201. dojo.date.locale.format = function(/*Date*/dateObject, /*dojo.date.locale.__FormatOptions?*/options){
  202. // summary:
  203. // Format a Date object as a String, using locale-specific settings.
  204. //
  205. // description:
  206. // Create a string from a Date object using a known localized pattern.
  207. // By default, this method formats both date and time from dateObject.
  208. // Formatting patterns are chosen appropriate to the locale. Different
  209. // formatting lengths may be chosen, with "full" used by default.
  210. // Custom patterns may be used or registered with translations using
  211. // the dojo.date.locale.addCustomFormats method.
  212. // Formatting patterns are implemented using [the syntax described at
  213. // unicode.org](http://www.unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns)
  214. //
  215. // dateObject:
  216. // the date and/or time to be formatted. If a time only is formatted,
  217. // the values in the year, month, and day fields are irrelevant. The
  218. // opposite is true when formatting only dates.
  219. options = options || {};
  220. var locale = dojo.i18n.normalizeLocale(options.locale),
  221. formatLength = options.formatLength || 'short',
  222. bundle = dojo.date.locale._getGregorianBundle(locale),
  223. str = [],
  224. sauce = dojo.hitch(this, formatPattern, dateObject, bundle, options);
  225. if(options.selector == "year"){
  226. return _processPattern(bundle["dateFormatItem-yyyy"] || "yyyy", sauce);
  227. }
  228. var pattern;
  229. if(options.selector != "date"){
  230. pattern = options.timePattern || bundle["timeFormat-"+formatLength];
  231. if(pattern){str.push(_processPattern(pattern, sauce));}
  232. }
  233. if(options.selector != "time"){
  234. pattern = options.datePattern || bundle["dateFormat-"+formatLength];
  235. if(pattern){str.push(_processPattern(pattern, sauce));}
  236. }
  237. return str.length == 1 ? str[0] : bundle["dateTimeFormat-"+formatLength].replace(/\{(\d+)\}/g,
  238. function(match, key){ return str[key]; }); // String
  239. };
  240. dojo.date.locale.regexp = function(/*dojo.date.locale.__FormatOptions?*/options){
  241. // summary:
  242. // Builds the regular needed to parse a localized date
  243. return dojo.date.locale._parseInfo(options).regexp; // String
  244. };
  245. dojo.date.locale._parseInfo = function(/*dojo.date.locale.__FormatOptions?*/options){
  246. options = options || {};
  247. var locale = dojo.i18n.normalizeLocale(options.locale),
  248. bundle = dojo.date.locale._getGregorianBundle(locale),
  249. formatLength = options.formatLength || 'short',
  250. datePattern = options.datePattern || bundle["dateFormat-" + formatLength],
  251. timePattern = options.timePattern || bundle["timeFormat-" + formatLength],
  252. pattern;
  253. if(options.selector == 'date'){
  254. pattern = datePattern;
  255. }else if(options.selector == 'time'){
  256. pattern = timePattern;
  257. }else{
  258. pattern = bundle["dateTimeFormat-"+formatLength].replace(/\{(\d+)\}/g,
  259. function(match, key){ return [timePattern, datePattern][key]; });
  260. }
  261. var tokens = [],
  262. re = _processPattern(pattern, dojo.hitch(this, _buildDateTimeRE, tokens, bundle, options));
  263. return {regexp: re, tokens: tokens, bundle: bundle};
  264. };
  265. dojo.date.locale.parse = function(/*String*/value, /*dojo.date.locale.__FormatOptions?*/options){
  266. // summary:
  267. // Convert a properly formatted string to a primitive Date object,
  268. // using locale-specific settings.
  269. //
  270. // description:
  271. // Create a Date object from a string using a known localized pattern.
  272. // By default, this method parses looking for both date and time in the string.
  273. // Formatting patterns are chosen appropriate to the locale. Different
  274. // formatting lengths may be chosen, with "full" used by default.
  275. // Custom patterns may be used or registered with translations using
  276. // the dojo.date.locale.addCustomFormats method.
  277. //
  278. // Formatting patterns are implemented using [the syntax described at
  279. // unicode.org](http://www.unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns)
  280. // When two digit years are used, a century is chosen according to a sliding
  281. // window of 80 years before and 20 years after present year, for both `yy` and `yyyy` patterns.
  282. // year < 100CE requires strict mode.
  283. //
  284. // value:
  285. // A string representation of a date
  286. // remove non-printing bidi control chars from input and pattern
  287. var controlChars = /[\u200E\u200F\u202A\u202E]/g,
  288. info = dojo.date.locale._parseInfo(options),
  289. tokens = info.tokens, bundle = info.bundle,
  290. re = new RegExp("^" + info.regexp.replace(controlChars, "") + "$",
  291. info.strict ? "" : "i"),
  292. match = re.exec(value && value.replace(controlChars, ""));
  293. if(!match){ return null; } // null
  294. var widthList = ['abbr', 'wide', 'narrow'],
  295. result = [1970,0,1,0,0,0,0], // will get converted to a Date at the end
  296. amPm = "",
  297. valid = dojo.every(match, function(v, i){
  298. if(!i){return true;}
  299. var token=tokens[i-1];
  300. var l=token.length;
  301. switch(token.charAt(0)){
  302. case 'y':
  303. if(l != 2 && options.strict){
  304. //interpret year literally, so '5' would be 5 A.D.
  305. result[0] = v;
  306. }else{
  307. if(v<100){
  308. v = Number(v);
  309. //choose century to apply, according to a sliding window
  310. //of 80 years before and 20 years after present year
  311. var year = '' + new Date().getFullYear(),
  312. century = year.substring(0, 2) * 100,
  313. cutoff = Math.min(Number(year.substring(2, 4)) + 20, 99),
  314. num = (v < cutoff) ? century + v : century - 100 + v;
  315. result[0] = num;
  316. }else{
  317. //we expected 2 digits and got more...
  318. if(options.strict){
  319. return false;
  320. }
  321. //interpret literally, so '150' would be 150 A.D.
  322. //also tolerate '1950', if 'yyyy' input passed to 'yy' format
  323. result[0] = v;
  324. }
  325. }
  326. break;
  327. case 'M':
  328. if(l>2){
  329. var months = bundle['months-format-' + widthList[l-3]].concat();
  330. if(!options.strict){
  331. //Tolerate abbreviating period in month part
  332. //Case-insensitive comparison
  333. v = v.replace(".","").toLowerCase();
  334. months = dojo.map(months, function(s){ return s.replace(".","").toLowerCase(); } );
  335. }
  336. v = dojo.indexOf(months, v);
  337. if(v == -1){
  338. // console.log("dojo.date.locale.parse: Could not parse month name: '" + v + "'.");
  339. return false;
  340. }
  341. }else{
  342. v--;
  343. }
  344. result[1] = v;
  345. break;
  346. case 'E':
  347. case 'e':
  348. var days = bundle['days-format-' + widthList[l-3]].concat();
  349. if(!options.strict){
  350. //Case-insensitive comparison
  351. v = v.toLowerCase();
  352. days = dojo.map(days, function(d){return d.toLowerCase();});
  353. }
  354. v = dojo.indexOf(days, v);
  355. if(v == -1){
  356. // console.log("dojo.date.locale.parse: Could not parse weekday name: '" + v + "'.");
  357. return false;
  358. }
  359. //TODO: not sure what to actually do with this input,
  360. //in terms of setting something on the Date obj...?
  361. //without more context, can't affect the actual date
  362. //TODO: just validate?
  363. break;
  364. case 'D':
  365. result[1] = 0;
  366. // fallthrough...
  367. case 'd':
  368. result[2] = v;
  369. break;
  370. case 'a': //am/pm
  371. var am = options.am || bundle['dayPeriods-format-wide-am'],
  372. pm = options.pm || bundle['dayPeriods-format-wide-pm'];
  373. if(!options.strict){
  374. var period = /\./g;
  375. v = v.replace(period,'').toLowerCase();
  376. am = am.replace(period,'').toLowerCase();
  377. pm = pm.replace(period,'').toLowerCase();
  378. }
  379. if(options.strict && v != am && v != pm){
  380. // console.log("dojo.date.locale.parse: Could not parse am/pm part.");
  381. return false;
  382. }
  383. // we might not have seen the hours field yet, so store the state and apply hour change later
  384. amPm = (v == pm) ? 'p' : (v == am) ? 'a' : '';
  385. break;
  386. case 'K': //hour (1-24)
  387. if(v == 24){ v = 0; }
  388. // fallthrough...
  389. case 'h': //hour (1-12)
  390. case 'H': //hour (0-23)
  391. case 'k': //hour (0-11)
  392. //TODO: strict bounds checking, padding
  393. if(v > 23){
  394. // console.log("dojo.date.locale.parse: Illegal hours value");
  395. return false;
  396. }
  397. //in the 12-hour case, adjusting for am/pm requires the 'a' part
  398. //which could come before or after the hour, so we will adjust later
  399. result[3] = v;
  400. break;
  401. case 'm': //minutes
  402. result[4] = v;
  403. break;
  404. case 's': //seconds
  405. result[5] = v;
  406. break;
  407. case 'S': //milliseconds
  408. result[6] = v;
  409. // break;
  410. // case 'w':
  411. //TODO var firstDay = 0;
  412. // default:
  413. //TODO: throw?
  414. // console.log("dojo.date.locale.parse: unsupported pattern char=" + token.charAt(0));
  415. }
  416. return true;
  417. });
  418. var hours = +result[3];
  419. if(amPm === 'p' && hours < 12){
  420. result[3] = hours + 12; //e.g., 3pm -> 15
  421. }else if(amPm === 'a' && hours == 12){
  422. result[3] = 0; //12am -> 0
  423. }
  424. //TODO: implement a getWeekday() method in order to test
  425. //validity of input strings containing 'EEE' or 'EEEE'...
  426. var dateObject = new Date(result[0], result[1], result[2], result[3], result[4], result[5], result[6]); // Date
  427. if(options.strict){
  428. dateObject.setFullYear(result[0]);
  429. }
  430. // Check for overflow. The Date() constructor normalizes things like April 32nd...
  431. //TODO: why isn't this done for times as well?
  432. var allTokens = tokens.join(""),
  433. dateToken = allTokens.indexOf('d') != -1,
  434. monthToken = allTokens.indexOf('M') != -1;
  435. if(!valid ||
  436. (monthToken && dateObject.getMonth() > result[1]) ||
  437. (dateToken && dateObject.getDate() > result[2])){
  438. return null;
  439. }
  440. // Check for underflow, due to DST shifts. See #9366
  441. // This assumes a 1 hour dst shift correction at midnight
  442. // We could compare the timezone offset after the shift and add the difference instead.
  443. if((monthToken && dateObject.getMonth() < result[1]) ||
  444. (dateToken && dateObject.getDate() < result[2])){
  445. dateObject = dojo.date.add(dateObject, "hour", 1);
  446. }
  447. return dateObject; // Date
  448. };
  449. function _processPattern(pattern, applyPattern, applyLiteral, applyAll){
  450. //summary: Process a pattern with literals in it
  451. // Break up on single quotes, treat every other one as a literal, except '' which becomes '
  452. var identity = function(x){return x;};
  453. applyPattern = applyPattern || identity;
  454. applyLiteral = applyLiteral || identity;
  455. applyAll = applyAll || identity;
  456. //split on single quotes (which escape literals in date format strings)
  457. //but preserve escaped single quotes (e.g., o''clock)
  458. var chunks = pattern.match(/(''|[^'])+/g),
  459. literal = pattern.charAt(0) == "'";
  460. dojo.forEach(chunks, function(chunk, i){
  461. if(!chunk){
  462. chunks[i]='';
  463. }else{
  464. chunks[i]=(literal ? applyLiteral : applyPattern)(chunk.replace(/''/g, "'"));
  465. literal = !literal;
  466. }
  467. });
  468. return applyAll(chunks.join(''));
  469. }
  470. function _buildDateTimeRE(tokens, bundle, options, pattern){
  471. pattern = dojo.regexp.escapeString(pattern);
  472. if(!options.strict){ pattern = pattern.replace(" a", " ?a"); } // kludge to tolerate no space before am/pm
  473. return pattern.replace(/([a-z])\1*/ig, function(match){
  474. // Build a simple regexp. Avoid captures, which would ruin the tokens list
  475. var s,
  476. c = match.charAt(0),
  477. l = match.length,
  478. p2 = '', p3 = '';
  479. if(options.strict){
  480. if(l > 1){ p2 = '0' + '{'+(l-1)+'}'; }
  481. if(l > 2){ p3 = '0' + '{'+(l-2)+'}'; }
  482. }else{
  483. p2 = '0?'; p3 = '0{0,2}';
  484. }
  485. switch(c){
  486. case 'y':
  487. s = '\\d{2,4}';
  488. break;
  489. case 'M':
  490. s = (l>2) ? '\\S+?' : '1[0-2]|'+p2+'[1-9]';
  491. break;
  492. case 'D':
  493. s = '[12][0-9][0-9]|3[0-5][0-9]|36[0-6]|'+p3+'[1-9][0-9]|'+p2+'[1-9]';
  494. break;
  495. case 'd':
  496. s = '3[01]|[12]\\d|'+p2+'[1-9]';
  497. break;
  498. case 'w':
  499. s = '[1-4][0-9]|5[0-3]|'+p2+'[1-9]';
  500. break;
  501. case 'E':
  502. s = '\\S+';
  503. break;
  504. case 'h': //hour (1-12)
  505. s = '1[0-2]|'+p2+'[1-9]';
  506. break;
  507. case 'k': //hour (0-11)
  508. s = '1[01]|'+p2+'\\d';
  509. break;
  510. case 'H': //hour (0-23)
  511. s = '1\\d|2[0-3]|'+p2+'\\d';
  512. break;
  513. case 'K': //hour (1-24)
  514. s = '1\\d|2[0-4]|'+p2+'[1-9]';
  515. break;
  516. case 'm':
  517. case 's':
  518. s = '[0-5]\\d';
  519. break;
  520. case 'S':
  521. s = '\\d{'+l+'}';
  522. break;
  523. case 'a':
  524. var am = options.am || bundle['dayPeriods-format-wide-am'],
  525. pm = options.pm || bundle['dayPeriods-format-wide-pm'];
  526. s = am + '|' + pm;
  527. if(!options.strict){
  528. if(am != am.toLowerCase()){ s += '|' + am.toLowerCase(); }
  529. if(pm != pm.toLowerCase()){ s += '|' + pm.toLowerCase(); }
  530. if(s.indexOf('.') != -1){ s += '|' + s.replace(/\./g, ""); }
  531. }
  532. s = s.replace(/\./g, "\\.");
  533. break;
  534. default:
  535. // case 'v':
  536. // case 'z':
  537. // case 'Z':
  538. s = ".*";
  539. // console.log("parse of date format, pattern=" + pattern);
  540. }
  541. if(tokens){ tokens.push(match); }
  542. return "(" + s + ")"; // add capture
  543. }).replace(/[\xa0 ]/g, "[\\s\\xa0]"); // normalize whitespace. Need explicit handling of \xa0 for IE.
  544. }
  545. })();
  546. (function(){
  547. var _customFormats = [];
  548. dojo.date.locale.addCustomFormats = function(/*String*/packageName, /*String*/bundleName){
  549. // summary:
  550. // Add a reference to a bundle containing localized custom formats to be
  551. // used by date/time formatting and parsing routines.
  552. //
  553. // description:
  554. // The user may add custom localized formats where the bundle has properties following the
  555. // same naming convention used by dojo.cldr: `dateFormat-xxxx` / `timeFormat-xxxx`
  556. // The pattern string should match the format used by the CLDR.
  557. // See dojo.date.locale.format() for details.
  558. // The resources must be loaded by dojo.requireLocalization() prior to use
  559. _customFormats.push({pkg:packageName,name:bundleName});
  560. };
  561. dojo.date.locale._getGregorianBundle = function(/*String*/locale){
  562. var gregorian = {};
  563. dojo.forEach(_customFormats, function(desc){
  564. var bundle = dojo.i18n.getLocalization(desc.pkg, desc.name, locale);
  565. gregorian = dojo.mixin(gregorian, bundle);
  566. }, this);
  567. return gregorian; /*Object*/
  568. };
  569. })();
  570. dojo.date.locale.addCustomFormats("dojo.cldr","gregorian");
  571. dojo.date.locale.getNames = function(/*String*/item, /*String*/type, /*String?*/context, /*String?*/locale){
  572. // summary:
  573. // Used to get localized strings from dojo.cldr for day or month names.
  574. //
  575. // item:
  576. // 'months' || 'days'
  577. // type:
  578. // 'wide' || 'narrow' || 'abbr' (e.g. "Monday", "Mon", or "M" respectively, in English)
  579. // context:
  580. // 'standAlone' || 'format' (default)
  581. // locale:
  582. // override locale used to find the names
  583. var label,
  584. lookup = dojo.date.locale._getGregorianBundle(locale),
  585. props = [item, context, type];
  586. if(context == 'standAlone'){
  587. var key = props.join('-');
  588. label = lookup[key];
  589. // Fall back to 'format' flavor of name
  590. if(label[0] == 1){ label = undefined; } // kludge, in the absence of real aliasing support in dojo.cldr
  591. }
  592. props[1] = 'format';
  593. // return by copy so changes won't be made accidentally to the in-memory model
  594. return (label || lookup[props.join('-')]).concat(); /*Array*/
  595. };
  596. dojo.date.locale.isWeekend = function(/*Date?*/dateObject, /*String?*/locale){
  597. // summary:
  598. // Determines if the date falls on a weekend, according to local custom.
  599. var weekend = dojo.cldr.supplemental.getWeekend(locale),
  600. day = (dateObject || new Date()).getDay();
  601. if(weekend.end < weekend.start){
  602. weekend.end += 7;
  603. if(day < weekend.start){ day += 7; }
  604. }
  605. return day >= weekend.start && day <= weekend.end; // Boolean
  606. };
  607. // These are used only by format and strftime. Do they need to be public? Which module should they go in?
  608. dojo.date.locale._getDayOfYear = function(/*Date*/dateObject){
  609. // summary: gets the day of the year as represented by dateObject
  610. return dojo.date.difference(new Date(dateObject.getFullYear(), 0, 1, dateObject.getHours()), dateObject) + 1; // Number
  611. };
  612. dojo.date.locale._getWeekOfYear = function(/*Date*/dateObject, /*Number*/firstDayOfWeek){
  613. if(arguments.length == 1){ firstDayOfWeek = 0; } // Sunday
  614. var firstDayOfYear = new Date(dateObject.getFullYear(), 0, 1).getDay(),
  615. adj = (firstDayOfYear - firstDayOfWeek + 7) % 7,
  616. week = Math.floor((dojo.date.locale._getDayOfYear(dateObject) + adj - 1) / 7);
  617. // if year starts on the specified day, start counting weeks at 1
  618. if(firstDayOfYear == firstDayOfWeek){ week++; }
  619. return week; // Number
  620. };
  621. }