globalize.js 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. /* tslint:disable */
  2. /* eslint-disable */
  3. /*!
  4. * Globalize
  5. *
  6. * http://github.com/jquery/globalize
  7. *
  8. * Copyright Software Freedom Conservancy, Inc.
  9. * Dual licensed under the MIT or GPL Version 2 licenses.
  10. * http://jquery.org/license
  11. */
  12. (function( window, undefined ) {
  13. if (document.Globalize) {
  14. return;
  15. }
  16. var Globalize,
  17. // private variables
  18. regexHex,
  19. regexInfinity,
  20. regexParseFloat,
  21. regexTrim,
  22. // private JavaScript utility functions
  23. arrayIndexOf,
  24. endsWith,
  25. extend,
  26. isArray,
  27. isFunction,
  28. isObject,
  29. startsWith,
  30. trim,
  31. truncate,
  32. zeroPad,
  33. // private Globalization utility functions
  34. appendPreOrPostMatch,
  35. expandFormat,
  36. formatDate,
  37. formatNumber,
  38. getTokenRegExp,
  39. getEra,
  40. getEraYear,
  41. parseExact,
  42. parseNegativePattern;
  43. // Global variable (Globalize) or CommonJS module (globalize)
  44. Globalize = function( cultureSelector ) {
  45. return new Globalize.prototype.init( cultureSelector );
  46. };
  47. if ( typeof require !== "undefined" &&
  48. typeof exports !== "undefined" &&
  49. typeof module !== "undefined" ) {
  50. // Assume CommonJS
  51. module.exports = Globalize;
  52. } else {
  53. // Export as global variable
  54. window.Globalize = Globalize;
  55. }
  56. Globalize.cultures = {};
  57. Globalize.prototype = {
  58. constructor: Globalize,
  59. init: function( cultureSelector ) {
  60. this.cultures = Globalize.cultures;
  61. this.cultureSelector = cultureSelector;
  62. return this;
  63. }
  64. };
  65. Globalize.prototype.init.prototype = Globalize.prototype;
  66. // 1. When defining a culture, all fields are required except the ones stated as optional.
  67. // 2. Each culture should have a ".calendars" object with at least one calendar named "standard"
  68. // which serves as the default calendar in use by that culture.
  69. // 3. Each culture should have a ".calendar" object which is the current calendar being used,
  70. // it may be dynamically changed at any time to one of the calendars in ".calendars".
  71. Globalize.cultures[ "default" ] = {
  72. // A unique name for the culture in the form <language code>-<country/region code>
  73. name: "en",
  74. // the name of the culture in the english language
  75. englishName: "English",
  76. // the name of the culture in its own language
  77. nativeName: "English",
  78. // whether the culture uses right-to-left text
  79. isRTL: false,
  80. // "language" is used for so-called "specific" cultures.
  81. // For example, the culture "es-CL" means "Spanish, in Chili".
  82. // It represents the Spanish-speaking culture as it is in Chili,
  83. // which might have different formatting rules or even translations
  84. // than Spanish in Spain. A "neutral" culture is one that is not
  85. // specific to a region. For example, the culture "es" is the generic
  86. // Spanish culture, which may be a more generalized version of the language
  87. // that may or may not be what a specific culture expects.
  88. // For a specific culture like "es-CL", the "language" field refers to the
  89. // neutral, generic culture information for the language it is using.
  90. // This is not always a simple matter of the string before the dash.
  91. // For example, the "zh-Hans" culture is netural (Simplified Chinese).
  92. // And the "zh-SG" culture is Simplified Chinese in Singapore, whose lanugage
  93. // field is "zh-CHS", not "zh".
  94. // This field should be used to navigate from a specific culture to it's
  95. // more general, neutral culture. If a culture is already as general as it
  96. // can get, the language may refer to itself.
  97. language: "en",
  98. // numberFormat defines general number formatting rules, like the digits in
  99. // each grouping, the group separator, and how negative numbers are displayed.
  100. numberFormat: {
  101. // [negativePattern]
  102. // Note, numberFormat.pattern has no "positivePattern" unlike percent and currency,
  103. // but is still defined as an array for consistency with them.
  104. // negativePattern: one of "(n)|-n|- n|n-|n -"
  105. pattern: [ "-n" ],
  106. // number of decimal places normally shown
  107. decimals: 2,
  108. // string that separates number groups, as in 1,000,000
  109. ",": ",",
  110. // string that separates a number from the fractional portion, as in 1.99
  111. ".": ".",
  112. // array of numbers indicating the size of each number group.
  113. // TODO: more detailed description and example
  114. groupSizes: [ 3 ],
  115. // symbol used for positive numbers
  116. "+": "+",
  117. // symbol used for negative numbers
  118. "-": "-",
  119. // symbol used for NaN (Not-A-Number)
  120. "NaN": "NaN",
  121. // symbol used for Negative Infinity
  122. negativeInfinity: "-Infinity",
  123. // symbol used for Positive Infinity
  124. positiveInfinity: "Infinity",
  125. percent: {
  126. // [negativePattern, positivePattern]
  127. // negativePattern: one of "-n %|-n%|-%n|%-n|%n-|n-%|n%-|-% n|n %-|% n-|% -n|n- %"
  128. // positivePattern: one of "n %|n%|%n|% n"
  129. pattern: [ "-n %", "n %" ],
  130. // number of decimal places normally shown
  131. decimals: 2,
  132. // array of numbers indicating the size of each number group.
  133. // TODO: more detailed description and example
  134. groupSizes: [ 3 ],
  135. // string that separates number groups, as in 1,000,000
  136. ",": ",",
  137. // string that separates a number from the fractional portion, as in 1.99
  138. ".": ".",
  139. // symbol used to represent a percentage
  140. symbol: "%"
  141. },
  142. currency: {
  143. // [negativePattern, positivePattern]
  144. // negativePattern: one of "($n)|-$n|$-n|$n-|(n$)|-n$|n-$|n$-|-n $|-$ n|n $-|$ n-|$ -n|n- $|($ n)|(n $)"
  145. // positivePattern: one of "$n|n$|$ n|n $"
  146. pattern: [ "($n)", "$n" ],
  147. // number of decimal places normally shown
  148. decimals: 2,
  149. // array of numbers indicating the size of each number group.
  150. // TODO: more detailed description and example
  151. groupSizes: [ 3 ],
  152. // string that separates number groups, as in 1,000,000
  153. ",": ",",
  154. // string that separates a number from the fractional portion, as in 1.99
  155. ".": ".",
  156. // symbol used to represent currency
  157. symbol: "$"
  158. }
  159. },
  160. // calendars defines all the possible calendars used by this culture.
  161. // There should be at least one defined with name "standard", and is the default
  162. // calendar used by the culture.
  163. // A calendar contains information about how dates are formatted, information about
  164. // the calendar's eras, a standard set of the date formats,
  165. // translations for day and month names, and if the calendar is not based on the Gregorian
  166. // calendar, conversion functions to and from the Gregorian calendar.
  167. calendars: {
  168. standard: {
  169. // name that identifies the type of calendar this is
  170. name: "Gregorian_USEnglish",
  171. // separator of parts of a date (e.g. "/" in 11/05/1955)
  172. "/": "/",
  173. // separator of parts of a time (e.g. ":" in 05:44 PM)
  174. ":": ":",
  175. // the first day of the week (0 = Sunday, 1 = Monday, etc)
  176. firstDay: 0,
  177. days: {
  178. // full day names
  179. names: [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ],
  180. // abbreviated day names
  181. namesAbbr: [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ],
  182. // shortest day names
  183. namesShort: [ "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" ]
  184. },
  185. months: {
  186. // full month names (13 months for lunar calendards -- 13th month should be "" if not lunar)
  187. names: [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", "" ],
  188. // abbreviated month names
  189. namesAbbr: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "" ]
  190. },
  191. // AM and PM designators in one of these forms:
  192. // The usual view, and the upper and lower case versions
  193. // [ standard, lowercase, uppercase ]
  194. // The culture does not use AM or PM (likely all standard date formats use 24 hour time)
  195. // null
  196. AM: [ "AM", "am", "AM" ],
  197. PM: [ "PM", "pm", "PM" ],
  198. eras: [
  199. // eras in reverse chronological order.
  200. // name: the name of the era in this culture (e.g. A.D., C.E.)
  201. // start: when the era starts in ticks (gregorian, gmt), null if it is the earliest supported era.
  202. // offset: offset in years from gregorian calendar
  203. {
  204. "name": "A.D.",
  205. "start": null,
  206. "offset": 0
  207. }
  208. ],
  209. // when a two digit year is given, it will never be parsed as a four digit
  210. // year greater than this year (in the appropriate era for the culture)
  211. // Set it as a full year (e.g. 2029) or use an offset format starting from
  212. // the current year: "+19" would correspond to 2029 if the current year 2010.
  213. twoDigitYearMax: 2029,
  214. // set of predefined date and time patterns used by the culture
  215. // these represent the format someone in this culture would expect
  216. // to see given the portions of the date that are shown.
  217. patterns: {
  218. // short date pattern
  219. d: "M/d/yyyy",
  220. // long date pattern
  221. D: "dddd, MMMM dd, yyyy",
  222. // short time pattern
  223. t: "h:mm tt",
  224. // long time pattern
  225. T: "h:mm:ss tt",
  226. // long date, short time pattern
  227. f: "dddd, MMMM dd, yyyy h:mm tt",
  228. // long date, long time pattern
  229. F: "dddd, MMMM dd, yyyy h:mm:ss tt",
  230. // month/day pattern
  231. M: "MMMM dd",
  232. // month/year pattern
  233. Y: "yyyy MMMM",
  234. // S is a sortable format that does not vary by culture
  235. S: "yyyy\u0027-\u0027MM\u0027-\u0027dd\u0027T\u0027HH\u0027:\u0027mm\u0027:\u0027ss"
  236. }
  237. // optional fields for each calendar:
  238. /*
  239. monthsGenitive:
  240. Same as months but used when the day preceeds the month.
  241. Omit if the culture has no genitive distinction in month names.
  242. For an explaination of genitive months, see http://blogs.msdn.com/michkap/archive/2004/12/25/332259.aspx
  243. convert:
  244. Allows for the support of non-gregorian based calendars. This convert object is used to
  245. to convert a date to and from a gregorian calendar date to handle parsing and formatting.
  246. The two functions:
  247. fromGregorian( date )
  248. Given the date as a parameter, return an array with parts [ year, month, day ]
  249. corresponding to the non-gregorian based year, month, and day for the calendar.
  250. toGregorian( year, month, day )
  251. Given the non-gregorian year, month, and day, return a new Date() object
  252. set to the corresponding date in the gregorian calendar.
  253. */
  254. }
  255. },
  256. // For localized strings
  257. messages: {}
  258. };
  259. Globalize.cultures[ "default" ].calendar = Globalize.cultures[ "default" ].calendars.standard;
  260. Globalize.cultures.en = Globalize.cultures[ "default" ];
  261. Globalize.cultureSelector = "en";
  262. //
  263. // private variables
  264. //
  265. regexHex = /^0x[a-f0-9]+$/i;
  266. regexInfinity = /^[+\-]?infinity$/i;
  267. regexParseFloat = /^[+\-]?\d*\.?\d*(e[+\-]?\d+)?$/;
  268. regexTrim = /^\s+|\s+$/g;
  269. //
  270. // private JavaScript utility functions
  271. //
  272. arrayIndexOf = function( array, item ) {
  273. if ( array.indexOf ) {
  274. return array.indexOf( item );
  275. }
  276. for ( var i = 0, length = array.length; i < length; i++ ) {
  277. if ( array[i] === item ) {
  278. return i;
  279. }
  280. }
  281. return -1;
  282. };
  283. endsWith = function( value, pattern ) {
  284. return value.substr( value.length - pattern.length ) === pattern;
  285. };
  286. extend = function() {
  287. var options, name, src, copy, copyIsArray, clone,
  288. target = arguments[0] || {},
  289. i = 1,
  290. length = arguments.length,
  291. deep = false;
  292. // Handle a deep copy situation
  293. if ( typeof target === "boolean" ) {
  294. deep = target;
  295. target = arguments[1] || {};
  296. // skip the boolean and the target
  297. i = 2;
  298. }
  299. // Handle case when target is a string or something (possible in deep copy)
  300. if ( typeof target !== "object" && !isFunction(target) ) {
  301. target = {};
  302. }
  303. for ( ; i < length; i++ ) {
  304. // Only deal with non-null/undefined values
  305. if ( (options = arguments[ i ]) != null ) {
  306. // Extend the base object
  307. for ( name in options ) {
  308. src = target[ name ];
  309. copy = options[ name ];
  310. // Prevent never-ending loop
  311. if ( target === copy ) {
  312. continue;
  313. }
  314. // Recurse if we're merging plain objects or arrays
  315. if ( deep && copy && ( isObject(copy) || (copyIsArray = isArray(copy)) ) ) {
  316. if ( copyIsArray ) {
  317. copyIsArray = false;
  318. clone = src && isArray(src) ? src : [];
  319. } else {
  320. clone = src && isObject(src) ? src : {};
  321. }
  322. // Never move original objects, clone them
  323. target[ name ] = extend( deep, clone, copy );
  324. // Don't bring in undefined values
  325. } else if ( copy !== undefined ) {
  326. target[ name ] = copy;
  327. }
  328. }
  329. }
  330. }
  331. // Return the modified object
  332. return target;
  333. };
  334. isArray = Array.isArray || function( obj ) {
  335. return Object.prototype.toString.call( obj ) === "[object Array]";
  336. };
  337. isFunction = function( obj ) {
  338. return Object.prototype.toString.call( obj ) === "[object Function]";
  339. };
  340. isObject = function( obj ) {
  341. return Object.prototype.toString.call( obj ) === "[object Object]";
  342. };
  343. startsWith = function( value, pattern ) {
  344. return value.indexOf( pattern ) === 0;
  345. };
  346. trim = function( value ) {
  347. return ( value + "" ).replace( regexTrim, "" );
  348. };
  349. truncate = function( value ) {
  350. if ( isNaN( value ) ) {
  351. return NaN;
  352. }
  353. return Math[ value < 0 ? "ceil" : "floor" ]( value );
  354. };
  355. zeroPad = function( str, count, left ) {
  356. var l;
  357. for ( l = str.length; l < count; l += 1 ) {
  358. str = ( left ? ("0" + str) : (str + "0") );
  359. }
  360. return str;
  361. };
  362. //
  363. // private Globalization utility functions
  364. //
  365. appendPreOrPostMatch = function( preMatch, strings ) {
  366. // appends pre- and post- token match strings while removing escaped characters.
  367. // Returns a single quote count which is used to determine if the token occurs
  368. // in a string literal.
  369. var quoteCount = 0,
  370. escaped = false;
  371. for ( var i = 0, il = preMatch.length; i < il; i++ ) {
  372. var c = preMatch.charAt( i );
  373. switch ( c ) {
  374. case "\'":
  375. if ( escaped ) {
  376. strings.push( "\'" );
  377. }
  378. else {
  379. quoteCount++;
  380. }
  381. escaped = false;
  382. break;
  383. case "\\":
  384. if ( escaped ) {
  385. strings.push( "\\" );
  386. }
  387. escaped = !escaped;
  388. break;
  389. default:
  390. strings.push( c );
  391. escaped = false;
  392. break;
  393. }
  394. }
  395. return quoteCount;
  396. };
  397. expandFormat = function( cal, format ) {
  398. // expands unspecified or single character date formats into the full pattern.
  399. format = format || "F";
  400. var pattern,
  401. patterns = cal.patterns,
  402. len = format.length;
  403. if ( len === 1 ) {
  404. pattern = patterns[ format ];
  405. if ( !pattern ) {
  406. throw "Invalid date format string \'" + format + "\'.";
  407. }
  408. format = pattern;
  409. }
  410. else if ( len === 2 && format.charAt(0) === "%" ) {
  411. // %X escape format -- intended as a custom format string that is only one character, not a built-in format.
  412. format = format.charAt( 1 );
  413. }
  414. return format;
  415. };
  416. formatDate = function( value, format, culture ) {
  417. var cal = culture.calendar,
  418. convert = cal.convert,
  419. ret;
  420. if ( !format || !format.length || format === "i" ) {
  421. if ( culture && culture.name.length ) {
  422. if ( convert ) {
  423. // non-gregorian calendar, so we cannot use built-in toLocaleString()
  424. ret = formatDate( value, cal.patterns.F, culture );
  425. }
  426. else {
  427. var eraDate = new Date( value.getTime() ),
  428. era = getEra( value, cal.eras );
  429. eraDate.setFullYear( getEraYear(value, cal, era) );
  430. ret = eraDate.toLocaleString();
  431. }
  432. }
  433. else {
  434. ret = value.toString();
  435. }
  436. return ret;
  437. }
  438. var eras = cal.eras,
  439. sortable = format === "s";
  440. format = expandFormat( cal, format );
  441. // Start with an empty string
  442. ret = [];
  443. var hour,
  444. zeros = [ "0", "00", "000" ],
  445. foundDay,
  446. checkedDay,
  447. dayPartRegExp = /([^d]|^)(d|dd)([^d]|$)/g,
  448. quoteCount = 0,
  449. tokenRegExp = getTokenRegExp(),
  450. converted;
  451. //function padZeros( num, c ) {
  452. // var r, s = num + "";
  453. // if ( c > 1 && s.length < c ) {
  454. // r = ( zeros[c - 2] + s);
  455. // return r.substr( r.length - c, c );
  456. // }
  457. // else {
  458. // r = s;
  459. // }
  460. // return r;
  461. //}
  462. function padZeros(num, c) {
  463. if (num < 0) {
  464. return "-" + padZeros(-num, c);
  465. }
  466. var r, s = num + "";
  467. if (c > 1 && s.length < c) {
  468. r = (zeros[c - 2] + s);
  469. return r.substr(r.length - c, c);
  470. }
  471. else {
  472. r = s;
  473. }
  474. return r;
  475. }
  476. function hasDay() {
  477. if ( foundDay || checkedDay ) {
  478. return foundDay;
  479. }
  480. foundDay = dayPartRegExp.test( format );
  481. checkedDay = true;
  482. return foundDay;
  483. }
  484. function getPart( date, part ) {
  485. if ( converted ) {
  486. return converted[ part ];
  487. }
  488. switch ( part ) {
  489. case 0:
  490. return date.getFullYear();
  491. case 1:
  492. return date.getMonth();
  493. case 2:
  494. return date.getDate();
  495. default:
  496. throw "Invalid part value " + part;
  497. }
  498. }
  499. if ( !sortable && convert ) {
  500. converted = convert.fromGregorian( value );
  501. }
  502. for ( ; ; ) {
  503. // Save the current index
  504. var index = tokenRegExp.lastIndex,
  505. // Look for the next pattern
  506. ar = tokenRegExp.exec( format );
  507. // Append the text before the pattern (or the end of the string if not found)
  508. var preMatch = format.slice( index, ar ? ar.index : format.length );
  509. quoteCount += appendPreOrPostMatch( preMatch, ret );
  510. if ( !ar ) {
  511. break;
  512. }
  513. // do not replace any matches that occur inside a string literal.
  514. if ( quoteCount % 2 ) {
  515. ret.push( ar[0] );
  516. continue;
  517. }
  518. var current = ar[ 0 ],
  519. clength = current.length;
  520. switch ( current ) {
  521. case "ddd":
  522. //Day of the week, as a three-letter abbreviation
  523. case "dddd":
  524. // Day of the week, using the full name
  525. var names = ( clength === 3 ) ? cal.days.namesAbbr : cal.days.names;
  526. ret.push( names[value.getDay()] );
  527. break;
  528. case "d":
  529. // Day of month, without leading zero for single-digit days
  530. case "dd":
  531. // Day of month, with leading zero for single-digit days
  532. foundDay = true;
  533. ret.push(
  534. padZeros( getPart(value, 2), clength )
  535. );
  536. break;
  537. case "MMM":
  538. // Month, as a three-letter abbreviation
  539. case "MMMM":
  540. // Month, using the full name
  541. var part = getPart( value, 1 );
  542. ret.push(
  543. ( cal.monthsGenitive && hasDay() ) ?
  544. ( cal.monthsGenitive[ clength === 3 ? "namesAbbr" : "names" ][ part ] ) :
  545. ( cal.months[ clength === 3 ? "namesAbbr" : "names" ][ part ] )
  546. );
  547. break;
  548. case "M":
  549. // Month, as digits, with no leading zero for single-digit months
  550. case "MM":
  551. // Month, as digits, with leading zero for single-digit months
  552. ret.push(
  553. padZeros( getPart(value, 1) + 1, clength )
  554. );
  555. break;
  556. case "y":
  557. // Year, as two digits, but with no leading zero for years less than 10
  558. case "yy":
  559. // Year, as two digits, with leading zero for years less than 10
  560. case "yyyy":
  561. // Year represented by four full digits
  562. part = converted ? converted[ 0 ] : getEraYear( value, cal, getEra(value, eras), sortable );
  563. if ( clength < 4 ) {
  564. part = part % 100;
  565. }
  566. ret.push(
  567. padZeros( part, clength )
  568. );
  569. break;
  570. case "h":
  571. // Hours with no leading zero for single-digit hours, using 12-hour clock
  572. case "hh":
  573. // Hours with leading zero for single-digit hours, using 12-hour clock
  574. hour = value.getHours() % 12;
  575. if ( hour === 0 ) hour = 12;
  576. ret.push(
  577. padZeros( hour, clength )
  578. );
  579. break;
  580. case "H":
  581. // Hours with no leading zero for single-digit hours, using 24-hour clock
  582. case "HH":
  583. // Hours with leading zero for single-digit hours, using 24-hour clock
  584. ret.push(
  585. padZeros( value.getHours(), clength )
  586. );
  587. break;
  588. case "m":
  589. // Minutes with no leading zero for single-digit minutes
  590. case "mm":
  591. // Minutes with leading zero for single-digit minutes
  592. ret.push(
  593. padZeros( value.getMinutes(), clength )
  594. );
  595. break;
  596. case "s":
  597. // Seconds with no leading zero for single-digit seconds
  598. case "ss":
  599. // Seconds with leading zero for single-digit seconds
  600. ret.push(
  601. padZeros( value.getSeconds(), clength )
  602. );
  603. break;
  604. case "t":
  605. // One character am/pm indicator ("a" or "p")
  606. case "tt":
  607. // Multicharacter am/pm indicator
  608. part = value.getHours() < 12 ? ( cal.AM ? cal.AM[0] : " " ) : ( cal.PM ? cal.PM[0] : " " );
  609. ret.push( clength === 1 ? part.charAt(0) : part );
  610. break;
  611. case "f":
  612. // Deciseconds
  613. case "ff":
  614. // Centiseconds
  615. case "fff":
  616. // Milliseconds
  617. ret.push(
  618. padZeros( value.getMilliseconds(), 3 ).substr( 0, clength )
  619. );
  620. break;
  621. case "z":
  622. // Time zone offset, no leading zero
  623. case "zz":
  624. // Time zone offset with leading zero
  625. hour = value.getTimezoneOffset() / 60;
  626. ret.push(
  627. ( hour <= 0 ? "+" : "-" ) + padZeros( Math.floor(Math.abs(hour)), clength )
  628. );
  629. break;
  630. case "zzz":
  631. // Time zone offset with leading zero
  632. hour = value.getTimezoneOffset() / 60;
  633. ret.push(
  634. ( hour <= 0 ? "+" : "-" ) + padZeros( Math.floor(Math.abs(hour)), 2 ) +
  635. // Hard coded ":" separator, rather than using cal.TimeSeparator
  636. // Repeated here for consistency, plus ":" was already assumed in date parsing.
  637. ":" + padZeros( Math.abs(value.getTimezoneOffset() % 60), 2 )
  638. );
  639. break;
  640. case "g":
  641. case "gg":
  642. if ( cal.eras ) {
  643. ret.push(
  644. cal.eras[ getEra(value, eras) ].name
  645. );
  646. }
  647. break;
  648. case "/":
  649. ret.push( cal["/"] );
  650. break;
  651. default:
  652. throw "Invalid date format pattern \'" + current + "\'.";
  653. }
  654. }
  655. return ret.join( "" );
  656. };
  657. // formatNumber
  658. (function() {
  659. var expandNumber;
  660. expandNumber = function( number, precision, formatInfo ) {
  661. var groupSizes = formatInfo.groupSizes,
  662. curSize = groupSizes[ 0 ],
  663. curGroupIndex = 1,
  664. factor = Math.pow( 10, precision ),
  665. rounded = Math.round( number * factor ) / factor;
  666. if ( !isFinite(rounded) ) {
  667. rounded = number;
  668. }
  669. number = rounded;
  670. var numberString = number+"",
  671. right = "",
  672. split = numberString.split( /e/i ),
  673. exponent = split.length > 1 ? parseInt( split[1], 10 ) : 0;
  674. numberString = split[ 0 ];
  675. split = numberString.split( "." );
  676. numberString = split[ 0 ];
  677. right = split.length > 1 ? split[ 1 ] : "";
  678. var l;
  679. if ( exponent > 0 ) {
  680. right = zeroPad( right, exponent, false );
  681. numberString += right.slice( 0, exponent );
  682. right = right.substr( exponent );
  683. }
  684. else if ( exponent < 0 ) {
  685. exponent = -exponent;
  686. numberString = zeroPad( numberString, exponent + 1, true );
  687. right = numberString.slice( -exponent, numberString.length ) + right;
  688. numberString = numberString.slice( 0, -exponent );
  689. }
  690. if ( precision > 0 ) {
  691. right = formatInfo[ "." ] +
  692. ( (right.length > precision) ? right.slice(0, precision) : zeroPad(right, precision) );
  693. }
  694. else {
  695. right = "";
  696. }
  697. var stringIndex = numberString.length - 1,
  698. sep = formatInfo[ "," ],
  699. ret = "";
  700. while ( stringIndex >= 0 ) {
  701. if ( curSize === 0 || curSize > stringIndex ) {
  702. return numberString.slice( 0, stringIndex + 1 ) + ( ret.length ? (sep + ret + right) : right );
  703. }
  704. ret = numberString.slice( stringIndex - curSize + 1, stringIndex + 1 ) + ( ret.length ? (sep + ret) : "" );
  705. stringIndex -= curSize;
  706. if ( curGroupIndex < groupSizes.length ) {
  707. curSize = groupSizes[ curGroupIndex ];
  708. curGroupIndex++;
  709. }
  710. }
  711. return numberString.slice( 0, stringIndex + 1 ) + sep + ret + right;
  712. };
  713. formatNumber = function( value, format, culture ) {
  714. if ( !isFinite(value) ) {
  715. if ( value === Infinity ) {
  716. return culture.numberFormat.positiveInfinity;
  717. }
  718. if ( value === -Infinity ) {
  719. return culture.numberFormat.negativeInfinity;
  720. }
  721. return culture.numberFormat.NaN;
  722. }
  723. if ( !format || format === "i" ) {
  724. return culture.name.length ? value.toLocaleString() : value.toString();
  725. }
  726. format = format || "D";
  727. var nf = culture.numberFormat,
  728. number = Math.abs( value ),
  729. precision = -1,
  730. pattern;
  731. if ( format.length > 1 ) precision = parseInt( format.slice(1), 10 );
  732. var current = format.charAt( 0 ).toUpperCase(),
  733. formatInfo;
  734. switch ( current ) {
  735. case "D":
  736. pattern = "n";
  737. number = truncate( number );
  738. if ( precision !== -1 ) {
  739. number = zeroPad( "" + number, precision, true );
  740. }
  741. if ( value < 0 ) number = "-" + number;
  742. break;
  743. case "N":
  744. formatInfo = nf;
  745. /* falls through */
  746. case "C":
  747. formatInfo = formatInfo || nf.currency;
  748. /* falls through */
  749. case "P":
  750. formatInfo = formatInfo || nf.percent;
  751. pattern = value < 0 ? formatInfo.pattern[ 0 ] : ( formatInfo.pattern[1] || "n" );
  752. if ( precision === -1 ) precision = formatInfo.decimals;
  753. number = expandNumber( number * (current === "P" ? 100 : 1), precision, formatInfo );
  754. break;
  755. default:
  756. throw "Bad number format specifier: " + current;
  757. }
  758. var patternParts = /n|\$|-|%/g,
  759. ret = "";
  760. for ( ; ; ) {
  761. var index = patternParts.lastIndex,
  762. ar = patternParts.exec( pattern );
  763. ret += pattern.slice( index, ar ? ar.index : pattern.length );
  764. if ( !ar ) {
  765. break;
  766. }
  767. switch ( ar[0] ) {
  768. case "n":
  769. ret += number;
  770. break;
  771. case "$":
  772. ret += nf.currency.symbol;
  773. break;
  774. case "-":
  775. // don't make 0 negative
  776. if ( /[1-9]/.test(number) ) {
  777. ret += nf[ "-" ];
  778. }
  779. break;
  780. case "%":
  781. ret += nf.percent.symbol;
  782. break;
  783. }
  784. }
  785. return ret;
  786. };
  787. }());
  788. getTokenRegExp = function() {
  789. // regular expression for matching date and time tokens in format strings.
  790. return (/\/|dddd|ddd|dd|d|MMMM|MMM|MM|M|yyyy|yy|y|hh|h|HH|H|mm|m|ss|s|tt|t|fff|ff|f|zzz|zz|z|gg|g/g);
  791. };
  792. getEra = function( date, eras ) {
  793. if ( !eras ) return 0;
  794. var start, ticks = date.getTime();
  795. for ( var i = 0, l = eras.length; i < l; i++ ) {
  796. start = eras[ i ].start;
  797. if ( start === null || ticks >= start ) {
  798. return i;
  799. }
  800. }
  801. return 0;
  802. };
  803. getEraYear = function( date, cal, era, sortable ) {
  804. var year = date.getFullYear();
  805. if ( !sortable && cal.eras ) {
  806. // convert normal gregorian year to era-shifted gregorian
  807. // year by subtracting the era offset
  808. year -= cal.eras[ era ].offset;
  809. }
  810. return year;
  811. };
  812. // parseExact
  813. (function() {
  814. var expandYear,
  815. getDayIndex,
  816. getMonthIndex,
  817. getParseRegExp,
  818. outOfRange,
  819. toUpper,
  820. toUpperArray;
  821. expandYear = function( cal, year ) {
  822. // expands 2-digit year into 4 digits.
  823. if ( year < 100 ) {
  824. var now = new Date(),
  825. era = getEra( now ),
  826. curr = getEraYear( now, cal, era ),
  827. twoDigitYearMax = cal.twoDigitYearMax;
  828. twoDigitYearMax = typeof twoDigitYearMax === "string" ? new Date().getFullYear() % 100 + parseInt( twoDigitYearMax, 10 ) : twoDigitYearMax;
  829. year += curr - ( curr % 100 );
  830. if ( year > twoDigitYearMax ) {
  831. year -= 100;
  832. }
  833. }
  834. return year;
  835. };
  836. getDayIndex = function ( cal, value, abbr ) {
  837. var ret,
  838. days = cal.days,
  839. upperDays = cal._upperDays;
  840. if ( !upperDays ) {
  841. cal._upperDays = upperDays = [
  842. toUpperArray( days.names ),
  843. toUpperArray( days.namesAbbr ),
  844. toUpperArray( days.namesShort )
  845. ];
  846. }
  847. value = toUpper( value );
  848. if ( abbr ) {
  849. ret = arrayIndexOf( upperDays[1], value );
  850. if ( ret === -1 ) {
  851. ret = arrayIndexOf( upperDays[2], value );
  852. }
  853. }
  854. else {
  855. ret = arrayIndexOf( upperDays[0], value );
  856. }
  857. return ret;
  858. };
  859. getMonthIndex = function( cal, value, abbr ) {
  860. var months = cal.months,
  861. monthsGen = cal.monthsGenitive || cal.months,
  862. upperMonths = cal._upperMonths,
  863. upperMonthsGen = cal._upperMonthsGen;
  864. if ( !upperMonths ) {
  865. cal._upperMonths = upperMonths = [
  866. toUpperArray( months.names ),
  867. toUpperArray( months.namesAbbr )
  868. ];
  869. cal._upperMonthsGen = upperMonthsGen = [
  870. toUpperArray( monthsGen.names ),
  871. toUpperArray( monthsGen.namesAbbr )
  872. ];
  873. }
  874. value = toUpper( value );
  875. var i = arrayIndexOf( abbr ? upperMonths[1] : upperMonths[0], value );
  876. if ( i < 0 ) {
  877. i = arrayIndexOf( abbr ? upperMonthsGen[1] : upperMonthsGen[0], value );
  878. }
  879. return i;
  880. };
  881. getParseRegExp = function( cal, format ) {
  882. // converts a format string into a regular expression with groups that
  883. // can be used to extract date fields from a date string.
  884. // check for a cached parse regex.
  885. var re = cal._parseRegExp;
  886. if ( !re ) {
  887. cal._parseRegExp = re = {};
  888. }
  889. else {
  890. var reFormat = re[ format ];
  891. if ( reFormat ) {
  892. return reFormat;
  893. }
  894. }
  895. // expand single digit formats, then escape regular expression characters.
  896. var expFormat = expandFormat( cal, format ).replace( /([\^\$\.\*\+\?\|\[\]\(\)\{\}])/g, "\\\\$1" ),
  897. regexp = [ "^" ],
  898. groups = [],
  899. index = 0,
  900. quoteCount = 0,
  901. tokenRegExp = getTokenRegExp(),
  902. match;
  903. // iterate through each date token found.
  904. while ( (match = tokenRegExp.exec(expFormat)) !== null ) {
  905. var preMatch = expFormat.slice( index, match.index );
  906. index = tokenRegExp.lastIndex;
  907. // don't replace any matches that occur inside a string literal.
  908. quoteCount += appendPreOrPostMatch( preMatch, regexp );
  909. if ( quoteCount % 2 ) {
  910. regexp.push( match[0] );
  911. continue;
  912. }
  913. // add a regex group for the token.
  914. var m = match[ 0 ],
  915. len = m.length,
  916. add;
  917. switch ( m ) {
  918. case "dddd": case "ddd":
  919. case "MMMM": case "MMM":
  920. case "gg": case "g":
  921. add = "(\\D+)";
  922. break;
  923. case "tt": case "t":
  924. add = "(\\D*)";
  925. break;
  926. case "yyyy":
  927. case "fff":
  928. case "ff":
  929. case "f":
  930. add = "(\\d{" + len + "})";
  931. break;
  932. case "dd": case "d":
  933. case "MM": case "M":
  934. case "yy": case "y":
  935. case "HH": case "H":
  936. case "hh": case "h":
  937. case "mm": case "m":
  938. case "ss": case "s":
  939. add = "(\\d\\d?)";
  940. break;
  941. case "zzz":
  942. add = "([+-]?\\d\\d?:\\d{2})";
  943. break;
  944. case "zz": case "z":
  945. add = "([+-]?\\d\\d?)";
  946. break;
  947. case "/":
  948. add = "(\\/)";
  949. break;
  950. default:
  951. throw "Invalid date format pattern \'" + m + "\'.";
  952. }
  953. if ( add ) {
  954. regexp.push( add );
  955. }
  956. groups.push( match[0] );
  957. }
  958. appendPreOrPostMatch( expFormat.slice(index), regexp );
  959. regexp.push( "$" );
  960. // allow whitespace to differ when matching formats.
  961. var regexpStr = regexp.join( "" ).replace( /\s+/g, "\\s+" ),
  962. parseRegExp = { "regExp": regexpStr, "groups": groups };
  963. // cache the regex for this format.
  964. return re[ format ] = parseRegExp;
  965. };
  966. outOfRange = function( value, low, high ) {
  967. return value < low || value > high;
  968. };
  969. toUpper = function( value ) {
  970. // "he-IL" has non-breaking space in weekday names.
  971. return value.split( "\u00A0" ).join( " " ).toUpperCase();
  972. };
  973. toUpperArray = function( arr ) {
  974. var results = [];
  975. for ( var i = 0, l = arr.length; i < l; i++ ) {
  976. results[ i ] = toUpper( arr[i] );
  977. }
  978. return results;
  979. };
  980. parseExact = function( value, format, culture ) {
  981. // try to parse the date string by matching against the format string
  982. // while using the specified culture for date field names.
  983. value = trim( value );
  984. var cal = culture.calendar,
  985. // convert date formats into regular expressions with groupings.
  986. // use the regexp to determine the input format and extract the date fields.
  987. parseInfo = getParseRegExp( cal, format ),
  988. match = new RegExp( parseInfo.regExp ).exec( value );
  989. if ( match === null ) {
  990. return null;
  991. }
  992. // found a date format that matches the input.
  993. var groups = parseInfo.groups,
  994. era = null, year = null, month = null, date = null, weekDay = null,
  995. hour = 0, hourOffset, min = 0, sec = 0, msec = 0, tzMinOffset = null,
  996. pmHour = false;
  997. // iterate the format groups to extract and set the date fields.
  998. for ( var j = 0, jl = groups.length; j < jl; j++ ) {
  999. var matchGroup = match[ j + 1 ];
  1000. if ( matchGroup ) {
  1001. var current = groups[ j ],
  1002. clength = current.length,
  1003. matchInt = parseInt( matchGroup, 10 );
  1004. switch ( current ) {
  1005. case "dd": case "d":
  1006. // Day of month.
  1007. date = matchInt;
  1008. // check that date is generally in valid range, also checking overflow below.
  1009. if ( outOfRange(date, 1, 31) ) return null;
  1010. break;
  1011. case "MMM": case "MMMM":
  1012. month = getMonthIndex( cal, matchGroup, clength === 3 );
  1013. if ( outOfRange(month, 0, 11) ) return null;
  1014. break;
  1015. case "M": case "MM":
  1016. // Month.
  1017. month = matchInt - 1;
  1018. if ( outOfRange(month, 0, 11) ) return null;
  1019. break;
  1020. case "y": case "yy":
  1021. case "yyyy":
  1022. year = clength < 4 ? expandYear( cal, matchInt ) : matchInt;
  1023. if ( outOfRange(year, 0, 9999) ) return null;
  1024. break;
  1025. case "h": case "hh":
  1026. // Hours (12-hour clock).
  1027. hour = matchInt;
  1028. if ( hour === 12 ) hour = 0;
  1029. if ( outOfRange(hour, 0, 11) ) return null;
  1030. break;
  1031. case "H": case "HH":
  1032. // Hours (24-hour clock).
  1033. hour = matchInt;
  1034. if ( outOfRange(hour, 0, 23) ) return null;
  1035. break;
  1036. case "m": case "mm":
  1037. // Minutes.
  1038. min = matchInt;
  1039. if ( outOfRange(min, 0, 59) ) return null;
  1040. break;
  1041. case "s": case "ss":
  1042. // Seconds.
  1043. sec = matchInt;
  1044. if ( outOfRange(sec, 0, 59) ) return null;
  1045. break;
  1046. case "tt": case "t":
  1047. // AM/PM designator.
  1048. // see if it is standard, upper, or lower case PM. If not, ensure it is at least one of
  1049. // the AM tokens. If not, fail the parse for this format.
  1050. pmHour = cal.PM && ( matchGroup === cal.PM[0] || matchGroup === cal.PM[1] || matchGroup === cal.PM[2] );
  1051. if (
  1052. !pmHour && (
  1053. !cal.AM || ( matchGroup !== cal.AM[0] && matchGroup !== cal.AM[1] && matchGroup !== cal.AM[2] )
  1054. )
  1055. ) return null;
  1056. break;
  1057. case "f":
  1058. // Deciseconds.
  1059. case "ff":
  1060. // Centiseconds.
  1061. case "fff":
  1062. // Milliseconds.
  1063. msec = matchInt * Math.pow( 10, 3 - clength );
  1064. if ( outOfRange(msec, 0, 999) ) return null;
  1065. break;
  1066. case "ddd":
  1067. // Day of week.
  1068. case "dddd":
  1069. // Day of week.
  1070. weekDay = getDayIndex( cal, matchGroup, clength === 3 );
  1071. if ( outOfRange(weekDay, 0, 6) ) return null;
  1072. break;
  1073. case "zzz":
  1074. // Time zone offset in +/- hours:min.
  1075. var offsets = matchGroup.split( /:/ );
  1076. if ( offsets.length !== 2 ) return null;
  1077. hourOffset = parseInt( offsets[0], 10 );
  1078. if ( outOfRange(hourOffset, -12, 13) ) return null;
  1079. var minOffset = parseInt( offsets[1], 10 );
  1080. if ( outOfRange(minOffset, 0, 59) ) return null;
  1081. tzMinOffset = ( hourOffset * 60 ) + ( startsWith(matchGroup, "-") ? -minOffset : minOffset );
  1082. break;
  1083. case "z": case "zz":
  1084. // Time zone offset in +/- hours.
  1085. hourOffset = matchInt;
  1086. if ( outOfRange(hourOffset, -12, 13) ) return null;
  1087. tzMinOffset = hourOffset * 60;
  1088. break;
  1089. case "g": case "gg":
  1090. var eraName = matchGroup;
  1091. if ( !eraName || !cal.eras ) return null;
  1092. eraName = trim( eraName.toLowerCase() );
  1093. for ( var i = 0, l = cal.eras.length; i < l; i++ ) {
  1094. if ( eraName === cal.eras[i].name.toLowerCase() ) {
  1095. era = i;
  1096. break;
  1097. }
  1098. }
  1099. // could not find an era with that name
  1100. if ( era === null ) return null;
  1101. break;
  1102. }
  1103. }
  1104. }
  1105. var result = new Date(), defaultYear, convert = cal.convert;
  1106. defaultYear = convert ? convert.fromGregorian( result )[ 0 ] : result.getFullYear();
  1107. if ( year === null ) {
  1108. year = defaultYear;
  1109. }
  1110. else if ( cal.eras ) {
  1111. // year must be shifted to normal gregorian year
  1112. // but not if year was not specified, its already normal gregorian
  1113. // per the main if clause above.
  1114. year += cal.eras[( era || 0 )].offset;
  1115. }
  1116. // set default day and month to 1 and January, so if unspecified, these are the defaults
  1117. // instead of the current day/month.
  1118. if ( month === null ) {
  1119. month = 0;
  1120. }
  1121. if ( date === null ) {
  1122. date = 1;
  1123. }
  1124. // now have year, month, and date, but in the culture's calendar.
  1125. // convert to gregorian if necessary
  1126. if ( convert ) {
  1127. result = convert.toGregorian( year, month, date );
  1128. // conversion failed, must be an invalid match
  1129. if ( result === null ) return null;
  1130. }
  1131. else {
  1132. // have to set year, month and date together to avoid overflow based on current date.
  1133. result.setFullYear( year, month, date );
  1134. // check to see if date overflowed for specified month (only checked 1-31 above).
  1135. if ( result.getDate() !== date ) return null;
  1136. // invalid day of week.
  1137. if ( weekDay !== null && result.getDay() !== weekDay ) {
  1138. return null;
  1139. }
  1140. }
  1141. // if pm designator token was found make sure the hours fit the 24-hour clock.
  1142. if ( pmHour && hour < 12 ) {
  1143. hour += 12;
  1144. }
  1145. result.setHours( hour, min, sec, msec );
  1146. if ( tzMinOffset !== null ) {
  1147. // adjust timezone to utc before applying local offset.
  1148. var adjustedMin = result.getMinutes() - ( tzMinOffset + result.getTimezoneOffset() );
  1149. // Safari limits hours and minutes to the range of -127 to 127. We need to use setHours
  1150. // to ensure both these fields will not exceed this range. adjustedMin will range
  1151. // somewhere between -1440 and 1500, so we only need to split this into hours.
  1152. result.setHours( result.getHours() + parseInt(adjustedMin / 60, 10), adjustedMin % 60 );
  1153. }
  1154. return result;
  1155. };
  1156. }());
  1157. parseNegativePattern = function( value, nf, negativePattern ) {
  1158. var neg = nf[ "-" ],
  1159. pos = nf[ "+" ],
  1160. ret;
  1161. switch ( negativePattern ) {
  1162. case "n -":
  1163. neg = " " + neg;
  1164. pos = " " + pos;
  1165. /* falls through */
  1166. case "n-":
  1167. if ( endsWith(value, neg) ) {
  1168. ret = [ "-", value.substr(0, value.length - neg.length) ];
  1169. }
  1170. else if ( endsWith(value, pos) ) {
  1171. ret = [ "+", value.substr(0, value.length - pos.length) ];
  1172. }
  1173. break;
  1174. case "- n":
  1175. neg += " ";
  1176. pos += " ";
  1177. /* falls through */
  1178. case "-n":
  1179. if ( startsWith(value, neg) ) {
  1180. ret = [ "-", value.substr(neg.length) ];
  1181. }
  1182. else if ( startsWith(value, pos) ) {
  1183. ret = [ "+", value.substr(pos.length) ];
  1184. }
  1185. break;
  1186. case "(n)":
  1187. if ( startsWith(value, "(") && endsWith(value, ")") ) {
  1188. ret = [ "-", value.substr(1, value.length - 2) ];
  1189. }
  1190. break;
  1191. }
  1192. return ret || [ "", value ];
  1193. };
  1194. //
  1195. // public instance functions
  1196. //
  1197. Globalize.prototype.findClosestCulture = function( cultureSelector ) {
  1198. return Globalize.findClosestCulture.call( this, cultureSelector );
  1199. };
  1200. Globalize.prototype.format = function( value, format, cultureSelector ) {
  1201. return Globalize.format.call( this, value, format, cultureSelector );
  1202. };
  1203. Globalize.prototype.localize = function( key, cultureSelector ) {
  1204. return Globalize.localize.call( this, key, cultureSelector );
  1205. };
  1206. Globalize.prototype.parseInt = function( value, radix, cultureSelector ) {
  1207. return Globalize.parseInt.call( this, value, radix, cultureSelector );
  1208. };
  1209. Globalize.prototype.parseFloat = function( value, radix, cultureSelector ) {
  1210. return Globalize.parseFloat.call( this, value, radix, cultureSelector );
  1211. };
  1212. Globalize.prototype.culture = function( cultureSelector ) {
  1213. return Globalize.culture.call( this, cultureSelector );
  1214. };
  1215. //
  1216. // public singleton functions
  1217. //
  1218. Globalize.addCultureInfo = function( cultureName, baseCultureName, info ) {
  1219. var base = {},
  1220. isNew = false;
  1221. if ( typeof cultureName !== "string" ) {
  1222. // cultureName argument is optional string. If not specified, assume info is first
  1223. // and only argument. Specified info deep-extends current culture.
  1224. info = cultureName;
  1225. cultureName = this.culture().name;
  1226. base = this.cultures[ cultureName ];
  1227. } else if ( typeof baseCultureName !== "string" ) {
  1228. // baseCultureName argument is optional string. If not specified, assume info is second
  1229. // argument. Specified info deep-extends specified culture.
  1230. // If specified culture does not exist, create by deep-extending default
  1231. info = baseCultureName;
  1232. isNew = ( this.cultures[ cultureName ] == null );
  1233. base = this.cultures[ cultureName ] || this.cultures[ "default" ];
  1234. } else {
  1235. // cultureName and baseCultureName specified. Assume a new culture is being created
  1236. // by deep-extending an specified base culture
  1237. isNew = true;
  1238. base = this.cultures[ baseCultureName ];
  1239. }
  1240. this.cultures[ cultureName ] = extend(true, {},
  1241. base,
  1242. info
  1243. );
  1244. // Make the standard calendar the current culture if it's a new culture
  1245. if ( isNew ) {
  1246. this.cultures[ cultureName ].calendar = this.cultures[ cultureName ].calendars.standard;
  1247. }
  1248. };
  1249. Globalize.findClosestCulture = function( name ) {
  1250. var match;
  1251. if ( !name ) {
  1252. return this.findClosestCulture( this.cultureSelector ) || this.cultures[ "default" ];
  1253. }
  1254. if ( typeof name === "string" ) {
  1255. name = name.split( "," );
  1256. }
  1257. if ( isArray(name) ) {
  1258. var lang,
  1259. cultures = this.cultures,
  1260. list = name,
  1261. i, l = list.length,
  1262. prioritized = [];
  1263. for ( i = 0; i < l; i++ ) {
  1264. name = trim( list[i] );
  1265. var pri, parts = name.split( ";" );
  1266. lang = trim( parts[0] );
  1267. if ( parts.length === 1 ) {
  1268. pri = 1;
  1269. }
  1270. else {
  1271. name = trim( parts[1] );
  1272. if ( name.indexOf("q=") === 0 ) {
  1273. name = name.substr( 2 );
  1274. pri = parseFloat( name );
  1275. pri = isNaN( pri ) ? 0 : pri;
  1276. }
  1277. else {
  1278. pri = 1;
  1279. }
  1280. }
  1281. prioritized.push({ lang: lang, pri: pri });
  1282. }
  1283. prioritized.sort(function( a, b ) {
  1284. if ( a.pri < b.pri ) {
  1285. return 1;
  1286. } else if ( a.pri > b.pri ) {
  1287. return -1;
  1288. }
  1289. return 0;
  1290. });
  1291. // exact match
  1292. for ( i = 0; i < l; i++ ) {
  1293. lang = prioritized[ i ].lang;
  1294. match = cultures[ lang ];
  1295. if ( match ) {
  1296. return match;
  1297. }
  1298. }
  1299. // neutral language match
  1300. for ( i = 0; i < l; i++ ) {
  1301. lang = prioritized[ i ].lang;
  1302. do {
  1303. var index = lang.lastIndexOf( "-" );
  1304. if ( index === -1 ) {
  1305. break;
  1306. }
  1307. // strip off the last part. e.g. en-US => en
  1308. lang = lang.substr( 0, index );
  1309. match = cultures[ lang ];
  1310. if ( match ) {
  1311. return match;
  1312. }
  1313. }
  1314. while ( 1 );
  1315. }
  1316. // last resort: match first culture using that language
  1317. for ( i = 0; i < l; i++ ) {
  1318. lang = prioritized[ i ].lang;
  1319. for ( var cultureKey in cultures ) {
  1320. var culture = cultures[ cultureKey ];
  1321. if ( culture.language == lang ) {
  1322. return culture;
  1323. }
  1324. }
  1325. }
  1326. }
  1327. else if ( typeof name === "object" ) {
  1328. return name;
  1329. }
  1330. return match || null;
  1331. };
  1332. Globalize.format = function( value, format, cultureSelector ) {
  1333. var culture = this.findClosestCulture( cultureSelector );
  1334. if ( value instanceof Date ) {
  1335. value = formatDate( value, format, culture );
  1336. }
  1337. else if ( typeof value === "number" ) {
  1338. value = formatNumber( value, format, culture );
  1339. }
  1340. return value;
  1341. };
  1342. Globalize.localize = function( key, cultureSelector ) {
  1343. return this.findClosestCulture( cultureSelector ).messages[ key ] ||
  1344. this.cultures[ "default" ].messages[ key ];
  1345. };
  1346. Globalize.parseDate = function( value, formats, culture ) {
  1347. culture = this.findClosestCulture( culture );
  1348. var date, prop, patterns;
  1349. if ( formats ) {
  1350. if ( typeof formats === "string" ) {
  1351. formats = [ formats ];
  1352. }
  1353. if ( formats.length ) {
  1354. for ( var i = 0, l = formats.length; i < l; i++ ) {
  1355. var format = formats[ i ];
  1356. if ( format ) {
  1357. date = parseExact( value, format, culture );
  1358. if ( date ) {
  1359. break;
  1360. }
  1361. }
  1362. }
  1363. }
  1364. } else {
  1365. patterns = culture.calendar.patterns;
  1366. for ( prop in patterns ) {
  1367. date = parseExact( value, patterns[prop], culture );
  1368. if ( date ) {
  1369. break;
  1370. }
  1371. }
  1372. }
  1373. return date || null;
  1374. };
  1375. Globalize.parseInt = function( value, radix, cultureSelector ) {
  1376. return truncate( Globalize.parseFloat(value, radix, cultureSelector) );
  1377. };
  1378. Globalize.parseFloat = function( value, radix, cultureSelector ) {
  1379. // radix argument is optional
  1380. if ( typeof radix !== "number" ) {
  1381. cultureSelector = radix;
  1382. radix = 10;
  1383. }
  1384. var culture = this.findClosestCulture( cultureSelector );
  1385. var ret = NaN,
  1386. nf = culture.numberFormat;
  1387. if ( value.indexOf(culture.numberFormat.currency.symbol) > -1 ) {
  1388. // remove currency symbol
  1389. value = value.replace( culture.numberFormat.currency.symbol, "" );
  1390. // replace decimal seperator
  1391. value = value.replace( culture.numberFormat.currency["."], culture.numberFormat["."] );
  1392. }
  1393. //Remove percentage character from number string before parsing
  1394. if ( value.indexOf(culture.numberFormat.percent.symbol) > -1){
  1395. value = value.replace( culture.numberFormat.percent.symbol, "" );
  1396. }
  1397. // remove spaces: leading, trailing and between - and number. Used for negative currency pt-BR
  1398. value = value.replace( / /g, "" );
  1399. // allow infinity or hexidecimal
  1400. if ( regexInfinity.test(value) ) {
  1401. ret = parseFloat( value );
  1402. }
  1403. else if ( !radix && regexHex.test(value) ) {
  1404. ret = parseInt( value, 16 );
  1405. }
  1406. else {
  1407. // determine sign and number
  1408. var signInfo = parseNegativePattern( value, nf, nf.pattern[0] ),
  1409. sign = signInfo[ 0 ],
  1410. num = signInfo[ 1 ];
  1411. // #44 - try parsing as "(n)"
  1412. if ( sign === "" && nf.pattern[0] !== "(n)" ) {
  1413. signInfo = parseNegativePattern( value, nf, "(n)" );
  1414. sign = signInfo[ 0 ];
  1415. num = signInfo[ 1 ];
  1416. }
  1417. // try parsing as "-n"
  1418. if ( sign === "" && nf.pattern[0] !== "-n" ) {
  1419. signInfo = parseNegativePattern( value, nf, "-n" );
  1420. sign = signInfo[ 0 ];
  1421. num = signInfo[ 1 ];
  1422. }
  1423. sign = sign || "+";
  1424. // determine exponent and number
  1425. var exponent,
  1426. intAndFraction,
  1427. exponentPos = num.indexOf( "e" );
  1428. if ( exponentPos < 0 ) exponentPos = num.indexOf( "E" );
  1429. if ( exponentPos < 0 ) {
  1430. intAndFraction = num;
  1431. exponent = null;
  1432. }
  1433. else {
  1434. intAndFraction = num.substr( 0, exponentPos );
  1435. exponent = num.substr( exponentPos + 1 );
  1436. }
  1437. // determine decimal position
  1438. var integer,
  1439. fraction,
  1440. decSep = nf[ "." ],
  1441. decimalPos = intAndFraction.indexOf( decSep );
  1442. if ( decimalPos < 0 ) {
  1443. integer = intAndFraction;
  1444. fraction = null;
  1445. }
  1446. else {
  1447. integer = intAndFraction.substr( 0, decimalPos );
  1448. fraction = intAndFraction.substr( decimalPos + decSep.length );
  1449. }
  1450. // handle groups (e.g. 1,000,000)
  1451. var groupSep = nf[ "," ];
  1452. integer = integer.split( groupSep ).join( "" );
  1453. var altGroupSep = groupSep.replace( /\u00A0/g, " " );
  1454. if ( groupSep !== altGroupSep ) {
  1455. integer = integer.split( altGroupSep ).join( "" );
  1456. }
  1457. // build a natively parsable number string
  1458. var p = sign + integer;
  1459. if ( fraction !== null ) {
  1460. p += "." + fraction;
  1461. }
  1462. if ( exponent !== null ) {
  1463. // exponent itself may have a number patternd
  1464. var expSignInfo = parseNegativePattern( exponent, nf, "-n" );
  1465. p += "e" + ( expSignInfo[0] || "+" ) + expSignInfo[ 1 ];
  1466. }
  1467. if ( regexParseFloat.test(p) ) {
  1468. ret = parseFloat( p );
  1469. }
  1470. }
  1471. return ret;
  1472. };
  1473. Globalize.culture = function( cultureSelector ) {
  1474. // setter
  1475. if ( typeof cultureSelector !== "undefined" ) {
  1476. this.cultureSelector = cultureSelector;
  1477. }
  1478. // getter
  1479. return this.findClosestCulture( cultureSelector ) || this.cultures[ "default" ];
  1480. };
  1481. document.Globalize = Globalize;
  1482. }(this));