BadiDate.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  6. var _Astronomy = require('./Astronomy');
  7. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  8. var MillisPerHour = 1000 * 60 * 60;
  9. var MillisPerDay = MillisPerHour * 24;
  10. /**
  11. * Construct a date on the Badi Calendar. Objects of this class just have a
  12. * 'year', 'month' and 'day' field, along with some helper functions and extra
  13. * data (e.g. names of the months)
  14. */
  15. var BadiDate = function () {
  16. _createClass(BadiDate, null, [{
  17. key: 'badiFromGregorianYear',
  18. value: function badiFromGregorianYear(year) {
  19. return year - 1844 + 1;
  20. }
  21. }, {
  22. key: 'gregorianFromBadiYear',
  23. value: function gregorianFromBadiYear(year) {
  24. return year - 1 + 1844;
  25. }
  26. /**
  27. * Takes a Date and location and returns a BadiDate. badi_to_gregorian on the
  28. * returned value should produce an identical date object.
  29. */
  30. }, {
  31. key: 'fromGregorianDate',
  32. value: function fromGregorianDate(gregorianDate, place) {
  33. // TODO: THIS LOGIC SHOULD GET MOVED TO NORMALIZE METHOD!!!
  34. var gregorianYear = gregorianDate.getUTCFullYear();
  35. var gregorianNawRuz = (0, _Astronomy.getUTCDateForNawRuzOnYear)(gregorianYear);
  36. if (gregorianDate < gregorianNawRuz) {
  37. gregorianYear -= 1;
  38. gregorianNawRuz = (0, _Astronomy.getUTCDateForNawRuzOnYear)(gregorianYear);
  39. }
  40. var badiYear = BadiDate.badiFromGregorianYear(gregorianYear);
  41. var daysSinceNawRuz = Math.floor((gregorianDate - gregorianNawRuz) / MillisPerDay);
  42. var hoursAfterSunset = 0;
  43. // Count the days past Naw Ruz (@ 00:00:00 UTC) it is. If the sun
  44. // has already set on this day, we need to add one more day.
  45. if ((0, _Astronomy.getUTCDateForSunsetOnDate)(gregorianDate, place) < gregorianDate) {
  46. daysSinceNawRuz += 1;
  47. var gregorianSunset = (0, _Astronomy.getUTCDateForSunsetOnDate)(gregorianDate, place);
  48. hoursAfterSunset = (gregorianDate - gregorianSunset) / MillisPerHour;
  49. } else {
  50. var previousGregorianDate = new Date(gregorianDate.getTime() - MillisPerDay);
  51. var _gregorianSunset = (0, _Astronomy.getUTCDateForSunsetOnDate)(previousGregorianDate, place);
  52. hoursAfterSunset = (gregorianDate - _gregorianSunset) / MillisPerHour;
  53. }
  54. var month = Math.floor(daysSinceNawRuz / 19);
  55. if (month > 19) {
  56. throw Error('Corrupt date');
  57. }
  58. if (month < 18) {
  59. var day = daysSinceNawRuz % 19 + 1;
  60. return new BadiDate(badiYear, month, day, hoursAfterSunset, place);
  61. }
  62. // month === 18 || month === 19
  63. var daysAfterMulk = daysSinceNawRuz - 18 * 19;
  64. var nextGregorianNawRuz = (0, _Astronomy.getUTCDateForNawRuzOnYear)(gregorianYear + 1);
  65. var daysInYear = (nextGregorianNawRuz - gregorianNawRuz) / MillisPerDay;
  66. var interclaryDays = daysInYear - 19 * 19;
  67. // Check if we are in ayyam-i-ha.
  68. if (daysAfterMulk < interclaryDays) {
  69. return new BadiDate(badiYear, 18, daysAfterMulk + 1, hoursAfterSunset, place);
  70. } else {
  71. return new BadiDate(badiYear, 19, daysAfterMulk + 1 - interclaryDays, hoursAfterSunset, place);
  72. }
  73. }
  74. }]);
  75. function BadiDate(year, month, day, hoursAfterSunset, place) {
  76. _classCallCheck(this, BadiDate);
  77. this._year = year;
  78. this._month = month;
  79. this._day = day;
  80. this._hoursAfterSunset = hoursAfterSunset;
  81. this._place = place;
  82. }
  83. _createClass(BadiDate, [{
  84. key: 'getYear',
  85. value: function getYear() {
  86. return this._year;
  87. }
  88. }, {
  89. key: 'getMonthName',
  90. value: function getMonthName() {
  91. return MonthNames[this._month];
  92. }
  93. }, {
  94. key: 'getMonth',
  95. value: function getMonth() {
  96. return this._month;
  97. }
  98. }, {
  99. key: 'getDay',
  100. value: function getDay() {
  101. return this._day;
  102. }
  103. }, {
  104. key: 'getHoursAfterSunset',
  105. value: function getHoursAfterSunset() {
  106. return this._hoursAfterSunset;
  107. }
  108. }, {
  109. key: 'getPlace',
  110. value: function getPlace() {
  111. return this._place;
  112. }
  113. }, {
  114. key: 'toString',
  115. value: function toString() {
  116. return this._day + ' ' + this.getMonthName() + ' ' + this._year;
  117. }
  118. }, {
  119. key: 'compare',
  120. value: function compare(other) {
  121. return (this._year - other._year) * 400 + (this._month - other._month) * 19 + (this._day - other._day);
  122. }
  123. }, {
  124. key: 'equals',
  125. value: function equals(other) {
  126. return this.compare(other) === 0;
  127. }
  128. /**
  129. * Returns a Date Object (which includes the time). Note that BadiDates
  130. * contain a latitude/longitude, and this latitude/longitude is used to
  131. * determine the corresponding UTC time.
  132. */
  133. }, {
  134. key: 'toGregorianDate',
  135. value: function toGregorianDate() {
  136. if (this.getMonth() > 19) {
  137. throw Error('Corrupt state in BadiDate.getMonth()');
  138. }
  139. // Month 18 is Interclary Days, this is a special case.
  140. if (this.getMonth() < 19) {
  141. var gregorianYear = BadiDate.gregorianFromBadiYear(this.getYear());
  142. var gregorianNawRuz = (0, _Astronomy.getUTCDateForNawRuzOnYear)(gregorianYear);
  143. var _daysToAdd = this.getMonth() * 19 + this.getDay() - 2;
  144. var _gregorianStartOfDay = (0, _Astronomy.incrementGregorianDays)(gregorianNawRuz, _daysToAdd);
  145. var _gregorianSunset2 = (0, _Astronomy.getUTCDateForSunsetOnDate)(_gregorianStartOfDay, this.getPlace());
  146. return (0, _Astronomy.incrementGregorianDays)(_gregorianSunset2, this.getHoursAfterSunset() / 24);
  147. }
  148. // this.getMonth() === 19
  149. var gregorianEnd = BadiDate.gregorianFromBadiYear(this.getYear() + 1);
  150. var nextYearNawRuz = (0, _Astronomy.getUTCDateForNawRuzOnYear)(gregorianEnd);
  151. var daysToAdd = this.getDay() - 19 - 2; // Subtract 1 month.
  152. var gregorianStartOfDay = new Date(nextYearNawRuz.getTime() + daysToAdd * MillisPerDay);
  153. var gregorianSunset = (0, _Astronomy.getUTCDateForSunsetOnDate)(gregorianStartOfDay, this.getPlace());
  154. return (0, _Astronomy.incrementGregorianDays)(gregorianSunset, this.getHoursAfterSunset() / 24);
  155. }
  156. }]);
  157. return BadiDate;
  158. }();
  159. exports.default = BadiDate;
  160. var MonthNames = ['Bahá', 'Jalál', 'Jamál', '‘Aẓamat', 'Núr', 'Raḥmat', 'Kalimát', 'Kamál', 'Asmá’', '‘Izzat', 'Mashíyyat', '‘Ilm', 'Qudrat', 'Qawl', 'Masá’il', 'Sharaf', 'Sulṭán', 'Mulk', 'Ayyám-i-Há', '‘Alá’'];