iobroker.ebus 3.0.2 → 3.0.5

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