Date.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. define("dojox/date/islamic/Date", ["dojo/_base/kernel", "dojo/_base/declare", "dojo/date"], function(dojo, declare, dd){
  2. dojo.getObject("date.buddhist.Date", true, dojox);
  3. dojo.experimental("dojox.date.buddhist.Date");
  4. dojo.declare("dojox.date.islamic.Date", null, {
  5. // summary: The component defines the Islamic (Hijri) Calendar Object
  6. //
  7. // description:
  8. // This module is similar to the Date() object provided by JavaScript
  9. //
  10. // example:
  11. // | dojo.require("dojox.date.islamic.Date");
  12. // |
  13. // | var date = new dojox.date.islamic.Date();
  14. // | document.writeln(date.getFullYear()+'\'+date.getMonth()+'\'+date.getDate());
  15. _date: 0,
  16. _month: 0,
  17. _year: 0,
  18. _hours: 0,
  19. _minutes: 0,
  20. _seconds: 0,
  21. _milliseconds: 0,
  22. _day: 0,
  23. _GREGORIAN_EPOCH : 1721425.5,
  24. _ISLAMIC_EPOCH : 1948439.5,
  25. constructor: function(){
  26. // summary: This is the constructor
  27. // description:
  28. // This function initialize the date object values
  29. //
  30. // example:
  31. // | var date1 = new dojox.date.islamic.Date();
  32. // |
  33. // | var date2 = new dojox.date.islamic.Date("12\2\1429");
  34. // |
  35. // | var date3 = new dojox.date.islamic.Date(date2);
  36. // |
  37. // | var date4 = new dojox.date.islamic.Date(1429,2,12);
  38. var len = arguments.length;
  39. if(!len){// use the current date value, added "" to the similarity to date
  40. this.fromGregorian(new Date());
  41. }else if(len == 1){
  42. var arg0 = arguments[0];
  43. if(typeof arg0 == "number"){ // this is time "valueof"
  44. arg0 = new Date(arg0);
  45. }
  46. if(arg0 instanceof Date){
  47. this.fromGregorian(arg0);
  48. }else if(arg0 == ""){
  49. // date should be invalid. Dijit relies on this behavior.
  50. this._date = new Date(""); //TODO: should this be NaN? _date is not a Date object
  51. }else{ // this is Islamic.Date object
  52. this._year = arg0._year;
  53. this._month = arg0._month;
  54. this._date = arg0._date;
  55. this._hours = arg0._hours;
  56. this._minutes = arg0._minutes;
  57. this._seconds = arg0._seconds;
  58. this._milliseconds = arg0._milliseconds;
  59. }
  60. }else if(len >=3){
  61. // YYYY MM DD arguments passed, month is from 0-12
  62. this._year += arguments[0];
  63. this._month += arguments[1];
  64. this._date += arguments[2];
  65. this._hours += arguments[3] || 0;
  66. this._minutes += arguments[4] || 0;
  67. this._seconds += arguments[5] || 0;
  68. this._milliseconds += arguments[6] || 0;
  69. }
  70. },
  71. getDate:function(){
  72. // summary: This function returns the date value (1 - 30)
  73. //
  74. // example:
  75. // | var date1 = new dojox.date.islamic.Date();
  76. // |
  77. // | document.writeln(date1.getDate);
  78. return this._date;
  79. },
  80. getMonth:function(){
  81. // summary: This function return the month value ( 0 - 11 )
  82. //
  83. // example:
  84. // | var date1 = new dojox.date.islamic.Date();
  85. // |
  86. // | document.writeln(date1.getMonth()+1);
  87. return this._month;
  88. },
  89. getFullYear:function(){
  90. // summary: This function return the Year value
  91. //
  92. // example:
  93. // | var date1 = new dojox.date.islamic.Date();
  94. // |
  95. // | document.writeln(date1.getFullYear());
  96. return this._year;
  97. },
  98. getDay:function(){
  99. // summary: This function return Week Day value ( 0 - 6 )
  100. //
  101. // example:
  102. // | var date1 = new dojox.date.islamic.Date();
  103. // |
  104. // | document.writeln(date1.getDay());
  105. return this.toGregorian().getDay();
  106. },
  107. getHours:function(){
  108. //summary: returns the Hour value
  109. return this._hours;
  110. },
  111. getMinutes:function(){
  112. //summary: returns the Minuites value
  113. return this._minutes;
  114. },
  115. getSeconds:function(){
  116. //summary: returns the seconde value
  117. return this._seconds;
  118. },
  119. getMilliseconds:function(){
  120. //summary: returns the Milliseconds value
  121. return this._milliseconds;
  122. },
  123. setDate: function(/*number*/date){
  124. // summary: This function sets the Date
  125. // example:
  126. // | var date1 = new dojox.date.islamic.Date();
  127. // | date1.setDate(2);
  128. date = parseInt(date);
  129. if(date > 0 && date <= this.getDaysInIslamicMonth(this._month, this._year)){
  130. this._date = date;
  131. }else{
  132. var mdays;
  133. if(date>0){
  134. for(mdays = this.getDaysInIslamicMonth(this._month, this._year);
  135. date > mdays;
  136. date -= mdays,mdays =this.getDaysInIslamicMonth(this._month, this._year)){
  137. this._month++;
  138. if(this._month >= 12){this._year++; this._month -= 12;}
  139. }
  140. this._date = date;
  141. }else{
  142. for(mdays = this.getDaysInIslamicMonth((this._month-1)>=0 ?(this._month-1) :11 ,((this._month-1)>=0)? this._year: this._year-1);
  143. date <= 0;
  144. mdays = this.getDaysInIslamicMonth((this._month-1)>=0 ? (this._month-1) :11,((this._month-1)>=0)? this._year: this._year-1)){
  145. this._month--;
  146. if(this._month < 0){this._year--; this._month += 12;}
  147. date+=mdays;
  148. }
  149. this._date = date;
  150. }
  151. }
  152. return this;
  153. },
  154. setFullYear:function(/*number*/year){
  155. // summary: This function set Year
  156. //
  157. // example:
  158. // | var date1 = new dojox.date.islamic.Date();
  159. // | date1.setYear(1429);
  160. this._year = +year;
  161. },
  162. setMonth: function(/*number*/month) {
  163. // summary: This function set Month
  164. //
  165. // example:
  166. // | var date1 = new dojox.date.islamic.Date();
  167. // | date1.setMonth(2);
  168. this._year += Math.floor(month / 12);
  169. if(month > 0){
  170. this._month = Math.floor(month % 12);
  171. }else{
  172. this._month = Math.floor(((month % 12) + 12) % 12);
  173. }
  174. },
  175. setHours:function(){
  176. //summary: set the Hours
  177. var hours_arg_no = arguments.length;
  178. var hours = 0;
  179. if(hours_arg_no >= 1){
  180. hours = parseInt(arguments[0]);
  181. }
  182. if(hours_arg_no >= 2){
  183. this._minutes = parseInt(arguments[1]);
  184. }
  185. if(hours_arg_no >= 3){
  186. this._seconds = parseInt(arguments[2]);
  187. }
  188. if(hours_arg_no == 4){
  189. this._milliseconds = parseInt(arguments[3]);
  190. }
  191. while(hours >= 24){
  192. this._date++;
  193. var mdays = this.getDaysInIslamicMonth(this._month, this._year);
  194. if(this._date > mdays){
  195. this._month ++;
  196. if(this._month >= 12){this._year++; this._month -= 12;}
  197. this._date -= mdays;
  198. }
  199. hours -= 24;
  200. }
  201. this._hours = hours;
  202. },
  203. _addMinutes: function(/*Number*/minutes){
  204. minutes += this._minutes;
  205. this.setMinutes(minutes);
  206. this.setHours(this._hours + parseInt(minutes / 60));
  207. return this;
  208. },
  209. _addSeconds: function(/*Number*/seconds){
  210. seconds += this._seconds;
  211. this.setSeconds(seconds);
  212. this._addMinutes(parseInt(seconds / 60));
  213. return this;
  214. },
  215. _addMilliseconds: function(/*Number*/milliseconds){
  216. milliseconds += this._milliseconds;
  217. this.setMilliseconds(milliseconds);
  218. this._addSeconds(parseInt(milliseconds / 1000));
  219. return this;
  220. },
  221. setMinutes: function(/*Number*/minutes){
  222. //summary: sets the minutes (0-59) only.
  223. this._minutes = minutes % 60;
  224. return this;
  225. },
  226. setSeconds: function(/*Number*/seconds){
  227. //summary: sets the seconds (0-59) only.
  228. this._seconds = seconds % 60;
  229. return this;
  230. },
  231. setMilliseconds: function(/*Number*/milliseconds){
  232. this._milliseconds = milliseconds % 1000;
  233. return this;
  234. },
  235. toString:function(){
  236. // summary: This returns a string representation of the date in "DDDD MMMM DD YYYY HH:MM:SS" format
  237. // example:
  238. // | var date1 = new dojox.date.islamic.Date();
  239. // | document.writeln(date1.toString());
  240. //FIXME: TZ/DST issues?
  241. var x = new Date();
  242. x.setHours(this._hours);
  243. x.setMinutes(this._minutes);
  244. x.setSeconds(this._seconds);
  245. x.setMilliseconds(this._milliseconds);
  246. return this._month+" "+ this._date + " " + this._year + " " + x.toTimeString();
  247. },
  248. toGregorian:function(){
  249. // summary: This returns the equevalent Grogorian date value in Date object
  250. // example:
  251. // | var dateIslamic = new dojox.date.islamic.Date(1429,11,20);
  252. // | var dateGregorian = dateIslamic.toGregorian();
  253. var hYear = this._year;
  254. var hMonth = this._month;
  255. var hDate = this._date;
  256. var julianDay = hDate + Math.ceil(29.5 * hMonth) + (hYear - 1) * 354
  257. + Math.floor((3 + (11 * hYear)) / 30) + this._ISLAMIC_EPOCH - 1;
  258. var wjd = Math.floor(julianDay - 0.5) + 0.5,
  259. depoch = wjd - this._GREGORIAN_EPOCH,
  260. quadricent = Math.floor(depoch / 146097),
  261. dqc = this._mod(depoch, 146097),
  262. cent = Math.floor(dqc / 36524),
  263. dcent = this._mod(dqc, 36524),
  264. quad = Math.floor(dcent / 1461),
  265. dquad = this._mod(dcent, 1461),
  266. yindex = Math.floor(dquad / 365),
  267. year = (quadricent * 400) + (cent * 100) + (quad * 4) + yindex;
  268. if(!(cent == 4 || yindex == 4)){
  269. year++;
  270. }
  271. var gYearStart = this._GREGORIAN_EPOCH + (365 * (year - 1)) + Math.floor((year - 1) / 4)
  272. - ( Math.floor((year - 1) / 100)) + Math.floor((year - 1) / 400);
  273. var yearday = wjd - gYearStart;
  274. var tjd = (this._GREGORIAN_EPOCH - 1) + (365 * (year - 1)) + Math.floor((year - 1) / 4)
  275. -( Math.floor((year - 1) / 100)) + Math.floor((year - 1) / 400) + Math.floor( (739 / 12)
  276. + ( (dd.isLeapYear(new Date(year,3,1)) ? -1 : -2)) + 1);
  277. var leapadj = ((wjd < tjd ) ? 0 : (dd.isLeapYear(new Date(year,3,1)) ? 1 : 2));
  278. var month = Math.floor((((yearday + leapadj) * 12) + 373) / 367);
  279. var tjd2 = (this._GREGORIAN_EPOCH - 1) + (365 * (year - 1))
  280. + Math.floor((year - 1) / 4) - (Math.floor((year - 1) / 100))
  281. + Math.floor((year - 1) / 400) + Math.floor((((367 * month) - 362) / 12)
  282. + ((month <= 2) ? 0 : (dd.isLeapYear(new Date(year,month,1)) ? -1 : -2)) + 1);
  283. var day = (wjd - tjd2) + 1;
  284. var gdate = new Date(year, (month - 1), day, this._hours, this._minutes, this._seconds, this._milliseconds);
  285. return gdate;
  286. },
  287. //TODO: would it make more sense to make this a constructor option? or a static?
  288. // ported from the Java class com.ibm.icu.util.IslamicCalendar from ICU4J v3.6.1 at http://www.icu-project.org/
  289. fromGregorian:function(/*Date*/gdate){
  290. // summary: This function returns the equivalent Islamic Date value for the Gregorian Date
  291. // example:
  292. // | var dateIslamic = new dojox.date.islamic.Date();
  293. // | var dateGregorian = new Date(2008,10,12);
  294. // | dateIslamic.fromGregorian(dateGregorian);
  295. var date = new Date(gdate);
  296. var gYear = date.getFullYear(),
  297. gMonth = date.getMonth(),
  298. gDay = date.getDate();
  299. var julianDay = (this._GREGORIAN_EPOCH - 1) + (365 * (gYear - 1)) + Math.floor((gYear - 1) / 4)
  300. + (-Math.floor((gYear - 1) / 100)) + Math.floor((gYear - 1) / 400)
  301. + Math.floor((((367 * (gMonth+1)) - 362) / 12)
  302. + (((gMonth+1) <= 2) ? 0 : (dd.isLeapYear(date) ? -1 : -2)) + gDay);
  303. julianDay = Math.floor(julianDay) + 0.5;
  304. var days = julianDay - this._ISLAMIC_EPOCH;
  305. var hYear = Math.floor( (30 * days + 10646) / 10631.0 );
  306. var hMonth = Math.ceil((days - 29 - this._yearStart(hYear)) / 29.5 );
  307. hMonth = Math.min(hMonth, 11);
  308. var hDay = Math.ceil(days - this._monthStart(hYear, hMonth)) + 1;
  309. this._date = hDay;
  310. this._month = hMonth;
  311. this._year = hYear;
  312. this._hours = date.getHours();
  313. this._minutes = date.getMinutes();
  314. this._seconds = date.getSeconds();
  315. this._milliseconds = date.getMilliseconds();
  316. this._day = date.getDay();
  317. return this;
  318. },
  319. valueOf:function(){
  320. // summary: This function returns The stored time value in milliseconds
  321. // since midnight, January 1, 1970 UTC
  322. return this.toGregorian().valueOf();
  323. },
  324. // ported from the Java class com.ibm.icu.util.IslamicCalendar from ICU4J v3.6.1 at http://www.icu-project.org/
  325. _yearStart:function(/*Number*/year){
  326. //summary: return start of Islamic year
  327. return (year-1)*354 + Math.floor((3+11*year)/30.0);
  328. },
  329. // ported from the Java class com.ibm.icu.util.IslamicCalendar from ICU4J v3.6.1 at http://www.icu-project.org/
  330. _monthStart:function(/*Number*/year, /*Number*/month){
  331. //summary: return the start of Islamic Month
  332. return Math.ceil(29.5*month) +
  333. (year-1)*354 + Math.floor((3+11*year)/30.0);
  334. },
  335. // ported from the Java class com.ibm.icu.util.IslamicCalendar from ICU4J v3.6.1 at http://www.icu-project.org/
  336. _civilLeapYear:function(/*Number*/year){
  337. //summary: return Boolean value if Islamic leap year
  338. return (14 + 11 * year) % 30 < 11;
  339. },
  340. // ported from the Java class com.ibm.icu.util.IslamicCalendar from ICU4J v3.6.1 at http://www.icu-project.org/
  341. getDaysInIslamicMonth:function(/*Number*/month, /*Number*/ year){
  342. //summary: returns the number of days in the given Islamic Month
  343. var length = 0;
  344. length = 29 + ((month+1) % 2);
  345. if(month == 11 && this._civilLeapYear(year)){
  346. length++;
  347. }
  348. return length;
  349. },
  350. _mod:function(a, b){
  351. return a - (b * Math.floor(a / b));
  352. }
  353. });
  354. //TODOC
  355. dojox.date.islamic.Date.getDaysInIslamicMonth = function(/*dojox.date.islamic.Date*/month){
  356. return new dojox.date.islamic.Date().getDaysInIslamicMonth(month.getMonth(),month.getFullYear()); // dojox.date.islamic.Date
  357. };
  358. return dojox.date.islamic.Date;
  359. });