~smagoun/whoopsie/whoopsie-lp1017637

« back to all changes in this revision

Viewing changes to backend/stats/static/js/yui/build/datatype-date-math/datatype-date-math.js

  • Committer: Evan Dandrea
  • Date: 2012-05-09 05:53:45 UTC
  • Revision ID: evan.dandrea@canonical.com-20120509055345-z2j41tmcbf4as5uf
The backend now lives in lp:daisy and the website (errors.ubuntu.com) now lives in lp:errors.

Show diffs side-by-side

added added

removed removed

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