NOBUG: Fixed file access permissions
[moodle.git] / lib / yuilib / 3.13.0 / datatype-date-math / datatype-date-math.js
blob3904130e62c39e624ebb0ce1403ca6a845306e6b
1 /*
2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
8 YUI.add('datatype-date-math', function (Y, NAME) {
10 /**
11  * Date Math submodule.
12  *
13  * @module datatype-date
14  * @submodule datatype-date-math
15  * @for Date
16  */
17 var LANG = Y.Lang;
19 Y.mix(Y.namespace("Date"), {
21         /**
22          * Checks whether a native JavaScript Date contains a valid value.
23          * @for Date
24          * @method isValidDate
25          * @param oDate {Date} Date in the month for which the number of days is desired.
26          * @return {Boolean} True if the date argument contains a valid value.
27          */
28          isValidDate : function (oDate) {
29                 if(LANG.isDate(oDate) && (isFinite(oDate)) && (oDate != "Invalid Date") && !isNaN(oDate) && (oDate != null)) {
30             return true;
31         }
32         else {
33             return false;
34         }
35         },
37         /**
38          * Checks whether two dates correspond to the same date and time.
39          * @for Date
40          * @method areEqual
41          * @param aDate {Date} The first date to compare.
42          * @param bDate {Date} The second date to compare.
43          * @return {Boolean} True if the two dates correspond to the same
44          * date and time.
45          */
46         areEqual : function (aDate, bDate) {
47                 return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() == bDate.getTime()));
48         },
50         /**
51          * Checks whether the first date comes later than the second.
52          * @for Date
53          * @method isGreater
54          * @param aDate {Date} The first date to compare.
55          * @param bDate {Date} The second date to compare.
56          * @return {Boolean} True if the first date is later than the second.
57          */
58     isGreater : function (aDate, bDate) {
59         return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() > bDate.getTime()));
60     },
62         /**
63          * Checks whether the first date comes later than or is the same as
64          * the second.
65          * @for Date
66          * @method isGreaterOrEqual
67          * @param aDate {Date} The first date to compare.
68          * @param bDate {Date} The second date to compare.
69          * @return {Boolean} True if the first date is later than or
70          * the same as the second.
71          */
72     isGreaterOrEqual : function (aDate, bDate) {
73         return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() >= bDate.getTime()));
74     },
77     /**
78          * Checks whether the date is between two other given dates.
79          * @for Date
80          * @method isInRange
81          * @param aDate {Date} The date to check
82          * @param bDate {Date} Lower bound of the range.
83          * @param cDate {Date} Higher bound of the range.
84          * @return {Boolean} True if the date is between the two other given dates.
85          */
86     isInRange : function (aDate, bDate, cDate) {
87         return (this.isGreaterOrEqual(aDate, bDate) && this.isGreaterOrEqual(cDate, aDate));
88     },
90         /**
91          * Adds a specified number of days to the given date.
92          * @for Date
93          * @method addDays
94          * @param oDate {Date} The date to add days to.
95          * @param numDays {Number} The number of days to add (can be negative)
96          * @return {Date} A new Date with the specified number of days
97          * added to the original date.
98          */
99         addDays : function (oDate, numDays) {
100                 return new Date(oDate.getTime() + 86400000*numDays);
101         },
104         /**
105          * Adds a specified number of months to the given date.
106          * @for Date
107          * @method addMonths
108          * @param oDate {Date} The date to add months to.
109          * @param numMonths {Number} The number of months to add (can be negative)
110          * @return {Date} A new Date with the specified number of months
111          * added to the original date.
112          */
113         addMonths : function (oDate, numMonths) {
114                 var newYear = oDate.getFullYear();
115                 var newMonth = oDate.getMonth() + numMonths;
117                 newYear  = Math.floor(newYear + newMonth / 12);
118                 newMonth = (newMonth % 12 + 12) % 12;
120                 var newDate = new Date (oDate.getTime());
121                 newDate.setFullYear(newYear);
122                 newDate.setMonth(newMonth);
124                 return newDate;
125         },
127         /**
128          * Adds a specified number of years to the given date.
129          * @for Date
130          * @method addYears
131          * @param oDate {Date} The date to add years to.
132          * @param numYears {Number} The number of years to add (can be negative)
133          * @return {Date} A new Date with the specified number of years
134          * added to the original date.
135          */
136         addYears : function (oDate, numYears) {
137                 var newYear = oDate.getFullYear() + numYears;
138                 var newDate = new Date(oDate.getTime());
140                 newDate.setFullYear(newYear);
141                 return newDate;
142         },
144         /**
145          * Lists all dates in a given month.
146          * @for Date
147          * @method listOfDatesInMonth
148          * @param oDate {Date} The date corresponding to the month for
149          * which a list of dates is required.
150          * @return {Array} An `Array` of `Date`s from a given month.
151          */
152     listOfDatesInMonth : function (oDate) {
153        if (!this.isValidDate(oDate)) {
154          return [];
155        }
157        var daysInMonth = this.daysInMonth(oDate),
158            year        = oDate.getFullYear(),
159            month       = oDate.getMonth(),
160            output      = [];
162        for (var day = 1; day <= daysInMonth; day++) {
163            output.push(new Date(year, month, day, 12, 0, 0));
164        }
166        return output;
167     },
169         /**
170          * Takes a native JavaScript Date and returns the number of days
171          * in the month that the given date belongs to.
172          * @for Date
173          * @method daysInMonth
174          * @param oDate {Date} Date in the month for which the number
175          * of days is desired.
176          * @return {Number} A number (either 28, 29, 30 or 31) of days
177          * in the given month.
178          */
179          daysInMonth : function (oDate) {
180                 if (!this.isValidDate(oDate)) {
181                         return 0;
182                 }
184                 var mon = oDate.getMonth();
185                 var lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
187                 if (mon != 1) {
188                         return lengths[mon];
189                 }
190                 else {
192                         var year = oDate.getFullYear();
193                         if (year%400 === 0) {
194                                return 29;
195                         }
196                         else if (year%100 === 0) {
197                                    return 28;
198                         }
199                         else if (year%4 === 0) {
200                                return 29;
201                         }
202                         else {
203                                return 28;
204                     }
205            }
206         }
210 Y.namespace("DataType");
211 Y.DataType.Date = Y.Date;
214 }, '3.13.0', {"requires": ["yui-base"]});