0c122edae379794dd1f60f79bf11edbb2fb0c427.svn-base 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. if (!dojo._hasResource["dojo.date"]) { // _hasResource checks added by build.
  2. // Do not use _hasResource directly in
  3. // your code.
  4. dojo._hasResource["dojo.date"] = true;
  5. dojo.provide("dojo.date");
  6. dojo.date.getDaysInMonth = function(/* Date */dateObject) {
  7. // summary:
  8. // Returns the number of days in the month used by dateObject
  9. var month = dateObject.getMonth();
  10. var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  11. if (month == 1 && dojo.date.isLeapYear(dateObject)) {
  12. return 29;
  13. } // Number
  14. return days[month]; // Number
  15. }
  16. dojo.date.isLeapYear = function(/* Date */dateObject) {
  17. // summary:
  18. // Determines if the year of the dateObject is a leap year
  19. // description:
  20. // Leap years are years with an additional day YYYY-02-29, where the
  21. // year number is a multiple of four with the following exception: If
  22. // a year is a multiple of 100, then it is only a leap year if it is
  23. // also a multiple of 400. For example, 1900 was not a leap year, but
  24. // 2000 is one.
  25. var year = dateObject.getFullYear();
  26. return !(year % 400) || (!(year % 4) && !!(year % 100)); // Boolean
  27. }
  28. // FIXME: This is not localized
  29. dojo.date.getTimezoneName = function(/* Date */dateObject) {
  30. // summary:
  31. // Get the user's time zone as provided by the browser
  32. // dateObject:
  33. // Needed because the timezone may vary with time (daylight savings)
  34. // description:
  35. // Try to get time zone info from toString or toLocaleString method of
  36. // the Date object -- UTC offset is not a time zone. See
  37. // http://www.twinsun.com/tz/tz-link.htm Note: results may be
  38. // inconsistent across browsers.
  39. var str = dateObject.toString(); // Start looking in toString
  40. var tz = ''; // The result -- return empty string if nothing found
  41. var match;
  42. // First look for something in parentheses -- fast lookup, no regex
  43. var pos = str.indexOf('(');
  44. if (pos > -1) {
  45. tz = str.substring(++pos, str.indexOf(')'));
  46. } else {
  47. // If at first you don't succeed ...
  48. // If IE knows about the TZ, it appears before the year
  49. // Capital letters or slash before a 4-digit year
  50. // at the end of string
  51. var pat = /([A-Z\/]+) \d{4}$/;
  52. if ((match = str.match(pat))) {
  53. tz = match[1];
  54. } else {
  55. // Some browsers (e.g. Safari) glue the TZ on the end
  56. // of toLocaleString instead of putting it in toString
  57. str = dateObject.toLocaleString();
  58. // Capital letters or slash -- end of string,
  59. // after space
  60. pat = / ([A-Z\/]+)$/;
  61. if ((match = str.match(pat))) {
  62. tz = match[1];
  63. }
  64. }
  65. }
  66. // Make sure it doesn't somehow end up return AM or PM
  67. return (tz == 'AM' || tz == 'PM') ? '' : tz; // String
  68. }
  69. // Utility methods to do arithmetic calculations with Dates
  70. dojo.date.compare = function(/* Date */date1, /* Date? */date2, /* String? */
  71. portion) {
  72. // summary:
  73. // Compare two date objects by date, time, or both.
  74. // description:
  75. // Returns 0 if equal, positive if a > b, else negative.
  76. // date1:
  77. // Date object
  78. // date2:
  79. // Date object. If not specified, the current Date is used.
  80. // portion:
  81. // A string indicating the "date" or "time" portion of a Date object.
  82. // Compares both "date" and "time" by default. One of the following:
  83. // "date", "time", "datetime"
  84. // Extra step required in copy for IE - see #3112
  85. date1 = new Date(Number(date1));
  86. date2 = new Date(Number(date2 || allGetServerTime()));
  87. if (typeof portion !== "undefined") {
  88. if (portion == "date") {
  89. // Ignore times and compare dates.
  90. date1.setHours(0, 0, 0, 0);
  91. date2.setHours(0, 0, 0, 0);
  92. } else if (portion == "time") {
  93. // Ignore dates and compare times.
  94. date1.setFullYear(0, 0, 0);
  95. date2.setFullYear(0, 0, 0);
  96. }
  97. }
  98. if (date1 > date2) {
  99. return 1;
  100. } // int
  101. if (date1 < date2) {
  102. return -1;
  103. } // int
  104. return 0; // int
  105. };
  106. dojo.date.add = function(/* Date */date, /* String */interval, /* int */amount) {
  107. // summary:
  108. // Add to a Date in intervals of different size, from milliseconds to
  109. // years
  110. // date: Date
  111. // Date object to start with
  112. // interval:
  113. // A string representing the interval. One of the following:
  114. // "year", "month", "day", "hour", "minute", "second",
  115. // "millisecond", "quarter", "week", "weekday"
  116. // amount:
  117. // How much to add to the date.
  118. var sum = new Date(Number(date)); // convert to Number before copying
  119. // to accomodate IE (#3112)
  120. var fixOvershoot = false;
  121. var property = "Date";
  122. switch (interval) {
  123. case "day" :
  124. break;
  125. case "weekday" :
  126. // i18n FIXME: assumes Saturday/Sunday weekend, but even this is
  127. // not standard. There are CLDR entries to localize this.
  128. var days, weeks;
  129. var adj = 0;
  130. // Divide the increment time span into weekspans plus leftover
  131. // days
  132. // e.g., 8 days is one 5-day weekspan / and two leftover days
  133. // Can't have zero leftover days, so numbers divisible by 5 get
  134. // a days value of 5, and the remaining days make up the number
  135. // of weeks
  136. var mod = amount % 5;
  137. if (!mod) {
  138. days = (amount > 0) ? 5 : -5;
  139. weeks = (amount > 0)
  140. ? ((amount - 5) / 5)
  141. : ((amount + 5) / 5);
  142. } else {
  143. days = mod;
  144. weeks = parseInt(amount / 5);
  145. }
  146. // Get weekday value for orig date param
  147. var strt = date.getDay();
  148. // Orig date is Sat / positive incrementer
  149. // Jump over Sun
  150. if (strt == 6 && amount > 0) {
  151. adj = 1;
  152. } else if (strt == 0 && amount < 0) {
  153. // Orig date is Sun / negative incrementer
  154. // Jump back over Sat
  155. adj = -1;
  156. }
  157. // Get weekday val for the new date
  158. var trgt = strt + days;
  159. // New date is on Sat or Sun
  160. if (trgt == 0 || trgt == 6) {
  161. adj = (amount > 0) ? 2 : -2;
  162. }
  163. // Increment by number of weeks plus leftover days plus
  164. // weekend adjustments
  165. amount = 7 * weeks + days + adj;
  166. break;
  167. case "year" :
  168. property = "FullYear";
  169. // Keep increment/decrement from 2/29 out of March
  170. fixOvershoot = true;
  171. break;
  172. case "week" :
  173. amount *= 7;
  174. break;
  175. case "quarter" :
  176. // Naive quarter is just three months
  177. amount *= 3;
  178. // fallthrough...
  179. case "month" :
  180. // Reset to last day of month if you overshoot
  181. fixOvershoot = true;
  182. property = "Month";
  183. break;
  184. case "hour" :
  185. case "minute" :
  186. case "second" :
  187. case "millisecond" :
  188. property = "UTC" + interval.charAt(0).toUpperCase()
  189. + interval.substring(1) + "s";
  190. }
  191. if (property) {
  192. sum["set" + property](sum["get" + property]() + amount);
  193. }
  194. if (fixOvershoot && (sum.getDate() < date.getDate())) {
  195. sum.setDate(0);
  196. }
  197. return sum; // Date
  198. };
  199. dojo.date.difference = function(/* Date */date1, /* Date? */date2, /* String? */
  200. interval) {
  201. // summary:
  202. // Get the difference in a specific unit of time (e.g., number of
  203. // months, weeks, days, etc.) between two dates, rounded to the
  204. // nearest integer.
  205. // date1:
  206. // Date object
  207. // date2:
  208. // Date object. If not specified, the current Date is used.
  209. // interval:
  210. // A string representing the interval. One of the following:
  211. // "year", "month", "day", "hour", "minute", "second",
  212. // "millisecond", "quarter", "week", "weekday"
  213. // Defaults to "day".
  214. date2 = date2 || allGetServerTime();
  215. interval = interval || "day";
  216. var yearDiff = date2.getFullYear() - date1.getFullYear();
  217. var delta = 1; // Integer return value
  218. switch (interval) {
  219. case "quarter" :
  220. var m1 = date1.getMonth();
  221. var m2 = date2.getMonth();
  222. // Figure out which quarter the months are in
  223. var q1 = Math.floor(m1 / 3) + 1;
  224. var q2 = Math.floor(m2 / 3) + 1;
  225. // Add quarters for any year difference between the dates
  226. q2 += (yearDiff * 4);
  227. delta = q2 - q1;
  228. break;
  229. case "weekday" :
  230. var days = Math
  231. .round(dojo.date.difference(date1, date2, "day"));
  232. var weeks = parseInt(dojo.date.difference(date1, date2, "week"));
  233. var mod = days % 7;
  234. // Even number of weeks
  235. if (mod == 0) {
  236. days = weeks * 5;
  237. } else {
  238. // Weeks plus spare change (< 7 days)
  239. var adj = 0;
  240. var aDay = date1.getDay();
  241. var bDay = date2.getDay();
  242. weeks = parseInt(days / 7);
  243. mod = days % 7;
  244. // Mark the date advanced by the number of
  245. // round weeks (may be zero)
  246. var dtMark = new Date(date1);
  247. dtMark.setDate(dtMark.getDate() + (weeks * 7));
  248. var dayMark = dtMark.getDay();
  249. // Spare change days -- 6 or less
  250. if (days > 0) {
  251. switch (true) {
  252. // Range starts on Sat
  253. case aDay == 6 :
  254. adj = -1;
  255. break;
  256. // Range starts on Sun
  257. case aDay == 0 :
  258. adj = 0;
  259. break;
  260. // Range ends on Sat
  261. case bDay == 6 :
  262. adj = -1;
  263. break;
  264. // Range ends on Sun
  265. case bDay == 0 :
  266. adj = -2;
  267. break;
  268. // Range contains weekend
  269. case (dayMark + mod) > 5 :
  270. adj = -2;
  271. }
  272. } else if (days < 0) {
  273. switch (true) {
  274. // Range starts on Sat
  275. case aDay == 6 :
  276. adj = 0;
  277. break;
  278. // Range starts on Sun
  279. case aDay == 0 :
  280. adj = 1;
  281. break;
  282. // Range ends on Sat
  283. case bDay == 6 :
  284. adj = 2;
  285. break;
  286. // Range ends on Sun
  287. case bDay == 0 :
  288. adj = 1;
  289. break;
  290. // Range contains weekend
  291. case (dayMark + mod) < 0 :
  292. adj = 2;
  293. }
  294. }
  295. days += adj;
  296. days -= (weeks * 2);
  297. }
  298. delta = days;
  299. break;
  300. case "year" :
  301. delta = yearDiff;
  302. break;
  303. case "month" :
  304. delta = (date2.getMonth() - date1.getMonth()) + (yearDiff * 12);
  305. break;
  306. case "week" :
  307. // Truncate instead of rounding
  308. // Don't use Math.floor -- value may be negative
  309. delta = parseInt(dojo.date.difference(date1, date2, "day") / 7);
  310. break;
  311. case "day" :
  312. delta /= 24;
  313. // fallthrough
  314. case "hour" :
  315. delta /= 60;
  316. // fallthrough
  317. case "minute" :
  318. delta /= 60;
  319. // fallthrough
  320. case "second" :
  321. delta /= 1000;
  322. // fallthrough
  323. case "millisecond" :
  324. delta *= date2.getTime() - date1.getTime();
  325. }
  326. // Round for fractional values and DST leaps
  327. return Math.round(delta); // Number (integer)
  328. };
  329. }