2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 YUI.add('datatype-date-parse', function(Y) {
11 * Parse number submodule.
14 * @submodule datatype-date-parse
19 Y.mix(Y.namespace("DataType.Date"), {
21 * Converts data to type Date.
24 * @param data {String | Number} Data to convert. Values supported by the Date constructor are supported.
25 * @return {Date} A Date, or null.
27 parse: function(data) {
31 if(!(LANG.isDate(data))) {
32 date = new Date(data);
39 if(LANG.isDate(date) && (date != "Invalid Date") && !isNaN(date)) { // Workaround for bug 2527965
48 // Add Parsers shortcut
49 Y.namespace("Parsers").date = Y.DataType.Date.parse;
55 YUI.add('datatype-date-format', function(Y) {
58 * The DataType Utility provides type-conversion and string-formatting
59 * convenience methods for various JavaScript object types.
68 * @submodule datatype-date
72 * Format date submodule implements strftime formatters for javascript based on the
73 * Open Group specification defined at
74 * http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html
75 * This implementation does not include modified conversion specifiers (i.e., Ex and Ox)
78 * @submodule datatype-date-format
82 * DataType.Date provides a set of utility functions to operate against Date objects.
84 * @class DataType.Date
89 * Pad a number with leading spaces, zeroes or something else
91 * @param x {Number} The number to be padded
92 * @param pad {String} The character to pad the number with
93 * @param r {Number} (optional) The base of the pad, eg, 10 implies to two digits, 100 implies to 3 digits.
96 var xPad=function (x, pad, r)
98 if(typeof r === "undefined")
102 pad = pad.toString();
103 for( ; parseInt(x, 10)<r && r>1; r/=10) {
110 * Default date format.
113 * @property dateFormat
117 Y.config.dateFormat = Y.config.dateFormat || "%Y-%m-%d";
120 * Default locale for the YUI instance.
126 Y.config.locale = Y.config.locale || "en";
130 a: function (d, l) { return l.a[d.getDay()]; },
131 A: function (d, l) { return l.A[d.getDay()]; },
132 b: function (d, l) { return l.b[d.getMonth()]; },
133 B: function (d, l) { return l.B[d.getMonth()]; },
134 C: function (d) { return xPad(parseInt(d.getFullYear()/100, 10), 0); },
137 g: function (d) { return xPad(parseInt(Dt.formats.G(d)%100, 10), 0); },
139 var y = d.getFullYear();
140 var V = parseInt(Dt.formats.V(d), 10);
141 var W = parseInt(Dt.formats.W(d), 10);
145 } else if(W===0 && V>=52) {
151 H: ["getHours", "0"],
152 I: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, 0); },
154 var gmd_1 = new Date("" + d.getFullYear() + "/1/1 GMT");
155 var gmdate = new Date("" + d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate() + " GMT");
156 var ms = gmdate - gmd_1;
157 var doy = parseInt(ms/60000/60/24, 10)+1;
158 return xPad(doy, 0, 100);
160 k: ["getHours", " "],
161 l: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, " "); },
162 m: function (d) { return xPad(d.getMonth()+1, 0); },
163 M: ["getMinutes", "0"],
164 p: function (d, l) { return l.p[d.getHours() >= 12 ? 1 : 0 ]; },
165 P: function (d, l) { return l.P[d.getHours() >= 12 ? 1 : 0 ]; },
166 s: function (d, l) { return parseInt(d.getTime()/1000, 10); },
167 S: ["getSeconds", "0"],
168 u: function (d) { var dow = d.getDay(); return dow===0?7:dow; },
170 var doy = parseInt(Dt.formats.j(d), 10);
171 var rdow = 6-d.getDay();
172 var woy = parseInt((doy+rdow)/7, 10);
176 var woy = parseInt(Dt.formats.W(d), 10);
177 var dow1_1 = (new Date("" + d.getFullYear() + "/1/1")).getDay();
178 // First week is 01 and not 00 as in the case of %U and %W,
179 // so we add 1 to the final result except if day 1 of the year
180 // is a Monday (then %W returns 01).
181 // We also need to subtract 1 if the day 1 of the year is
182 // Friday-Sunday, so the resulting equation becomes:
183 var idow = woy + (dow1_1 > 4 || dow1_1 <= 1 ? 0 : 1);
184 if(idow === 53 && (new Date("" + d.getFullYear() + "/12/31")).getDay() < 4)
190 idow = Dt.formats.V(new Date("" + (d.getFullYear()-1) + "/12/31"));
193 return xPad(idow, 0);
197 var doy = parseInt(Dt.formats.j(d), 10);
198 var rdow = 7-Dt.formats.u(d);
199 var woy = parseInt((doy+rdow)/7, 10);
200 return xPad(woy, 0, 10);
202 y: function (d) { return xPad(d.getFullYear()%100, 0); },
205 var o = d.getTimezoneOffset();
206 var H = xPad(parseInt(Math.abs(o/60), 10), 0);
207 var M = xPad(Math.abs(o%60), 0);
208 return (o>0?"-":"+") + H + M;
211 var tz = d.toString().replace(/^.*:\d\d( GMT[+-]\d+)? \(?([A-Za-z ]+)\)?\d*$/, "$2").replace(/[a-z ]/g, "");
213 tz = Dt.formats.z(d);
217 "%": function (d) { return "%"; }
232 //"+": "%a %b %e %T %Z %Y"
236 * Takes a native JavaScript Date and formats it as a string for display to user.
240 * @param oDate {Date} Date.
241 * @param oConfig {Object} (Optional) Object literal of configuration values:
243 * <dt>format {String} (Optional)</dt>
246 * Any strftime string is supported, such as "%I:%M:%S %p". strftime has several format specifiers defined by the Open group at
247 * <a href="http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html">http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html</a>
248 * PHP added a few of its own, defined at <a href="http://www.php.net/strftime">http://www.php.net/strftime</a>
251 * This javascript implementation supports all the PHP specifiers and a few more. The full list is below.
254 * If not specified, it defaults to the ISO8601 standard date format: %Y-%m-%d. This may be overridden by changing Y.config.dateFormat
257 * <dt>%a</dt> <dd>abbreviated weekday name according to the current locale</dd>
258 * <dt>%A</dt> <dd>full weekday name according to the current locale</dd>
259 * <dt>%b</dt> <dd>abbreviated month name according to the current locale</dd>
260 * <dt>%B</dt> <dd>full month name according to the current locale</dd>
261 * <dt>%c</dt> <dd>preferred date and time representation for the current locale</dd>
262 * <dt>%C</dt> <dd>century number (the year divided by 100 and truncated to an integer, range 00 to 99)</dd>
263 * <dt>%d</dt> <dd>day of the month as a decimal number (range 01 to 31)</dd>
264 * <dt>%D</dt> <dd>same as %m/%d/%y</dd>
265 * <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>
266 * <dt>%F</dt> <dd>same as %Y-%m-%d (ISO 8601 date format)</dd>
267 * <dt>%g</dt> <dd>like %G, but without the century</dd>
268 * <dt>%G</dt> <dd>The 4-digit year corresponding to the ISO week number</dd>
269 * <dt>%h</dt> <dd>same as %b</dd>
270 * <dt>%H</dt> <dd>hour as a decimal number using a 24-hour clock (range 00 to 23)</dd>
271 * <dt>%I</dt> <dd>hour as a decimal number using a 12-hour clock (range 01 to 12)</dd>
272 * <dt>%j</dt> <dd>day of the year as a decimal number (range 001 to 366)</dd>
273 * <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>
274 * <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>
275 * <dt>%m</dt> <dd>month as a decimal number (range 01 to 12)</dd>
276 * <dt>%M</dt> <dd>minute as a decimal number</dd>
277 * <dt>%n</dt> <dd>newline character</dd>
278 * <dt>%p</dt> <dd>either "AM" or "PM" according to the given time value, or the corresponding strings for the current locale</dd>
279 * <dt>%P</dt> <dd>like %p, but lower case</dd>
280 * <dt>%r</dt> <dd>time in a.m. and p.m. notation equal to %I:%M:%S %p</dd>
281 * <dt>%R</dt> <dd>time in 24 hour notation equal to %H:%M</dd>
282 * <dt>%s</dt> <dd>number of seconds since the Epoch, ie, since 1970-01-01 00:00:00 UTC</dd>
283 * <dt>%S</dt> <dd>second as a decimal number</dd>
284 * <dt>%t</dt> <dd>tab character</dd>
285 * <dt>%T</dt> <dd>current time, equal to %H:%M:%S</dd>
286 * <dt>%u</dt> <dd>weekday as a decimal number [1,7], with 1 representing Monday</dd>
287 * <dt>%U</dt> <dd>week number of the current year as a decimal number, starting with the
288 * first Sunday as the first day of the first week</dd>
289 * <dt>%V</dt> <dd>The ISO 8601:1988 week number of the current year as a decimal number,
290 * range 01 to 53, where week 1 is the first week that has at least 4 days
291 * in the current year, and with Monday as the first day of the week.</dd>
292 * <dt>%w</dt> <dd>day of the week as a decimal, Sunday being 0</dd>
293 * <dt>%W</dt> <dd>week number of the current year as a decimal number, starting with the
294 * first Monday as the first day of the first week</dd>
295 * <dt>%x</dt> <dd>preferred date representation for the current locale without the time</dd>
296 * <dt>%X</dt> <dd>preferred time representation for the current locale without the date</dd>
297 * <dt>%y</dt> <dd>year as a decimal number without a century (range 00 to 99)</dd>
298 * <dt>%Y</dt> <dd>year as a decimal number including the century</dd>
299 * <dt>%z</dt> <dd>numerical time zone representation</dd>
300 * <dt>%Z</dt> <dd>time zone name or abbreviation</dd>
301 * <dt>%%</dt> <dd>a literal "%" character</dd>
304 * <dt>locale {String} (Optional)</dt>
306 * The locale to use when displaying days of week, months of the year, and other locale specific
307 * strings. If not specified, this defaults to "en" (though this may be overridden by changing Y.config.locale).
308 * The following locales are built in:
313 * <dd>US English</dd>
315 * <dd>British English</dd>
317 * <dd>Australian English (identical to British English)</dd>
319 * More locales may be added by subclassing of Y.DataType.Date.Locale["en"].
320 * See Y.DataType.Date.Locale for more information.
323 * @return {String} Formatted date for display.
325 format : function (oDate, oConfig) {
326 oConfig = oConfig || {};
328 if(!Y.Lang.isDate(oDate)) {
329 return Y.Lang.isValue(oDate) ? oDate : "";
332 var format = oConfig.format || Y.config.dateFormat,
333 sLocale = oConfig.locale || Y.config.locale,
334 LOCALE = Y.DataType.Date.Locale;
336 sLocale = sLocale.replace(/_/g, "-");
338 // Make sure we have a definition for the requested locale, or default to en.
339 if(!LOCALE[sLocale]) {
340 var tmpLocale = sLocale.replace(/-[a-zA-Z]+$/, "");
341 if(tmpLocale in LOCALE) {
343 } else if(Y.config.locale in LOCALE) {
344 sLocale = Y.config.locale;
350 var aLocale = LOCALE[sLocale];
352 var replace_aggs = function (m0, m1) {
353 var f = Dt.aggregates[m1];
354 return (f === "locale" ? aLocale[m1] : f);
357 var replace_formats = function (m0, m1) {
358 var f = Dt.formats[m1];
359 switch(Y.Lang.type(f)) {
360 case "string": // string => built in date function
362 case "function": // function => our own function
363 return f.call(oDate, oDate, aLocale);
364 case "array": // built in function with padding
365 if(Y.Lang.type(f[0]) === "string") {
366 return xPad(oDate[f[0]](), f[1]);
367 } // no break; (fall through to default:)
373 // First replace aggregates (run in a loop because an agg may be made up of other aggs)
374 while(format.match(/%[cDFhnrRtTxX]/)) {
375 format = format.replace(/%([cDFhnrRtTxX])/g, replace_aggs);
378 // Now replace formats (do not run in a loop otherwise %%a will be replace with the value of %a)
379 var str = format.replace(/%([aAbBCdegGHIjklmMpPsSuUVwWyYzZ%])/g, replace_formats);
381 replace_aggs = replace_formats = undefined;
387 Y.mix(Y.namespace("DataType.Date"), Dt);
394 * The Date.Locale class is a container for all localised date strings
395 * used by Y.DataType.Date. It is used internally, but may be extended
396 * to provide new date localisations.
398 * To create your own Locale, follow these steps:
400 * <li>Find an existing locale that matches closely with your needs</li>
401 * <li>Use this as your base class. Use Y.DataType.Date.Locale["en"] if nothing
403 * <li>Create your own class as an extension of the base class using
404 * Y.merge, and add your own localisations where needed.</li>
406 * See the Y.DataType.Date.Locale["en-US"] and Y.DataType.Date.Locale["en-GB"]
407 * classes which extend Y.DataType.Date.Locale["en"].
409 * For example, to implement locales for French french and Canadian french,
410 * we would do the following:
412 * <li>For French french, we have no existing similar locale, so use
413 * Y.DataType.Date.Locale["en"] as the base, and extend it:
415 * Y.DataType.Date.Locale["fr"] = Y.merge(Y.DataType.Date.Locale, {
416 * a: ["dim", "lun", "mar", "mer", "jeu", "ven", "sam"],
417 * A: ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"],
418 * b: ["jan", "fév", "mar", "avr", "mai", "jun", "jui", "aoû", "sep", "oct", "nov", "déc"],
419 * B: ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"],
420 * c: "%a %d %b %Y %T %Z",
428 * <li>For Canadian french, we start with French french and change the meaning of \%x:
430 * Y.DataType.Date.Locale["fr-CA"] = Y.merge(Y.DataType.Date.Locale["fr"], {
437 * With that, you can use your new locales:
439 * var d = new Date("2008/04/22");
440 * Y.DataType.Date.format(d, { format: "%A, %d %B == %x", locale: "fr" });
444 * mardi, 22 avril == 22.04.2008
448 * Y.DataType.Date.format(d, {format: "%A, %d %B == %x", locale: "fr-CA" });
452 * mardi, 22 avril == 2008-04-22
455 * @class DataType.Date.Locale
459 a: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
460 A: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
461 b: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
462 B: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
463 c: "%a %d %b %Y %T %Z",
471 Y.namespace("DataType.Date.Locale");
473 Y.DataType.Date.Locale["en"] = YDateEn;
475 Y.DataType.Date.Locale["en-US"] = Y.merge(YDateEn, {
476 c: "%a %d %b %Y %I:%M:%S %p %Z",
481 Y.DataType.Date.Locale["en-GB"] = Y.merge(YDateEn, {
484 Y.DataType.Date.Locale["en-AU"] = Y.merge(YDateEn);
494 YUI.add('datatype-date', function(Y){}, '3.0.0' ,{use:['datatype-date-parse', 'datatype-date-format']});