Date.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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.hebrew.Date"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.date.hebrew.Date"] = true;
  8. dojo.provide("dojox.date.hebrew.Date");
  9. dojo.require("dojox.date.hebrew.numerals");
  10. dojo.declare("dojox.date.hebrew.Date", null, {
  11. // summary: A Date-like object which implements the Hebrew calendar
  12. //
  13. // description:
  14. // A Date-like object which implements the Hebrew Calendar. Because this object
  15. // implements many of the same methods as the native JavaScript Date object, which
  16. // implements the Gregorian calendar, it can often be used its place. Note that
  17. // this object does not extend Date or use its prototype.
  18. //
  19. // example:
  20. // | dojo.require("dojox.date.hebrew.Date");
  21. // |
  22. // | var date = new dojox.date.hebrew.Date();
  23. // | console.log(date.getFullYear()+'\'+date.getMonth()+'\'+date.getDate());
  24. // Hebrew date calculations are performed in terms of days, hours, and
  25. // "parts" (or halakim), which are 1/1080 of an hour, or 3 1/3 seconds.
  26. //_HOUR_PARTS: 1080,
  27. //_DAY_PARTS: 24*1080,
  28. // An approximate value for the length of a lunar month.
  29. // It is used to calculate the approximate year and month of a given
  30. // absolute date.
  31. //_MONTH_FRACT: 12*1080 + 793,
  32. //_MONTH_PARTS: 29*24*1080 + 12*1080 + 793,
  33. // The time of the new moon (in parts) on 1 Tishri, year 1 (the epoch)
  34. // counting from noon on the day before. BAHARAD is an abbreviation of
  35. // Bet (Monday), Hey (5 hours from sunset), Resh-Daled (204).
  36. //_BAHARAD: 11*1080 + 204,
  37. // The Julian day of the Gregorian epoch, that is, January 1, 1 on the
  38. // Gregorian calendar.
  39. //_JAN_1_1_JULIAN_DAY: 1721426,
  40. /**
  41. * The lengths of the Hebrew months. This is complicated, because there
  42. * are three different types of years, or six if you count leap years.
  43. * Due to the rules for postponing the start of the year to avoid having
  44. * certain holidays fall on the sabbath, the year can end up being three
  45. * different lengths, called "deficient", "normal", and "complete".
  46. */
  47. //"Absolute" indexes of months: Tishri - 0, Heshvan - 1, Kislev - 2, Tevet - 3, Shevat - 4, Adar I (leap years only) - 5, Adar - 6, Nisan - 7, Iyar - 8, Sivan - 9, Tammuz-10, Av - 11, Elul - 12.
  48. _MONTH_LENGTH: [
  49. // Deficient Normal Complete
  50. [ 30, 30, 30 ], //Tishri 0
  51. [ 29, 29, 30 ], //Heshvan 1
  52. [ 29, 30, 30 ], //Kislev 2
  53. [ 29, 29, 29 ], //Tevet 3
  54. [ 30, 30, 30 ], //Shevat 4
  55. [ 30, 30, 30 ], //Adar I (leap years only) 5
  56. [ 29, 29, 29 ], //Adar 6
  57. [ 30, 30, 30 ], //Nisan 7
  58. [ 29, 29, 29 ], //Iyar 8
  59. [ 30, 30, 30 ], //Sivan 9
  60. [ 29, 29, 29 ], //Tammuz 10
  61. [ 30, 30, 30 ], //Av 11
  62. [ 29, 29, 29 ] //Elul 12
  63. ],
  64. /**
  65. * The cumulative # of days to the end of each month in a non-leap year
  66. * Although this can be calculated from the MONTH_LENGTH table,
  67. * keeping it around separately makes some calculations a lot faster
  68. */
  69. _MONTH_START: [
  70. // Deficient Normal Complete
  71. [ 0, 0, 0 ], // (placeholder)
  72. [ 30, 30, 30 ], // Tishri
  73. [ 59, 59, 60 ], // Heshvan
  74. [ 88, 89, 90 ], // Kislev
  75. [ 117, 118, 119 ], // Tevet
  76. [ 147, 148, 149 ], // Shevat
  77. [ 147, 148, 149 ], // (Adar I placeholder)
  78. [ 176, 177, 178 ], // Adar
  79. [ 206, 207, 208 ], // Nisan
  80. [ 235, 236, 237 ], // Iyar
  81. [ 265, 266, 267 ], // Sivan
  82. [ 294, 295, 296 ], // Tammuz
  83. [ 324, 325, 326 ], // Av
  84. [ 353, 354, 355 ] // Elul
  85. ],
  86. /**
  87. * The cumulative # of days to the end of each month in a leap year
  88. */
  89. _LEAP_MONTH_START: [
  90. // Deficient Normal Complete
  91. [ 0, 0, 0 ], // (placeholder)
  92. [ 30, 30, 30 ], // Tishri
  93. [ 59, 59, 60 ], // Heshvan
  94. [ 88, 89, 90 ], // Kislev
  95. [ 117, 118, 119 ], // Tevet
  96. [ 147, 148, 149 ], // Shevat
  97. [ 177, 178, 179 ], // Adar I
  98. [ 206, 207, 208 ], // Adar II
  99. [ 236, 237, 238 ], // Nisan
  100. [ 265, 266, 267 ], // Iyar
  101. [ 295, 296, 297 ], // Sivan
  102. [ 324, 325, 326 ], // Tammuz
  103. [ 354, 355, 356 ], // Av
  104. [ 383, 384, 385 ] // Elul
  105. ],
  106. _GREGORIAN_MONTH_COUNT: [
  107. //len len2 st st2
  108. [ 31, 31, 0, 0 ], // Jan
  109. [ 28, 29, 31, 31 ], // Feb
  110. [ 31, 31, 59, 60 ], // Mar
  111. [ 30, 30, 90, 91 ], // Apr
  112. [ 31, 31, 120, 121 ], // May
  113. [ 30, 30, 151, 152 ], // Jun
  114. [ 31, 31, 181, 182 ], // Jul
  115. [ 31, 31, 212, 213 ], // Aug
  116. [ 30, 30, 243, 244 ], // Sep
  117. [ 31, 31, 273, 274 ], // Oct
  118. [ 30, 30, 304, 305 ], // Nov
  119. [ 31, 31, 334, 335 ] // Dec
  120. // len length of month
  121. // len2 length of month in a leap year
  122. // st days in year before start of month
  123. // st2 days in year before month in leap year
  124. ],
  125. _date: 0,
  126. _month: 0,
  127. _year: 0,
  128. _hours: 0,
  129. _minutes: 0,
  130. _seconds: 0,
  131. _milliseconds: 0,
  132. _day: 0,
  133. constructor: function(){
  134. // summary: initialize the date object value
  135. //
  136. // example:
  137. // | var date1 = new dojox.date.hebrew.Date();
  138. // |
  139. // | var date2 = new dojox.date.hebrew.Date(date1);
  140. // |
  141. // | var date3 = new dojox.date.hebrew.Date(5768,2,12);
  142. var len = arguments.length;
  143. if(!len){// use the current date value, added "" to the similarity to date
  144. this.fromGregorian(new Date());
  145. }else if(len == 1){
  146. var arg0 = arguments[0];
  147. if(typeof arg0 == "number"){ // this is time "valueof"
  148. arg0 = new Date(arg0);
  149. }
  150. if(arg0 instanceof Date){
  151. this.fromGregorian(arg0);
  152. }else if(arg0 == ""){
  153. // date should be invalid. Dijit relies on this behavior.
  154. this._year = this._month = this._date = this._hours = this._minutes = this._seconds = this._milliseconds = NaN;
  155. }else{ // this is hebrew.Date object
  156. this._year = arg0._year;
  157. this._month = arg0._month;
  158. this._date = arg0._date;
  159. this._hours = arg0._hours;
  160. this._minutes = arg0._minutes;
  161. this._seconds = arg0._seconds;
  162. this._milliseconds = arg0._milliseconds;
  163. }
  164. }else if(len >= 3){
  165. // YYYY, MM, DD arguments passed, month is from 0-12, "absolute" index of month
  166. this._year += arguments[0];
  167. this._month += arguments[1];
  168. this._date += arguments[2];
  169. if(this._month > 12){
  170. console.warn("the month is incorrect , set 0 " + this._month + " " + this._year );
  171. this._month = 0;
  172. }
  173. this._hours += arguments[3] || 0;
  174. this._minutes += arguments[4] || 0;
  175. this._seconds += arguments[5] || 0;
  176. this._milliseconds += arguments[6] || 0;
  177. }
  178. this._setDay();
  179. },
  180. getDate: function(){
  181. // summary: returns the date value (1 - 30)
  182. //
  183. // example:
  184. // | var date1 = new dojox.date.hebrew.Date();
  185. // |
  186. // | console.log(date1.getDate());
  187. return this._date; // int
  188. },
  189. getDateLocalized: function(/*String?*/locale){
  190. // summary: returns the date value as hebrew numerals for the Hebrew locale,
  191. // a number for all others.
  192. //
  193. // example:
  194. // | var date1 = new dojox.date.hebrew.Date();
  195. // |
  196. // | console.log(date1.getDate());
  197. return (locale || dojo.locale).match(/^he(?:-.+)?$/) ?
  198. dojox.date.hebrew.numerals.getDayHebrewLetters(this._date) : this.getDate();
  199. },
  200. getMonth: function(){
  201. // summary: returns the month value (0 - 12)
  202. //
  203. // description: the result is the index in the month array:
  204. // 0. Tishri
  205. // 1. Heshvan
  206. // 2. Kislev
  207. // 3. Tevet
  208. // 4. Shevat
  209. // 5. Adar I (leap years only)
  210. // 6. Adar
  211. // 7. Nisan
  212. // 8. Iyar
  213. // 9. Sivan
  214. // 10. Tammuz
  215. // 11. Av
  216. // 12. Elul - 12
  217. // For non leap years, for months after Shevat, the actual position of
  218. // the month in the year (used for short format) is less than
  219. // the "absolute" index by 1.
  220. //
  221. // example:
  222. // | var date1 = new dojox.date.hebrew.Date(5769, 6, 1);
  223. // |
  224. // | console.log(date1.getMonth()+1);
  225. // | >> 7
  226. return this._month;
  227. },
  228. getFullYear: function(){
  229. // summary: returns the Year value
  230. //
  231. // example:
  232. // | var date1 = new dojox.date.hebrew.Date(5769, 6, 1);
  233. // |
  234. // | console.log(date1.getFullYear());
  235. // | >> 5769
  236. return this._year;
  237. },
  238. getHours: function(){
  239. //summary: returns the hour value
  240. return this._hours;
  241. },
  242. getMinutes: function(){
  243. //summary: returns the minutes value
  244. return this._minutes;
  245. },
  246. getSeconds: function(){
  247. //summary: returns the seconds value
  248. return this._seconds;
  249. },
  250. getMilliseconds: function(){
  251. //summary: returns the milliseconds value
  252. return this._milliseconds;
  253. },
  254. setDate: function(/*number*/date){
  255. // summary: sets the date number for a given month
  256. // example:
  257. // | var date1 = new dojox.date.hebrew.Date(5769, 6, 1);
  258. // | date1.setDate(2);
  259. date = +date;
  260. var mdays;
  261. if(date>0){
  262. while (date > (mdays = this.getDaysInHebrewMonth(this._month, this._year))){
  263. date -= mdays;
  264. this._month++;
  265. if(this._month >= 13){this._year++; this._month -= 13;}
  266. }
  267. }else{
  268. while(date<=0){
  269. mdays = this.getDaysInHebrewMonth((this._month-1)>=0 ? (this._month-1) : 12, ((this._month-1)>=0)? this._year : this._year-1);
  270. this._month--;
  271. if(this._month < 0){this._year--; this._month += 13;}
  272. date += mdays;
  273. }
  274. }
  275. this._date = date;
  276. this._setDay();
  277. return this;
  278. },
  279. setFullYear: function(/*number*/year, /*number?*/month, /*number?*/ date){
  280. // summary: set the year
  281. //
  282. // example:
  283. // | var date1 = new dojox.date.hebrew.Date();
  284. // | date1.setFullYear(5768);
  285. // | date1.setFullYear(5768, 1, 1);
  286. this._year = year = +year;
  287. if(!this.isLeapYear(year) && this._month==5){ //incorrect month number for non leap year
  288. this._month++;
  289. }
  290. if(month !== undefined){this.setMonth(month);}
  291. if(date !== undefined){this.setDate(date);}
  292. var dnum = this.getDaysInHebrewMonth(this._month, this._year);
  293. if(dnum < this._date){
  294. this._date = dnum;
  295. } // if the date in this month more than number of the days in this month
  296. this._setDay();
  297. return this;
  298. },
  299. setMonth: function(/*number*/month){
  300. // summary: sets the month. You should use "absolute" index in the month array:
  301. // 0. Tishri
  302. // 1. Heshvan
  303. // 2. Kislev
  304. // 3. Tevet
  305. // 4. Shevat
  306. // 5. Adar I (leap years only)
  307. // 6. Adar
  308. // 7. Nisan
  309. // 8. Iyar
  310. // 9. Sivan
  311. // 10. Tammuz
  312. // 11. Av
  313. // 12. Elul - 12
  314. // For non leap years, for months after Shevat, the actual position of
  315. // the month in the year (used for short format) is less than
  316. // the "absolute" index by 1.
  317. //
  318. // example:
  319. // | var date1 = new dojox.date.hebrew.Date();
  320. // | date1.setMonth(0); //first month
  321. month = +month; // coerce to a Number
  322. if(!this.isLeapYear(this._year) && month == 5){month++;}
  323. if(month>=0){
  324. while(month >12){
  325. this._year++;
  326. month -= 13;
  327. if (!this.isLeapYear(this._year) && month >= 5){month++;}
  328. }
  329. }else{
  330. while(month<0){
  331. this._year--;
  332. month += (!this.isLeapYear(this._year) && month < -7) ? 12 : 13;
  333. }
  334. }
  335. this._month = month;
  336. var dnum = this.getDaysInHebrewMonth(this._month, this._year);
  337. if(dnum < this._date){
  338. this._date = dnum;
  339. } // if the date in this month more than number of the days in this month
  340. this._setDay();
  341. return this;
  342. },
  343. setHours: function(){
  344. // summary: sets the hour
  345. //
  346. // description: Sets the hour and optionally minutes, seconds, milliseconds also.
  347. //
  348. // example:
  349. // | var date1 = new dojox.date.hebrew.Date();
  350. // | date1.setHours(12, 30, 0, 0);
  351. var hours_arg_no = arguments.length;
  352. var hours = 0;
  353. if(hours_arg_no >= 1){
  354. hours += +arguments[0];
  355. }
  356. if(hours_arg_no >= 2){
  357. this._minutes += +arguments[1];
  358. }
  359. if(hours_arg_no >= 3){
  360. this._seconds += +arguments[2];
  361. }
  362. if(hours_arg_no == 4){
  363. this._milliseconds += +arguments[3];
  364. }
  365. while(hours >= 24){
  366. this._date++;
  367. var mdays = this.getDaysInHebrewMonth(this._month, this._year);
  368. if(this._date > mdays)
  369. {
  370. this._month++;
  371. if(!this.isLeapYear(this._year) && this._month==5){ this._month++; }
  372. if(this._month >= 13){this._year++; this._month -= 13;}
  373. this._date -= mdays;
  374. }
  375. hours -= 24;
  376. }
  377. this._hours = hours;
  378. this._setDay();
  379. return this;
  380. },
  381. setMinutes: function(/*Number*/minutes){
  382. //summary: sets the minutes (0-59)
  383. minutes = +minutes;
  384. this._minutes = minutes % 60;
  385. this.setHours(parseInt(minutes / 60));
  386. this._setDay();
  387. return this;
  388. },
  389. setSeconds: function(/*Number*/seconds){
  390. //summary: sets the seconds (0-59)
  391. seconds = +seconds;
  392. this._seconds = seconds % 60;
  393. this.setMinutes(parseInt(seconds / 60));
  394. this._setDay();
  395. return this;
  396. },
  397. setMilliseconds: function(/*Number*/milliseconds){
  398. //summary: sets the milliseconds
  399. milliseconds = +milliseconds;
  400. this._milliseconds = milliseconds % 1000;
  401. this.setSeconds(parseInt(milliseconds / 1000));
  402. this._setDay();
  403. return this;
  404. },
  405. _setDay: function(){
  406. var day = this._startOfYear(this._year);
  407. if(this._month != 0){
  408. day += (this.isLeapYear(this._year) ? this._LEAP_MONTH_START : this._MONTH_START)[this._month || 0][this._yearType(this._year)];
  409. }
  410. day += this._date - 1;
  411. this._day = (day+1) % 7;
  412. },
  413. toString: function(){
  414. // summary: returns a string representation of the date in "dd, MM, yyyy HH:mm:ss" format
  415. //
  416. // description: returns a string representation of the date in "dd, MM, yyyy HH:mm:ss" format (all numeric)
  417. // For user presentation, use dojox.date.hebrew.locale.format which will present in the appropriate language
  418. // and format. toString() language- and culturally-specific conventions to keep this module free of
  419. // dependencies on dojox.date.locale and dojo.cldr.
  420. //
  421. // example:
  422. // | var date1 = new dojox.date.hebrew.Date(5769, 6, 1);
  423. // | console.log(date1.toString());
  424. // | >>> "1, 6, 5769 0:0:0"
  425. return this._date + ", " + this._month + ", " + this._year + " " + this._hours + ":" + this._minutes + ":" + this._seconds; // String
  426. },
  427. // ported from the Java class com.ibm.icu.util.HebrewCalendar from ICU4J v3.6.1 at http://www.icu-project.org/
  428. getDaysInHebrewMonth: function(/*Number*/month, /*Number*/ year){
  429. // summary: returns the number of days in the given month and year
  430. // Aside from the leap month, these two months can vary: 1=HESHVAN, 2=KISLEV
  431. // The rest are a fixed length
  432. var yearType = (month == 1 || month == 2) ? this._yearType(year) : 0;
  433. return (!this.isLeapYear(this._year) && month == 5) ? 0 : this._MONTH_LENGTH[month][yearType];
  434. },
  435. // ported from the Java class com.ibm.icu.util.HebrewCalendar from ICU4J v3.6.1 at http://www.icu-project.org/
  436. _yearType: function(/*Number*/year){
  437. var yearLength = this._handleGetYearLength(Number(year));
  438. if(yearLength > 380){
  439. yearLength -= 30; // Subtract length of leap month.
  440. }
  441. var yearType = yearLength - 353;
  442. if (yearType < 0 || yearType > 2){
  443. throw new Error("Illegal year length " + yearLength + " in year " + year);
  444. }
  445. return yearType;
  446. },
  447. // ported from the Java class com.ibm.icu.util.HebrewCalendar from ICU4J v3.6.1 at http://www.icu-project.org/
  448. _handleGetYearLength: function(/*number*/eyear){
  449. return this._startOfYear(eyear+1) - this._startOfYear(eyear);
  450. },
  451. // ported from the Java class com.ibm.icu.util.HebrewCalendar from ICU4J v3.6.1 at http://www.icu-project.org/
  452. _startOfYear: function(/*number*/year){
  453. var months = Math.floor((235 * year - 234) / 19), // # of months before year
  454. frac = months * (12*1080 + 793) + 11*1080 + 204/*BAHARAD*/, // Fractional part of day #
  455. day = months * 29 + Math.floor(frac / (24*1080)); // Whole # part of calculation
  456. frac %= 24*1080; // Time of day
  457. var wd = day % 7; // Day of week (0 == Monday)
  458. if(wd == 2 || wd == 4 || wd == 6){
  459. // If the 1st is on Sun, Wed, or Fri, postpone to the next day
  460. day += 1;
  461. wd = day % 7;
  462. }
  463. if(wd == 1 && frac > 15 * 1080 + 204 && !this.isLeapYear(year)){
  464. // If the new moon falls after 3:11:20am (15h204p from the previous noon)
  465. // on a Tuesday and it is not a leap year, postpone by 2 days.
  466. // This prevents 356-day years.
  467. day += 2;
  468. }else if(wd == 0 && frac > 21 * 1080 + 589 && this.isLeapYear(year-1)){
  469. // If the new moon falls after 9:32:43 1/3am (21h589p from yesterday noon)
  470. // on a Monday and *last* year was a leap year, postpone by 1 day.
  471. // Prevents 382-day years.
  472. day += 1;
  473. }
  474. return day;
  475. },
  476. // ported from the Java class com.ibm.icu.util.HebrewCalendar from ICU4J v3.6.1 at http://www.icu-project.org/
  477. isLeapYear: function(/*Number*/year){
  478. // summary:
  479. // Determines if the year (argument) is a leap year
  480. // description: The Leap year contains additional month adar sheni
  481. //
  482. //return (year * 12 + 17) % 19 >= 12;
  483. var x = (year*12 + 17) % 19;
  484. return x >= ((x < 0) ? -7 : 12);
  485. },
  486. fromGregorian: function(/*Date*/gdate){
  487. // summary: This function sets this Date to the Hebrew Date corresponding to the Gregorian Date
  488. // example:
  489. // | var dateHebrew = new dojox.date.hebrew.Date();
  490. // | var dateGregorian = new Date(2008,10,12);
  491. // | dateHebrew.fromGregorian(dateGregorian);
  492. var result = (!isNaN(gdate)) ? this._computeHebrewFields(gdate) : NaN;
  493. this._year = (!isNaN(gdate)) ? result[0] : NaN;
  494. this._month = (!isNaN(gdate))? result[1] : NaN;
  495. this._date = (!isNaN(gdate)) ? result[2] : NaN;
  496. this._hours = gdate.getHours();
  497. this._milliseconds = gdate.getMilliseconds();
  498. this._minutes = gdate.getMinutes();
  499. this._seconds = gdate.getSeconds();
  500. if (!isNaN(gdate)) this._setDay();
  501. return this;
  502. },
  503. // ported from the Java class com.ibm.icu.util.HebrewCalendar.handleComputeFields from ICU4J v3.6.1 at http://www.icu-project.org/
  504. _computeHebrewFields: function(/*Date*/gdate){
  505. var julianDay = this._getJulianDayFromGregorianDate(gdate),
  506. d = julianDay - 347997,
  507. m = Math.floor((d * 24*1080) / (29*24*1080 + 12*1080 + 793)), // Months (approx)
  508. year = Math.floor((19 * m + 234) / 235) + 1, // Years (approx)
  509. ys = this._startOfYear(year), // 1st day of year
  510. dayOfYear = (d - ys);
  511. // Because of the postponement rules, it's possible to guess wrong. Fix it.
  512. while(dayOfYear < 1){
  513. year--;
  514. ys = this._startOfYear(year);
  515. dayOfYear = d - ys;
  516. }
  517. // Now figure out which month we're in, and the date within that month
  518. var typeofYear = this._yearType(year),
  519. monthStart = this.isLeapYear(year) ? this._LEAP_MONTH_START : this._MONTH_START,
  520. month = 0;
  521. while(dayOfYear > monthStart[month][typeofYear]){
  522. month++;
  523. }
  524. month--;
  525. var dayOfMonth = dayOfYear - monthStart[month][typeofYear];
  526. return [year, month, dayOfMonth];
  527. },
  528. // ported from the Java class com.ibm.icu.util.Calendar.computeGregorianFields from ICU4J v3.6.1 at http://www.icu-project.org/
  529. toGregorian: function(){
  530. // summary: returns the equivalent Grogorian date value as a native Date object
  531. // example:
  532. // | var dateHebrew = new dojox.date.hebrew.Date(5768,11,20);
  533. // | var dateGregorian = dateHebrew.toGregorian();
  534. var hYear = this._year || 0,
  535. hMonth = this._month || 0,
  536. hDate = this._date || 0,
  537. day = this._startOfYear(hYear);
  538. if(hMonth != 0){
  539. day += (this.isLeapYear(hYear) ? this._LEAP_MONTH_START : this._MONTH_START)[hMonth][this._yearType(hYear)];
  540. }
  541. var julianDay = (hDate + day + 347997),
  542. // The Gregorian epoch day is zero for Monday January 1, year 1.
  543. gregorianEpochDay = julianDay - 1721426;
  544. // Here we convert from the day number to the multiple radix
  545. // representation. We use 400-year, 100-year, and 4-year cycles.
  546. // For example, the 4-year cycle has 4 years + 1 leap day; giving
  547. // 1461 == 365*4 + 1 days.
  548. var rem = [];
  549. var n400 = this._floorDivide(gregorianEpochDay , 146097, rem), // 400-year cycle length
  550. n100 = this._floorDivide(rem[0] , 36524, rem), // 100-year cycle length
  551. n4 = this._floorDivide(rem[0] , 1461, rem), // 4-year cycle length
  552. n1 = this._floorDivide(rem[0] , 365, rem),
  553. year = 400*n400 + 100*n100 + 4*n4 + n1,
  554. dayOfYear = rem[0]; // zero-based day of year
  555. if(n100 == 4 || n1 == 4){
  556. dayOfYear = 365; // Dec 31 at end of 4- or 400-yr cycle
  557. }else{
  558. ++year;
  559. }
  560. var isLeap = !(year%4) && // equiv. to (year%4 == 0)
  561. (year%100 || !(year%400)),
  562. correction = 0,
  563. march1 = isLeap ? 60 : 59; // zero-based DOY for March 1
  564. if(dayOfYear >= march1){ correction = isLeap ? 1 : 2; }
  565. var month = Math.floor((12 * (dayOfYear + correction) + 6) / 367); // zero-based month
  566. var dayOfMonth = dayOfYear -
  567. this._GREGORIAN_MONTH_COUNT[month][isLeap ? 3 : 2] + 1; // one-based DOM
  568. return new Date(year, month, dayOfMonth, this._hours, this._minutes, this._seconds, this._milliseconds); // Date
  569. },
  570. _floorDivide: function(numerator, denominator, remainder){
  571. if(numerator >= 0){
  572. remainder[0] = (numerator % denominator);
  573. return Math.floor(numerator / denominator);
  574. }
  575. var quotient = Math.floor(numerator / denominator);
  576. remainder[0] = numerator - (quotient * denominator);
  577. return quotient;
  578. },
  579. getDay: function(){
  580. // summary: returns weekday value (0 - 6)
  581. //
  582. // example:
  583. // | var date1 = new dojox.date.hebrew.Date();
  584. // |
  585. // | console.log(date1.getDay());
  586. var hYear = this._year,
  587. hMonth = this._month,
  588. hDate = this._date,
  589. day = this._startOfYear(hYear);
  590. if(hMonth != 0){
  591. day += (this.isLeapYear(hYear) ? this._LEAP_MONTH_START : this._MONTH_START)[hMonth][this._yearType(hYear)];
  592. }
  593. day += hDate - 1;
  594. return (day+1) % 7;
  595. },
  596. // ported from the Java class com.ibm.icu.util.Calendar.computeGregorianMonthStart from ICU4J v3.6.1 at http://www.icu-project.org/
  597. _getJulianDayFromGregorianDate: function(gdate){
  598. //summary: returns the Julian day of a Gregorian date
  599. var year = gdate.getFullYear(),
  600. month = gdate.getMonth(),
  601. d = gdate.getDate(),
  602. isLeap = !(year%4) && (year%100 || !(year%400)), //TODO: dup
  603. y = year - 1;
  604. // This computation is actually ... + (_JAN_1_1_JULIAN_DAY - 3) + 2.
  605. // Add 2 because Gregorian calendar starts 2 days after Julian
  606. // calendar.
  607. var julianDay = 365*y + Math.floor(y/4) - Math.floor(y/100) +
  608. Math.floor(y/400) + 1721426 - 1;
  609. // At this point julianDay indicates the day BEFORE the first day
  610. // of January 1, <eyear> of the Gregorian calendar.
  611. if(month > 0) {
  612. julianDay += this._GREGORIAN_MONTH_COUNT[month][isLeap ? 3 : 2];
  613. }
  614. julianDay += d;
  615. return julianDay;
  616. }
  617. });
  618. dojox.date.hebrew.Date.prototype.valueOf = function(){
  619. return this.toGregorian().valueOf();
  620. };
  621. }