buddhist.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.date.buddhist"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.date.buddhist"] = true;
  8. dojo.provide("dojox.date.buddhist");
  9. dojo.experimental("dojox.date.buddhist");
  10. dojo.require("dojox.date.buddhist.Date");
  11. dojo.require("dojo.date"); // for compare
  12. // Utility methods to do arithmetic calculations with buddhist.Dates
  13. dojox.date.buddhist.getDaysInMonth = function(/*buddhist.Date*/dateObject){
  14. return dojo.date.getDaysInMonth(dateObject.toGregorian());
  15. };
  16. dojox.date.buddhist.isLeapYear = function(/*buddhist.Date*/dateObject){
  17. return dojo.date.isLeapYear(dateObject.toGregorian());
  18. };
  19. //FIXME: reduce compare, add, diff also
  20. dojox.date.buddhist.compare = function(/*buddhist.Date*/date1, /*buddhist.Date*/date2, /*String?*/portion){
  21. // summary:
  22. // Compare two buddhist date objects by date, time, or both.
  23. return dojo.date.compare(date1,date2, portion); //FIXME
  24. };
  25. dojox.date.buddhist.add = function(/*dojox.date.buddhist.Date*/date, /*String*/interval, /*int*/amount){
  26. // based on and similar to dojo.date.add
  27. // summary:
  28. // Add to a Date in intervals of different size, from milliseconds to years
  29. // date: buddhist.Date
  30. // Date object to start with
  31. // interval:
  32. // A string representing the interval. One of the following:
  33. // "year", "month", "day", "hour", "minute", "second",
  34. // "millisecond", "week", "weekday"
  35. // amount:
  36. // How much to add to the date.
  37. var newBuddDate = new dojox.date.buddhist.Date(date);
  38. switch(interval){
  39. case "day":
  40. newBuddDate.setDate(date.getDate(true) + amount);
  41. break;
  42. case "weekday":
  43. var days, weeks;
  44. var mod = amount % 5;
  45. if(!mod){
  46. days = (amount > 0) ? 5 : -5;
  47. weeks = (amount > 0) ? ((amount-5)/5) : ((amount+5)/5);
  48. }else{
  49. days = mod;
  50. weeks = parseInt(amount/5);
  51. }
  52. // Get weekday value for orig date param
  53. var strt = date.getDay();
  54. // Orig date is Sat / positive incrementer
  55. // Jump over Sun
  56. var adj = 0;
  57. if(strt == 6 && amount > 0){
  58. adj = 1;
  59. }else if(strt == 0 && amount < 0){
  60. // Orig date is Sun / negative incrementer
  61. // Jump back over Sat
  62. adj = -1;
  63. }
  64. // Get weekday val for the new date
  65. var trgt = strt + days;
  66. // New date is on Sat or Sun
  67. if(trgt == 0 || trgt == 6){
  68. adj = (amount > 0) ? 2 : -2;
  69. }
  70. // Increment by number of weeks plus leftover days plus
  71. // weekend adjustments
  72. amount = (7 * weeks) + days + adj;
  73. newBuddDate.setDate(date.getDate(true) + amount);
  74. break;
  75. case "year":
  76. newBuddDate.setFullYear(date.getFullYear() + amount );
  77. break;
  78. case "week":
  79. amount *= 7;
  80. newBuddDate.setDate(date.getDate(true) + amount);
  81. break;
  82. case "month":
  83. newBuddDate.setMonth(date.getMonth() + amount);
  84. break;
  85. case "hour":
  86. newBuddDate.setHours(date.getHours() + amount );
  87. break;
  88. case "minute":
  89. newBuddDate.setMinutes(date.getMinutes() + amount );
  90. break;
  91. case "second":
  92. newBuddDate.setSeconds(date.getSeconds() + amount );
  93. break;
  94. case "millisecond":
  95. newBuddDate.setMilliseconds(date.getMilliseconds() + amount );
  96. break;
  97. }
  98. return newBuddDate; // dojox.date.buddhist.Date
  99. };
  100. dojox.date.buddhist.difference = function(/*dojox.date.buddhist.Date*/date1, /*dojox.date.buddhist.Date?*/date2, /*String?*/interval){
  101. // based on and similar to dojo.date.difference
  102. // summary:
  103. // date1 - date2
  104. // date2 is hebrew.Date object. If not specified, the current hebrew.Date is used.
  105. // interval:
  106. // A string representing the interval. One of the following:
  107. // "year", "month", "day", "hour", "minute", "second",
  108. // "millisecond", "week", "weekday"
  109. // Defaults to "day".
  110. date2 = date2 || new dojox.date.buddhist.Date();
  111. interval = interval || "day";
  112. var yearDiff = date1.getFullYear() - date2.getFullYear();
  113. var delta = 1; // Integer return value
  114. switch(interval){
  115. case "weekday":
  116. var days = Math.round(dojox.date.buddhist.difference(date1, date2, "day"));
  117. var weeks = parseInt(dojox.date.buddhist.difference(date1, date2, "week"));
  118. var mod = days % 7;
  119. // Even number of weeks
  120. if(mod == 0){
  121. days = weeks*5;
  122. }else{
  123. // Weeks plus spare change (< 7 days)
  124. var adj = 0;
  125. var aDay = date2.getDay();
  126. var bDay = date1.getDay();
  127. weeks = parseInt(days/7);
  128. mod = days % 7;
  129. // Mark the date advanced by the number of
  130. // round weeks (may be zero)
  131. var dtMark = new dojox.date.buddhist.Date(date1);
  132. dtMark.setDate(dtMark.getDate(true)+(weeks*7));
  133. var dayMark = dtMark.getDay();
  134. // Spare change days -- 6 or less
  135. if(days > 0){
  136. switch(true){
  137. // Range starts on Fri
  138. case aDay == 5:
  139. adj = -1;
  140. break;
  141. // Range starts on Sat
  142. case aDay == 6:
  143. adj = 0;
  144. break;
  145. // Range ends on Fri
  146. case bDay == 5:
  147. adj = -1;
  148. break;
  149. // Range ends on Sat
  150. case bDay == 6:
  151. adj = -2;
  152. break;
  153. // Range contains weekend
  154. case (dayMark + mod) > 5:
  155. adj = -2;
  156. }
  157. }else if(days < 0){
  158. switch(true){
  159. // Range starts on Fri
  160. case aDay == 5:
  161. adj = 0;
  162. break;
  163. // Range starts on Sat
  164. case aDay == 6:
  165. adj = 1;
  166. break;
  167. // Range ends on Fri
  168. case bDay == 5:
  169. adj = 2;
  170. break;
  171. // Range ends on Sat
  172. case bDay == 6:
  173. adj = 1;
  174. break;
  175. // Range contains weekend
  176. case (dayMark + mod) < 0:
  177. adj = 2;
  178. }
  179. }
  180. days += adj;
  181. days -= (weeks*2);
  182. }
  183. delta = days;
  184. break;
  185. case "year":
  186. delta = yearDiff;
  187. break;
  188. case "month":
  189. var startdate = (date1.toGregorian() > date2.toGregorian()) ? date1 : date2; // more
  190. var enddate = (date1.toGregorian() > date2.toGregorian()) ? date2 : date1;
  191. var month1 = startdate.getMonth();
  192. var month2 = enddate.getMonth();
  193. if (yearDiff == 0){
  194. delta = startdate.getMonth() - enddate.getMonth() ;
  195. }else{
  196. delta = 12-month2;
  197. delta += month1;
  198. var i = enddate.getFullYear()+1;
  199. var e = startdate.getFullYear();
  200. for (i; i < e; i++){
  201. delta += 12;
  202. }
  203. }
  204. if (date1.toGregorian() < date2.toGregorian()){
  205. delta = -delta;
  206. }
  207. break;
  208. case "week":
  209. // Truncate instead of rounding
  210. // Don't use Math.floor -- value may be negative
  211. delta = parseInt(dojox.date.buddhist.difference(date1, date2, "day")/7);
  212. break;
  213. case "day":
  214. delta /= 24;
  215. // fallthrough
  216. case "hour":
  217. delta /= 60;
  218. // fallthrough
  219. case "minute":
  220. delta /= 60;
  221. // fallthrough
  222. case "second":
  223. delta /= 1000;
  224. // fallthrough
  225. case "millisecond":
  226. delta *= date1.toGregorian().getTime()- date2.toGregorian().getTime();
  227. }
  228. // Round for fractional values and DST leaps
  229. return Math.round(delta); // Number (integer)
  230. };
  231. }