]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/date.js.uncompressed.js
1 define("dojo/date", ["./_base/kernel", "./_base/lang"], function(dojo
, lang
) {
7 lang
.getObject("date", true, dojo
);
11 // summary: Date manipulation utilities
15 dojo
.date
.getDaysInMonth = function(/*Date*/dateObject
){
17 // Returns the number of days in the month used by dateObject
18 var month
= dateObject
.getMonth();
19 var days
= [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
20 if(month
== 1 && dojo
.date
.isLeapYear(dateObject
)){ return 29; } // Number
21 return days
[month
]; // Number
24 dojo
.date
.isLeapYear = function(/*Date*/dateObject
){
26 // Determines if the year of the dateObject is a leap year
28 // Leap years are years with an additional day YYYY-02-29, where the
29 // year number is a multiple of four with the following exception: If
30 // a year is a multiple of 100, then it is only a leap year if it is
31 // also a multiple of 400. For example, 1900 was not a leap year, but
34 var year
= dateObject
.getFullYear();
35 return !(year
%400) || (!(year
%4) && !!(year
%100)); // Boolean
38 // FIXME: This is not localized
39 dojo
.date
.getTimezoneName = function(/*Date*/dateObject
){
41 // Get the user's time zone as provided by the browser
43 // Needed because the timezone may vary with time (daylight savings)
45 // Try to get time zone info from toString or toLocaleString method of
46 // the Date object -- UTC offset is not a time zone. See
47 // http://www.twinsun.com/tz/tz-link.htm Note: results may be
48 // inconsistent across browsers.
50 var str
= dateObject
.toString(); // Start looking in toString
51 var tz
= ''; // The result -- return empty string if nothing found
54 // First look for something in parentheses -- fast lookup, no regex
55 var pos
= str
.indexOf('(');
57 tz
= str
.substring(++pos
, str
.indexOf(')'));
59 // If at first you don't succeed ...
60 // If IE knows about the TZ, it appears before the year
61 // Capital letters or slash before a 4-digit year
62 // at the end of string
63 var pat
= /([A-Z\/]+) \d{4}$/;
64 if((match
= str
.match(pat
))){
67 // Some browsers (e.g. Safari) glue the TZ on the end
68 // of toLocaleString instead of putting it in toString
69 str
= dateObject
.toLocaleString();
70 // Capital letters or slash -- end of string,
73 if((match
= str
.match(pat
))){
79 // Make sure it doesn't somehow end up return AM or PM
80 return (tz
== 'AM' || tz
== 'PM') ? '' : tz
; // String
83 // Utility methods to do arithmetic calculations with Dates
85 dojo
.date
.compare = function(/*Date*/date1
, /*Date?*/date2
, /*String?*/portion
){
87 // Compare two date objects by date, time, or both.
89 // Returns 0 if equal, positive if a > b, else negative.
93 // Date object. If not specified, the current Date is used.
95 // A string indicating the "date" or "time" portion of a Date object.
96 // Compares both "date" and "time" by default. One of the following:
97 // "date", "time", "datetime"
99 // Extra step required in copy for IE - see #3112
100 date1
= new Date(+date1
);
101 date2
= new Date(+(date2
|| new Date()));
103 if(portion
== "date"){
104 // Ignore times and compare dates.
105 date1
.setHours(0, 0, 0, 0);
106 date2
.setHours(0, 0, 0, 0);
107 }else if(portion
== "time"){
108 // Ignore dates and compare times.
109 date1
.setFullYear(0, 0, 0);
110 date2
.setFullYear(0, 0, 0);
113 if(date1
> date2
){ return 1; } // int
114 if(date1
< date2
){ return -1; } // int
118 dojo
.date
.add = function(/*Date*/date
, /*String*/interval
, /*int*/amount
){
120 // Add to a Date in intervals of different size, from milliseconds to years
122 // Date object to start with
124 // A string representing the interval. One of the following:
125 // "year", "month", "day", "hour", "minute", "second",
126 // "millisecond", "quarter", "week", "weekday"
128 // How much to add to the date.
130 var sum
= new Date(+date
); // convert to Number before copying to accomodate IE (#3112)
131 var fixOvershoot
= false;
132 var property
= "Date";
138 //i18n FIXME: assumes Saturday/Sunday weekend, but this is not always true. see dojo.cldr.supplemental
140 // Divide the increment time span into weekspans plus leftover days
141 // e.g., 8 days is one 5-day weekspan / and two leftover days
142 // Can't have zero leftover days, so numbers divisible by 5 get
143 // a days value of 5, and the remaining days make up the number of weeks
145 var mod
= amount
% 5;
147 days
= (amount
> 0) ? 5 : -5;
148 weeks
= (amount
> 0) ? ((amount
-5)/5) : ((amount+5)/5);
151 weeks
= parseInt(amount
/5);
153 // Get weekday value for orig date param
154 var strt
= date
.getDay();
155 // Orig date is Sat / positive incrementer
158 if(strt
== 6 && amount
> 0){
160 }else if(strt
== 0 && amount
< 0){
161 // Orig date is Sun / negative incrementer
162 // Jump back over Sat
165 // Get weekday val for the new date
166 var trgt
= strt
+ days
;
167 // New date is on Sat or Sun
168 if(trgt
== 0 || trgt
== 6){
169 adj
= (amount
> 0) ? 2 : -2;
171 // Increment by number of weeks plus leftover days plus
172 // weekend adjustments
173 amount
= (7 * weeks
) + days
+ adj
;
176 property
= "FullYear";
177 // Keep increment/decrement from 2/29 out of March
184 // Naive quarter is just three months
188 // Reset to last day of month if you overshoot
195 // case "millisecond":
197 property
= "UTC"+interval
.charAt(0).toUpperCase() + interval
.substring(1) + "s";
201 sum
["set"+property
](sum
["get"+property
]()+amount
);
204 if(fixOvershoot
&& (sum
.getDate() < date
.getDate())){
211 dojo
.date
.difference = function(/*Date*/date1
, /*Date?*/date2
, /*String?*/interval
){
213 // Get the difference in a specific unit of time (e.g., number of
214 // months, weeks, days, etc.) between two dates, rounded to the
219 // Date object. If not specified, the current Date is used.
221 // A string representing the interval. One of the following:
222 // "year", "month", "day", "hour", "minute", "second",
223 // "millisecond", "quarter", "week", "weekday"
224 // Defaults to "day".
226 date2
= date2
|| new Date();
227 interval
= interval
|| "day";
228 var yearDiff
= date2
.getFullYear() - date1
.getFullYear();
229 var delta
= 1; // Integer return value
233 var m1
= date1
.getMonth();
234 var m2
= date2
.getMonth();
235 // Figure out which quarter the months are in
236 var q1
= Math
.floor(m1
/3) + 1;
237 var q2
= Math
.floor(m2
/3) + 1;
238 // Add quarters for any year difference between the dates
239 q2
+= (yearDiff
* 4);
243 var days
= Math
.round(dojo
.date
.difference(date1
, date2
, "day"));
244 var weeks
= parseInt(dojo
.date
.difference(date1
, date2
, "week"));
247 // Even number of weeks
251 // Weeks plus spare change (< 7 days)
253 var aDay
= date1
.getDay();
254 var bDay
= date2
.getDay();
256 weeks
= parseInt(days
/7);
258 // Mark the date advanced by the number of
259 // round weeks (may be zero)
260 var dtMark
= new Date(date1
);
261 dtMark
.setDate(dtMark
.getDate()+(weeks
*7));
262 var dayMark
= dtMark
.getDay();
264 // Spare change days -- 6 or less
267 // Range starts on Sat
271 // Range starts on Sun
283 // Range contains weekend
284 case (dayMark
+ mod
) > 5:
289 // Range starts on Sat
293 // Range starts on Sun
305 // Range contains weekend
306 case (dayMark
+ mod
) < 0:
319 delta
= (date2
.getMonth() - date1
.getMonth()) + (yearDiff
* 12);
322 // Truncate instead of rounding
323 // Don't use Math.floor -- value may be negative
324 delta
= parseInt(dojo
.date
.difference(date1
, date2
, "day")/7);
339 delta
*= date2
.getTime() - date1
.getTime();
342 // Round for fractional values and DST leaps
343 return Math
.round(delta
); // Number (integer)