Date.js 13 KB

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