datatype-date.js revision aad83354a51620a64a2453eaf7b0e8460523a137
0N/AYUI.add('datatype-date-parse', function(Y) {
2362N/A
1008N/A/**
1008N/A * The DataType utility provides a set of utility functions to operate on native
1008N/A * JavaScript data types.
1008N/A *
2362N/A * @module datatype
1008N/A */
2362N/Avar LANG = Y.Lang;
1008N/A
1008N/A/**
1008N/A * Parse number submodule.
1008N/A *
1008N/A * @class DataType.Number
1008N/A * @submodule datatype-number-format
1008N/A * @static
1008N/A */
1008N/AY.mix(Y.namespace("DataType.Date"), {
1008N/A /**
1008N/A * Converts data to type Date.
2362N/A *
2362N/A * @method parse
2362N/A * @param data {String | Number} Data to convert. Values supported by the Date constructor are supported.
1008N/A * @return {Date} A Date, or null.
1008N/A * @static
1008N/A */
1008N/A parse: function(data) {
0N/A var date = null;
0N/A
1821N/A //Convert to date
1821N/A if(!(LANG.isDate(data))) {
1821N/A date = new Date(data);
1821N/A }
1821N/A else {
1821N/A return date;
1821N/A }
1821N/A
1821N/A // Validate
1821N/A if(LANG.isDate(date) && (date != "Invalid Date")) { // Workaround for bug 2527965
2391N/A return date;
1821N/A }
1821N/A else {
2391N/A return null;
1821N/A }
1821N/A }
1821N/A});
2391N/A
1821N/A// Add Parsers shortcut
1821N/AY.namespace("Parsers").date = Y.DataType.Date.parse;
1821N/A
1821N/A
1821N/A
1821N/A}, '@VERSION@' );
1821N/A
1821N/AYUI.add('datatype-date-format', function(Y) {
1821N/A
1821N/A/**
1821N/A * The Date formatter utility implements strftime formatters for javascript based on the
1821N/A * Open Group specification defined at
1008N/A * http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html
1008N/A * This implementation does not include modified conversion specifiers (i.e., Ex and Ox)
1821N/A *
1008N/A * The following format specifiers are supported:
1008N/A *
1008N/A * \copydoc formats
1008N/A *
1008N/A * \par Usage:
1821N/A * This library may be used as follows:
1821N/A * \code
1821N/A * var d = Y.DataType.Date.format(new Date("2009/04/10", { format: "Today is %A, the %d of %B, %Y" });
1821N/A * \endcode
1821N/A *
1821N/A *
1821N/A * @module datatype
1821N/A * @submodule datatype-date
2228N/A * @requires oop
2228N/A * @title DataType Date Formatter Submodule
2228N/A */
2228N/A
1821N/A/**
0N/A * Pad a number with leading spaces, zeroes or something else
2402N/A * @param x {Number} The number to be padded
2402N/A * @param pad {String} The character to pad the number with
2402N/A * @param r {Number} (optional) The base of the pad, eg, 10 implies to two digits, 100 implies to 3 digits.
2402N/A * @private
2402N/A */
2402N/Avar xPad=function (x, pad, r)
2402N/A{
2402N/A if(typeof r === "undefined")
2402N/A {
2402N/A r=10;
2402N/A }
2402N/A pad = pad.toString();
2402N/A for( ; parseInt(x, 10)<r && r>1; r/=10) {
2402N/A x = pad + x;
2402N/A }
2402N/A return x.toString();
2402N/A};
2402N/A
2402N/AY.config.dateFormat = Y.config.dateFormat || "%Y-%m-%d";
0N/AY.config.locale = Y.config.locale || "en";
0N/A
1008N/A/**
1821N/A * Date subclass for the YUI DataType utility.
1821N/A * @class DataType.Date
1821N/A * @requires base
2228N/A * @static
2228N/A */
2228N/Avar Dt = {
2228N/A formats: {
2228N/A a: function (d, l) { return l.a[d.getDay()]; },
2228N/A A: function (d, l) { return l.A[d.getDay()]; },
2228N/A b: function (d, l) { return l.b[d.getMonth()]; },
2228N/A B: function (d, l) { return l.B[d.getMonth()]; },
2228N/A C: function (d) { return xPad(parseInt(d.getFullYear()/100, 10), 0); },
2228N/A d: ["getDate", "0"],
2402N/A e: ["getDate", " "],
2402N/A g: function (d) { return xPad(parseInt(Dt.formats.G(d)%100, 10), 0); },
2402N/A G: function (d) {
2402N/A var y = d.getFullYear();
2402N/A var V = parseInt(Dt.formats.V(d), 10);
2402N/A var W = parseInt(Dt.formats.W(d), 10);
2402N/A
2402N/A if(W > V) {
2402N/A y++;
2402N/A } else if(W===0 && V>=52) {
2402N/A y--;
2402N/A }
2402N/A
2402N/A return y;
2402N/A },
2402N/A H: ["getHours", "0"],
2402N/A I: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, 0); },
2402N/A j: function (d) {
2402N/A var gmd_1 = new Date("" + d.getFullYear() + "/1/1 GMT");
2402N/A var gmdate = new Date("" + d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate() + " GMT");
2402N/A var ms = gmdate - gmd_1;
1821N/A var doy = parseInt(ms/60000/60/24, 10)+1;
2228N/A return xPad(doy, 0, 100);
2228N/A },
2228N/A k: ["getHours", " "],
2228N/A l: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, " "); },
2228N/A m: function (d) { return xPad(d.getMonth()+1, 0); },
2228N/A M: ["getMinutes", "0"],
2228N/A p: function (d, l) { return l.p[d.getHours() >= 12 ? 1 : 0 ]; },
2228N/A P: function (d, l) { return l.P[d.getHours() >= 12 ? 1 : 0 ]; },
2228N/A s: function (d, l) { return parseInt(d.getTime()/1000, 10); },
2228N/A S: ["getSeconds", "0"],
2228N/A u: function (d) { var dow = d.getDay(); return dow===0?7:dow; },
1821N/A U: function (d) {
1821N/A var doy = parseInt(Dt.formats.j(d), 10);
1821N/A var rdow = 6-d.getDay();
2228N/A var woy = parseInt((doy+rdow)/7, 10);
2228N/A return xPad(woy, 0);
2228N/A },
2228N/A V: function (d) {
2228N/A var woy = parseInt(Dt.formats.W(d), 10);
1821N/A var dow1_1 = (new Date("" + d.getFullYear() + "/1/1")).getDay();
2228N/A // First week is 01 and not 00 as in the case of %U and %W,
1821N/A // so we add 1 to the final result except if day 1 of the year
2228N/A // is a Monday (then %W returns 01).
2228N/A // We also need to subtract 1 if the day 1 of the year is
1821N/A // Friday-Sunday, so the resulting equation becomes:
2228N/A var idow = woy + (dow1_1 > 4 || dow1_1 <= 1 ? 0 : 1);
1821N/A if(idow === 53 && (new Date("" + d.getFullYear() + "/12/31")).getDay() < 4)
1821N/A {
1821N/A idow = 1;
1821N/A }
1821N/A else if(idow === 0)
1821N/A {
1821N/A idow = Dt.formats.V(new Date("" + (d.getFullYear()-1) + "/12/31"));
1821N/A }
1821N/A
1821N/A return xPad(idow, 0);
2228N/A },
1821N/A w: "getDay",
1821N/A W: function (d) {
1821N/A var doy = parseInt(Dt.formats.j(d), 10);
1821N/A var rdow = 7-Dt.formats.u(d);
1821N/A var woy = parseInt((doy+rdow)/7, 10);
1821N/A return xPad(woy, 0, 10);
1821N/A },
1821N/A y: function (d) { return xPad(d.getFullYear()%100, 0); },
0N/A Y: "getFullYear",
1821N/A z: function (d) {
1008N/A var o = d.getTimezoneOffset();
0N/A var H = xPad(parseInt(Math.abs(o/60), 10), 0);
0N/A var M = xPad(Math.abs(o%60), 0);
2228N/A return (o>0?"-":"+") + H + M;
2228N/A },
2228N/A Z: function (d) {
2228N/A var tz = d.toString().replace(/^.*:\d\d( GMT[+-]\d+)? \(?([A-Za-z ]+)\)?\d*$/, "$2").replace(/[a-z ]/g, "");
2228N/A if(tz.length > 4) {
2228N/A tz = Dt.formats.z(d);
0N/A }
1821N/A return tz;
0N/A },
1008N/A "%": function (d) { return "%"; }
1008N/A },
1008N/A
1008N/A aggregates: {
2228N/A c: "locale",
1008N/A D: "%m/%d/%y",
2228N/A F: "%Y-%m-%d",
2228N/A h: "%b",
0N/A n: "\n",
1008N/A r: "locale",
1008N/A R: "%H:%M",
1008N/A t: "\t",
2228N/A T: "%H:%M:%S",
2228N/A x: "locale",
2228N/A X: "locale"
2228N/A //"+": "%a %b %e %T %Z %Y"
2228N/A },
2228N/A
2228N/A /**
1008N/A * Takes a native JavaScript Date and formats it as a string for display to user.
1008N/A *
1008N/A * @method format
1008N/A * @param oDate {Date} Date.
1008N/A * @param oConfig {Object} (Optional) Object literal of configuration values:
1008N/A * <dl>
1821N/A * <dt>format {String} (Optional)</dt>
1008N/A * <dd>
1008N/A * <p>
0N/A * Any strftime string is supported, such as "%I:%M:%S %p". strftime has several format specifiers defined by the Open group at
0N/A * <a href="http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html">http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html</a>
1008N/A * PHP added a few of its own, defined at <a href="http://www.php.net/strftime">http://www.php.net/strftime</a>
1008N/A * </p>
1008N/A * <p>
1008N/A * This javascript implementation supports all the PHP specifiers and a few more. The full list is below.
1008N/A * </p>
0N/A * <p>
1821N/A * If not specified, it defaults to the ISO8601 standard date format: %Y-%m-%d. This may be overridden by changing Y.config.dateFormat
1821N/A * </p>
1821N/A * <dl>
1821N/A * <dt>%a</dt> <dd>abbreviated weekday name according to the current locale</dd>
1821N/A * <dt>%A</dt> <dd>full weekday name according to the current locale</dd>
1821N/A * <dt>%b</dt> <dd>abbreviated month name according to the current locale</dd>
1821N/A * <dt>%B</dt> <dd>full month name according to the current locale</dd>
1821N/A * <dt>%c</dt> <dd>preferred date and time representation for the current locale</dd>
1821N/A * <dt>%C</dt> <dd>century number (the year divided by 100 and truncated to an integer, range 00 to 99)</dd>
1821N/A * <dt>%d</dt> <dd>day of the month as a decimal number (range 01 to 31)</dd>
1821N/A * <dt>%D</dt> <dd>same as %m/%d/%y</dd>
1821N/A * <dt>%e</dt> <dd>day of the month as a decimal number, a single digit is preceded by a space (range " 1" to "31")</dd>
1821N/A * <dt>%F</dt> <dd>same as %Y-%m-%d (ISO 8601 date format)</dd>
1821N/A * <dt>%g</dt> <dd>like %G, but without the century</dd>
1821N/A * <dt>%G</dt> <dd>The 4-digit year corresponding to the ISO week number</dd>
2402N/A * <dt>%h</dt> <dd>same as %b</dd>
2402N/A * <dt>%H</dt> <dd>hour as a decimal number using a 24-hour clock (range 00 to 23)</dd>
2402N/A * <dt>%I</dt> <dd>hour as a decimal number using a 12-hour clock (range 01 to 12)</dd>
2391N/A * <dt>%j</dt> <dd>day of the year as a decimal number (range 001 to 366)</dd>
2391N/A * <dt>%k</dt> <dd>hour as a decimal number using a 24-hour clock (range 0 to 23); single digits are preceded by a blank. (See also %H.)</dd>
1008N/A * <dt>%l</dt> <dd>hour as a decimal number using a 12-hour clock (range 1 to 12); single digits are preceded by a blank. (See also %I.) </dd>
1008N/A * <dt>%m</dt> <dd>month as a decimal number (range 01 to 12)</dd>
1008N/A * <dt>%M</dt> <dd>minute as a decimal number</dd>
1008N/A * <dt>%n</dt> <dd>newline character</dd>
1008N/A * <dt>%p</dt> <dd>either "AM" or "PM" according to the given time value, or the corresponding strings for the current locale</dd>
0N/A * <dt>%P</dt> <dd>like %p, but lower case</dd>
1008N/A * <dt>%r</dt> <dd>time in a.m. and p.m. notation equal to %I:%M:%S %p</dd>
1008N/A * <dt>%R</dt> <dd>time in 24 hour notation equal to %H:%M</dd>
2391N/A * <dt>%s</dt> <dd>number of seconds since the Epoch, ie, since 1970-01-01 00:00:00 UTC</dd>
1008N/A * <dt>%S</dt> <dd>second as a decimal number</dd>
3866N/A * <dt>%t</dt> <dd>tab character</dd>
1008N/A * <dt>%T</dt> <dd>current time, equal to %H:%M:%S</dd>
1821N/A * <dt>%u</dt> <dd>weekday as a decimal number [1,7], with 1 representing Monday</dd>
1821N/A * <dt>%U</dt> <dd>week number of the current year as a decimal number, starting with the
1821N/A * first Sunday as the first day of the first week</dd>
1821N/A * <dt>%V</dt> <dd>The ISO 8601:1988 week number of the current year as a decimal number,
1821N/A * range 01 to 53, where week 1 is the first week that has at least 4 days
1821N/A * in the current year, and with Monday as the first day of the week.</dd>
1947N/A * <dt>%w</dt> <dd>day of the week as a decimal, Sunday being 0</dd>
1947N/A * <dt>%W</dt> <dd>week number of the current year as a decimal number, starting with the
1947N/A * first Monday as the first day of the first week</dd>
1947N/A * <dt>%x</dt> <dd>preferred date representation for the current locale without the time</dd>
1947N/A * <dt>%X</dt> <dd>preferred time representation for the current locale without the date</dd>
1947N/A * <dt>%y</dt> <dd>year as a decimal number without a century (range 00 to 99)</dd>
1947N/A * <dt>%Y</dt> <dd>year as a decimal number including the century</dd>
1947N/A * <dt>%z</dt> <dd>numerical time zone representation</dd>
1947N/A * <dt>%Z</dt> <dd>time zone name or abbreviation</dd>
2017N/A * <dt>%%</dt> <dd>a literal "%" character</dd>
1947N/A * </dl>
1821N/A * </dd>
1821N/A * <dt>locale {String} (Optional)</dt>
1947N/A * <dd>
1821N/A * The locale to use when displaying days of week, months of the year, and other locale specific
1947N/A * strings. If not specified, this defaults to "en" (though this may be overridden by changing Y.config.locale).
1947N/A * The following locales are built in:
1821N/A * <dl>
2017N/A * <dt>en</dt>
1821N/A * <dd>English</dd>
1821N/A * <dt>en-US</dt>
1821N/A * <dd>US English</dd>
1821N/A * <dt>en-GB</dt>
1821N/A * <dd>British English</dd>
1821N/A * <dt>en-AU</dt>
1821N/A * <dd>Australian English (identical to British English)</dd>
1821N/A * </dl>
1947N/A * More locales may be added by subclassing of Y.DataType.Date.Locale["en"].
1821N/A * See Y.DataType.Date.Locale for more information.
1821N/A * </dd>
1821N/A * </dl>
1947N/A * @return {String} Formatted date for display.
1947N/A * @sa Y.DataType.Date.Locale
1947N/A */
1821N/A format : function (oDate, oConfig) {
1821N/A oConfig = oConfig || {};
1821N/A
1821N/A if(!Y.Lang.isDate(oDate)) {
1821N/A return Y.Lang.isValue(oDate) ? oDate : "";
2500N/A }
1821N/A
1821N/A var format = oConfig.format || Y.config.dateFormat,
1821N/A sLocale = oConfig.locale || Y.config.locale;
1821N/A
3866N/A sLocale = sLocale.replace(/_/g, "-");
3866N/A
3866N/A // Make sure we have a definition for the requested locale, or default to en.
2203N/A if(!Dt.Locale[sLocale]) {
1947N/A var tmpLocale = sLocale.replace(/-[a-zA-Z]+$/, "");
1821N/A if(tmpLocale in Dt.Locale) {
1008N/A sLocale = tmpLocale;
1008N/A } else if(Y.config.locale in Dt.Locale) {
0N/A sLocale = Y.config.locale;
1008N/A } else {
1008N/A sLocale = "en";
1008N/A }
0N/A }
1008N/A
1008N/A var aLocale = Dt.Locale[sLocale];
1008N/A
2500N/A var replace_aggs = function (m0, m1) {
0N/A var f = Dt.aggregates[m1];
0N/A return (f === "locale" ? aLocale[m1] : f);
0N/A };
1008N/A
1008N/A var replace_formats = function (m0, m1) {
1008N/A var f = Dt.formats[m1];
1008N/A switch(Y.Lang.type(f)) {
1008N/A case "string": // string => built in date function
1008N/A return oDate[f]();
1008N/A case "function": // function => our own function
1008N/A return f.call(oDate, oDate, aLocale);
1934N/A case "array": // built in function with padding
1934N/A if(Y.Lang.type(f[0]) === "string") {
1934N/A return xPad(oDate[f[0]](), f[1]);
1934N/A } // no break; (fall through to default:)
1934N/A default:
1008N/A return m1;
1008N/A }
1008N/A };
1008N/A
1821N/A // First replace aggregates (run in a loop because an agg may be made up of other aggs)
1821N/A while(format.match(/%[cDFhnrRtTxX]/)) {
1821N/A format = format.replace(/%([cDFhnrRtTxX])/g, replace_aggs);
1821N/A }
1821N/A
1821N/A // Now replace formats (do not run in a loop otherwise %%a will be replace with the value of %a)
1821N/A var str = format.replace(/%([aAbBCdegGHIjklmMpPsSuUVwWyYzZ%])/g, replace_formats);
1821N/A
1821N/A replace_aggs = replace_formats = undefined;
2020N/A
2020N/A return str;
1821N/A }
1821N/A};
1821N/A
1821N/AY.mix(Y.namespace("DataType.Date"), Dt);
1821N/A
1821N/A/**
1821N/A * The Date.Locale class is a container for all localised date strings
1934N/A * used by Y.DataType.Date. It is used internally, but may be extended
1821N/A * to provide new date localisations.
1821N/A *
1821N/A * To create your own Locale, follow these steps:
1821N/A * <ol>
3866N/A * <li>Find an existing locale that matches closely with your needs</li>
1821N/A * <li>Use this as your base class. Use Y.DataType.Date.Locale["en"] if nothing
1821N/A * matches.</li>
3866N/A * <li>Create your own class as an extension of the base class using
1821N/A * Y.merge, and add your own localisations where needed.</li>
1934N/A * </ol>
2500N/A * See the Y.DataType.Date.Locale["en-US"] and Y.DataType.Date.Locale["en-GB"]
2228N/A * classes which extend Y.DataType.Date.Locale["en"].
1934N/A *
1934N/A * For example, to implement locales for French french and Canadian french,
2228N/A * we would do the following:
1934N/A * <ol>
1934N/A * <li>For French french, we have no existing similar locale, so use
1821N/A * Y.DataType.Date.Locale["en"] as the base, and extend it:
1821N/A * <pre>
3866N/A * Y.DataType.Date.Locale["fr"] = Y.merge(Y.DataType.Date.Locale, {
1821N/A * a: ["dim", "lun", "mar", "mer", "jeu", "ven", "sam"],
1821N/A * A: ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"],
1821N/A * b: ["jan", "f&eacute;v", "mar", "avr", "mai", "jun", "jui", "ao&ucirc;", "sep", "oct", "nov", "d&eacute;c"],
1821N/A * B: ["janvier", "f&eacute;vrier", "mars", "avril", "mai", "juin", "juillet", "ao&ucirc;t", "septembre", "octobre", "novembre", "d&eacute;cembre"],
1821N/A * c: "%a %d %b %Y %T %Z",
1008N/A * p: ["", ""],
1008N/A * P: ["", ""],
1934N/A * x: "%d.%m.%Y",
1934N/A * X: "%T"
1934N/A * });
1934N/A * </pre>
1821N/A * </li>
1821N/A * <li>For Canadian french, we start with French french and change the meaning of \%x:
3740N/A * <pre>
3866N/A * Y.DataType.Date.Locale["fr-CA"] = Y.merge(Y.DataType.Date.Locale["fr"], {
1821N/A * x: "%Y-%m-%d"
1821N/A * });
3740N/A * </pre>
3866N/A * </li>
1821N/A * </ol>
1821N/A *
2017N/A * With that, you can use your new locales:
1821N/A * <pre>
2017N/A * var d = new Date("2008/04/22");
1821N/A * Y.DataType.Date.format(d, { format: "%A, %d %B == %x", locale: "fr" });
1821N/A * </pre>
1821N/A * will return:
1821N/A * <pre>
1821N/A * mardi, 22 avril == 22.04.2008
1821N/A * </pre>
1821N/A * And
1821N/A * <pre>
1821N/A * Y.DataType.Date.format(d, {format: "%A, %d %B == %x", locale: "fr-CA" });
1821N/A * </pre>
3740N/A * Will return:
1821N/A * <pre>
1821N/A * mardi, 22 avril == 2008-04-22
1821N/A * </pre>
1821N/A * @module datatype
3740N/A * @requires oop
3740N/A * @class DataType.Date.Locale
1821N/A */
3740N/Avar YDateEn = {
1821N/A a: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
1821N/A A: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
1821N/A b: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
1821N/A B: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
1821N/A c: "%a %d %b %Y %T %Z",
3740N/A p: ["AM", "PM"],
3740N/A P: ["am", "pm"],
3740N/A r: "%I:%M:%S %p",
1821N/A x: "%d/%m/%y",
2017N/A X: "%T"
2017N/A};
2017N/A
1821N/AY.namespace("DataType.Date.Locale");
3740N/A
1821N/AY.DataType.Date.Locale["en"] = YDateEn;
1821N/A
2017N/AY.DataType.Date.Locale["en-US"] = Y.merge(YDateEn, {
1934N/A c: "%a %d %b %Y %I:%M:%S %p %Z",
1934N/A x: "%m/%d/%Y",
1934N/A X: "%I:%M:%S %p"
1821N/A});
1821N/A
3740N/AY.DataType.Date.Locale["en-GB"] = Y.merge(YDateEn, {
1821N/A r: "%l:%M:%S %P %Z"
1821N/A});
1821N/AY.DataType.Date.Locale["en-AU"] = Y.merge(YDateEn);
1821N/A
3740N/A
1821N/A
1821N/A
1821N/A
1821N/A}, '@VERSION@' );
1821N/A
3740N/A
1821N/A
1821N/AYUI.add('datatype-date', function(Y){}, '@VERSION@' ,{use:['datatype-date-parse', 'datatype-date-format']});
1821N/A
1821N/A