]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/datatype/datatype-date-format.js
Release 6.2.0beta4
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / datatype / datatype-date-format.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-format', function(Y) {
9
10 /**
11  * The DataType Utility provides type-conversion and string-formatting
12  * convenience methods for various JavaScript object types.
13  *
14  * @module datatype
15  */
16
17 /**
18  * Date submodule.
19  *
20  * @module datatype
21  * @submodule datatype-date
22  */
23
24 /**
25  * Format date submodule implements strftime formatters for javascript based on the
26  * Open Group specification defined at
27  * http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html
28  * This implementation does not include modified conversion specifiers (i.e., Ex and Ox)
29  *
30  * @module datatype
31  * @submodule datatype-date-format
32  */
33
34 /**
35  * DataType.Date provides a set of utility functions to operate against Date objects.
36  *
37  * @class DataType.Date
38  * @static
39  */
40
41 /**
42  * Pad a number with leading spaces, zeroes or something else
43  * @method xPad
44  * @param x {Number}    The number to be padded
45  * @param pad {String}  The character to pad the number with
46  * @param r {Number}    (optional) The base of the pad, eg, 10 implies to two digits, 100 implies to 3 digits.
47  * @private
48  */
49 var xPad=function (x, pad, r)
50 {
51         if(typeof r === "undefined")
52         {
53                 r=10;
54         }
55         pad = pad.toString();
56         for( ; parseInt(x, 10)<r && r>1; r/=10) {
57                 x = pad + x;
58         }
59         return x.toString();
60 };
61
62 /**
63  * Default date format.
64  *
65  * @for config
66  * @property dateFormat
67  * @type String
68  * @value "%Y-%m-%d"
69  */
70 Y.config.dateFormat = Y.config.dateFormat || "%Y-%m-%d";
71
72 /**
73  * Default locale for the YUI instance.
74  *
75  * @property locale
76  * @type String
77  * @value "en"
78  */
79 Y.config.locale = Y.config.locale || "en";
80
81 var Dt = {
82         formats: {
83                 a: function (d, l) { return l.a[d.getDay()]; },
84                 A: function (d, l) { return l.A[d.getDay()]; },
85                 b: function (d, l) { return l.b[d.getMonth()]; },
86                 B: function (d, l) { return l.B[d.getMonth()]; },
87                 C: function (d) { return xPad(parseInt(d.getFullYear()/100, 10), 0); },
88                 d: ["getDate", "0"],
89                 e: ["getDate", " "],
90                 g: function (d) { return xPad(parseInt(Dt.formats.G(d)%100, 10), 0); },
91                 G: function (d) {
92                                 var y = d.getFullYear();
93                                 var V = parseInt(Dt.formats.V(d), 10);
94                                 var W = parseInt(Dt.formats.W(d), 10);
95         
96                                 if(W > V) {
97                                         y++;
98                                 } else if(W===0 && V>=52) {
99                                         y--;
100                                 }
101         
102                                 return y;
103                         },
104                 H: ["getHours", "0"],
105                 I: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, 0); },
106                 j: function (d) {
107                                 var gmd_1 = new Date("" + d.getFullYear() + "/1/1 GMT");
108                                 var gmdate = new Date("" + d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate() + " GMT");
109                                 var ms = gmdate - gmd_1;
110                                 var doy = parseInt(ms/60000/60/24, 10)+1;
111                                 return xPad(doy, 0, 100);
112                         },
113                 k: ["getHours", " "],
114                 l: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, " "); },
115                 m: function (d) { return xPad(d.getMonth()+1, 0); },
116                 M: ["getMinutes", "0"],
117                 p: function (d, l) { return l.p[d.getHours() >= 12 ? 1 : 0 ]; },
118                 P: function (d, l) { return l.P[d.getHours() >= 12 ? 1 : 0 ]; },
119                 s: function (d, l) { return parseInt(d.getTime()/1000, 10); },
120                 S: ["getSeconds", "0"],
121                 u: function (d) { var dow = d.getDay(); return dow===0?7:dow; },
122                 U: function (d) {
123                                 var doy = parseInt(Dt.formats.j(d), 10);
124                                 var rdow = 6-d.getDay();
125                                 var woy = parseInt((doy+rdow)/7, 10);
126                                 return xPad(woy, 0);
127                         },
128                 V: function (d) {
129                                 var woy = parseInt(Dt.formats.W(d), 10);
130                                 var dow1_1 = (new Date("" + d.getFullYear() + "/1/1")).getDay();
131                                 // First week is 01 and not 00 as in the case of %U and %W,
132                                 // so we add 1 to the final result except if day 1 of the year
133                                 // is a Monday (then %W returns 01).
134                                 // We also need to subtract 1 if the day 1 of the year is 
135                                 // Friday-Sunday, so the resulting equation becomes:
136                                 var idow = woy + (dow1_1 > 4 || dow1_1 <= 1 ? 0 : 1);
137                                 if(idow === 53 && (new Date("" + d.getFullYear() + "/12/31")).getDay() < 4)
138                                 {
139                                         idow = 1;
140                                 }
141                                 else if(idow === 0)
142                                 {
143                                         idow = Dt.formats.V(new Date("" + (d.getFullYear()-1) + "/12/31"));
144                                 }
145         
146                                 return xPad(idow, 0);
147                         },
148                 w: "getDay",
149                 W: function (d) {
150                                 var doy = parseInt(Dt.formats.j(d), 10);
151                                 var rdow = 7-Dt.formats.u(d);
152                                 var woy = parseInt((doy+rdow)/7, 10);
153                                 return xPad(woy, 0, 10);
154                         },
155                 y: function (d) { return xPad(d.getFullYear()%100, 0); },
156                 Y: "getFullYear",
157                 z: function (d) {
158                                 var o = d.getTimezoneOffset();
159                                 var H = xPad(parseInt(Math.abs(o/60), 10), 0);
160                                 var M = xPad(Math.abs(o%60), 0);
161                                 return (o>0?"-":"+") + H + M;
162                         },
163                 Z: function (d) {
164                         var tz = d.toString().replace(/^.*:\d\d( GMT[+-]\d+)? \(?([A-Za-z ]+)\)?\d*$/, "$2").replace(/[a-z ]/g, "");
165                         if(tz.length > 4) {
166                                 tz = Dt.formats.z(d);
167                         }
168                         return tz;
169                 },
170                 "%": function (d) { return "%"; }
171         },
172
173         aggregates: {
174                 c: "locale",
175                 D: "%m/%d/%y",
176                 F: "%Y-%m-%d",
177                 h: "%b",
178                 n: "\n",
179                 r: "locale",
180                 R: "%H:%M",
181                 t: "\t",
182                 T: "%H:%M:%S",
183                 x: "locale",
184                 X: "locale"
185                 //"+": "%a %b %e %T %Z %Y"
186         },
187
188          /**
189          * Takes a native JavaScript Date and formats it as a string for display to user.
190          *
191          * @for DataType.Date
192          * @method format
193          * @param oDate {Date} Date.
194          * @param oConfig {Object} (Optional) Object literal of configuration values:
195          *  <dl>
196          *   <dt>format {String} (Optional)</dt>
197          *   <dd>
198          *   <p>
199          *   Any strftime string is supported, such as "%I:%M:%S %p". strftime has several format specifiers defined by the Open group at 
200          *   <a href="http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html">http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html</a>
201          *   PHP added a few of its own, defined at <a href="http://www.php.net/strftime">http://www.php.net/strftime</a>
202          *   </p>
203          *   <p>
204          *   This javascript implementation supports all the PHP specifiers and a few more.  The full list is below.
205          *   </p>
206          *   <p>
207          *   If not specified, it defaults to the ISO8601 standard date format: %Y-%m-%d.  This may be overridden by changing Y.config.dateFormat
208          *   </p>
209          *   <dl>
210          *      <dt>%a</dt> <dd>abbreviated weekday name according to the current locale</dd>
211          *      <dt>%A</dt> <dd>full weekday name according to the current locale</dd>
212          *      <dt>%b</dt> <dd>abbreviated month name according to the current locale</dd>
213          *      <dt>%B</dt> <dd>full month name according to the current locale</dd>
214          *      <dt>%c</dt> <dd>preferred date and time representation for the current locale</dd>
215          *      <dt>%C</dt> <dd>century number (the year divided by 100 and truncated to an integer, range 00 to 99)</dd>
216          *      <dt>%d</dt> <dd>day of the month as a decimal number (range 01 to 31)</dd>
217          *      <dt>%D</dt> <dd>same as %m/%d/%y</dd>
218          *      <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>
219          *      <dt>%F</dt> <dd>same as %Y-%m-%d (ISO 8601 date format)</dd>
220          *      <dt>%g</dt> <dd>like %G, but without the century</dd>
221          *      <dt>%G</dt> <dd>The 4-digit year corresponding to the ISO week number</dd>
222          *      <dt>%h</dt> <dd>same as %b</dd>
223          *      <dt>%H</dt> <dd>hour as a decimal number using a 24-hour clock (range 00 to 23)</dd>
224          *      <dt>%I</dt> <dd>hour as a decimal number using a 12-hour clock (range 01 to 12)</dd>
225          *      <dt>%j</dt> <dd>day of the year as a decimal number (range 001 to 366)</dd>
226          *      <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>
227          *      <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>
228          *      <dt>%m</dt> <dd>month as a decimal number (range 01 to 12)</dd>
229          *      <dt>%M</dt> <dd>minute as a decimal number</dd>
230          *      <dt>%n</dt> <dd>newline character</dd>
231          *      <dt>%p</dt> <dd>either "AM" or "PM" according to the given time value, or the corresponding strings for the current locale</dd>
232          *      <dt>%P</dt> <dd>like %p, but lower case</dd>
233          *      <dt>%r</dt> <dd>time in a.m. and p.m. notation equal to %I:%M:%S %p</dd>
234          *      <dt>%R</dt> <dd>time in 24 hour notation equal to %H:%M</dd>
235          *      <dt>%s</dt> <dd>number of seconds since the Epoch, ie, since 1970-01-01 00:00:00 UTC</dd>
236          *      <dt>%S</dt> <dd>second as a decimal number</dd>
237          *      <dt>%t</dt> <dd>tab character</dd>
238          *      <dt>%T</dt> <dd>current time, equal to %H:%M:%S</dd>
239          *      <dt>%u</dt> <dd>weekday as a decimal number [1,7], with 1 representing Monday</dd>
240          *      <dt>%U</dt> <dd>week number of the current year as a decimal number, starting with the
241          *                      first Sunday as the first day of the first week</dd>
242          *      <dt>%V</dt> <dd>The ISO 8601:1988 week number of the current year as a decimal number,
243          *                      range 01 to 53, where week 1 is the first week that has at least 4 days
244          *                      in the current year, and with Monday as the first day of the week.</dd>
245          *      <dt>%w</dt> <dd>day of the week as a decimal, Sunday being 0</dd>
246          *      <dt>%W</dt> <dd>week number of the current year as a decimal number, starting with the
247          *                      first Monday as the first day of the first week</dd>
248          *      <dt>%x</dt> <dd>preferred date representation for the current locale without the time</dd>
249          *      <dt>%X</dt> <dd>preferred time representation for the current locale without the date</dd>
250          *      <dt>%y</dt> <dd>year as a decimal number without a century (range 00 to 99)</dd>
251          *      <dt>%Y</dt> <dd>year as a decimal number including the century</dd>
252          *      <dt>%z</dt> <dd>numerical time zone representation</dd>
253          *      <dt>%Z</dt> <dd>time zone name or abbreviation</dd>
254          *      <dt>%%</dt> <dd>a literal "%" character</dd>
255          *   </dl>
256          *  </dd>
257          *  <dt>locale {String} (Optional)</dt>
258          *  <dd>
259          *   The locale to use when displaying days of week, months of the year, and other locale specific
260          *   strings. If not specified, this defaults to "en" (though this may be overridden by changing Y.config.locale).
261          *   The following locales are built in:
262          *   <dl>
263          *    <dt>en</dt>
264          *    <dd>English</dd>
265          *    <dt>en-US</dt>
266          *    <dd>US English</dd>
267          *    <dt>en-GB</dt>
268          *    <dd>British English</dd>
269          *    <dt>en-AU</dt>
270          *    <dd>Australian English (identical to British English)</dd>
271          *   </dl>
272          *   More locales may be added by subclassing of Y.DataType.Date.Locale["en"].
273          *   See Y.DataType.Date.Locale for more information.
274          *  </dd>
275          * </dl>
276          * @return {String} Formatted date for display.
277          */
278         format : function (oDate, oConfig) {
279                 oConfig = oConfig || {};
280                 
281                 if(!Y.Lang.isDate(oDate)) {
282                         return Y.Lang.isValue(oDate) ? oDate : "";
283                 }
284
285                 var format = oConfig.format || Y.config.dateFormat,
286                         sLocale = oConfig.locale || Y.config.locale,
287                         LOCALE = Y.DataType.Date.Locale;
288
289                 sLocale = sLocale.replace(/_/g, "-");
290                 
291                 // Make sure we have a definition for the requested locale, or default to en.
292                 if(!LOCALE[sLocale]) {
293                         var tmpLocale = sLocale.replace(/-[a-zA-Z]+$/, "");
294                         if(tmpLocale in LOCALE) {
295                                 sLocale = tmpLocale;
296                         } else if(Y.config.locale in LOCALE) {
297                                 sLocale = Y.config.locale;
298                         } else {
299                                 sLocale = "en";
300                         }
301                 }
302
303                 var aLocale = LOCALE[sLocale];
304
305                 var replace_aggs = function (m0, m1) {
306                         var f = Dt.aggregates[m1];
307                         return (f === "locale" ? aLocale[m1] : f);
308                 };
309
310                 var replace_formats = function (m0, m1) {
311                         var f = Dt.formats[m1];
312                         switch(Y.Lang.type(f)) {
313                                 case "string":                                  // string => built in date function
314                                         return oDate[f]();
315                                 case "function":                                // function => our own function
316                                         return f.call(oDate, oDate, aLocale);
317                                 case "array":                                   // built in function with padding
318                                         if(Y.Lang.type(f[0]) === "string") {
319                                                 return xPad(oDate[f[0]](), f[1]);
320                                         } // no break; (fall through to default:)
321                                 default:
322                                         return m1;
323                         }
324                 };
325
326                 // First replace aggregates (run in a loop because an agg may be made up of other aggs)
327                 while(format.match(/%[cDFhnrRtTxX]/)) {
328                         format = format.replace(/%([cDFhnrRtTxX])/g, replace_aggs);
329                 }
330
331                 // Now replace formats (do not run in a loop otherwise %%a will be replace with the value of %a)
332                 var str = format.replace(/%([aAbBCdegGHIjklmMpPsSuUVwWyYzZ%])/g, replace_formats);
333
334                 replace_aggs = replace_formats = undefined;
335
336                 return str;
337         }
338 };
339
340 Y.mix(Y.namespace("DataType.Date"), Dt);
341
342 /**
343  * @module datatype
344 */
345
346 /**
347  * The Date.Locale class is a container for all localised date strings
348  * used by Y.DataType.Date. It is used internally, but may be extended
349  * to provide new date localisations.
350  *
351  * To create your own Locale, follow these steps:
352  * <ol>
353  *  <li>Find an existing locale that matches closely with your needs</li>
354  *  <li>Use this as your base class.  Use Y.DataType.Date.Locale["en"] if nothing
355  *   matches.</li>
356  *  <li>Create your own class as an extension of the base class using
357  *   Y.merge, and add your own localisations where needed.</li>
358  * </ol>
359  * See the Y.DataType.Date.Locale["en-US"] and Y.DataType.Date.Locale["en-GB"]
360  * classes which extend Y.DataType.Date.Locale["en"].
361  *
362  * For example, to implement locales for French french and Canadian french,
363  * we would do the following:
364  * <ol>
365  *  <li>For French french, we have no existing similar locale, so use
366  *   Y.DataType.Date.Locale["en"] as the base, and extend it:
367  *   <pre>
368  *      Y.DataType.Date.Locale["fr"] = Y.merge(Y.DataType.Date.Locale, {
369  *          a: ["dim", "lun", "mar", "mer", "jeu", "ven", "sam"],
370  *          A: ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"],
371  *          b: ["jan", "f&eacute;v", "mar", "avr", "mai", "jun", "jui", "ao&ucirc;", "sep", "oct", "nov", "d&eacute;c"],
372  *          B: ["janvier", "f&eacute;vrier", "mars", "avril", "mai", "juin", "juillet", "ao&ucirc;t", "septembre", "octobre", "novembre", "d&eacute;cembre"],
373  *          c: "%a %d %b %Y %T %Z",
374  *          p: ["", ""],
375  *          P: ["", ""],
376  *          x: "%d.%m.%Y",
377  *          X: "%T"
378  *      });
379  *   </pre>
380  *  </li>
381  *  <li>For Canadian french, we start with French french and change the meaning of \%x:
382  *   <pre>
383  *      Y.DataType.Date.Locale["fr-CA"] = Y.merge(Y.DataType.Date.Locale["fr"], {
384  *          x: "%Y-%m-%d"
385  *      });
386  *   </pre>
387  *  </li>
388  * </ol>
389  *
390  * With that, you can use your new locales:
391  * <pre>
392  *    var d = new Date("2008/04/22");
393  *    Y.DataType.Date.format(d, { format: "%A, %d %B == %x", locale: "fr" });
394  * </pre>
395  * will return:
396  * <pre>
397  *    mardi, 22 avril == 22.04.2008
398  * </pre>
399  * And
400  * <pre>
401  *    Y.DataType.Date.format(d, {format: "%A, %d %B == %x", locale: "fr-CA" });
402  * </pre>
403  * Will return:
404  * <pre>
405  *   mardi, 22 avril == 2008-04-22
406  * </pre>
407  * @requires oop
408  * @class DataType.Date.Locale
409  * @static
410  */
411 var YDateEn = {
412         a: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
413         A: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
414         b: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
415         B: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
416         c: "%a %d %b %Y %T %Z",
417         p: ["AM", "PM"],
418         P: ["am", "pm"],
419         r: "%I:%M:%S %p",
420         x: "%d/%m/%y",
421         X: "%T"
422 };
423
424 Y.namespace("DataType.Date.Locale");
425
426 Y.DataType.Date.Locale["en"] = YDateEn;
427
428 Y.DataType.Date.Locale["en-US"] = Y.merge(YDateEn, {
429         c: "%a %d %b %Y %I:%M:%S %p %Z",
430         x: "%m/%d/%Y",
431         X: "%I:%M:%S %p"
432 });
433
434 Y.DataType.Date.Locale["en-GB"] = Y.merge(YDateEn, {
435         r: "%l:%M:%S %P %Z"
436 });
437 Y.DataType.Date.Locale["en-AU"] = Y.merge(YDateEn);
438
439
440
441
442
443 }, '3.0.0' );