X-Git-Url: http://git.ithinksw.org/extjs.git/blobdiff_plain/b37ceabb82336ee82757cd32efe353cfab8ec267..0494b8d9b9bb03ab6c22b34dae81261e3cd7e3e6:/src/util/Date.js diff --git a/src/util/Date.js b/src/util/Date.js index ede53c0a..44eaa3a5 100644 --- a/src/util/Date.js +++ b/src/util/Date.js @@ -1,8 +1,8 @@ /*! - * Ext JS Library 3.2.2 - * Copyright(c) 2006-2010 Ext JS, Inc. - * licensing@extjs.com - * http://www.extjs.com/license + * Ext JS Library 3.3.1 + * Copyright(c) 2006-2010 Sencha Inc. + * licensing@sencha.com + * http://www.sencha.com/license */ /** * @class Date @@ -158,7 +158,7 @@ Date.formatCodeToRegex = function(character, currentGroup) { g:0, c:null, s:Ext.escapeRe(character) // treat unrecognised characters as literals - } + }; }; // private shorthand for Date.formatCodeToRegex since we'll be using it fairly often @@ -452,7 +452,7 @@ Date.formatCodes.x = "String.leftPad(this.getDate(), 2, '0')"; t: "this.getDaysInMonth()", L: "(this.isLeapYear() ? 1 : 0)", o: "(this.getFullYear() + (this.getWeekOfYear() == 1 && this.getMonth() > 0 ? +1 : (this.getWeekOfYear() >= 52 && this.getMonth() < 11 ? -1 : 0)))", - Y: "this.getFullYear()", + Y: "String.leftPad(this.getFullYear(), 4, '0')", y: "('' + this.getFullYear()).substring(2, 4)", a: "(this.getHours() < 12 ? 'am' : 'pm')", A: "(this.getHours() < 12 ? 'AM' : 'PM')", @@ -512,7 +512,8 @@ Date.formatCodes.x = "String.leftPad(this.getDate(), 2, '0')"; s = s || 0; ms = ms || 0; - var dt = new Date(y, m - 1, d, h, i, s, ms); + // Special handling for year < 100 + var dt = new Date(y < 100 ? 100 : y, m - 1, d, h, i, s, ms).add(Date.YEAR, y < 100 ? y - 100 : 0); return y == dt.getFullYear() && m == dt.getMonth() + 1 && @@ -589,7 +590,7 @@ dt = Date.parseDate("2006-02-29 03:20:01", "Y-m-d H:i:s", true); // returns null special = false; code.push("'" + String.escape(ch) + "'"); } else { - code.push(Date.getFormatCode(ch)) + code.push(Date.getFormatCode(ch)); } } Date.formatFunctions[format] = new Function("return " + code.join('+')); @@ -629,7 +630,8 @@ dt = Date.parseDate("2006-02-29 03:20:01", "Y-m-d H:i:s", true); // returns null // these 2 values alone provide sufficient info to create a full date object // create Date object representing January 1st for the given year - "v = new Date(y, 0, 1, h, i, s, ms);", + // handle years < 100 appropriately + "v = new Date(y < 100 ? 100 : y, 0, 1, h, i, s, ms).add(Date.YEAR, y < 100 ? y - 100 : 0);", // then add day of year, checking for Date "rollover" if necessary "v = !strict? v : (strict === true && (z <= 364 || (v.isLeapYear() && z <= 365))? v.add(Date.DAY, z) : null);", @@ -637,7 +639,8 @@ dt = Date.parseDate("2006-02-29 03:20:01", "Y-m-d H:i:s", true); // returns null "v = null;", // invalid date, so return null "}else{", // plain old Date object - "v = new Date(y, m, d, h, i, s, ms);", + // handle years < 100 properly + "v = new Date(y < 100 ? 100 : y, m, d, h, i, s, ms).add(Date.YEAR, y < 100 ? y - 100 : 0);", "}", "}", "}", @@ -662,9 +665,12 @@ dt = Date.parseDate("2006-02-29 03:20:01", "Y-m-d H:i:s", true); // returns null calc = [], regex = [], special = false, - ch = ""; + ch = "", + i = 0, + obj, + last; - for (var i = 0; i < format.length; ++i) { + for (; i < format.length; ++i) { ch = format.charAt(i); if (!special && ch == "\\") { special = true; @@ -672,18 +678,26 @@ dt = Date.parseDate("2006-02-29 03:20:01", "Y-m-d H:i:s", true); // returns null special = false; regex.push(String.escape(ch)); } else { - var obj = $f(ch, currentGroup); + obj = $f(ch, currentGroup); currentGroup += obj.g; regex.push(obj.s); if (obj.g && obj.c) { - calc.push(obj.c); + if (obj.calcLast) { + last = obj.c; + } else { + calc.push(obj.c); + } } } } + + if (last) { + calc.push(last); + } - Date.parseRegexes[regexNum] = new RegExp("^" + regex.join('') + "$"); + Date.parseRegexes[regexNum] = new RegExp("^" + regex.join('') + "$", 'i'); Date.parseFunctions[format] = new Function("input", "strict", xf(code, regexNum, calc.join(''))); - } + }; }(), // private @@ -710,14 +724,14 @@ dt = Date.parseDate("2006-02-29 03:20:01", "Y-m-d H:i:s", true); // returns null g:0, c:null, s:"(?:" + a.join("|") +")" - } + }; }, l: function() { return { g:0, c:null, s:"(?:" + Date.dayNames.join("|") + ")" - } + }; }, N: { g:0, @@ -749,7 +763,7 @@ dt = Date.parseDate("2006-02-29 03:20:01", "Y-m-d H:i:s", true); // returns null g:1, c:"m = parseInt(Date.getMonthNumber(results[{0}]), 10);\n", // get localised month number s:"(" + Date.monthNames.join("|") + ")" - } + }; }, M: function() { for (var a = [], i = 0; i < 12; a.push(Date.getShortMonthName(i)), ++i); // get localised short month names @@ -791,19 +805,22 @@ dt = Date.parseDate("2006-02-29 03:20:01", "Y-m-d H:i:s", true); // returns null + "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n", // 2-digit year s:"(\\d{1,2})" }, - a: { - g:1, - c:"if (results[{0}] == 'am') {\n" - + "if (!h || h == 12) { h = 0; }\n" - + "} else { if (!h || h < 12) { h = (h || 0) + 12; }}", - s:"(am|pm)" + /** + * In the am/pm parsing routines, we allow both upper and lower case + * even though it doesn't exactly match the spec. It gives much more flexibility + * in being able to specify case insensitive regexes. + */ + a: function(){ + return $f("A"); }, A: { + // We need to calculate the hour before we apply AM/PM when parsing + calcLast: true, g:1, - c:"if (results[{0}] == 'AM') {\n" + c:"if (/(am)/i.test(results[{0}])) {\n" + "if (!h || h == 12) { h = 0; }\n" + "} else { if (!h || h < 12) { h = (h || 0) + 12; }}", - s:"(AM|PM)" + s:"(AM|PM|am|pm)" }, g: function() { return $f("G"); @@ -913,7 +930,7 @@ dt = Date.parseDate("2006-02-29 03:20:01", "Y-m-d H:i:s", true); // returns null ")?", ")?" ].join("") - } + }; }, U: { g:1, @@ -1005,7 +1022,7 @@ Ext.apply(Date.prototype, { Wyr = new Date(AWN * ms7d).getUTCFullYear(); return AWN - Math.floor(Date.UTC(Wyr, 0, 7) / ms7d) + 1; - } + }; }(), /** @@ -1076,7 +1093,7 @@ document.write(Date.dayNames[dt.getLastDayOfMonth()]); //output: 'Wednesday' var m = this.getMonth(); return m == 1 && this.isLeapYear() ? 29 : daysInMonth[m]; - } + }; }(), /** @@ -1314,4 +1331,4 @@ console.group('ISO-8601 Granularity Test (see http://www.w3.org/TR/NOTE-datetime console.log('Date.parseDate("1997-13-16T19:20:30.45+01:00", "c", true)= %o', Date.parseDate("1997-13-16T19:20:30.45+01:00", "c", true)); // strict date parsing with invalid month value console.groupEnd(); -//*/ +*/