localBadiDate-msm.js 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765
  1. /**
  2. * @license BadiDate v3.0.2
  3. * (c) 2018 Jan Greis
  4. * licensed under MIT
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('luxon')) :
  8. typeof define === 'function' && define.amd ? define(['exports', 'luxon'], factory) :
  9. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.window = global.window || {}, global.luxon));
  10. }(this, (function (exports, luxon) { 'use strict';
  11. /**
  12. * @license MeeusSunMoon v3.0.0
  13. * (c) 2018 Jan Greis
  14. * licensed under MIT
  15. */
  16. /**
  17. * Converts angles in degrees to radians.
  18. * @param {number} deg Angle in degrees.
  19. * @returns {number} Angle in radians.
  20. */
  21. const deg2rad = (deg) => deg * 0.017453292519943295;
  22. /**
  23. * Converts angles in radians to degrees.
  24. * @param {number} rad Angle in radians.
  25. * @returns {number} Angle in degrees.
  26. */
  27. const rad2deg = (rad) => rad * 57.29577951308232;
  28. /**
  29. * Calculates the sine of an angle given in degrees.
  30. * @param {number} deg Angle in degrees.
  31. * @returns {number} Sine of the angle.
  32. */
  33. const sind = (deg) => Math.sin(deg2rad(deg));
  34. /**
  35. * Calculates the cosine of an angle given in degrees.
  36. * @param {number} deg Angle in degrees.
  37. * @returns {number} Cosine of the angle.
  38. */
  39. const cosd = (deg) => Math.cos(deg2rad(deg));
  40. /**
  41. * Reduces an angle to the interval 0-360°.
  42. * @param {number} angle Angle in degrees.
  43. * @returns {number} Reduced angle in degrees.
  44. */
  45. const reduceAngle = (angle) => angle - (360 * Math.floor(angle / 360));
  46. /**
  47. * Evaluates a polynomial in the form A + Bx + Cx^2...
  48. * @param {number} variable Value of x in the polynomial.
  49. * @param {array} coeffs Array of coefficients [A, B, C...].
  50. * @returns {number} Sum of the polynomial.
  51. */
  52. const polynomial = (variable, coeffs) => {
  53. let varPower = 1;
  54. let sum = 0.0;
  55. const numCoeffs = coeffs.length;
  56. for (let i = 0; i < numCoeffs; i++) {
  57. sum += varPower * coeffs[i];
  58. varPower *= variable;
  59. }
  60. return sum;
  61. };
  62. /**
  63. * Interpolates a value from 3 known values (see AA p24 Eq3.3).
  64. * @param {number} y1 Start value of the interval.
  65. * @param {number} y2 Middle value of the interval.
  66. * @param {number} y3 End value of the interval.
  67. * @param {number} n Location (-0.5 >= n >= 0.5) of result in the interval.
  68. * @param {boolean} normalize Whether the final result should be normalized.
  69. * @returns {number} Interpolated result.
  70. */
  71. const interpolateFromThree = (y1, y2, y3, n, normalize = false) => {
  72. let a = y2 - y1;
  73. let b = y3 - y2;
  74. if (typeof normalize !== 'undefined' && normalize) {
  75. if (a < 0) {
  76. a += 360;
  77. }
  78. if (b < 0) {
  79. b += 360;
  80. }
  81. }
  82. const c = b - a;
  83. return y2 + (n / 2) * (a + b + n * c);
  84. };
  85. /**
  86. * Converts a datetime in UTC to the corresponding Julian Date (see AA p60f).
  87. * @param {DateTime} datetime Datetime to be converted.
  88. * @returns {number} Julian date (fractional number of days since 1 January
  89. * 4713BC according to the proleptic Julian calendar.
  90. */
  91. const datetimeToJD = (datetime) => {
  92. let Y = datetime.year;
  93. let M = datetime.month;
  94. const D = datetime.day + (datetime.hour + (datetime.minute + datetime.second / 60) / 60) / 24;
  95. if (M < 3) {
  96. Y -= 1;
  97. M += 12;
  98. }
  99. const A = Math.floor(Y / 100);
  100. // Need a different B if we are before introduction of the Gregorian Calendar
  101. const gregorianCutoff = luxon.DateTime.fromISO('1582-10-15T12:00:00Z', { zone: 'UTC' });
  102. let B = 0;
  103. if (datetime > gregorianCutoff) {
  104. B = 2 - A + Math.floor(A / 4);
  105. }
  106. return Math.floor(365.25 * (Y + 4716)) + Math.floor(30.6001 * (M + 1)) + D + B - 1524.5;
  107. };
  108. /**
  109. * Converts a Julian date to the number of Julian centuries since
  110. * 2000-01-01T12:00:00Z (see AA p87 Eq12.1).
  111. * @param {number} JD Julian date.
  112. * @returns {number} T.
  113. */
  114. const JDToT = (JD) => (JD - 2451545) / 36525;
  115. /**
  116. * Converts a datetime in UTC to the number of Julian centuries since
  117. * 2000-01-01T12:00:00Z.
  118. * @param {DateTime} datetime Datetime to be converted.
  119. * @returns {number} T.
  120. */
  121. const datetimeToT = (datetime) => JDToT(datetimeToJD(datetime));
  122. /* eslint-disable complexity */
  123. /**
  124. * Calculates the value of ΔT=TT−UT (see
  125. * http://eclipse.gsfc.nasa.gov/SEcat5/deltatpoly.html).
  126. * @param {DateTime} datetime Datetime for which ΔT should be calculated.
  127. * @returns {number} ΔT.
  128. */
  129. const DeltaT = (datetime) => {
  130. let y = datetime.year;
  131. y += (datetime.month - 0.5) / 12;
  132. let u;
  133. let t;
  134. switch (true) {
  135. case y < -1999 || y > 3000:
  136. throw 'DeltaT can only be calculated between 1999 BCE and 3000 CE';
  137. case y < -500:
  138. u = (y - 1820) / 100;
  139. return -20 + 32 * Math.pow(u, 2);
  140. case y < 500:
  141. u = y / 100;
  142. return polynomial(u, [10583.6, -1014.41, 33.78311, -5.952053, -0.1798452, 0.022174192, 0.0090316521]);
  143. case y < 1600:
  144. u = (y - 1000) / 100;
  145. return polynomial(u, [1574.2, -556.01, 71.23472, 0.319781, -0.8503463, -0.005050998, 0.0083572073]);
  146. case y < 1700:
  147. t = y - 1600;
  148. return polynomial(t, [120, -0.9808, -0.01532, 1 / 7129]);
  149. case y < 1800:
  150. t = y - 1700;
  151. return polynomial(t, [8.83, 0.1603, -0.0059285, 0.00013336, -1 / 1174000]);
  152. case y < 1860:
  153. t = y - 1800;
  154. return polynomial(t, [13.72, -0.332447, 0.0068612, 0.0041116, -0.00037436, 0.0000121272, -0.0000001699, 0.000000000875]);
  155. case y < 1900:
  156. t = y - 1860;
  157. return polynomial(t, [7.62, 0.5737, -0.251754, 0.01680668, -0.0004473624, 1 / 233174]);
  158. case y < 1920:
  159. t = y - 1900;
  160. return polynomial(t, [-2.79, 1.494119, -0.0598939, 0.0061966, -0.000197]);
  161. case y < 1941:
  162. t = y - 1920;
  163. return polynomial(t, [21.20, 0.84493, -0.076100, 0.0020936]);
  164. case y < 1961:
  165. t = y - 1950;
  166. return polynomial(t, [29.07, 0.407, -1 / 233, 1 / 2547]);
  167. case y < 1986:
  168. t = y - 1975;
  169. return polynomial(t, [45.45, 1.067, -1 / 260, -1 / 718]);
  170. case y < 2005:
  171. t = y - 2000;
  172. return polynomial(t, [63.86, 0.3345, -0.060374, 0.0017275, 0.000651814, 0.00002373599]);
  173. case y < 2050:
  174. t = y - 2000;
  175. return polynomial(t, [62.92, 0.32217, 0.005589]);
  176. case y < 2150:
  177. return -20 + 32 * Math.pow(((y - 1820) / 100), 2) - 0.5628 * (2150 - y);
  178. default:
  179. u = (y - 1820) / 100;
  180. return -20 + 32 * Math.pow(u, 2);
  181. }
  182. };
  183. let roundToNearestMinute = false;
  184. let returnTimeForNoEventCase = false;
  185. let dateFormatKeys = {
  186. SUN_HIGH: '‡',
  187. SUN_LOW: '†',
  188. };
  189. const settings = (settings) => {
  190. if (typeof settings.roundToNearestMinute === 'boolean') {
  191. roundToNearestMinute = settings.roundToNearestMinute;
  192. }
  193. if (typeof settings.returnTimeForNoEventCase === 'boolean') {
  194. returnTimeForNoEventCase = settings.returnTimeForNoEventCase;
  195. }
  196. if (typeof settings.dateFormatKeys === 'object') {
  197. dateFormatKeys = settings.dateFormatKeys;
  198. }
  199. };
  200. /** See AA p144 */
  201. const sunMeanAnomaly = [357.52772, 35999.050340, -0.0001603, -1 / 300000];
  202. /** See AA p163 Eq 25.2 */
  203. const sunMeanLongitude = [280.46646, 36000.76983, 0.0003032];
  204. /** See AA p147 Eq22.3 */
  205. const meanObliquityOfEcliptic = [84381.448 / 3600, -4680.93 / 3600, -1.55 / 3600, 1999.25 / 3600, -51.38 / 3600, -249.67 / 3600, -39.05 / 3600,
  206. 7.12 / 3600, 27.87 / 3600, 5.79 / 3600, 2.45 / 3600];
  207. /** See AA p144 */
  208. const moonArgumentOfLatitude = [93.27191, 483202.017538, -0.0036825, 1 / 327270];
  209. /** See AA p144 */
  210. const moonAscendingNodeLongitude = [125.04452, -1934.136261, 0.0020708, 1 / 450000];
  211. /** See AA p144 */
  212. const moonMeanAnomaly = [134.96298, 477198.867398, 0.0086972, 1 / 56250];
  213. /** See AA p144 */
  214. const moonMeanElongation = [297.85036, 445267.111480, -0.0019142, 1 / 189474];
  215. /* eslint-disable no-multi-spaces, array-bracket-spacing */
  216. /**
  217. * Nutations in longitude and obliquity
  218. * See AA p145f
  219. */
  220. const nutations = [
  221. [0, 0, 0, 0, 1, -171996, -174.2, 92025, 8.9],
  222. [-2, 0, 0, 2, 2, -13187, -1.6, 5736, -3.1],
  223. [0, 0, 0, 2, 2, -2274, -0.2, 977, -0.5],
  224. [0, 0, 0, 0, 2, 2062, 0.2, -895, 0.5],
  225. [0, 1, 0, 0, 0, 1426, -3.4, 54, -0.1],
  226. [0, 0, 1, 0, 0, 712, 0.1, -7, 0],
  227. [-2, 1, 0, 2, 2, -517, 1.2, 224, -0.6],
  228. [0, 0, 0, 2, 1, -386, -0.4, 200, 0],
  229. [0, 0, 1, 2, 2, -301, 0, 129, -0.1],
  230. [-2, -1, 0, 2, 2, 217, -0.5, -95, 0.3],
  231. [-2, 0, 1, 0, 0, -158, 0, 0, 0],
  232. [-2, 0, 0, 2, 1, 129, 0.1, -70, 0],
  233. [0, 0, -1, 2, 2, 123, 0, -53, 0],
  234. [2, 0, 0, 0, 0, 63, 0, 0, 0],
  235. [0, 0, 1, 0, 1, 63, 0.1, -33, 0],
  236. [2, 0, -1, 2, 2, -59, 0, 26, 0],
  237. [0, 0, -1, 0, 1, -58, -0.1, 32, 0],
  238. [0, 0, 1, 2, 1, -51, 0, 27, 0],
  239. [-2, 0, 2, 0, 0, 48, 0, 0, 0],
  240. [0, 0, -2, 2, 1, 46, 0, -24, 0],
  241. [2, 0, 0, 2, 2, -38, 0, 16, 0],
  242. [0, 0, 2, 2, 2, -31, 0, 13, 0],
  243. [0, 0, 2, 0, 0, 29, 0, 0, 0],
  244. [-2, 0, 1, 2, 2, 29, 0, -12, 0],
  245. [0, 0, 0, 2, 0, 26, 0, 0, 0],
  246. [-2, 0, 0, 2, 0, -22, 0, 0, 0],
  247. [0, 0, -1, 2, 1, 21, 0, -10, 0],
  248. [0, 2, 0, 0, 0, 17, -0.1, 0, 0],
  249. [2, 0, -1, 0, 1, 16, 0, -8, 0],
  250. [-2, 2, 0, 2, 2, -16, 0.1, 7, 0],
  251. [0, 1, 0, 0, 1, -15, 0, 9, 0],
  252. [-2, 0, 1, 0, 1, -13, 0, 7, 0],
  253. [0, -1, 0, 0, 1, -12, 0, 6, 0],
  254. [0, 0, 2, -2, 0, 11, 0, 0, 0],
  255. [2, 0, -1, 2, 1, -10, 0, 5, 0],
  256. [2, 0, 1, 2, 2, -8, 0, 3, 0],
  257. [0, 1, 0, 2, 2, 7, 0, -3, 0],
  258. [-2, 1, 1, 0, 0, -7, 0, 0, 0],
  259. [0, -1, 0, 2, 2, -7, 0, 3, 0],
  260. [2, 0, 0, 2, 1, -7, 0, 3, 0],
  261. [2, 0, 1, 0, 0, 6, 0, 0, 0],
  262. [-2, 0, 2, 2, 2, 6, 0, -3, 0],
  263. [-2, 0, 1, 2, 1, 6, 0, -3, 0],
  264. [2, 0, -2, 0, 1, -6, 0, 3, 0],
  265. [2, 0, 0, 0, 1, -6, 0, 3, 0],
  266. [0, -1, 1, 0, 0, 5, 0, 0, 0],
  267. [-2, -1, 0, 2, 1, -5, 0, 3, 0],
  268. [-2, 0, 0, 0, 1, -5, 0, 3, 0],
  269. [0, 0, 2, 2, 1, -5, 0, 3, 0],
  270. [-2, 0, 2, 0, 1, 4, 0, 0, 0],
  271. [-2, 1, 0, 2, 1, 4, 0, 0, 0],
  272. [0, 0, 1, -2, 0, 4, 0, 0, 0],
  273. [-1, 0, 1, 0, 0, -4, 0, 0, 0],
  274. [-2, 1, 0, 0, 0, -4, 0, 0, 0],
  275. [1, 0, 0, 0, 0, -4, 0, 0, 0],
  276. [0, 0, 1, 2, 0, 3, 0, 0, 0],
  277. [0, 0, -2, 2, 2, -3, 0, 0, 0],
  278. [-1, -1, 1, 0, 0, -3, 0, 0, 0],
  279. [0, 1, 1, 0, 0, -3, 0, 0, 0],
  280. [0, -1, 1, 2, 2, -3, 0, 0, 0],
  281. [2, -1, -1, 2, 2, -3, 0, 0, 0],
  282. [0, 0, 3, 2, 2, 3, 0, 0, 0],
  283. [2, -1, 0, 2, 2, -3, 0, 0, 0],
  284. ];
  285. /**
  286. * Calculates the solar transit time on a date at a given longitude (see AA
  287. * p102f).
  288. * @param {DateTime} datetime Date for which transit is calculated.
  289. * @param {number} L Longitude.
  290. * @returns {DateTime} Solar transit time.
  291. */
  292. const sunTransit = (datetime, L) => {
  293. const timezone = datetime.zone;
  294. let transit = datetime.set({ hour: 0, minute: 0, second: 0, millisecond: 0 })
  295. .setZone('UTC', { keepLocalTime: true });
  296. const deltaT = DeltaT(transit);
  297. const T = datetimeToT(transit);
  298. const Theta0 = apparentSiderealTimeGreenwich(T);
  299. // Want 0h TD for this, not UT
  300. const TD = T - (deltaT / (3600 * 24 * 36525));
  301. const alpha = sunApparentRightAscension(TD);
  302. // Sign flip for longitude from AA as we take East as positive
  303. let m = (alpha - L - Theta0) / 360;
  304. m = normalizeM(m, datetime.offset);
  305. const DeltaM = sunTransitCorrection(T, Theta0, deltaT, L, m);
  306. m += DeltaM;
  307. transit = transit.plus({ seconds: Math.floor(m * 3600 * 24 + 0.5) });
  308. if (roundToNearestMinute) {
  309. transit = transit.plus({ seconds: 30 }).set({ second: 0 });
  310. }
  311. return transit.setZone(timezone);
  312. };
  313. /**
  314. * Calculates the sunrise or sunset time on a date at a given latitude and
  315. * longitude (see AA p102f).
  316. * @param {DateTime} datetime Date for which sunrise or sunset is calculated.
  317. * @param {number} phi Latitude.
  318. * @param {number} L Longitude.
  319. * @param {string} flag 'RISE' or 'SET' depending on which event should be
  320. * calculated.
  321. * @param {number} offset number of degrees below the horizon for the desired
  322. * event (50/60 for sunrise/set, 6 for civil, 12 for nautical, 18 for
  323. * astronomical dawn/dusk.
  324. * @returns {DateTime} Sunrise or sunset time.
  325. */
  326. // eslint-disable-next-line complexity,require-jsdoc
  327. const sunRiseSet = (datetime, phi, L, flag, offset = 50 / 60) => {
  328. const timezone = datetime.zone;
  329. let suntime = datetime.set({ hour: 0, minute: 0, second: 0, millisecond: 0 })
  330. .setZone('UTC', { keepLocalTime: true });
  331. const deltaT = DeltaT(suntime);
  332. const T = datetimeToT(suntime);
  333. const Theta0 = apparentSiderealTimeGreenwich(T);
  334. // Want 0h TD for this, not UT
  335. const TD = T - (deltaT / (3600 * 24 * 36525));
  336. const alpha = sunApparentRightAscension(TD);
  337. const delta = sunApparentDeclination(TD);
  338. const H0 = approxLocalHourAngle(phi, delta, offset);
  339. // Sign flip for longitude from AA as we take East as positive
  340. let m0 = (alpha - L - Theta0) / 360;
  341. m0 = normalizeM(m0, datetime.offset);
  342. let m;
  343. if (flag === 'RISE') {
  344. m = m0 - H0 / 360;
  345. }
  346. else {
  347. m = m0 + H0 / 360;
  348. }
  349. let counter = 0;
  350. let DeltaM = 1;
  351. // Repeat if correction is larger than ~9s
  352. while ((Math.abs(DeltaM) > 0.0001) && (counter < 3)) {
  353. DeltaM = sunRiseSetCorrection(T, Theta0, deltaT, phi, L, m, offset);
  354. m += DeltaM;
  355. counter++;
  356. }
  357. if (m > 0) {
  358. suntime = suntime.plus({ seconds: Math.floor(m * 3600 * 24 + 0.5) });
  359. }
  360. else {
  361. suntime = suntime.minus({ seconds: Math.floor(m * 3600 * 24 + 0.5) });
  362. }
  363. if (roundToNearestMinute) {
  364. suntime = suntime.plus({ seconds: 30 }).set({ second: 0 });
  365. }
  366. return suntime.setZone(timezone);
  367. };
  368. /**
  369. * Returns a fixed time as given by the hour parameter, an hour later during DST) if the
  370. * specified event does not occur on the date and returnTimeForNoEventCase is true. If
  371. * false, return whether the reason for no event is the sun being too high ('SUN_HIGH')
  372. * or too low ('SUN_LOW').
  373. * @param {DateTime} date The original date from which the event was calculated.
  374. * @param {string|undefined} errorCode The error code in case no event was found
  375. * @param {number} hour Hour to which the returned datetime should be set.
  376. * @param {number} minute Minute to which the returned datetime should be set.
  377. * @returns {(DateTime|string)} Time given by parameter 'hour' (+ correction for
  378. * DST if applicable) or a string indicating why there was no event ('SUN_HIGH'
  379. * or 'SUN_LOW')
  380. */
  381. const handleNoEventCase = (date, errorCode, hour, minute = 0) => {
  382. if (returnTimeForNoEventCase) {
  383. const returnDate = date.set({ hour, minute, second: 0 }).plus({ minutes: date.isInDST ? 60 : 0 });
  384. returnDate.errorCode = errorCode;
  385. return returnDate;
  386. }
  387. return errorCode;
  388. };
  389. /**
  390. * Calculates the approximate local hour angle of the sun at sunrise or sunset.
  391. * @param {number} phi Latitude (see AA p102 Eq15.1).
  392. * @param {number} delta Apparent declination of the sun.
  393. * @param {number} offset number of degrees below the horizon for the desired
  394. * event (50/60 for sunrise/set, 6 for civil, 12 for nautical, 18 for
  395. * astronomical dawn/dusk.
  396. * @returns {number} Approximate local hour angle.
  397. */
  398. const approxLocalHourAngle = (phi, delta, offset) => {
  399. const cosH0 = (sind(-offset) -
  400. sind(phi) * sind(delta)) /
  401. (cosd(phi) * cosd(delta));
  402. if (cosH0 < -1) {
  403. throw noEventCodes.SUN_HIGH;
  404. }
  405. else if (cosH0 > 1) {
  406. throw noEventCodes.SUN_LOW;
  407. }
  408. return rad2deg(Math.acos(cosH0));
  409. };
  410. /**
  411. * Normalizes a fractional time of day to be on the correct date.
  412. * @param {number} m Fractional time of day
  413. * @param {number} utcOffset Offset in minutes from UTC.
  414. * @returns {number} m Normalized m.
  415. */
  416. const normalizeM = (m, utcOffset) => {
  417. const localM = m + utcOffset / 1440;
  418. if (localM < 0) {
  419. return m + 1;
  420. }
  421. else if (localM > 1) {
  422. return m - 1;
  423. }
  424. return m;
  425. };
  426. /**
  427. * Calculates the correction for the solar transit time (see AA p103).
  428. * @param {number} T Fractional number of Julian centuries since
  429. * 2000-01-01T12:00:00Z.
  430. * @param {number} Theta0 Apparent sidereal time at Greenwich.
  431. * @param {number} deltaT ΔT = TT − UT.
  432. * @param {number} L Longitude.
  433. * @param {number} m Fractional time of day of the event.
  434. * @returns {number} Currection for the solar transit time.
  435. */
  436. const sunTransitCorrection = (T, Theta0, deltaT, L, m) => {
  437. const theta0 = Theta0 + 360.985647 * m;
  438. const n = m + deltaT / 864000;
  439. const alpha = interpolatedRa(T, n);
  440. const H = localHourAngle(theta0, L, alpha);
  441. return -H / 360;
  442. };
  443. /**
  444. * Calculates the correction for the sunrise/sunset time (see AA p103).
  445. * @param {number} T Fractional number of Julian centuries since
  446. * 2000-01-01T12:00:00Z.
  447. * @param {number} Theta0 Apparent sidereal time at Greenwich.
  448. * @param {number} deltaT ΔT = TT − UT.
  449. * @param {number} phi Latitude.
  450. * @param {number} L Longitude.
  451. * @param {number} m Fractional time of day of the event.
  452. * @param {number} offset number of degrees below the horizon for the desired
  453. * event (50/60 for sunrise/set, 6 for civil, 12 for nautical, 18 for
  454. * astronomical dawn/dusk.
  455. * @returns {number} Correction for the sunrise/sunset time.
  456. */
  457. const sunRiseSetCorrection = (T, Theta0, deltaT, phi, L, m, offset) => {
  458. const theta0 = Theta0 + 360.985647 * m;
  459. const n = m + deltaT / 864000;
  460. const alpha = interpolatedRa(T, n);
  461. const delta = interpolatedDec(T, n);
  462. const H = localHourAngle(theta0, L, alpha);
  463. const h = altitude(phi, delta, H);
  464. return (h + offset) / (360 * cosd(delta) * cosd(phi) * sind(H));
  465. };
  466. /**
  467. * Calculates the local hour angle of the sun (see AA p103).
  468. * @param {number} theta0 Sidereal time at Greenwich in degrees.
  469. * @param {number} L Longitude.
  470. * @param {number} alpha Apparent right ascension of the sun.
  471. * @returns {number} Local hour angle of the sun.
  472. */
  473. const localHourAngle = (theta0, L, alpha) => {
  474. // Sign flip for longitude
  475. let H = reduceAngle(theta0 + L - alpha);
  476. if (H > 180) {
  477. H -= 360;
  478. }
  479. return H;
  480. };
  481. /**
  482. * Calculates the altitude of the sun above the horizon (see AA P93 Eq13.6).
  483. * @param {number} phi Latitude.
  484. * @param {number} delta Apparent declination of the sun.
  485. * @param {number} H Local hour angle of the sun.
  486. * @returns {number} Altitude of the sun above the horizon.
  487. */
  488. const altitude = (phi, delta, H) => rad2deg(Math.asin(sind(phi) * sind(delta) + cosd(phi) * cosd(delta) * cosd(H)));
  489. /**
  490. * Interpolates the sun's right ascension (see AA p103).
  491. * @param {number} T Fractional number of Julian centuries since
  492. * 2000-01-01T12:00:00Z.
  493. * @param {number} n Fractional time of day of the event corrected by ΔT.
  494. * @returns {number} Interpolated right ascension.
  495. */
  496. const interpolatedRa = (T, n) => {
  497. const alpha1 = sunApparentRightAscension(T - (1 / 36525));
  498. const alpha2 = sunApparentRightAscension(T);
  499. const alpha3 = sunApparentRightAscension(T + (1 / 36525));
  500. const alpha = interpolateFromThree(alpha1, alpha2, alpha3, n, true);
  501. return reduceAngle(alpha);
  502. };
  503. /**
  504. * Interpolates the sun's declination (see AA p103).
  505. * @param {number} T Fractional number of Julian centuries since
  506. * 2000-01-01T12:00:00Z.
  507. * @param {number} n Fractional time of day of the event corrected by ΔT.
  508. * @returns {number} Interpolated declination.
  509. */
  510. const interpolatedDec = (T, n) => {
  511. const delta1 = sunApparentDeclination(T - (1 / 36525));
  512. const delta2 = sunApparentDeclination(T);
  513. const delta3 = sunApparentDeclination(T + (1 / 36525));
  514. const delta = interpolateFromThree(delta1, delta2, delta3, n);
  515. return reduceAngle(delta);
  516. };
  517. /**
  518. * Calculates the apparent right ascension of the sun (see AA p165 Eq25.6).
  519. * @param {number} T Fractional number of Julian centuries since
  520. * 2000-01-01T12:00:00Z.
  521. * @returns {number} Apparent right ascension of the sun.
  522. */
  523. const sunApparentRightAscension = (T) => {
  524. const Omega = moonAscendingNodeLongitude$1(T);
  525. const epsilon = trueObliquityOfEcliptic(T) + 0.00256 * cosd(Omega);
  526. const lambda = sunApparentLongitude(T);
  527. const alpha = rad2deg(Math.atan2(cosd(epsilon) * sind(lambda), cosd(lambda)));
  528. return reduceAngle(alpha);
  529. };
  530. /**
  531. * Calculates the apparent declination of the sun (see AA p165 Eq25.7).
  532. * @param {number} T Fractional number of Julian centuries since
  533. * 2000-01-01T12:00:00Z.
  534. * @returns {number} Apparent declination of the sun.
  535. */
  536. const sunApparentDeclination = (T) => {
  537. const Omega = moonAscendingNodeLongitude$1(T);
  538. const epsilon = trueObliquityOfEcliptic(T) + 0.00256 * cosd(Omega);
  539. const lambda = sunApparentLongitude(T);
  540. return rad2deg(Math.asin(sind(epsilon) * sind(lambda)));
  541. };
  542. /**
  543. * Calculates the apparent sidereal time at Greenwich (see AA p88).
  544. * @param {number} T Fractional number of Julian centuries since
  545. * 2000-01-01T12:00:00Z.
  546. * @returns {number} Apparent sidereal time at Greenwich
  547. */
  548. const apparentSiderealTimeGreenwich = (T) => {
  549. const theta0 = meanSiderealTimeGreenwich(T);
  550. const epsilon = trueObliquityOfEcliptic(T);
  551. const DeltaPsi = nutationInLongitude(T);
  552. const theta = theta0 + DeltaPsi * cosd(epsilon);
  553. return reduceAngle(theta);
  554. };
  555. /**
  556. * Calculates the mean sidereal time at Greenwich (see AA p88 Eq12.4).
  557. * @param {number} T Fractional number of Julian centuries since
  558. * 2000-01-01T12:00:00Z.
  559. * @returns {number} Mean sidereal time at Greenwich
  560. */
  561. const meanSiderealTimeGreenwich = (T) => {
  562. const JD2000 = T * 36525;
  563. return 280.46061837 + 360.98564736629 * JD2000 + 0.000387933 * Math.pow(T, 2) - Math.pow(T, 3) / 38710000;
  564. };
  565. /**
  566. * Calculates the true obliquity of the ecliptic (see AA p147).
  567. * @param {number} T Fractional number of Julian centuries since
  568. * 2000-01-01T12:00:00Z.
  569. * @returns {number} True obliquity of the ecliptic.
  570. */
  571. const trueObliquityOfEcliptic = (T) => {
  572. const epsilon0 = meanObliquityOfEcliptic$1(T);
  573. const DeltaEpsilon = nutationInObliquity(T);
  574. return epsilon0 + DeltaEpsilon;
  575. };
  576. /**
  577. * Calculates the mean obliquity of the ecliptic (see AA p147 Eq 22.3).
  578. * @param {number} T Fractional number of Julian centuries since
  579. * 2000-01-01T12:00:00Z.
  580. * @returns {number} Mean obliquity of the ecliptic.
  581. */
  582. const meanObliquityOfEcliptic$1 = (T) => {
  583. const U = T / 100;
  584. return polynomial(U, meanObliquityOfEcliptic);
  585. };
  586. /**
  587. * Calculates the apparent longitude of the sun (see AA p164).
  588. * @param {number} T Fractional number of Julian centuries since
  589. * 2000-01-01T12:00:00Z.
  590. * @returns {number} Apparent longitude of the sun.
  591. */
  592. const sunApparentLongitude = (T) => {
  593. const Sol = sunTrueLongitude(T);
  594. const Omega = moonAscendingNodeLongitude$1(T);
  595. return Sol - 0.00569 - 0.00478 * sind(Omega);
  596. };
  597. /**
  598. * Calculates the true longitude of the sun (see AA p164).
  599. * @param {number} T Fractional number of Julian centuries since
  600. * 2000-01-01T12:00:00Z.
  601. * @returns {number} True longitude of the sun.
  602. */
  603. const sunTrueLongitude = (T) => {
  604. const L0 = sunMeanLongitude$1(T);
  605. const C = sunEquationOfCenter(T);
  606. return L0 + C;
  607. };
  608. /**
  609. * Calculates the equation of center of the sun (see AA p164).
  610. * @param {number} T Fractional number of Julian centuries since
  611. * 2000-01-01T12:00:00Z.
  612. * @returns {number} Equation of center of the sun.
  613. */
  614. const sunEquationOfCenter = (T) => {
  615. const M = sunMeanAnomaly$1(T);
  616. return (1.914602 - 0.004817 * T - 0.000014 * Math.pow(T, 2)) * sind(M) +
  617. (0.019993 - 0.000101 * T) * sind(2 * M) + 0.000290 * sind(3 * M);
  618. };
  619. /**
  620. * Calculates the nutation in longitude of the sun (see AA p144ff).
  621. * @param {number} T Fractional number of Julian centuries since
  622. * 2000-01-01T12:00:00Z.
  623. * @returns {number} Nutation in longitude of the sun.
  624. */
  625. const nutationInLongitude = (T) => {
  626. const D = moonMeanElongation$1(T);
  627. const M = sunMeanAnomaly$1(T);
  628. const MPrime = moonMeanAnomaly$1(T);
  629. const F = moonArgumentOfLatitude$1(T);
  630. const Omega = moonAscendingNodeLongitude$1(T);
  631. let DeltaPsi = 0;
  632. let sineArg;
  633. for (let i = 0; i < 63; i++) {
  634. sineArg = nutations[i][0] * D + nutations[i][1] * M + nutations[i][2] * MPrime +
  635. nutations[i][3] * F + nutations[i][4] * Omega;
  636. DeltaPsi += (nutations[i][5] + nutations[i][6] * T) * sind(sineArg);
  637. }
  638. return DeltaPsi / 36000000;
  639. };
  640. /**
  641. * Calculates the nutation in obliquity of the sun (see AA p144ff).
  642. * @param {number} T Fractional number of Julian centuries since
  643. * 2000-01-01T12:00:00Z.
  644. * @returns {number} Nutation in obliquity of the sun.
  645. */
  646. const nutationInObliquity = (T) => {
  647. const D = moonMeanElongation$1(T);
  648. const M = sunMeanAnomaly$1(T);
  649. const MPrime = moonMeanAnomaly$1(T);
  650. const F = moonArgumentOfLatitude$1(T);
  651. const Omega = moonAscendingNodeLongitude$1(T);
  652. let DeltaEpsilon = 0;
  653. let cosArg;
  654. for (let i = 0; i < 63; i++) {
  655. cosArg = nutations[i][0] * D + nutations[i][1] * M + nutations[i][2] * MPrime +
  656. nutations[i][3] * F + nutations[i][4] * Omega;
  657. DeltaEpsilon += (nutations[i][7] + nutations[i][8] * T) * cosd(cosArg);
  658. }
  659. return DeltaEpsilon / 36000000;
  660. };
  661. /**
  662. * Calculates the argument of latitude of the moon (see AA p144).
  663. * @param {number} T Fractional number of Julian centuries since
  664. * 2000-01-01T12:00:00Z.
  665. * @returns {number} Argument of latitude of the moon.
  666. */
  667. const moonArgumentOfLatitude$1 = (T) => {
  668. const F = polynomial(T, moonArgumentOfLatitude);
  669. return reduceAngle(F);
  670. };
  671. /**
  672. * Calculates the longitude of the ascending node of the Moon's mean orbit on
  673. * the ecliptic, measured from the mean equinox of the datea (see AA p144).
  674. * @param {number} T Fractional number of Julian centuries since
  675. * 2000-01-01T12:00:00Z.
  676. * @returns {number} Longitude of the asc. node of the moon's mean orbit.
  677. */
  678. const moonAscendingNodeLongitude$1 = (T) => {
  679. const Omega = polynomial(T, moonAscendingNodeLongitude);
  680. return reduceAngle(Omega);
  681. };
  682. /**
  683. * Calculates the mean anomaly of the moon (see AA p144).
  684. * @param {number} T Fractional number of Julian centuries since
  685. * 2000-01-01T12:00:00Z.
  686. * @returns {number} Mean anomaly of the moon.
  687. */
  688. const moonMeanAnomaly$1 = (T) => {
  689. const MPrime = polynomial(T, moonMeanAnomaly);
  690. return reduceAngle(MPrime);
  691. };
  692. /**
  693. * Calculates the mean elongation of the moon from the sun (see AA p144).
  694. * @param {number} T Fractional number of Julian centuries since
  695. * 2000-01-01T12:00:00Z.
  696. * @returns {number} Mean elongation of the moon from the sun.
  697. */
  698. const moonMeanElongation$1 = (T) => {
  699. const D = polynomial(T, moonMeanElongation);
  700. return reduceAngle(D);
  701. };
  702. /**
  703. * Calculates the mean anomaly of the sun (see AA p144).
  704. * @param {number} T Fractional number of Julian centuries since
  705. * 2000-01-01T12:00:00Z.
  706. * @returns {number} Mean anomaly of the sun.
  707. */
  708. const sunMeanAnomaly$1 = (T) => {
  709. const M = polynomial(T, sunMeanAnomaly);
  710. return reduceAngle(M);
  711. };
  712. /**
  713. * Calculates the mean longitude of the sun referred to the mean equinox of the
  714. * date (see AA p163).
  715. * @param {number} T Fractional number of Julian centuries since
  716. * 2000-01-01T12:00:00Z.
  717. * @returns {number} Mean longitude of the sun referred to the mean equinox of
  718. * the date.
  719. */
  720. const sunMeanLongitude$1 = (T) => {
  721. const L0 = polynomial(T, sunMeanLongitude);
  722. return reduceAngle(L0);
  723. };
  724. const noEventCodes = {
  725. SUN_HIGH: 'SUN_HIGH',
  726. SUN_LOW: 'SUN_LOW',
  727. };
  728. /**
  729. * Calculates sunrise on the provided date.
  730. * @param {DateTime} datetime Datetime for which sunrise is calculated. Should
  731. * always contain a timezone or be in UTC, lone UTC offsets might lead to
  732. * unexpected behaviour.
  733. * @param {number} latitude Latitude of target location.
  734. * @param {number} longitude longitude of target location.
  735. * @returns {(DateTime|string)} Time of sunrise or a string indicating that no
  736. * event could be calculated as the sun was too high ('SUN_HIGH') or too low
  737. * ('SUN_LOW') during the entire day (unless returnTimeForNoEventCase is true).
  738. */
  739. const sunrise = (datetime, latitude, longitude) => {
  740. try {
  741. return sunRiseSet(datetime, latitude, longitude, 'RISE');
  742. }
  743. catch (err) {
  744. return handleNoEventCase(datetime, err, 6);
  745. }
  746. };
  747. /**
  748. * Calculates sunset on the provided date.
  749. * @param {DateTime} datetime Datetime for which sunset is calculated. Should
  750. * always contain a timezone or be in UTC, lone UTC offsets might lead to
  751. * unexpected behaviour.
  752. * @param {number} latitude Latitude of target location.
  753. * @param {number} longitude longitude of target location.
  754. * @returns {(DateTime|string)} Time of sunset or a string indicating that no
  755. * event could be calculated as the sun was too high ('SUN_HIGH') or too low
  756. * ('SUN_LOW') during the entire day (unless returnTimeForNoEventCase is true).
  757. */
  758. const sunset = (datetime, latitude, longitude) => {
  759. try {
  760. return sunRiseSet(datetime, latitude, longitude, 'SET');
  761. }
  762. catch (err) {
  763. return handleNoEventCase(datetime, err, 18);
  764. }
  765. };
  766. /**
  767. * Calculates solar noon on the provided date.
  768. * @param {DateTime} datetime Datetime for which solar noon is calculated. Should
  769. * always contain a timezone or be in UTC, lone UTC offsets might lead to
  770. * unexpected behaviour.
  771. * @param {number} longitude longitude of target location.
  772. * @returns {DateTime} Time of solar noon at the given longitude.
  773. */
  774. const solarNoon = (datetime, longitude) => sunTransit(datetime, longitude);
  775. const month = {
  776. 1: 'Bahá',
  777. 2: 'Jalál',
  778. 3: 'Jamál',
  779. 4: '‘Aẓamat',
  780. 5: 'Núr',
  781. 6: 'Raḥmat',
  782. 7: 'Kalimát',
  783. 8: 'Kamál',
  784. 9: 'Asmá’',
  785. 10: '‘Izzat',
  786. 11: 'Ma_sh_íyyat',
  787. 12: '‘Ilm',
  788. 13: 'Qudrat',
  789. 14: 'Qawl',
  790. 15: 'Masá’il',
  791. 16: '_Sh_araf',
  792. 17: 'Sulṭán',
  793. 18: 'Mulk',
  794. 19: '‘Alá’',
  795. 20: 'Ayyám-i-Há',
  796. };
  797. const monthL = {
  798. 1: 'Splendour',
  799. 2: 'Glory',
  800. 3: 'Beauty',
  801. 4: 'Grandeur',
  802. 5: 'Light',
  803. 6: 'Mercy',
  804. 7: 'Words',
  805. 8: 'Perfection',
  806. 9: 'Names',
  807. 10: 'Might',
  808. 11: 'Will',
  809. 12: 'Knowledge',
  810. 13: 'Power',
  811. 14: 'Speech',
  812. 15: 'Questions',
  813. 16: 'Honour',
  814. 17: 'Sovereignty',
  815. 18: 'Dominion',
  816. 19: 'Loftiness',
  817. 20: 'Ayyám-i-Há',
  818. };
  819. const holyDay = {
  820. 1: 'Naw-Rúz',
  821. 2: 'First day of Riḍván',
  822. 3: 'Ninth day of Riḍván',
  823. 4: 'Twelfth day of Riḍván',
  824. 5: 'Declaration of the Báb',
  825. 6: 'Ascension of Bahá’u’lláh',
  826. 7: 'Martyrdom of the Báb',
  827. 8: 'Birth of the Báb',
  828. 9: 'Birth of Bahá’u’lláh',
  829. 10: 'Day of the Covenant',
  830. 11: 'Ascension of ‘Abdu’l-Bahá',
  831. };
  832. // CAREFUL: Numbering corresponds to Badí' week, i.e. 1 is Jalál (-> Saturday)
  833. const weekday = {
  834. 1: 'Jalál',
  835. 2: 'Jamál',
  836. 3: 'Kamál',
  837. 4: 'Fiḍál',
  838. 5: '‘Idál',
  839. 6: 'Istijlál',
  840. 7: 'Istiqlál',
  841. };
  842. const weekdayAbbr3 = {
  843. 1: 'Jal',
  844. 2: 'Jam',
  845. 3: 'Kam',
  846. 4: 'Fiḍ',
  847. 5: '‘Idá',
  848. 6: 'Isj',
  849. 7: 'Isq',
  850. };
  851. const weekdayAbbr2 = {
  852. 1: 'Jl',
  853. 2: 'Jm',
  854. 3: 'Ka',
  855. 4: 'Fi',
  856. 5: '‘Id',
  857. 6: 'Ij',
  858. 7: 'Iq',
  859. };
  860. const weekdayL = {
  861. 1: 'Glory',
  862. 2: 'Beauty',
  863. 3: 'Perfection',
  864. 4: 'Grace',
  865. 5: 'Justice',
  866. 6: 'Majesty',
  867. 7: 'Independence',
  868. };
  869. const yearInVahid = {
  870. 1: 'Alif',
  871. 2: 'Bá’',
  872. 3: 'Ab',
  873. 4: 'Dál',
  874. 5: 'Báb',
  875. 6: 'Váv',
  876. 7: 'Abad',
  877. 8: 'Jád',
  878. 9: 'Bahá',
  879. 10: 'Ḥubb',
  880. 11: 'Bahháj',
  881. 12: 'Javáb',
  882. 13: 'Aḥad',
  883. 14: 'Vahháb',
  884. 15: 'Vidád',
  885. 16: 'Badí‘',
  886. 17: 'Bahí',
  887. 18: 'Abhá',
  888. 19: 'Váḥid',
  889. };
  890. const vahid = 'Váḥid';
  891. const kulliShay = 'Kull-i-_Sh_ay’';
  892. const BE = 'B.E.';
  893. const badiCalendar = 'Badí‘ Calendar';
  894. const unicodeCharForZero = '0';
  895. const defaultFormat = 'd MM+ y BE';
  896. var en = /*#__PURE__*/Object.freeze({
  897. __proto__: null,
  898. month: month,
  899. monthL: monthL,
  900. holyDay: holyDay,
  901. weekday: weekday,
  902. weekdayAbbr3: weekdayAbbr3,
  903. weekdayAbbr2: weekdayAbbr2,
  904. weekdayL: weekdayL,
  905. yearInVahid: yearInVahid,
  906. vahid: vahid,
  907. kulliShay: kulliShay,
  908. BE: BE,
  909. badiCalendar: badiCalendar,
  910. unicodeCharForZero: unicodeCharForZero,
  911. defaultFormat: defaultFormat
  912. });
  913. /* eslint-disable dot-notation, line-comment-position, camelcase, sort-imports */
  914. const badiLocale = { en, default: en };
  915. const setDefaultLanguage = (language) => {
  916. if (badiLocale[language] === undefined) {
  917. // eslint-disable-next-line no-console
  918. console.log('Chosen language does not exist. Setting has not been changed.');
  919. }
  920. else {
  921. badiLocale['default'] = badiLocale[language];
  922. }
  923. };
  924. let underlineFormat = 'css';
  925. const setUnderlineFormat = (format) => {
  926. if (['css', 'u', 'diacritic', 'none'].includes(format)) {
  927. underlineFormat = format;
  928. }
  929. else {
  930. // eslint-disable-next-line no-console
  931. console.log('Invalid underline format. Choose one of ["css", "u", "diacritic", "none"]. ' +
  932. 'Setting has not been changed.');
  933. }
  934. };
  935. const formatTokens = [
  936. ['DDL', 'DD+', 'MML', 'MM+', 'WWL', 'yyv', 'KiS'],
  937. ['dd', 'DD', 'mm', 'MM', 'ww', 'WW', 'yv', 'YV', 'vv', 'kk', 'yy', 'BE', 'BC', 'Va'],
  938. ['d', 'D', 'm', 'M', 'W', 'v', 'k', 'y']
  939. ];
  940. // eslint-disable-next-line complexity
  941. const formatBadiDate = (badiDate, formatString, language) => {
  942. if (!badiDate.isValid) {
  943. return 'Not a valid Badí‘ date';
  944. }
  945. if (typeof language === 'string' && badiLocale[language] === undefined && language.includes('-')) {
  946. language = language.split('-')[0];
  947. }
  948. if (language === undefined || badiLocale[language] === undefined) {
  949. language = 'default';
  950. }
  951. formatString = formatString !== null && formatString !== void 0 ? formatString : formatItemFallback(language, 'defaultFormat');
  952. let formattedDate = '';
  953. const length = formatString.length;
  954. for (let i = 0; i < length; i++) {
  955. // Text wrapped in {} is output as-is. A '{' without a matching '}'
  956. // results in invalid input
  957. if (formatString[i] === '{' && i < length - 1) {
  958. for (let j = i + 1; j <= length; j++) {
  959. if (j === length) {
  960. return 'Invalid formatting string.';
  961. }
  962. if (formatString[j] === '}') {
  963. i = j;
  964. break;
  965. }
  966. formattedDate += formatString[j];
  967. }
  968. }
  969. else {
  970. const next1 = formatString[i];
  971. const next2 = next1 + formatString[i + 1];
  972. const next3 = next2 + formatString[i + 2];
  973. if (formatTokens[0].includes(next3)) {
  974. formattedDate += getFormatItem(badiDate, next3, language);
  975. i += 2;
  976. }
  977. else if (formatTokens[1].includes(next2)) {
  978. formattedDate += getFormatItem(badiDate, next2, language);
  979. i += 1;
  980. }
  981. else if (formatTokens[2].includes(next1)) {
  982. formattedDate += getFormatItem(badiDate, next1, language);
  983. }
  984. else {
  985. formattedDate += next1;
  986. }
  987. }
  988. }
  989. return formattedDate;
  990. };
  991. // eslint-disable-next-line complexity
  992. const getFormatItem = (badiDate, token, language) => {
  993. switch (token) {
  994. // Single character tokens
  995. case 'd':
  996. return digitRewrite(badiDate.day, language);
  997. case 'D':
  998. return postProcessLocaleItem(formatItemFallback(language, 'month', badiDate.day), 3);
  999. case 'm':
  1000. return digitRewrite(badiDate.month, language);
  1001. case 'M':
  1002. return postProcessLocaleItem(formatItemFallback(language, 'month', badiDate.month), 3);
  1003. case 'W':
  1004. return formatItemFallback(language, 'weekdayAbbr3', (badiDate.gregorianDate.weekday + 1) % 7 + 1);
  1005. case 'y':
  1006. return digitRewrite(badiDate.year, language);
  1007. case 'v':
  1008. return digitRewrite((Math.floor((badiDate.year - 1) / 19) % 19) + 1, language);
  1009. case 'k':
  1010. return digitRewrite(Math.floor((badiDate.year - 1) / 361) + 1, language);
  1011. // Two character tokens
  1012. case 'dd':
  1013. return digitRewrite((`0${String(badiDate.day)}`).slice(-2), language);
  1014. case 'DD':
  1015. return postProcessLocaleItem(formatItemFallback(language, 'month', badiDate.day));
  1016. case 'mm':
  1017. return digitRewrite((`0${String(badiDate.month)}`).slice(-2), language);
  1018. case 'MM':
  1019. return postProcessLocaleItem(formatItemFallback(language, 'month', badiDate.month));
  1020. case 'ww':
  1021. return formatItemFallback(language, 'weekdayAbbr2', (badiDate.gregorianDate.weekday + 1) % 7 + 1);
  1022. case 'WW':
  1023. return formatItemFallback(language, 'weekday', (badiDate.gregorianDate.weekday + 1) % 7 + 1);
  1024. case 'yy':
  1025. return digitRewrite((`00${String(badiDate.year)}`).slice(-3), language);
  1026. case 'yv':
  1027. return digitRewrite((badiDate.year - 1) % 19 + 1, language);
  1028. case 'YV':
  1029. return formatItemFallback(language, 'yearInVahid', (badiDate.year - 1) % 19 + 1);
  1030. case 'vv':
  1031. return digitRewrite((`0${String((Math.floor((badiDate.year - 1) / 19) + 2) % 19 - 1)}`).slice(-2), language);
  1032. case 'kk':
  1033. return digitRewrite((`0${String(Math.floor((badiDate.year - 1) / 361) + 1)}`).slice(-2), language);
  1034. case 'Va':
  1035. return formatItemFallback(language, 'vahid');
  1036. case 'BE':
  1037. return formatItemFallback(language, 'BE');
  1038. case 'BC':
  1039. return formatItemFallback(language, 'badiCalendar');
  1040. // Three character tokens
  1041. case 'DDL':
  1042. return formatItemFallback(language, 'monthL', badiDate.day);
  1043. case 'DD+': {
  1044. const day = postProcessLocaleItem(formatItemFallback(language, 'month', badiDate.day));
  1045. const dayL = formatItemFallback(language, 'monthL', badiDate.day);
  1046. if (day === dayL) {
  1047. return day;
  1048. }
  1049. if (badiLocale[language] === badiLocale.fa) {
  1050. return `<span dir="rtl">${day} (${dayL})</span>`;
  1051. }
  1052. return `${day} (${dayL})`;
  1053. }
  1054. case 'MML':
  1055. return formatItemFallback(language, 'monthL', badiDate.month);
  1056. case 'MM+': {
  1057. const month = postProcessLocaleItem(formatItemFallback(language, 'month', badiDate.month));
  1058. const monthL = formatItemFallback(language, 'monthL', badiDate.month);
  1059. if (month === monthL) {
  1060. return month;
  1061. }
  1062. if (badiLocale[language] === badiLocale.fa) {
  1063. return `<span dir="rtl">${month} (${monthL})</span>`;
  1064. }
  1065. return `${month} (${monthL})`;
  1066. }
  1067. case 'WWL':
  1068. return formatItemFallback(language, 'weekdayL', (badiDate.gregorianDate.weekday + 1) % 7 + 1);
  1069. case 'yyv':
  1070. return digitRewrite((`0${String((badiDate.year - 1) % 19 + 1)}`).slice(-2), language);
  1071. case 'KiS':
  1072. return postProcessLocaleItem(formatItemFallback(language, 'kulliShay'));
  1073. // istanbul ignore next
  1074. default:
  1075. return '';
  1076. }
  1077. };
  1078. const postProcessLocaleItem = (item, crop) => {
  1079. if (crop && crop < item.length) {
  1080. let char = 0;
  1081. let counter = 0;
  1082. while (counter < crop) {
  1083. if (!'_’‘'.includes(item[char])) {
  1084. counter++;
  1085. }
  1086. char++;
  1087. }
  1088. if ('_’‘'.includes(item[char])) {
  1089. char++;
  1090. }
  1091. item = item.slice(0, char);
  1092. if (item.split('_').length % 2 === 0) {
  1093. item += '_';
  1094. }
  1095. }
  1096. const stringComponents = item.split('_');
  1097. for (let i = 1; i < stringComponents.length; i += 2) {
  1098. stringComponents[i] = underlineString(stringComponents[i]);
  1099. }
  1100. return stringComponents.join('');
  1101. };
  1102. const underlineString = (str) => {
  1103. switch (underlineFormat) {
  1104. case 'css':
  1105. return `<span style="text-decoration:underline">${str}</span>`;
  1106. case 'diacritic':
  1107. return str.split('').map(char => `${char}\u0332`).join('');
  1108. case 'u':
  1109. return `<u>${str}</u>`;
  1110. case 'none':
  1111. return str;
  1112. // istanbul ignore next
  1113. default:
  1114. throw new TypeError('Unexpected underlineFormat');
  1115. }
  1116. };
  1117. const digitRewrite = (number, language) => {
  1118. number = String(number);
  1119. const unicodeOffset = formatItemFallback(language, 'unicodeCharForZero').charCodeAt(0) - '0'.charCodeAt(0);
  1120. if (unicodeOffset === 0) {
  1121. return number;
  1122. }
  1123. const codePoints = [...number].map(num => num.charCodeAt(0) + unicodeOffset);
  1124. return String.fromCharCode(...codePoints);
  1125. };
  1126. const formatItemFallback = (language, category, index) => {
  1127. var _a;
  1128. if (index === undefined) {
  1129. while (badiLocale[language][category] === undefined) {
  1130. language = languageFallback(language);
  1131. }
  1132. return badiLocale[language][category];
  1133. }
  1134. while (((_a = badiLocale[language][category]) === null || _a === void 0 ? void 0 : _a[index]) === undefined) {
  1135. language = languageFallback(language);
  1136. }
  1137. return badiLocale[language][category][index];
  1138. };
  1139. const languageFallback = (languageCode) => {
  1140. if (languageCode.includes('-')) {
  1141. return languageCode.split('-')[0];
  1142. // eslint-disable-next-line no-negated-condition
  1143. }
  1144. else if (languageCode !== 'default') {
  1145. return 'default';
  1146. }
  1147. return 'en';
  1148. };
  1149. const badiYears = [
  1150. 'l4da', 'k4ci', 'k5c7', 'l4d6', 'l4ce', 'k4c4', 'k5d4', 'l4cb', 'l4c1', 'k4cj', 'k5c8', 'l4d7', 'l4cf', 'k4c5',
  1151. 'k4d5', 'k5ce', 'l4c2', 'k4d2', 'k4ca', 'k5da', 'l4ch', 'k4c6', 'k4d6', 'k5cf', 'l4c4', 'k4d4', 'k4cc', 'k5c1',
  1152. 'l4cj', 'k4c8', 'k4d8', 'k5cg', 'l4c5', 'k4d5', 'k4ce', 'k5c3', 'l4d2', 'k4ca', 'k4d9', 'k5ci', 'l4c6', 'k4d6',
  1153. 'k4cf', 'k4c4', 'k5d4', 'k4cb', 'k4bj', 'k4cj', 'k5c9', 'k4d8', 'k4cg', 'k4c6', 'k5d6', 'k4cd', 'k4c2', 'k4d2',
  1154. 'k5ca', 'k4d9', 'k4ci', 'k4c7', 'k5d7', 'k4cf', 'k4c4', 'k4d4', 'k5cc', 'k4bj', 'k4cj', 'k4c9', 'k5d9', 'k4cg',
  1155. 'k4c6', 'k4d5', 'k5cd', 'k4c2', 'k4d1', 'k4ca', 'k4da', 'j5cj', 'k4c7', 'k4d7', 'k4cf', 'j5c4', 'k4d3', 'k4cb',
  1156. 'k4c1', 'k5d1', 'l4c9', 'l4d9', 'l4ch', 'k5c6', 'l4d5', 'l4cd', 'l4c2', 'k5d2', 'l4ca', 'l4da', 'l4cj', 'k5c8',
  1157. 'l4d7', 'l4cf', 'l4c4', 'k5d4', 'l4cb', 'l4c1', 'l4d1', 'k5c9', 'l4d8', 'l4cg', 'l4c5', 'k4d5', 'k5ce', 'l4c2',
  1158. 'l4d2', 'k4cb', 'k5db', 'l4ci', 'l4c7', 'k4d7', 'k5cf', 'l4c4', 'l4d4', 'k4cc', 'k5c2', 'l4d1', 'l4c9', 'k4d9',
  1159. 'k5ch', 'l4c5', 'l4d5', 'k4ce', 'k5c3', 'l4d2', 'l4cb', 'k4da', 'k5ci', 'l4c6', 'l4d6', 'k4cf', 'k5c5', 'l4d4',
  1160. 'l4cc', 'k4c1', 'k4d1', 'k5c9', 'l4d8', 'k4cg', 'k4c6', 'k5d6', 'l4ce', 'k4c3', 'k4d3', 'k5cb', 'l4da', 'k4ci',
  1161. 'k4c7', 'k5d7', 'l4cf', 'k4c5', 'k4d5', 'k5cd', 'l4c1', 'k4cj', 'k4c9', 'k5d9', 'l4cg', 'k4c6', 'k4d6', 'k5ce',
  1162. 'l4c3', 'k4d2', 'k4ca', 'k5bj', 'l4ci', 'k4c7', 'k4d7', 'k4cg', 'k5c5', 'k4d4', 'k4cc', 'k4c1', 'k5d1', 'k4c9',
  1163. 'k4d9', 'k4ch', 'k5c7', 'l4d6', 'l4ce', 'l4c3', 'l5d3', 'l4ca', 'l4da', 'l4cj', 'l5c8', 'l4d7', 'l4cg', 'l4c5',
  1164. 'l5d4', 'l4cb', 'l4c1', 'l4d1', 'l5ca', 'l4d9', 'l4ch', 'l4c6', 'l5d6', 'l4cd', 'l4c2', 'l4d2', 'l4cb', 'k5c1',
  1165. 'l4cj', 'l4c8', 'l4d8', 'k5cg', 'l4c4', 'l4d4', 'l4cc', 'k5c2', 'l4d1', 'l4ca', 'l4da', 'k5ci', 'l4c6', 'l4d5',
  1166. 'l4ce', 'k5c3', 'l4d2', 'l4cb', 'l4db', 'k5cj', 'l4c8', 'l4d7', 'l4cf', 'k5c5', 'l4d4', 'l4cc', 'l4c2', 'k5d2',
  1167. 'l4c9', 'l4d9', 'l4ch', 'k4c6', 'k5d6', 'l4ce', 'l4c3', 'k4d3', 'k5cc', 'l4db', 'l4cj', 'k4c8', 'k5d8', 'l4cf',
  1168. 'l4c4', 'k4d5', 'k5cd', 'l4c2', 'l4d2', 'k4ca', 'k5d9', 'l4cg', 'l4c6', 'k4d6', 'k5cf', 'l4c3', 'l4d3', 'k4cb',
  1169. 'k5bj', 'l4ci', 'l4c7', 'k4d7', 'k5cg', 'l4c5', 'l4d5', 'k4cd', 'k4c2', 'k5d2', 'l4c9', 'k4d9', 'k4ch', 'k5c7',
  1170. 'l4d6', 'k4cf', 'k4c4', 'k5d4', 'l4cb', 'l4bj', 'l4cj', 'l5c8', 'm4d7', 'l4cg', 'l4c5', 'l5d5', 'm4cc', 'l4c1',
  1171. 'l4d1', 'l5ca', 'm4d9', 'l4ch', 'l4c7', 'l5d7', 'm4ce', 'l4c3', 'l4d3', 'l5cb', 'm4bi', 'l4ci', 'l4c8', 'l4d8',
  1172. 'l5ch', 'l4c5', 'l4d5', 'l4cd', 'l5c2', 'l4d1', 'l4c9', 'l4da', 'l5ci', 'l4c7', 'l4d7', 'l4cf', 'l5c4', 'l4d2',
  1173. 'l4cb', 'l4bj', 'l5d1', 'l4c8', 'l4d8', 'l4cg', 'l5c5', 'l4d4', 'l4cc', 'l4c2', 'l5d2', 'l4c9', 'l4da', 'l4ci',
  1174. ];
  1175. class BadiDate {
  1176. constructor(date) {
  1177. this._holyDay = undefined;
  1178. this._valid = true;
  1179. this._invalidReason = undefined;
  1180. try {
  1181. if (this._isDateObject(date)) {
  1182. this._gregorianDate = luxon.DateTime.fromObject({ year: date.getFullYear(), month: date.getMonth() + 1, day: date.getDate(), zone: 'UTC' });
  1183. }
  1184. else if (luxon.DateTime.isDateTime(date)) {
  1185. this._gregorianDate = luxon.DateTime.fromObject({ year: date.year, month: date.month, day: date.day, zone: 'UTC' });
  1186. }
  1187. else if (this._isYearMonthDay(date) || this._isYearHolyDayNumber(date)) {
  1188. this._setFromBadiDate(date);
  1189. }
  1190. else {
  1191. throw new TypeError('Unrecognized input format');
  1192. }
  1193. if (this._year === undefined) {
  1194. // We haven't set the Badí' date yet
  1195. this._setFromGregorianDate();
  1196. }
  1197. this._setHolyDay();
  1198. }
  1199. catch (err) {
  1200. this._setInvalid(err);
  1201. }
  1202. Object.freeze(this);
  1203. }
  1204. format(formatString, language) {
  1205. return formatBadiDate(this, formatString, language);
  1206. }
  1207. _isDateObject(arg) {
  1208. return Object.prototype.toString.call(arg) === '[object Date]';
  1209. }
  1210. _isYearMonthDay(arg) {
  1211. return typeof arg.year === 'number' && typeof arg.month === 'number' &&
  1212. typeof arg.day === 'number';
  1213. }
  1214. _isYearHolyDayNumber(arg) {
  1215. return typeof arg.year === 'number' && arg.month === undefined &&
  1216. arg.day === undefined && typeof arg.holyDayNumber === 'number';
  1217. }
  1218. _notInValidGregorianDateRange(datetime) {
  1219. const lowerBound = luxon.DateTime.fromObject({ year: 1844, month: 3, day: 21, zone: 'UTC' });
  1220. const upperBound = luxon.DateTime.fromObject({ year: 2351, month: 3, day: 20, zone: 'UTC' });
  1221. return datetime < lowerBound || datetime > upperBound;
  1222. }
  1223. _setFromGregorianDate() {
  1224. if (this._notInValidGregorianDateRange(this._gregorianDate)) {
  1225. throw new RangeError('Input date outside of valid range (1844-03-21 - 2351-03-20)');
  1226. }
  1227. const gregorianYear = this._gregorianDate.year;
  1228. const oldImplementationCutoff = luxon.DateTime.fromObject({ year: 2015, month: 3, day: 21, zone: 'UTC' });
  1229. if (this._gregorianDate < oldImplementationCutoff) {
  1230. const { month, day } = this._gregorianDate;
  1231. if (month < 3 || (month === 3 && day < 21)) {
  1232. this._nawRuz = luxon.DateTime.fromObject({ year: gregorianYear - 1, month: 3, day: 21, zone: 'UTC' });
  1233. this._year = gregorianYear - 1844;
  1234. }
  1235. else {
  1236. this._nawRuz = luxon.DateTime.fromObject({ year: gregorianYear, month: 3, day: 21, zone: 'UTC' });
  1237. this._year = gregorianYear - 1843;
  1238. }
  1239. this._setOldAyyamiHaLength();
  1240. this._yearTwinBirthdays = [12, 5, 13, 9];
  1241. }
  1242. else {
  1243. this._year = gregorianYear - 1843;
  1244. this._setBadiYearInfo(true);
  1245. }
  1246. this._setBadiMonthAndDay();
  1247. }
  1248. /**
  1249. * Set Badí' month and day from Gregorian date
  1250. */
  1251. _setBadiMonthAndDay() {
  1252. const dayOfBadiYear = this._dayOfYear(this._gregorianDate);
  1253. if (dayOfBadiYear < 343) {
  1254. this._month = Math.floor((dayOfBadiYear - 1) / 19 + 1);
  1255. this._day = (dayOfBadiYear - 1) % 19 + 1;
  1256. }
  1257. else if (dayOfBadiYear < 343 + this._ayyamiHaLength) {
  1258. this._month = 20;
  1259. this._day = dayOfBadiYear - 342;
  1260. }
  1261. else {
  1262. this._month = 19;
  1263. this._day = dayOfBadiYear - (342 + this._ayyamiHaLength);
  1264. }
  1265. }
  1266. _setFromBadiDate(date) {
  1267. this._year = date.year;
  1268. if (this._year < 1 || this._year > 507) {
  1269. throw new RangeError('Input date outside of valid range (1 - 507 B.E.)');
  1270. }
  1271. else if (this._year < 172) {
  1272. this._nawRuz = luxon.DateTime.fromObject({ year: 1843 + this._year, month: 3, day: 21, zone: 'UTC' });
  1273. this._setOldAyyamiHaLength();
  1274. this._yearTwinBirthdays = [12, 5, 13, 9];
  1275. }
  1276. else {
  1277. this._setBadiYearInfo();
  1278. }
  1279. if (this._isYearMonthDay(date)) {
  1280. this._month = date.month;
  1281. this._day = date.day;
  1282. if (this._month === 20 && this._day > this._ayyamiHaLength) {
  1283. // If only off by one day, we'll bubble up so that 5th Ayyám-i-Há in a year with only 4 days of
  1284. // Ayyám-i-Há can be salvaged
  1285. if (this._day - this._ayyamiHaLength === 1) {
  1286. this._month = 19;
  1287. this._day = 1;
  1288. }
  1289. else {
  1290. throw new TypeError('Input numbers do not designate a valid date');
  1291. }
  1292. }
  1293. if (this._month < 1 || this._month > 20 || this._day < 1 || this.day > 19) {
  1294. throw new TypeError('Input numbers do not designate a valid date');
  1295. }
  1296. }
  1297. else {
  1298. if (date.holyDayNumber < 1 || date.holyDayNumber > 11) {
  1299. throw new TypeError('Input numbers do not designate a valid Holy Day');
  1300. }
  1301. this._holyDay = date.holyDayNumber;
  1302. [this._month, this._day] = this._holyDayMapping()[this._holyDay];
  1303. }
  1304. this._gregorianDate = this._nawRuz.plus(luxon.Duration.fromObject({ days: this._dayOfYear([this._year, this._month, this._day]) - 1 }));
  1305. }
  1306. _setOldAyyamiHaLength() {
  1307. if (luxon.DateTime.fromObject({ year: this._nawRuz.year + 1 }).isInLeapYear) {
  1308. this._ayyamiHaLength = 5;
  1309. }
  1310. else {
  1311. this._ayyamiHaLength = 4;
  1312. }
  1313. }
  1314. _setBadiYearInfo(fromGregorianDate = false) {
  1315. let yearData = this._extractBadiYearInfo();
  1316. if (fromGregorianDate && this._gregorianDate < yearData.nawRuz) {
  1317. this._year -= 1;
  1318. yearData = this._extractBadiYearInfo();
  1319. }
  1320. this._nawRuz = yearData.nawRuz;
  1321. this._ayyamiHaLength = yearData.ayyamiHaLength;
  1322. this._yearTwinBirthdays = yearData.twinBirthdays;
  1323. }
  1324. _extractBadiYearInfo() {
  1325. let nawRuz, ayyamiHaLength, twinBirthdays;
  1326. // Check whether data needs to be unpacked or exists in the verbose version
  1327. // istanbul ignore else
  1328. if (badiYears[0] === 'l4da') {
  1329. const components = badiYears[this._year - 172].split('');
  1330. nawRuz = luxon.DateTime.fromObject({ year: this._year - 172 + 2015, month: 3, day: parseInt(components[0], 36), zone: 'UTC' });
  1331. ayyamiHaLength = parseInt(components[1], 36);
  1332. const TB1 = [parseInt(components[2], 36), parseInt(components[3], 36)];
  1333. const TB2 = TB1[1] < 19 ? [TB1[0], TB1[1] + 1] : [TB1[0] + 1, 1];
  1334. twinBirthdays = [TB1[0], TB1[1], TB2[0], TB2[1]];
  1335. }
  1336. else {
  1337. ({ nawRuz, ayyamiHaLength, twinBirthdays } = badiYears[this._year]);
  1338. nawRuz = luxon.DateTime.fromISO(nawRuz, { zone: 'UTC' });
  1339. }
  1340. return { nawRuz, ayyamiHaLength, twinBirthdays };
  1341. }
  1342. _dayOfYear(date) {
  1343. // Naw-Rúz is day 1
  1344. if (Array.isArray(date)) {
  1345. // We have a Badí' date
  1346. if (date[1] < 19) {
  1347. return 19 * (date[1] - 1) + date[2];
  1348. }
  1349. else if (date[1] === 20) {
  1350. return 342 + date[2];
  1351. }
  1352. // date[1] === 19
  1353. return 342 + this._ayyamiHaLength + date[2];
  1354. }
  1355. return date.diff(this._nawRuz).as('days') + 1;
  1356. }
  1357. _setInvalid(invalidReason) {
  1358. this._gregorianDate = luxon.DateTime.invalid('Not a valid Badí‘ date');
  1359. this._year = NaN;
  1360. this._month = NaN;
  1361. this._day = NaN;
  1362. this._ayyamiHaLength = NaN;
  1363. this._nawRuz = luxon.DateTime.invalid('Not a valid Badí‘ date');
  1364. this._valid = false;
  1365. this._invalidReason = invalidReason;
  1366. }
  1367. _setHolyDay() {
  1368. const mapping = this._holyDayMapping();
  1369. this._holyDay = parseInt(Object.keys(mapping)
  1370. .find(key => mapping[key][0] === this._month && mapping[key][1] === this._day), 10);
  1371. }
  1372. _holyDayMapping() {
  1373. return {
  1374. [1 /* NawRuz */]: [1, 1],
  1375. [2 /* FirstRidvan */]: [2, 13],
  1376. [3 /* NinthRidvan */]: [3, 2],
  1377. [4 /* TwelfthRidvan */]: [3, 5],
  1378. [5 /* DeclarationOfTheBab */]: [4, this._year < 172 ? 7 : 8],
  1379. [6 /* AscensionOfBahaullah */]: [4, 13],
  1380. [7 /* MartyrdomOfTheBab */]: [6, this._year < 172 ? 16 : 17],
  1381. [8 /* BirthOfTheBab */]: [this._yearTwinBirthdays[0], this._yearTwinBirthdays[1]],
  1382. [9 /* BirthOfBahaullah */]: [this._yearTwinBirthdays[2], this._yearTwinBirthdays[3]],
  1383. [10 /* DayOfTheCovenant */]: [14, 4],
  1384. [11 /* AscensionOfAbdulBaha */]: [14, 6],
  1385. };
  1386. }
  1387. _leapYearsBefore() {
  1388. let leapYearsBefore = Math.floor(Math.min(this.year - 1, 171) / 4);
  1389. if (this.year > 172) {
  1390. // istanbul ignore else
  1391. if (badiYears[0] === 'l4da') {
  1392. leapYearsBefore += badiYears.slice(0, this.year - 172).filter(entry => entry[1] === '5').length;
  1393. }
  1394. else {
  1395. leapYearsBefore += Object.entries(badiYears)
  1396. .filter(([year, data]) => parseInt(year, 10) < this.year &&
  1397. data.ayyamiHaLength === 5).length;
  1398. }
  1399. }
  1400. return leapYearsBefore;
  1401. }
  1402. holyDay(language = undefined) {
  1403. if (!this._holyDay) {
  1404. return '';
  1405. }
  1406. if (language === undefined || badiLocale[language] === undefined) {
  1407. language = 'default';
  1408. }
  1409. return formatItemFallback(language, 'holyDay', this._holyDay);
  1410. }
  1411. valueOf() {
  1412. return this._dayOfYear([this.year, this.month, this.day]) + this._leapYearsBefore() + (this.year - 1) * 365;
  1413. }
  1414. equals(other) {
  1415. return this.isValid && other.isValid && this.valueOf() === other.valueOf();
  1416. }
  1417. get isValid() {
  1418. return this._valid;
  1419. }
  1420. get invalidReason() {
  1421. return this._invalidReason;
  1422. }
  1423. get day() {
  1424. return this._day;
  1425. }
  1426. get month() {
  1427. return this._month;
  1428. }
  1429. get year() {
  1430. return this._year;
  1431. }
  1432. // number of the Badí' weekday between 1 (Jalál ~> Saturday) and 7 (Istiqlál ~> Friday).
  1433. get weekday() {
  1434. return (this._gregorianDate.weekday + 1) % 7 + 1;
  1435. }
  1436. get yearInVahid() {
  1437. return (this._year - 1) % 19 + 1;
  1438. }
  1439. get vahid() {
  1440. return (Math.floor((this._year - 1) / 19) % 19) + 1;
  1441. }
  1442. get kullIShay() {
  1443. return Math.floor((this._year - 1) / 361) + 1;
  1444. }
  1445. // Gregorian date on whose sunset the Badí' date ends.
  1446. get gregorianDate() {
  1447. return this._gregorianDate;
  1448. }
  1449. get ayyamiHaLength() {
  1450. return this._ayyamiHaLength;
  1451. }
  1452. get holyDayNumber() {
  1453. return this._holyDay ? this._holyDay : undefined;
  1454. }
  1455. get workSuspended() {
  1456. return this._holyDay ? this.holyDayNumber < 10 : undefined;
  1457. }
  1458. get nextMonth() {
  1459. let { year, month } = this;
  1460. switch (month) {
  1461. case 18:
  1462. month = 20;
  1463. break;
  1464. case 19:
  1465. month = 1;
  1466. year += 1;
  1467. break;
  1468. case 20:
  1469. month = 19;
  1470. break;
  1471. default:
  1472. month += 1;
  1473. }
  1474. return new BadiDate({ year, month, day: 1 });
  1475. }
  1476. get previousMonth() {
  1477. let { year, month } = this;
  1478. switch (month) {
  1479. case 1:
  1480. month = 19;
  1481. year -= 1;
  1482. break;
  1483. case 19:
  1484. month = 20;
  1485. break;
  1486. case 20:
  1487. month = 18;
  1488. break;
  1489. default:
  1490. month -= 1;
  1491. }
  1492. return new BadiDate({ year, month, day: 1 });
  1493. }
  1494. get nextDay() {
  1495. if (this._day === 19 || (this._month === 20 && this._day === this._ayyamiHaLength)) {
  1496. return this.nextMonth;
  1497. }
  1498. return new BadiDate({ year: this._year, month: this._month, day: this._day + 1 });
  1499. }
  1500. get previousDay() {
  1501. if (this._day === 1) {
  1502. const { previousMonth } = this;
  1503. let day = 19;
  1504. if (this._month === 19) {
  1505. day = this._ayyamiHaLength;
  1506. }
  1507. return new BadiDate({
  1508. year: previousMonth.year,
  1509. month: previousMonth.month,
  1510. day,
  1511. });
  1512. }
  1513. return new BadiDate({ year: this._year, month: this._month, day: this._day - 1 });
  1514. }
  1515. }
  1516. const badiDateSettings = (settings) => {
  1517. if (settings.defaultLanguage) {
  1518. setDefaultLanguage(settings.defaultLanguage);
  1519. }
  1520. if (settings.underlineFormat) {
  1521. setUnderlineFormat(settings.underlineFormat);
  1522. }
  1523. };
  1524. /* eslint-disable max-len, complexity */
  1525. const clockLocations = {
  1526. Canada: [[[-63.29333, 60], [-138.9386, 60], [-139.1889, 60.08888], [-139.0681, 60.35222], [-139.6767, 60.34055], [-139.9794, 60.18777], [-140.45081, 60.30972], [-140.52139, 60.22221], [-140.9955, 60.30721], [-140.99686, 61.8948], [-141.00005, 65.84028], [-141.00206, 68.42821], [-141.00296, 69.58786], [-141.00477, 69.58884], [-140.99813, 70.12335], [-124.80692, 77.04204], [-117.95462, 78.95431], [-99.46935, 82.3539], [-75.0348, 84.79736], [-59.3117, 83.84122], [-60.98493, 82.07503], [-69.57686, 80.21588], [-71.1173, 79.6183], [-74.13178, 79.24647], [-73.93259, 78.5692], [-75.69878, 77.78571], [-77.43842, 77.49355], [-77.55793, 76.52414], [-78.54063, 76.17887], [-79.31085, 74.25332], [-75.79174, 73.25735], [-73.13581, 72.0489], [-69.1652, 71.09276], [-66.31007, 69.91087], [-66.05776, 68.70243], [-60.73262, 66.89639], [-62.3129, 65.07708], [-63.60102, 64.69197], [-64.19861, 60.84087], [-63.29333, 60.00012]]],
  1527. Finland: [[[31.5848296, 62.9070356], [31.4390606, 62.785375], [31.3454013, 62.64032620000001], [31.2218346, 62.49829550000001], [31.138311, 62.4420838], [30.720412, 62.20890580000002], [30.6564061, 62.2085877], [30.602068, 62.14134890000001], [30.4231749, 62.02237140000001], [30.3061104, 61.964546], [30.1556605, 61.8579888], [30.0752371, 61.8183646], [30.0387281, 61.76500110000001], [29.8185491, 61.6549278], [29.74029919999999, 61.5737044], [29.5030724, 61.461338900000015], [29.3304371, 61.3526198], [29.2330501, 61.268169], [29.0298879, 61.191815300000016], [28.9583837, 61.1514492], [28.818984, 61.1216471], [28.7136921, 61.0443349], [28.6578963, 60.95109439999999], [28.5246697, 60.9571371], [28.1354613, 60.7408695], [27.873414, 60.604559], [27.7736111, 60.53333330000002], [27.725, 60.3913889], [27.4550934, 60.223534], [27.2938862, 60.2003975], [26.8756332, 60.200342100000015], [26.6110136, 60.161753200000014], [26.2947105, 60.0465237], [26.0173046, 59.97679690000001], [25.1693516, 59.9434386], [24.2815873, 59.79155570000002], [23.4566746, 59.67247360000001], [22.9224144, 59.6384411], [22.6345729, 59.6079549], [22.3965563, 59.5130947], [21.4475658, 59.4772985], [20.7608658, 59.5324815], [20.3839584, 59.4576178], [20.2843364, 59.4660819], [19.083209799999988, 60.19169020000001], [19.2202109, 60.61151010000001], [20.0251664, 60.72755450000001], [20.7714495, 61.12690790000001], [20.903203, 61.6462488], [20.1658123, 63.1648577], [20.4010006, 63.3318822], [20.8175143, 63.5011379], [21.4628083, 63.6552312], [21.8845783, 63.70121190000001], [22.9611467, 64.2200974], [23.835799, 64.66547409999997], [24.1545056, 65.29247769999998], [24.131900100000014, 65.5153846], [24.1776819, 65.6603564], [24.1318042, 65.7716089], [24.152978, 65.862572], [24.0536762, 65.95152940000006], [24.0491701, 65.99502970000003], [23.9394784, 66.07568309999998], [23.9170552, 66.16186640000002], [23.7313763, 66.19408560000002], [23.6489848, 66.30377249999997], [23.6880374, 66.3815611], [23.650965700000015, 66.4557476], [23.8605347, 66.5595503], [23.86853209999999, 66.6568254], [23.9078441, 66.72140390000003], [23.880337, 66.76350940000003], [23.99566289999999, 66.822049], [23.8525565, 66.9573479], [23.677678, 67.0620298], [23.5545444, 67.16789390000002], [23.596079, 67.20820560000003], [23.5637833, 67.2606725], [23.7311639, 67.28763560000003], [23.7172209, 67.38530669999997], [23.7639366, 67.42772120000002], [23.408239899999984, 67.46939490000003], [23.4059159, 67.50091320000003], [23.5452477, 67.5838871], [23.492249099999984, 67.6652745], [23.47871239999999, 67.8419848], [23.5171915, 67.88433529999998], [23.6407972, 67.9151784], [23.6525654, 67.9589433], [23.3937061, 68.0452571], [23.3077618, 68.14837649999997], [23.1656349, 68.13315060000002], [23.152641, 68.2333806], [23.0702517, 68.29970360000003], [22.9181313, 68.3335115], [22.8028778, 68.39328420000002], [22.3437523, 68.45688960000003], [22.2960914, 68.4840408], [22.045040799999988, 68.479329], [21.8898693, 68.5844051], [21.7010887, 68.59686950000003], [21.6061629, 68.6678769], [21.4298688, 68.691352], [21.39042, 68.76478960000003], [20.9988391, 68.89612380000003], [20.8441913, 68.93656440000004], [20.9116456, 68.96882420000003], [20.775042799999987, 69.0326073], [20.5523258, 69.0600767], [20.7173208, 69.1197912], [21.057543, 69.03628970000003], [21.1086742, 69.1039291], [20.9875741, 69.19192740000003], [21.0961691, 69.260912], [21.2788202, 69.3118841], [21.6270859, 69.27658829999997], [22.1757622, 68.95632440000003], [22.1918678, 68.9187737], [22.3407806, 68.82722570000003], [22.3745217, 68.71666660000004], [22.5353893, 68.74451260000004], [22.800824, 68.68754809999997], [23.0459522, 68.6893436], [23.1675822, 68.6285189], [23.4406356, 68.6921635], [23.6735202, 68.70552140000002], [23.7753915, 68.81885129999998], [23.983330799999987, 68.82714340000003], [24.0755916, 68.7799668], [24.30226, 68.71735020000003], [24.6083879, 68.6819016], [24.9170187, 68.60529109999997], [25.1193208, 68.6428308], [25.1212144, 68.7458351], [25.1573697, 68.80006390000003], [25.2931271, 68.8600372], [25.47250939999999, 68.90329120000003], [25.6543285, 68.90577049999997], [25.745596499999987, 69.03984729999998], [25.742717799999987, 69.14430209999998], [25.6939225, 69.1957144], [25.7410164, 69.31839509999998], [25.8462009, 69.3929115], [25.8084981, 69.4259367], [25.8768225, 69.5261298], [25.9760403, 69.610225], [25.8925512, 69.66539549999997], [26.0071395, 69.7228555], [26.1255598, 69.7345401], [26.3835888, 69.8541585], [26.4653759, 69.93980490000003], [26.6834067, 69.96301920000003], [26.8407548, 69.9603025], [27.0316081, 69.9107924], [27.3049484, 69.95762760000004], [27.43070959999999, 70.0194461], [27.5206048, 70.02243659999996], [27.614207, 70.074151], [27.9593778, 70.0921111], [27.9842853, 70.0139707], [28.160713, 69.92099370000003], [28.3452694, 69.88083179999997], [28.4042254, 69.818425], [29.1339095, 69.69534039999996], [29.1705369, 69.6390414], [29.3364956, 69.47832269999998], [29.2193395, 69.39763620000002], [28.831539, 69.2243617], [28.80543, 69.1111558], [28.929451, 69.0519407], [28.4953735, 68.9300403], [28.468076, 68.8855137], [28.66118, 68.8864737], [28.8014499, 68.8693665], [28.7072131, 68.732555], [28.4341202, 68.53979460000002], [28.6478382, 68.19591340000002], [29.3271337, 68.0745162], [29.6593888, 67.80297219999996], [30.0173409, 67.67356889999996], [29.9305102, 67.5228214], [29.8567823, 67.48926540000004], [29.6361151, 67.332861], [29.522709499999987, 67.3099172], [29.48660609999999, 67.26011490000003], [29.0732544, 66.99615390000004], [29.0331239, 66.92547219999996], [29.0607529, 66.85269279999997], [29.3507185, 66.6439171], [29.4726751, 66.5434478], [29.6969469, 66.277347], [29.9239353, 66.1262486], [29.997268, 65.97889249999997], [30.0647878, 65.90105890000002], [30.138463, 65.66868749999998], [30.0170916, 65.6965272], [29.722432799999986, 65.637045], [29.8637508, 65.5604702], [29.7331208, 65.472637], [29.7467636, 65.347391], [29.6018471, 65.2599435], [29.893525, 65.19295509999998], [29.8193446, 65.1444587], [29.896916, 65.1051579], [29.7328054, 65.09129760000003], [29.6255535, 65.06020520000003], [29.5993537, 64.99509809999998], [29.6470353, 64.8674467], [29.739663, 64.7897553], [30.0430007, 64.7928625], [30.0416232, 64.74110840000003], [30.1365729, 64.6488835], [29.9894058, 64.58761530000002], [29.9869609, 64.5338998], [30.0583348, 64.4508749], [30.0448933, 64.4020122], [30.482439699999983, 64.2623385], [30.466399899999985, 64.2044319], [30.5534271, 64.1322443], [30.5280169, 64.0488769], [30.320039, 63.9082685], [30.260416, 63.82200320000001], [29.9718903, 63.7571676], [30.24571609999999, 63.60696830000001], [30.385620199999988, 63.54577980000001], [30.4841978, 63.4670887], [30.789711, 63.4050884], [30.9330443, 63.3559208], [30.9798739, 63.3078177], [31.1483116, 63.26151890000002], [31.2416464, 63.2166421], [31.2658547, 63.1154671], [31.46252279999998, 63.02421930000001], [31.5848296, 62.9070356]]],
  1528. // Greenland: [[[-57.44887, 82.28507], [-60.15022, 82.05782], [-61.87928, 81.82771], [-62.2191, 81.7294], [-63.42448, 81.28486], [-65.32658, 80.98138], [-66.57577, 80.83605], [-67.38791, 80.54753], [-67.66468, 80.1436], [-68.73755, 79.10919], [-72.47765, 78.62618], [-72.96065, 78.36972], [-73.1359, 78.13036], [-72.78968, 77.34387], [-73.38382, 76.66424], [-72.79822, 76.5702], [-69.80615, 76.29664], [-68.45971, 75.97179], [-66.32252, 75.80508], [-64.89914, 75.80081], [-63.13809, 76.04018], [-62.31741, 75.9034], [-60.47087, 75.78371], [-60.19731, 75.62983], [-58.94919, 75.49305], [-58.81241, 74.92883], [-58.38497, 74.89464], [-58.21399, 74.63817], [-57.47879, 74.17654], [-57.15394, 73.47554], [-55.83743, 71.40673], [-55.23901, 70.48346], [-55.10223, 69.40632], [-53.87121, 68.825], [-54.21316, 66.80748], [-53.75152, 65.52517], [-52.5034, 63.43926], [-47.39122, 59.6265], [-42.68939, 59.38714], [-41.16771, 61.50723], [-30.05428, 67.67946], [-26.83993, 68.124], [-21.04386, 70.27829], [-21.24903, 72.74034], [-16.78656, 74.91174], [-16.39331, 77.2541], [-17.64144, 78.51933], [-16.82075, 79.78455], [-11.02468, 81.34043], [-11.93085, 82.02433], [-19.48798, 82.45177], [-19.71024, 83.01599], [-27.19898, 83.85377], [-39.64602, 83.80248], [-50.82784, 82.9476], [-57.44887, 82.28507]]],
  1529. Iceland: [[[-25.0, 63.0], [-12.8, 63.0], [-12.8, 66.8], [-25.0, 66.8]]],
  1530. Norway: [[[30.79367, 69.78758], [30.89032, 69.73729], [30.95448, 69.63243], [30.93257, 69.55989], [30.81756, 69.52877], [30.51593, 69.54042], [30.41768, 69.58992], [30.23373, 69.65016], [30.13777, 69.64353], [30.18838, 69.56846], [30.12305, 69.51749], [30.11721, 69.46989], [30.00876, 69.41591], [29.85802, 69.42374], [29.7244, 69.38965], [29.56938, 69.31756], [29.39594, 69.32384], [29.28845, 69.29618], [29.31313, 69.23752], [29.24224, 69.11306], [29.05666, 69.01528], [28.85456, 69.07664], [28.80541, 69.11116], [28.83152, 69.22436], [29.21932, 69.39764], [29.33647, 69.47832], [29.17052, 69.63904], [29.13389, 69.69534], [28.40421, 69.81842], [28.33046, 69.84919], [28.34506, 69.8808], [28.1607, 69.92099], [27.98428, 70.01397], [27.94828, 70.09187], [27.79768, 70.07731], [27.61245, 70.07456], [27.52598, 70.02346], [27.42855, 70.01921], [27.27471, 69.97591], [27.29177, 69.95225], [27.03749, 69.91039], [26.89776, 69.93245], [26.85129, 69.96013], [26.71807, 69.94499], [26.67869, 69.96477], [26.46435, 69.93939], [26.38594, 69.85535], [26.24129, 69.81453], [26.13562, 69.73861], [26.01418, 69.72334], [25.89149, 69.6655], [25.97672, 69.61067], [25.93749, 69.57253], [25.83994, 69.54298], [25.87704, 69.5222], [25.80934, 69.42639], [25.8461, 69.39325], [25.75938, 69.34038], [25.74753, 69.28679], [25.70204, 69.25366], [25.69302, 69.19674], [25.74351, 69.13879], [25.72429, 69.0796], [25.77744, 69.01828], [25.71241, 68.98063], [25.65423, 68.90587], [25.60033, 68.88487], [25.48119, 68.90507], [25.2677, 68.85099], [25.15713, 68.79989], [25.11152, 68.70252], [25.11924, 68.6428], [24.91692, 68.60525], [24.85717, 68.56221], [24.78342, 68.63623], [24.60839, 68.6819], [24.30226, 68.71735], [24.07559, 68.77997], [23.98333, 68.82714], [23.87146, 68.83652], [23.77539, 68.81885], [23.73106, 68.75075], [23.67352, 68.70552], [23.44064, 68.69216], [23.16758, 68.62852], [23.04595, 68.68934], [22.80082, 68.68755], [22.53539, 68.74451], [22.37452, 68.71667], [22.34078, 68.82723], [22.19187, 68.91877], [22.17576, 68.95632], [21.98361, 69.07289], [21.8464, 69.14416], [21.62709, 69.27659], [21.27882, 69.31188], [21.09617, 69.26091], [21.00331, 69.22234], [20.98758, 69.19193], [21.05563, 69.12209], [21.10868, 69.10393], [21.05754, 69.03629], [20.71732, 69.11979], [20.55233, 69.06008], [20.06005, 69.04576], [20.30659, 68.92618], [20.33587, 68.80231], [20.20284, 68.66592], [20.05225, 68.59107], [19.9375, 68.55794], [20.02589, 68.53081], [20.22654, 68.49081], [19.97796, 68.38816], [19.9214, 68.35601], [18.9838, 68.51696], [18.62122, 68.50696], [18.40569, 68.58188], [18.12592, 68.53652], [18.10109, 68.40605], [18.15135, 68.19879], [17.89976, 67.96937], [17.66475, 68.03838], [17.28152, 68.11881], [17.18051, 68.05046], [16.73812, 67.91421], [16.55628, 67.64719], [16.40757, 67.53403], [16.158, 67.51916], [16.08983, 67.43528], [16.4041, 67.20497], [16.38776, 67.04546], [16.19402, 66.98259], [16.03876, 66.91245], [15.99364, 66.87323], [15.62137, 66.59434], [15.37723, 66.4843], [15.48473, 66.28246], [15.03568, 66.15356], [14.51629, 66.13258], [14.58441, 65.90134], [14.62548, 65.81181], [14.54147, 65.70075], [14.49877, 65.5213], [14.50683, 65.30973], [14.3788, 65.24762], [14.32598, 65.11892], [14.12989, 64.97856], [13.70547, 64.63996], [13.65426, 64.58034], [13.89118, 64.50713], [14.08523, 64.47825], [14.11387, 64.46248], [14.15711, 64.19505], [13.96752, 64.00797], [13.7154, 64.04629], [13.21111, 64.09537], [12.92672, 64.05795], [12.68356, 63.97422], [12.48023, 63.81876], [12.33057, 63.71507], [12.29946, 63.67198], [12.14977, 63.59395], [12.21288, 63.47859], [12.08407, 63.35558], [11.97458, 63.26923], [12.21823, 63.00033], [12.07469, 62.90254], [12.13638, 62.74792], [12.05614, 62.61192], [12.29937, 62.26749], [12.13766, 61.72382], [12.41961, 61.56298], [12.56932, 61.56875], [12.87085, 61.3565], [12.83383, 61.25846], [12.79035, 61.19705], [12.70703, 61.14327], [12.68258, 61.06122], [12.61251, 61.04683], [12.44761, 61.05073], [12.22399, 61.01308], [12.33279, 60.89017], [12.33448, 60.85236], [12.39537, 60.73389], [12.51102, 60.64246], [12.51578, 60.60015], [12.60688, 60.51274], [12.60605, 60.40593], [12.49879, 60.32365], [12.54191, 60.19338], [12.50064, 60.09908], [12.44856, 60.03917], [12.34114, 59.96567], [12.23104, 59.92759], [12.17429, 59.88981], [12.05346, 59.88594], [11.98518, 59.90072], [11.84045, 59.84174], [11.92597, 59.794], [11.93988, 59.69458], [11.88922, 59.69321], [11.85571, 59.64829], [11.72056, 59.62549], [11.69113, 59.58955], [11.75993, 59.45818], [11.77987, 59.38646], [11.81625, 59.34474], [11.82979, 59.24223], [11.78393, 59.20838], [11.77539, 59.08659], [11.71051, 59.03368], [11.68908, 58.95685], [11.59063, 58.89072], [11.45623, 58.89021], [11.45853, 58.99597], [11.34184, 59.12041], [11.20498, 59.08311], [11.17718, 59.09736], [11.1, 59], [11.0203, 58.97], [9.67858, 58.87844], [8.51901, 58.15871], [7.92368, 57.95878], [6.62638, 57.9188], [5.34686, 58.63409], [4.70265, 59.35382], [4.57381, 61.1576], [4.78262, 62.0506], [5.46681, 62.55263], [6.79965, 62.99691], [8.29243, 63.77884], [9.92293, 64.11205], [10.71819, 65.0095], [11.4246, 65.12057], [11.79779, 65.84919], [11.95329, 67.64852], [13.20171, 68.29717], [14.5701, 68.89694], [16.08064, 69.41675], [17.91552, 69.8166], [19.1906, 70.36306], [19.81259, 70.33196], [20.19467, 70.19424], [21.78519, 70.50523], [21.89626, 70.73182], [23.70892, 70.96284], [23.91773, 71.1139], [24.46864, 71.07391], [24.71744, 71.21608], [25.89478, 71.26051], [26.77445, 71.08724], [27.79185, 71.22052], [28.65819, 71.06503], [30.03102, 70.78069], [31.23946, 70.43859], [31.19482, 70.34084], [30.79367, 69.78758]], [[4.2, 80.84], [-11.5, 70.1], [19.2, 73.5], [39.2, 81.4]]],
  1531. Sweden: [[[15.4538561, 66.34534869999999], [15.3772302, 66.4843117], [15.625833, 66.605833], [15.80794, 66.735271], [16.0387632, 66.9124213], [16.195223, 66.982232], [16.3877, 67.0455], [16.4040109, 67.2049795], [16.09015, 67.435232], [16.1566, 67.519458], [16.407797, 67.533978], [16.555733, 67.647289], [16.7381292, 67.91418620000002], [17.180003, 68.050508], [17.2818957, 68.1188101], [17.6648128, 68.0384733], [17.8998048, 67.9693359], [18.1514126, 68.198755], [18.1010915, 68.406043], [18.1258499, 68.5364954], [18.4056102, 68.5818554], [18.6211478, 68.5069382], [18.9836971, 68.5169473], [19.921397, 68.3560137], [19.9778586, 68.3881535], [20.2264196, 68.4908071], [19.9375039, 68.5579418], [20.0521233, 68.5910515], [20.2027029, 68.6659076], [20.3358646, 68.8023404], [20.3064282, 68.9261735], [20.0600472, 69.0457578], [20.5486422, 69.05996990000001], [20.7750428, 69.0326073], [20.9137291, 68.9603927], [20.8441913, 68.93656440000002], [20.9156942, 68.8971424], [20.9967921, 68.896741], [21.2340165, 68.8140862], [21.3194271, 68.7592708], [21.3893348, 68.76495460000002], [21.4298688, 68.691352], [21.5651505, 68.6752534], [21.7013706, 68.6305605], [21.7016655, 68.5963461], [21.8898693, 68.5844051], [21.9919125, 68.5339794], [22.0182391, 68.495951], [22.1528153, 68.4701805], [22.2945732, 68.4838241], [22.4661749, 68.4413001], [22.6482126, 68.41604160000001], [22.7362404, 68.3852018], [22.8041064, 68.39294], [22.9181313, 68.3335115], [23.0702517, 68.29970360000002], [23.1528179, 68.2310713], [23.1415318, 68.1543005], [23.2783645, 68.15733889999998], [23.3216014, 68.1347101], [23.3966203, 68.044179], [23.5310194, 68.0067455], [23.6632301, 67.94218640000001], [23.6407972, 67.9151784], [23.5098377, 67.87994509999999], [23.4739757, 67.81714420000002], [23.4946531, 67.7903019], [23.493057, 67.6641861], [23.5588847, 67.6192741], [23.5450496, 67.5829545], [23.4081036, 67.50173829999999], [23.4104738, 67.46759370000002], [23.5365192, 67.4599963], [23.7632859, 67.4262029], [23.7179667, 67.384843], [23.7750768, 67.3393805], [23.7311639, 67.28763560000002], [23.5834506, 67.269308], [23.5535126, 67.2468025], [23.5958386, 67.2071971], [23.5569385, 67.16578719999998], [23.6536532, 67.1042345], [23.6739708, 67.0650834], [23.8564714, 66.9558968], [23.8640579, 66.9221303], [23.9330592, 66.8845665], [23.9945079, 66.82348849999998], [23.9782068, 66.78409040000001], [23.8797209, 66.7620511], [23.9078441, 66.72140390000001], [23.8685321, 66.6568254], [23.8846737, 66.61277119999998], [23.8605347, 66.5595503], [23.7853219, 66.5333886], [23.6509657, 66.4557476], [23.6880374, 66.3815611], [23.6489848, 66.3037725], [23.7263744, 66.1968556], [23.9159179, 66.1621612], [23.936749, 66.0794759], [24.0374327, 66.0090364], [24.0421963, 65.9633925], [24.152978, 65.862572], [24.1318042, 65.7716089], [24.1721721, 65.72528229999999], [24.1776819, 65.6603564], [24.1319001, 65.5153846], [24.1444599, 65.3956667], [23.1299456, 65.2854532], [21.8250561, 64.8363612], [22.0872366, 64.43431070000001], [21.5096176, 64.04121570000002], [21.4570471, 63.7528427], [20.20662871333013, 63.274568586669865], [19.4322896, 63.0737152], [18.2961641, 62.4173632], [17.7755886, 61.1718712], [17.8981165, 60.9377595], [17.7095869, 60.7102649], [17.3865202, 60.6893467], [17.3489744, 60.5862714], [17.3024177, 60.508762], [17.29774, 60.4647038], [17.2565412, 60.4243351], [17.1955585, 60.4105852], [17.1986283, 60.3077815], [17.0585097, 60.2727725], [16.908878, 60.281498], [16.9048859, 60.2394077], [16.7046001, 60.1950497], [16.6294785, 60.2384924], [16.6154023, 60.2786235], [16.5166127, 60.3554293], [16.3927146, 60.3794045], [16.2589904, 60.4931441], [16.1947891, 60.5354328], [16.13651, 60.6103267], [16.2382972, 60.6230491], [16.3769218, 60.7434488], [16.386117, 60.7868], [16.2552139, 60.8636119], [16.1310092, 60.9920575], [15.9216155, 61.00763], [15.7619207, 61.0496869], [15.6803816, 61.11321], [15.6573361, 61.2154788], [15.4760187, 61.3149858], [15.3370007, 61.4016369], [15.20475, 61.503826], [15.1531933, 61.5956892], [14.8564014, 61.7835491], [14.7971, 61.798451], [14.6666465, 61.8918775], [14.5296202, 61.783626], [14.4997464, 61.62599], [14.3947754, 61.5637652], [14.3364964, 61.59913920000001], [14.1822587, 61.6175455], [13.9769516, 61.6213397], [13.8902353, 61.6525473], [13.6131488, 61.6726273], [13.564749, 61.656455], [13.5066718, 61.6929666], [13.5145384, 61.7377738], [13.4160916, 61.8280592], [13.2092287, 61.9365972], [13.0799221, 62.0376119], [13.0423631, 62.0182008], [12.9513736, 62.1334555], [12.9026405, 62.1418727], [12.8059683, 62.2205277], [12.6078489, 62.214806], [12.299389, 62.2659814], [12.056144, 62.6119191], [12.1363845, 62.7479169], [12.074689, 62.9025463], [12.218233, 63.0003345], [11.9745822, 63.2692252], [12.0840901, 63.3555796], [12.2128783, 63.4785906], [12.1497625, 63.593946], [12.2975812, 63.6732169], [12.3399662, 63.7269855], [12.4797773, 63.8196667], [12.6860556, 63.9738931], [12.9268369, 64.05783829999999], [13.2109436, 64.0951725], [13.7151219, 64.045304], [13.981667, 64.013056], [14.1579301, 64.1860759], [14.120556, 64.452778], [14.086006, 64.47814109999999], [13.8924406, 64.507004], [13.6540802, 64.579929], [13.7050997, 64.6396655], [14.1081927, 64.96225790000001], [14.3257603, 65.1190618], [14.3790211, 65.24804960000002], [14.5056577, 65.3099238], [14.4967711, 65.5174317], [14.5295213, 65.682227], [14.6240045, 65.81419090000001], [14.584253, 65.9013501], [14.5162846, 66.132567], [15.035653, 66.1535649], [15.4847146, 66.282458], [15.4538561, 66.34534869999999]]],
  1532. USA: [[[-130.01989, 55.9153], [-130.17038, 55.77749], [-130.13861, 55.55335], [-129.99201, 55.28955], [-130.25933, 54.99635], [-130.66666, 54.71444], [-131.17048, 54.72103], [-132.10046, 54.6269], [-132.86477, 54.63066], [-133.60649, 54.72479], [-134.93933, 56.02375], [-136.80681, 57.75192], [-137.09296, 58.25079], [-139.07716, 59.1017], [-141.32115, 59.76436], [-143.47102, 59.81707], [-146.37014, 59.17701], [-149.21654, 59.54598], [-152.0253, 57.0535], [-155.80544, 55.02035], [-159.93198, 54.32757], [-173.1399, 51.33056], [-179.49537, 50.84863], [-179.28453, 52.29443], [-171.78447, 63.95114], [-169.94709, 63.91437], [-169.09903, 65.86662], [-168.1474, 65.7885], [-164.9772, 66.85025], [-167.15342, 68.37135], [-166.29498, 69.12437], [-161.71663, 70.74335], [-156.23466, 71.55661], [-143.75716, 70.6304], [-141.58847, 70.26895], [-141.56335, 69.73575], [-141.39798, 69.64277], [-141.00304, 69.64616], [-141.00189, 60.6745], [-141.00157, 60.30507], [-140.52034, 60.21906], [-140.44797, 60.30796], [-139.97408, 60.18451], [-139.68007, 60.33572], [-139.05208, 60.35373], [-139.17702, 60.08286], [-138.70578, 59.90624], [-138.60921, 59.76], [-137.60744, 59.24348], [-137.45151, 58.90854], [-136.82468, 59.1598], [-136.58199, 59.16554], [-136.19525, 59.63881], [-135.9476, 59.66343], [-135.47958, 59.7981], [-135.02888, 59.56364], [-135.10063, 59.42776], [-134.95978, 59.28104], [-134.7007, 59.2489], [-134.48273, 59.13097], [-134.258, 58.86087], [-133.84105, 58.72985], [-133.37997, 58.43181], [-133.45987, 58.38848], [-133.17195, 58.15383], [-132.55389, 57.4967], [-132.2478, 57.21112], [-132.36871, 57.09167], [-132.0448, 57.0451], [-132.12311, 56.8739], [-131.87311, 56.80627], [-131.83539, 56.59912], [-131.5813, 56.6123], [-131.08698, 56.40613], [-130.7818, 56.36713], [-130.4682, 56.24329], [-130.42548, 56.14172], [-130.10541, 56.12268], [-130.01989, 55.9153]], [[179.9, 52.2], [172.0, 53.3], [172.0, 52.4], [179.9, 51.0]]],
  1533. };
  1534. /* eslint-enable max-len */
  1535. let usingClockLocations = true;
  1536. const useClockLocations = (useClockLocations) => {
  1537. usingClockLocations = useClockLocations;
  1538. };
  1539. const pointInPolygon = (coords, polygon) => {
  1540. const [x, y] = coords;
  1541. let inside = false;
  1542. for (let i = 0, j = polygon.length - 1; i < polygon.length; i++) {
  1543. const [xi, yi] = polygon[i];
  1544. const [xj, yj] = polygon[j];
  1545. // Check that a) the segment crosses the y coordinate of the point
  1546. // b) at least one of the two vertices is left of the point
  1547. // c) at the y coordinate of the point, the segment is left of it
  1548. if ((((yi < y) !== (yj < y)) && (xi <= x || xj <= x)) && ((xi + (y - yi) * (xj - xi) / (yj - yi)) < x)) {
  1549. inside = !inside;
  1550. }
  1551. j = i;
  1552. }
  1553. return inside;
  1554. };
  1555. // The name of a country being returned doesn't just mean that the coordinates are within that country, but that they
  1556. // are within the region of that country where a fixed time rule applies.
  1557. const clockLocationFromPolygons = (latitude, longitude) => {
  1558. if (!usingClockLocations) {
  1559. return undefined;
  1560. }
  1561. // First exclude as large an area as possible from having to check polygons
  1562. if (latitude < 51.0) {
  1563. return undefined;
  1564. }
  1565. if (latitude < 57.0 && longitude > -129.0 && longitude < 172.0) {
  1566. return undefined;
  1567. }
  1568. // Make a list of plausible areas based on longitude, then only check those
  1569. const countries = [];
  1570. const labels = [];
  1571. if (longitude < -129.9 || longitude > 172.4) {
  1572. countries.push(clockLocations.USA);
  1573. labels.push('USA');
  1574. }
  1575. if (longitude > -141.1 && longitude < -61.1) {
  1576. countries.push(clockLocations.Canada);
  1577. labels.push('Canada');
  1578. }
  1579. // Greenland doesn't currently have a rule for this
  1580. // if (longitude > -73.1 && longitude < -11.3) {
  1581. // countries.push(clockLocations.Greenland);
  1582. // labels.push('Greenland');
  1583. // }
  1584. if (longitude > -25.0 && longitude < -12.8) {
  1585. countries.push(clockLocations.Iceland);
  1586. labels.push('Iceland');
  1587. }
  1588. if (longitude > -9.2 && longitude < 33.6) {
  1589. countries.push(clockLocations.Norway);
  1590. labels.push('Norway');
  1591. }
  1592. if (longitude > 10.9 && longitude < 24.2) {
  1593. countries.push(clockLocations.Sweden);
  1594. labels.push('Sweden');
  1595. }
  1596. if (longitude > 19.1 && longitude < 31.6) {
  1597. countries.push(clockLocations.Finland);
  1598. labels.push('Finland');
  1599. }
  1600. // Russia currently doesn't have a rule for this
  1601. // if (longitude > 27.3 || longitude < -169.6) {
  1602. // countries.push(clockLocations.Russia);
  1603. // labels.push('Russia');
  1604. // }
  1605. for (let i = 0; i < countries.length; i++) {
  1606. for (let j = 0; j < countries[i].length; j++) {
  1607. if (pointInPolygon([longitude, latitude], countries[i][j])) {
  1608. return labels[i];
  1609. }
  1610. }
  1611. }
  1612. return undefined;
  1613. };
  1614. /* eslint-disable complexity */
  1615. class LocalBadiDate {
  1616. constructor(date, latitude, longitude, timezoneId) {
  1617. this._latitude = latitude;
  1618. this._longitude = longitude;
  1619. this._timezoneId = timezoneId;
  1620. // If a datetime object is being passed, we use date and time, not just the
  1621. // date. For a JS Date object, we can't assume it's in the correct timezone,
  1622. // so in that case we use the date information only.
  1623. this._badiDate = new BadiDate(this._setInputDateToCorrectDay(date, latitude, longitude));
  1624. const gregDate = this._badiDate.gregorianDate.setZone(timezoneId, { keepLocalTime: true });
  1625. this._clockLocation = clockLocationFromPolygons(latitude, longitude);
  1626. if (!this._clockLocation ||
  1627. (this._clockLocation === 'Finland' &&
  1628. this._badiDate.month === 19)) {
  1629. this._end = sunset(gregDate, latitude, longitude);
  1630. this._solarNoon = solarNoon(gregDate, longitude);
  1631. this._sunrise = sunrise(gregDate, latitude, longitude);
  1632. this._start = sunset(gregDate.minus({ days: 1 }), latitude, longitude);
  1633. }
  1634. else {
  1635. // First we set times to 18:00, 06:00, 12:00, 18:00, modifications are
  1636. // then made depending on the region.
  1637. this._start = gregDate.minus({ days: 1 }).set({ hour: 18 });
  1638. this._solarNoon = gregDate.set({ hour: 12 });
  1639. this._sunrise = gregDate.set({ hour: 6 });
  1640. this._end = gregDate.set({ hour: 18 });
  1641. if (this._clockLocation === 'Canada') {
  1642. this._sunrise = this._sunrise.plus({ minutes: 30 });
  1643. }
  1644. else if (this._clockLocation === 'Iceland') {
  1645. this._solarNoon = this._solarNoon.plus({ hours: 1 });
  1646. }
  1647. else if (this._clockLocation === 'Finland' ||
  1648. this._clockLocation === 'USA') {
  1649. if (this._end.isInDST) {
  1650. this._sunrise = this._sunrise.plus({ hours: 1 });
  1651. this._solarNoon = this._solarNoon.plus({ hours: 1 });
  1652. this._end = this._end.plus({ hours: 1 });
  1653. }
  1654. if (this._start.isInDST) {
  1655. this._start = this._start.plus({ hours: 1 });
  1656. }
  1657. }
  1658. }
  1659. switch (this._badiDate.holyDayNumber) {
  1660. case 2:
  1661. // First Day of Ridvan: 15:00 local standard time
  1662. this._holyDayCommemoration = gregDate.set({ hour: gregDate.isInDST ? 16 : 15 });
  1663. break;
  1664. case 5:
  1665. // Declaration of the Báb: 2 hours 11 minutes after sunset
  1666. this._holyDayCommemoration = this._start.plus({ minutes: 131 });
  1667. break;
  1668. case 6:
  1669. // Ascension of Bahá'u'lláh: 03:00 local standard time
  1670. this._holyDayCommemoration = gregDate.set({ hour: gregDate.isInDST ? 4 : 3 });
  1671. break;
  1672. case 7:
  1673. // Martyrdom of the Báb: solar noon
  1674. this._holyDayCommemoration = this._solarNoon;
  1675. break;
  1676. case 11:
  1677. // Ascension of 'Abdu'l-Bahá: 01:00 local standard time
  1678. this._holyDayCommemoration = gregDate.set({ hour: gregDate.isInDST ? 2 : 1 });
  1679. break;
  1680. // skip default
  1681. }
  1682. }
  1683. _setInputDateToCorrectDay(date, latitude, longitude) {
  1684. if (luxon.DateTime.isDateTime(date)) {
  1685. const sunset$1 = sunset(date, latitude, longitude);
  1686. return (date > sunset$1) ? date.plus({ days: 1 }) : date;
  1687. }
  1688. return date;
  1689. }
  1690. get badiDate() {
  1691. return this._badiDate;
  1692. }
  1693. get start() {
  1694. return this._start;
  1695. }
  1696. get sunrise() {
  1697. return this._sunrise;
  1698. }
  1699. get solarNoon() {
  1700. return this._solarNoon;
  1701. }
  1702. get end() {
  1703. return this._end;
  1704. }
  1705. get holyDayCommemoration() {
  1706. return this._holyDayCommemoration;
  1707. }
  1708. get clockLocation() {
  1709. return this._clockLocation;
  1710. }
  1711. get latitude() {
  1712. return this._latitude;
  1713. }
  1714. get longitude() {
  1715. return this._longitude;
  1716. }
  1717. get timezoneId() {
  1718. return this._timezoneId;
  1719. }
  1720. get nextMonth() {
  1721. return new LocalBadiDate(this.badiDate.nextMonth, this._latitude, this._longitude, this._timezoneId);
  1722. }
  1723. get previousMonth() {
  1724. return new LocalBadiDate(this.badiDate.previousMonth, this._latitude, this._longitude, this._timezoneId);
  1725. }
  1726. get nextDay() {
  1727. return new LocalBadiDate(this.badiDate.nextDay, this._latitude, this._longitude, this._timezoneId);
  1728. }
  1729. get previousDay() {
  1730. return new LocalBadiDate(this.badiDate.previousDay, this._latitude, this._longitude, this._timezoneId);
  1731. }
  1732. }
  1733. const badiDateSettings$1 = (settings) => {
  1734. if (typeof settings.defaultLanguage === 'string' ||
  1735. typeof settings.underlineFormat === 'string') {
  1736. badiDateSettings(settings);
  1737. }
  1738. if (typeof settings.useClockLocations === 'boolean') {
  1739. useClockLocations(settings.useClockLocations);
  1740. }
  1741. };
  1742. settings({ returnTimeForNoEventCase: true, roundToNearestMinute: true });
  1743. exports.BadiDate = BadiDate;
  1744. exports.LocalBadiDate = LocalBadiDate;
  1745. exports.badiDateSettings = badiDateSettings$1;
  1746. Object.defineProperty(exports, '__esModule', { value: true });
  1747. })));