]> git.wh0rd.org - tt-rss.git/blobdiff - lib/dojo/date.js
build custom layer of Dojo to speed up loading of tt-rss (refs #293)
[tt-rss.git] / lib / dojo / date.js
index dc51853f39ff6ef1fdc5a26e858a5debda475ff4..d4fe218d30e36cd3361a28a38c82bfbed20174d5 100644 (file)
 */
 
 
-if(!dojo._hasResource["dojo.date"]){
-dojo._hasResource["dojo.date"]=true;
+if(!dojo._hasResource["dojo.date"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
+dojo._hasResource["dojo.date"] = true;
 dojo.provide("dojo.date");
-dojo.date.getDaysInMonth=function(_1){
-var _2=_1.getMonth();
-var _3=[31,28,31,30,31,30,31,31,30,31,30,31];
-if(_2==1&&dojo.date.isLeapYear(_1)){
-return 29;
-}
-return _3[_2];
-};
-dojo.date.isLeapYear=function(_4){
-var _5=_4.getFullYear();
-return !(_5%400)||(!(_5%4)&&!!(_5%100));
-};
-dojo.date.getTimezoneName=function(_6){
-var _7=_6.toString();
-var tz="";
-var _8;
-var _9=_7.indexOf("(");
-if(_9>-1){
-tz=_7.substring(++_9,_7.indexOf(")"));
-}else{
-var _a=/([A-Z\/]+) \d{4}$/;
-if((_8=_7.match(_a))){
-tz=_8[1];
-}else{
-_7=_6.toLocaleString();
-_a=/ ([A-Z\/]+)$/;
-if((_8=_7.match(_a))){
-tz=_8[1];
-}
-}
-}
-return (tz=="AM"||tz=="PM")?"":tz;
-};
-dojo.date.compare=function(_b,_c,_d){
-_b=new Date(+_b);
-_c=new Date(+(_c||new Date()));
-if(_d=="date"){
-_b.setHours(0,0,0,0);
-_c.setHours(0,0,0,0);
-}else{
-if(_d=="time"){
-_b.setFullYear(0,0,0);
-_c.setFullYear(0,0,0);
+
+/*=====
+dojo.date = {
+       // summary: Date manipulation utilities
 }
+=====*/
+
+dojo.date.getDaysInMonth = function(/*Date*/dateObject){
+       //      summary:
+       //              Returns the number of days in the month used by dateObject
+       var month = dateObject.getMonth();
+       var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
+       if(month == 1 && dojo.date.isLeapYear(dateObject)){ return 29; } // Number
+       return days[month]; // Number
 }
-if(_b>_c){
-return 1;
+
+dojo.date.isLeapYear = function(/*Date*/dateObject){
+       //      summary:
+       //              Determines if the year of the dateObject is a leap year
+       //      description:
+       //              Leap years are years with an additional day YYYY-02-29, where the
+       //              year number is a multiple of four with the following exception: If
+       //              a year is a multiple of 100, then it is only a leap year if it is
+       //              also a multiple of 400. For example, 1900 was not a leap year, but
+       //              2000 is one.
+
+       var year = dateObject.getFullYear();
+       return !(year%400) || (!(year%4) && !!(year%100)); // Boolean
 }
-if(_b<_c){
-return -1;
+
+// FIXME: This is not localized
+dojo.date.getTimezoneName = function(/*Date*/dateObject){
+       //      summary:
+       //              Get the user's time zone as provided by the browser
+       // dateObject:
+       //              Needed because the timezone may vary with time (daylight savings)
+       //      description:
+       //              Try to get time zone info from toString or toLocaleString method of
+       //              the Date object -- UTC offset is not a time zone.  See
+       //              http://www.twinsun.com/tz/tz-link.htm Note: results may be
+       //              inconsistent across browsers.
+
+       var str = dateObject.toString(); // Start looking in toString
+       var tz = ''; // The result -- return empty string if nothing found
+       var match;
+
+       // First look for something in parentheses -- fast lookup, no regex
+       var pos = str.indexOf('(');
+       if(pos > -1){
+               tz = str.substring(++pos, str.indexOf(')'));
+       }else{
+               // If at first you don't succeed ...
+               // If IE knows about the TZ, it appears before the year
+               // Capital letters or slash before a 4-digit year 
+               // at the end of string
+               var pat = /([A-Z\/]+) \d{4}$/;
+               if((match = str.match(pat))){
+                       tz = match[1];
+               }else{
+               // Some browsers (e.g. Safari) glue the TZ on the end
+               // of toLocaleString instead of putting it in toString
+                       str = dateObject.toLocaleString();
+                       // Capital letters or slash -- end of string, 
+                       // after space
+                       pat = / ([A-Z\/]+)$/;
+                       if((match = str.match(pat))){
+                               tz = match[1];
+                       }
+               }
+       }
+
+       // Make sure it doesn't somehow end up return AM or PM
+       return (tz == 'AM' || tz == 'PM') ? '' : tz; // String
 }
-return 0;
+
+// Utility methods to do arithmetic calculations with Dates
+
+dojo.date.compare = function(/*Date*/date1, /*Date?*/date2, /*String?*/portion){
+       //      summary:
+       //              Compare two date objects by date, time, or both.
+       //      description:
+       //      Returns 0 if equal, positive if a > b, else negative.
+       //      date1:
+       //              Date object
+       //      date2:
+       //              Date object.  If not specified, the current Date is used.
+       //      portion:
+       //              A string indicating the "date" or "time" portion of a Date object.
+       //              Compares both "date" and "time" by default.  One of the following:
+       //              "date", "time", "datetime"
+
+       // Extra step required in copy for IE - see #3112
+       date1 = new Date(+date1);
+       date2 = new Date(+(date2 || new Date()));
+
+       if(portion == "date"){
+               // Ignore times and compare dates.
+               date1.setHours(0, 0, 0, 0);
+               date2.setHours(0, 0, 0, 0);
+       }else if(portion == "time"){
+               // Ignore dates and compare times.
+               date1.setFullYear(0, 0, 0);
+               date2.setFullYear(0, 0, 0);
+       }
+       
+       if(date1 > date2){ return 1; } // int
+       if(date1 < date2){ return -1; } // int
+       return 0; // int
 };
-dojo.date.add=function(_e,_f,_10){
-var sum=new Date(+_e);
-var _11=false;
-var _12="Date";
-switch(_f){
-case "day":
-break;
-case "weekday":
-var _13,_14;
-var mod=_10%5;
-if(!mod){
-_13=(_10>0)?5:-5;
-_14=(_10>0)?((_10-5)/5):((_10+5)/5);
-}else{
-_13=mod;
-_14=parseInt(_10/5);
-}
-var _15=_e.getDay();
-var adj=0;
-if(_15==6&&_10>0){
-adj=1;
-}else{
-if(_15==0&&_10<0){
-adj=-1;
-}
-}
-var _16=_15+_13;
-if(_16==0||_16==6){
-adj=(_10>0)?2:-2;
-}
-_10=(7*_14)+_13+adj;
-break;
-case "year":
-_12="FullYear";
-_11=true;
-break;
-case "week":
-_10*=7;
-break;
-case "quarter":
-_10*=3;
-case "month":
-_11=true;
-_12="Month";
-break;
-default:
-_12="UTC"+_f.charAt(0).toUpperCase()+_f.substring(1)+"s";
-}
-if(_12){
-sum["set"+_12](sum["get"+_12]()+_10);
-}
-if(_11&&(sum.getDate()<_e.getDate())){
-sum.setDate(0);
-}
-return sum;
+
+dojo.date.add = function(/*Date*/date, /*String*/interval, /*int*/amount){
+       //      summary:
+       //              Add to a Date in intervals of different size, from milliseconds to years
+       //      date: Date
+       //              Date object to start with
+       //      interval:
+       //              A string representing the interval.  One of the following:
+       //                      "year", "month", "day", "hour", "minute", "second",
+       //                      "millisecond", "quarter", "week", "weekday"
+       //      amount:
+       //              How much to add to the date.
+
+       var sum = new Date(+date); // convert to Number before copying to accomodate IE (#3112)
+       var fixOvershoot = false;
+       var property = "Date";
+
+       switch(interval){
+               case "day":
+                       break;
+               case "weekday":
+                       //i18n FIXME: assumes Saturday/Sunday weekend, but this is not always true.  see dojo.cldr.supplemental
+
+                       // Divide the increment time span into weekspans plus leftover days
+                       // e.g., 8 days is one 5-day weekspan / and two leftover days
+                       // Can't have zero leftover days, so numbers divisible by 5 get
+                       // a days value of 5, and the remaining days make up the number of weeks
+                       var days, weeks;
+                       var mod = amount % 5;
+                       if(!mod){
+                               days = (amount > 0) ? 5 : -5;
+                               weeks = (amount > 0) ? ((amount-5)/5) : ((amount+5)/5);
+                       }else{
+                               days = mod;
+                               weeks = parseInt(amount/5);
+                       }
+                       // Get weekday value for orig date param
+                       var strt = date.getDay();
+                       // Orig date is Sat / positive incrementer
+                       // Jump over Sun
+                       var adj = 0;
+                       if(strt == 6 && amount > 0){
+                               adj = 1;
+                       }else if(strt == 0 && amount < 0){
+                       // Orig date is Sun / negative incrementer
+                       // Jump back over Sat
+                               adj = -1;
+                       }
+                       // Get weekday val for the new date
+                       var trgt = strt + days;
+                       // New date is on Sat or Sun
+                       if(trgt == 0 || trgt == 6){
+                               adj = (amount > 0) ? 2 : -2;
+                       }
+                       // Increment by number of weeks plus leftover days plus
+                       // weekend adjustments
+                       amount = (7 * weeks) + days + adj;
+                       break;
+               case "year":
+                       property = "FullYear";
+                       // Keep increment/decrement from 2/29 out of March
+                       fixOvershoot = true;
+                       break;
+               case "week":
+                       amount *= 7;
+                       break;
+               case "quarter":
+                       // Naive quarter is just three months
+                       amount *= 3;
+                       // fallthrough...
+               case "month":
+                       // Reset to last day of month if you overshoot
+                       fixOvershoot = true;
+                       property = "Month";
+                       break;
+//             case "hour":
+//             case "minute":
+//             case "second":
+//             case "millisecond":
+               default:
+                       property = "UTC"+interval.charAt(0).toUpperCase() + interval.substring(1) + "s";
+       }
+
+       if(property){
+               sum["set"+property](sum["get"+property]()+amount);
+       }
+
+       if(fixOvershoot && (sum.getDate() < date.getDate())){
+               sum.setDate(0);
+       }
+
+       return sum; // Date
 };
-dojo.date.difference=function(_17,_18,_19){
-_18=_18||new Date();
-_19=_19||"day";
-var _1a=_18.getFullYear()-_17.getFullYear();
-var _1b=1;
-switch(_19){
-case "quarter":
-var m1=_17.getMonth();
-var m2=_18.getMonth();
-var q1=Math.floor(m1/3)+1;
-var q2=Math.floor(m2/3)+1;
-q2+=(_1a*4);
-_1b=q2-q1;
-break;
-case "weekday":
-var _1c=Math.round(dojo.date.difference(_17,_18,"day"));
-var _1d=parseInt(dojo.date.difference(_17,_18,"week"));
-var mod=_1c%7;
-if(mod==0){
-_1c=_1d*5;
-}else{
-var adj=0;
-var _1e=_17.getDay();
-var _1f=_18.getDay();
-_1d=parseInt(_1c/7);
-mod=_1c%7;
-var _20=new Date(_17);
-_20.setDate(_20.getDate()+(_1d*7));
-var _21=_20.getDay();
-if(_1c>0){
-switch(true){
-case _1e==6:
-adj=-1;
-break;
-case _1e==0:
-adj=0;
-break;
-case _1f==6:
-adj=-1;
-break;
-case _1f==0:
-adj=-2;
-break;
-case (_21+mod)>5:
-adj=-2;
-}
-}else{
-if(_1c<0){
-switch(true){
-case _1e==6:
-adj=0;
-break;
-case _1e==0:
-adj=1;
-break;
-case _1f==6:
-adj=2;
-break;
-case _1f==0:
-adj=1;
-break;
-case (_21+mod)<0:
-adj=2;
-}
-}
-}
-_1c+=adj;
-_1c-=(_1d*2);
-}
-_1b=_1c;
-break;
-case "year":
-_1b=_1a;
-break;
-case "month":
-_1b=(_18.getMonth()-_17.getMonth())+(_1a*12);
-break;
-case "week":
-_1b=parseInt(dojo.date.difference(_17,_18,"day")/7);
-break;
-case "day":
-_1b/=24;
-case "hour":
-_1b/=60;
-case "minute":
-_1b/=60;
-case "second":
-_1b/=1000;
-case "millisecond":
-_1b*=_18.getTime()-_17.getTime();
-}
-return Math.round(_1b);
+
+dojo.date.difference = function(/*Date*/date1, /*Date?*/date2, /*String?*/interval){
+       //      summary:
+       //              Get the difference in a specific unit of time (e.g., number of
+       //              months, weeks, days, etc.) between two dates, rounded to the
+       //              nearest integer.
+       //      date1:
+       //              Date object
+       //      date2:
+       //              Date object.  If not specified, the current Date is used.
+       //      interval:
+       //              A string representing the interval.  One of the following:
+       //                      "year", "month", "day", "hour", "minute", "second",
+       //                      "millisecond", "quarter", "week", "weekday"
+       //              Defaults to "day".
+
+       date2 = date2 || new Date();
+       interval = interval || "day";
+       var yearDiff = date2.getFullYear() - date1.getFullYear();
+       var delta = 1; // Integer return value
+
+       switch(interval){
+               case "quarter":
+                       var m1 = date1.getMonth();
+                       var m2 = date2.getMonth();
+                       // Figure out which quarter the months are in
+                       var q1 = Math.floor(m1/3) + 1;
+                       var q2 = Math.floor(m2/3) + 1;
+                       // Add quarters for any year difference between the dates
+                       q2 += (yearDiff * 4);
+                       delta = q2 - q1;
+                       break;
+               case "weekday":
+                       var days = Math.round(dojo.date.difference(date1, date2, "day"));
+                       var weeks = parseInt(dojo.date.difference(date1, date2, "week"));
+                       var mod = days % 7;
+
+                       // Even number of weeks
+                       if(mod == 0){
+                               days = weeks*5;
+                       }else{
+                               // Weeks plus spare change (< 7 days)
+                               var adj = 0;
+                               var aDay = date1.getDay();
+                               var bDay = date2.getDay();
+
+                               weeks = parseInt(days/7);
+                               mod = days % 7;
+                               // Mark the date advanced by the number of
+                               // round weeks (may be zero)
+                               var dtMark = new Date(date1);
+                               dtMark.setDate(dtMark.getDate()+(weeks*7));
+                               var dayMark = dtMark.getDay();
+
+                               // Spare change days -- 6 or less
+                               if(days > 0){
+                                       switch(true){
+                                               // Range starts on Sat
+                                               case aDay == 6:
+                                                       adj = -1;
+                                                       break;
+                                               // Range starts on Sun
+                                               case aDay == 0:
+                                                       adj = 0;
+                                                       break;
+                                               // Range ends on Sat
+                                               case bDay == 6:
+                                                       adj = -1;
+                                                       break;
+                                               // Range ends on Sun
+                                               case bDay == 0:
+                                                       adj = -2;
+                                                       break;
+                                               // Range contains weekend
+                                               case (dayMark + mod) > 5:
+                                                       adj = -2;
+                                       }
+                               }else if(days < 0){
+                                       switch(true){
+                                               // Range starts on Sat
+                                               case aDay == 6:
+                                                       adj = 0;
+                                                       break;
+                                               // Range starts on Sun
+                                               case aDay == 0:
+                                                       adj = 1;
+                                                       break;
+                                               // Range ends on Sat
+                                               case bDay == 6:
+                                                       adj = 2;
+                                                       break;
+                                               // Range ends on Sun
+                                               case bDay == 0:
+                                                       adj = 1;
+                                                       break;
+                                               // Range contains weekend
+                                               case (dayMark + mod) < 0:
+                                                       adj = 2;
+                                       }
+                               }
+                               days += adj;
+                               days -= (weeks*2);
+                       }
+                       delta = days;
+                       break;
+               case "year":
+                       delta = yearDiff;
+                       break;
+               case "month":
+                       delta = (date2.getMonth() - date1.getMonth()) + (yearDiff * 12);
+                       break;
+               case "week":
+                       // Truncate instead of rounding
+                       // Don't use Math.floor -- value may be negative
+                       delta = parseInt(dojo.date.difference(date1, date2, "day")/7);
+                       break;
+               case "day":
+                       delta /= 24;
+                       // fallthrough
+               case "hour":
+                       delta /= 60;
+                       // fallthrough
+               case "minute":
+                       delta /= 60;
+                       // fallthrough
+               case "second":
+                       delta /= 1000;
+                       // fallthrough
+               case "millisecond":
+                       delta *= date2.getTime() - date1.getTime();
+       }
+
+       // Round for fractional values and DST leaps
+       return Math.round(delta); // Number (integer)
 };
+
 }