civilTabularCalendar.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. define([ "../impl/Record", "../impl/calendarFunctions"], function (Record, calendarFunctions) {
  2. return {
  3. CIVIL_EPOC : 1948439.5,
  4. ASTRONOMICAL_EPOC : 1948438.5,
  5. GREGORIAN_EPOCH : 1721425.5,
  6. isGregorianLeapYear : function (year) {
  7. // summary:
  8. // Determines if the year is a leap year in the Gregorian calendar.
  9. // description:
  10. // Leap years are years with an additional day YYYY-02-29, where the
  11. // Year number is a multiple of four with the following exception: If
  12. // A year is a multiple of 100, then it is only a leap year if it is
  13. // Also a multiple of 400. For example, 1900 was not a leap year, but
  14. // 2000 is one.
  15. return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
  16. },
  17. getIslamicYearStart : function (/*Number*/ year) {
  18. // summary:
  19. // Return start of Islamic year.
  20. return (year - 1) * 354 + Math.floor((3 + 11 * year) / 30.0);
  21. },
  22. getIslamicMonthStart : function (/*Number*/ year, /*Number*/ month) {
  23. // summary:
  24. // Return the start of Islamic Month.
  25. return Math.ceil(29.5 * month) + (year - 1) * 354 + Math.floor((3 + 11 * year) / 30.0);
  26. },
  27. fromGregorian : function (/*Date*/ gdate, /*String*/ cType) {
  28. // summary:
  29. // This function returns the equivalent islamic(civil/tabular) date value
  30. // for a give input gregorian date.
  31. // gdate: Date
  32. // Gregorian date which will be converted to islamic(civil/tabular) date
  33. // cType: String
  34. // The type of the islamic calendar the gregorain date should converted to.
  35. // The expected values are 'civil' or 'tbla'
  36. // returns:
  37. // Islamic(civil/tabular) date.
  38. var date = new Date(gdate);
  39. var gYear = date.getFullYear(),
  40. gMonth = date.getMonth(),
  41. gDay = date.getDate();
  42. var julianDay = (this.GREGORIAN_EPOCH - 1) + (365 * (gYear - 1)) + Math.floor((gYear - 1) / 4)
  43. + (-Math.floor((gYear - 1) / 100)) + Math.floor((gYear - 1) / 400)
  44. + Math.floor((((367 * (gMonth + 1)) - 362) / 12)
  45. + (((gMonth + 1) <= 2) ? 0 : (this.isGregorianLeapYear(gYear) ? -1 : -2)) + gDay);
  46. julianDay = Math.floor(julianDay) + 0.5;
  47. var days = julianDay - this.CIVIL_EPOC;
  48. if (cType === "tbla") {
  49. days = julianDay - this.ASTRONOMICAL_EPOC;
  50. }
  51. var hYear = Math.floor((30 * days + 10646) / 10631.0);
  52. var hMonth = Math.ceil((days - 29 - this.getIslamicYearStart(hYear)) / 29.5);
  53. hMonth = Math.min(hMonth, 11);
  54. var hDay = Math.ceil(days - this.getIslamicMonthStart(hYear, hMonth) + 1);
  55. this.date = hDay;
  56. this.month = hMonth;
  57. this.year = hYear;
  58. this.hours = date.getHours();
  59. this.minutes = date.getMinutes();
  60. this.seconds = date.getSeconds();
  61. this.milliseconds = date.getMilliseconds();
  62. this.day = date.getDay();
  63. return this;
  64. },
  65. toLocalTime : function (date, timeZone, cType) {
  66. var islamicDate = this.fromGregorian(date, cType);
  67. var dt = new Date(date);
  68. var result = new Record();
  69. result.set("weekday", timeZone === "UTC" ? dt.getUTCDay() : dt.getDay());
  70. result.set("era", 0);
  71. result.set("year", islamicDate.year);
  72. result.set("month", islamicDate.month);
  73. result.set("day", islamicDate.date);
  74. calendarFunctions.setTimeFields(dt, timeZone, result);
  75. return result;
  76. }
  77. };
  78. });