globalize.js 45 KB

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