]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/datatype/datatype-date.js
Release 6.2.0beta4
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / datatype / datatype-date.js
1 /*
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
5 version: 3.0.0
6 build: 1549
7 */
8 YUI.add('datatype-date-parse', function(Y) {
9
10 /**
11  * Parse number submodule.
12  *
13  * @module datatype
14  * @submodule datatype-date-parse
15  * @for DataType.Date
16  */
17 var LANG = Y.Lang;
18
19 Y.mix(Y.namespace("DataType.Date"), {
20     /**
21      * Converts data to type Date.
22      *
23      * @method parse
24      * @param data {String | Number} Data to convert. Values supported by the Date constructor are supported.
25      * @return {Date} A Date, or null.
26      */
27     parse: function(data) {
28         var date = null;
29
30         //Convert to date
31         if(!(LANG.isDate(data))) {
32             date = new Date(data);
33         }
34         else {
35             return date;
36         }
37
38         // Validate
39         if(LANG.isDate(date) && (date != "Invalid Date") && !isNaN(date)) { // Workaround for bug 2527965
40             return date;
41         }
42         else {
43             return null;
44         }
45     }
46 });
47
48 // Add Parsers shortcut
49 Y.namespace("Parsers").date = Y.DataType.Date.parse;
50
51
52
53 }, '3.0.0' );
54
55 YUI.add('datatype-date-format', function(Y) {
56
57 /**
58  * The DataType Utility provides type-conversion and string-formatting
59  * convenience methods for various JavaScript object types.
60  *
61  * @module datatype
62  */
63
64 /**
65  * Date submodule.
66  *
67  * @module datatype
68  * @submodule datatype-date
69  */
70
71 /**
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)
76  *
77  * @module datatype
78  * @submodule datatype-date-format
79  */
80
81 /**
82  * DataType.Date provides a set of utility functions to operate against Date objects.
83  *
84  * @class DataType.Date
85  * @static
86  */
87
88 /**
89  * Pad a number with leading spaces, zeroes or something else
90  * @method xPad
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.
94  * @private
95  */
96 var xPad=function (x, pad, r)
97 {
98         if(typeof r === "undefined")
99         {
100                 r=10;
101         }
102         pad = pad.toString();
103         for( ; parseInt(x, 10)<r && r>1; r/=10) {
104                 x = pad + x;
105         }
106         return x.toString();
107 };
108
109 /**
110  * Default date format.
111  *
112  * @for config
113  * @property dateFormat
114  * @type String
115  * @value "%Y-%m-%d"
116  */
117 Y.config.dateFormat = Y.config.dateFormat || "%Y-%m-%d";
118
119 /**
120  * Default locale for the YUI instance.
121  *
122  * @property locale
123  * @type String
124  * @value "en"
125  */
126 Y.config.locale = Y.config.locale || "en";
127
128 var Dt = {
129         formats: {
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); },
135                 d: ["getDate", "0"],
136                 e: ["getDate", " "],
137                 g: function (d) { return xPad(parseInt(Dt.formats.G(d)%100, 10), 0); },
138                 G: function (d) {
139                                 var y = d.getFullYear();
140                                 var V = parseInt(Dt.formats.V(d), 10);
141                                 var W = parseInt(Dt.formats.W(d), 10);
142         
143                                 if(W > V) {
144                                         y++;
145                                 } else if(W===0 && V>=52) {
146                                         y--;
147                                 }
148         
149                                 return y;
150                         },
151                 H: ["getHours", "0"],
152                 I: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, 0); },
153                 j: function (d) {
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);
159                         },
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; },
169                 U: function (d) {
170                                 var doy = parseInt(Dt.formats.j(d), 10);
171                                 var rdow = 6-d.getDay();
172                                 var woy = parseInt((doy+rdow)/7, 10);
173                                 return xPad(woy, 0);
174                         },
175                 V: function (d) {
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)
185                                 {
186                                         idow = 1;
187                                 }
188                                 else if(idow === 0)
189                                 {
190                                         idow = Dt.formats.V(new Date("" + (d.getFullYear()-1) + "/12/31"));
191                                 }
192         
193                                 return xPad(idow, 0);
194                         },
195                 w: "getDay",
196                 W: function (d) {
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);
201                         },
202                 y: function (d) { return xPad(d.getFullYear()%100, 0); },
203                 Y: "getFullYear",
204                 z: function (d) {
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;
209                         },
210                 Z: function (d) {
211                         var tz = d.toString().replace(/^.*:\d\d( GMT[+-]\d+)? \(?([A-Za-z ]+)\)?\d*$/, "$2").replace(/[a-z ]/g, "");
212                         if(tz.length > 4) {
213                                 tz = Dt.formats.z(d);
214                         }
215                         return tz;
216                 },
217                 "%": function (d) { return "%"; }
218         },
219
220         aggregates: {
221                 c: "locale",
222                 D: "%m/%d/%y",
223                 F: "%Y-%m-%d",
224                 h: "%b",
225                 n: "\n",
226                 r: "locale",
227                 R: "%H:%M",
228                 t: "\t",
229                 T: "%H:%M:%S",
230                 x: "locale",
231                 X: "locale"
232                 //"+": "%a %b %e %T %Z %Y"
233         },
234
235          /**
236          * Takes a native JavaScript Date and formats it as a string for display to user.
237          *
238          * @for DataType.Date
239          * @method format
240          * @param oDate {Date} Date.
241          * @param oConfig {Object} (Optional) Object literal of configuration values:
242          *  <dl>
243          *   <dt>format {String} (Optional)</dt>
244          *   <dd>
245          *   <p>
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>
249          *   </p>
250          *   <p>
251          *   This javascript implementation supports all the PHP specifiers and a few more.  The full list is below.
252          *   </p>
253          *   <p>
254          *   If not specified, it defaults to the ISO8601 standard date format: %Y-%m-%d.  This may be overridden by changing Y.config.dateFormat
255          *   </p>
256          *   <dl>
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>
302          *   </dl>
303          *  </dd>
304          *  <dt>locale {String} (Optional)</dt>
305          *  <dd>
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:
309          *   <dl>
310          *    <dt>en</dt>
311          *    <dd>English</dd>
312          *    <dt>en-US</dt>
313          *    <dd>US English</dd>
314          *    <dt>en-GB</dt>
315          *    <dd>British English</dd>
316          *    <dt>en-AU</dt>
317          *    <dd>Australian English (identical to British English)</dd>
318          *   </dl>
319          *   More locales may be added by subclassing of Y.DataType.Date.Locale["en"].
320          *   See Y.DataType.Date.Locale for more information.
321          *  </dd>
322          * </dl>
323          * @return {String} Formatted date for display.
324          */
325         format : function (oDate, oConfig) {
326                 oConfig = oConfig || {};
327                 
328                 if(!Y.Lang.isDate(oDate)) {
329                         return Y.Lang.isValue(oDate) ? oDate : "";
330                 }
331
332                 var format = oConfig.format || Y.config.dateFormat,
333                         sLocale = oConfig.locale || Y.config.locale,
334                         LOCALE = Y.DataType.Date.Locale;
335
336                 sLocale = sLocale.replace(/_/g, "-");
337                 
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) {
342                                 sLocale = tmpLocale;
343                         } else if(Y.config.locale in LOCALE) {
344                                 sLocale = Y.config.locale;
345                         } else {
346                                 sLocale = "en";
347                         }
348                 }
349
350                 var aLocale = LOCALE[sLocale];
351
352                 var replace_aggs = function (m0, m1) {
353                         var f = Dt.aggregates[m1];
354                         return (f === "locale" ? aLocale[m1] : f);
355                 };
356
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
361                                         return oDate[f]();
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:)
368                                 default:
369                                         return m1;
370                         }
371                 };
372
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);
376                 }
377
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);
380
381                 replace_aggs = replace_formats = undefined;
382
383                 return str;
384         }
385 };
386
387 Y.mix(Y.namespace("DataType.Date"), Dt);
388
389 /**
390  * @module datatype
391 */
392
393 /**
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.
397  *
398  * To create your own Locale, follow these steps:
399  * <ol>
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
402  *   matches.</li>
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>
405  * </ol>
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"].
408  *
409  * For example, to implement locales for French french and Canadian french,
410  * we would do the following:
411  * <ol>
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:
414  *   <pre>
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&eacute;v", "mar", "avr", "mai", "jun", "jui", "ao&ucirc;", "sep", "oct", "nov", "d&eacute;c"],
419  *          B: ["janvier", "f&eacute;vrier", "mars", "avril", "mai", "juin", "juillet", "ao&ucirc;t", "septembre", "octobre", "novembre", "d&eacute;cembre"],
420  *          c: "%a %d %b %Y %T %Z",
421  *          p: ["", ""],
422  *          P: ["", ""],
423  *          x: "%d.%m.%Y",
424  *          X: "%T"
425  *      });
426  *   </pre>
427  *  </li>
428  *  <li>For Canadian french, we start with French french and change the meaning of \%x:
429  *   <pre>
430  *      Y.DataType.Date.Locale["fr-CA"] = Y.merge(Y.DataType.Date.Locale["fr"], {
431  *          x: "%Y-%m-%d"
432  *      });
433  *   </pre>
434  *  </li>
435  * </ol>
436  *
437  * With that, you can use your new locales:
438  * <pre>
439  *    var d = new Date("2008/04/22");
440  *    Y.DataType.Date.format(d, { format: "%A, %d %B == %x", locale: "fr" });
441  * </pre>
442  * will return:
443  * <pre>
444  *    mardi, 22 avril == 22.04.2008
445  * </pre>
446  * And
447  * <pre>
448  *    Y.DataType.Date.format(d, {format: "%A, %d %B == %x", locale: "fr-CA" });
449  * </pre>
450  * Will return:
451  * <pre>
452  *   mardi, 22 avril == 2008-04-22
453  * </pre>
454  * @requires oop
455  * @class DataType.Date.Locale
456  * @static
457  */
458 var YDateEn = {
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",
464         p: ["AM", "PM"],
465         P: ["am", "pm"],
466         r: "%I:%M:%S %p",
467         x: "%d/%m/%y",
468         X: "%T"
469 };
470
471 Y.namespace("DataType.Date.Locale");
472
473 Y.DataType.Date.Locale["en"] = YDateEn;
474
475 Y.DataType.Date.Locale["en-US"] = Y.merge(YDateEn, {
476         c: "%a %d %b %Y %I:%M:%S %p %Z",
477         x: "%m/%d/%Y",
478         X: "%I:%M:%S %p"
479 });
480
481 Y.DataType.Date.Locale["en-GB"] = Y.merge(YDateEn, {
482         r: "%l:%M:%S %P %Z"
483 });
484 Y.DataType.Date.Locale["en-AU"] = Y.merge(YDateEn);
485
486
487
488
489
490 }, '3.0.0' );
491
492
493
494 YUI.add('datatype-date', function(Y){}, '3.0.0' ,{use:['datatype-date-parse', 'datatype-date-format']});
495