@hebcal/core 3.39.2 → 3.41.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,457 +0,0 @@
1
- /*! @hebcal/core v3.39.1 */
2
- /*
3
- * More minimal greg routines
4
- */
5
-
6
- /** @private */
7
- const lengths = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
8
- /** @private */
9
- const monthLengths = [
10
- lengths,
11
- lengths.slice(),
12
- ];
13
- monthLengths[1][2] = 29;
14
-
15
- /**
16
- * @private
17
- * @param {number} x
18
- * @param {number} y
19
- * @return {number}
20
- */
21
- function mod(x, y) {
22
- return x - y * Math.floor(x / y);
23
- }
24
-
25
- /**
26
- * @private
27
- * @param {number} x
28
- * @param {number} y
29
- * @return {number}
30
- */
31
- function quotient(x, y) {
32
- return Math.floor(x / y);
33
- }
34
-
35
- /**
36
- * Returns true if the Gregorian year is a leap year
37
- * @private
38
- * @param {number} year Gregorian year
39
- * @return {boolean}
40
- */
41
- function isLeapYear$1(year) {
42
- return !(year % 4) && (!!(year % 100) || !(year % 400));
43
- }
44
-
45
- /**
46
- * Returns true if the object is a Javascript Date
47
- * @private
48
- * @param {Object} obj
49
- * @return {boolean}
50
- */
51
- function isDate(obj) {
52
- return typeof obj === 'object' && Date.prototype === obj.__proto__;
53
- }
54
-
55
- /**
56
- * Returns number of days since January 1 of that year
57
- * @private
58
- * @param {Date} date Gregorian date
59
- * @return {number}
60
- */
61
- function dayOfYear(date) {
62
- if (!isDate(date)) {
63
- throw new TypeError(`Argument not a Date: ${date}`);
64
- }
65
- let doy = date.getDate() + 31 * date.getMonth();
66
- if (date.getMonth() > 1) {
67
- // FEB
68
- doy -= Math.floor((4 * (date.getMonth() + 1) + 23) / 10);
69
- if (isLeapYear$1(date.getFullYear())) {
70
- doy++;
71
- }
72
- }
73
- return doy;
74
- }
75
-
76
- /**
77
- * Converts Gregorian date to absolute R.D. (Rata Die) days
78
- * @private
79
- * @param {Date} date Gregorian date
80
- * @return {number}
81
- */
82
- function greg2abs(date) {
83
- if (!isDate(date)) {
84
- throw new TypeError(`Argument not a Date: ${date}`);
85
- }
86
- const year = date.getFullYear() - 1;
87
- return (
88
- dayOfYear(date) + // days this year
89
- 365 * year + // + days in prior years
90
- (Math.floor(year / 4) - // + Julian Leap years
91
- Math.floor(year / 100) + // - century years
92
- Math.floor(year / 400))
93
- ); // + Gregorian leap years
94
- }
95
-
96
- /**
97
- * @private
98
- * @param {number} abs - R.D. number of days
99
- * @return {number}
100
- */
101
- function yearFromFixed(abs) {
102
- const l0 = abs - 1;
103
- const n400 = quotient(l0, 146097);
104
- const d1 = mod(l0, 146097);
105
- const n100 = quotient(d1, 36524);
106
- const d2 = mod(d1, 36524);
107
- const n4 = quotient(d2, 1461);
108
- const d3 = mod(d2, 1461);
109
- const n1 = quotient(d3, 365);
110
- const year = 400 * n400 + 100 * n100 + 4 * n4 + n1;
111
- return n100 != 4 && n1 != 4 ? year + 1 : year;
112
- }
113
-
114
- /**
115
- * @private
116
- * @param {number} year
117
- * @param {number} month
118
- * @param {number} day
119
- * @return {number}
120
- */
121
- function toFixed(year, month, day) {
122
- const py = year - 1;
123
- return 0 +
124
- 365 * py +
125
- quotient(py, 4) -
126
- quotient(py, 100) +
127
- quotient(py, 400) +
128
- quotient((367 * month - 362), 12) +
129
- Math.floor(month <= 2 ? 0 : (isLeapYear$1(year) ? -1 : -2)) +
130
- day;
131
- }
132
-
133
- /**
134
- * Converts from Rata Die (R.D. number) to Gregorian date.
135
- * See the footnote on page 384 of ``Calendrical Calculations, Part II:
136
- * Three Historical Calendars'' by E. M. Reingold, N. Dershowitz, and S. M.
137
- * Clamen, Software--Practice and Experience, Volume 23, Number 4
138
- * (April, 1993), pages 383-404 for an explanation.
139
- * @private
140
- * @param {number} abs - R.D. number of days
141
- * @return {Date}
142
- */
143
- function abs2greg(abs) {
144
- if (typeof abs !== 'number') {
145
- throw new TypeError(`Argument not a Number: ${abs}`);
146
- }
147
- abs = Math.trunc(abs);
148
- const year = yearFromFixed(abs);
149
- const priorDays = abs - toFixed(year, 1, 1);
150
- const correction = abs < toFixed(year, 3, 1) ? 0 : (isLeapYear$1(year) ? 1 : 2);
151
- const month = quotient((12 * (priorDays + correction) + 373), 367);
152
- const day = abs - toFixed(year, month, 1) + 1;
153
- const dt = new Date(year, month - 1, day);
154
- if (year < 100 && year >= 0) {
155
- dt.setFullYear(year);
156
- }
157
- return dt;
158
- }
159
-
160
- /*
161
- * More minimal HDate
162
- */
163
-
164
- const NISAN = 1;
165
- const IYYAR = 2;
166
- // const SIVAN = 3;
167
- const TAMUZ = 4;
168
- // const AV = 5;
169
- const ELUL = 6;
170
- const TISHREI = 7;
171
- const CHESHVAN = 8;
172
- const KISLEV = 9;
173
- const TEVET = 10;
174
- // const SHVAT = 11;
175
- const ADAR_I = 12;
176
- const ADAR_II = 13;
177
-
178
- /**
179
- * Hebrew months of the year (NISAN=1, TISHREI=7)
180
- * @readonly
181
- * @enum {number}
182
- */
183
- const months = {
184
- /** Nissan / ניסן */
185
- NISAN: 1,
186
- /** Iyyar / אייר */
187
- IYYAR: 2,
188
- /** Sivan / סיון */
189
- SIVAN: 3,
190
- /** Tamuz (sometimes Tammuz) / תמוז */
191
- TAMUZ: 4,
192
- /** Av / אב */
193
- AV: 5,
194
- /** Elul / אלול */
195
- ELUL: 6,
196
- /** Tishrei / תִשְׁרֵי */
197
- TISHREI: 7,
198
- /** Cheshvan / חשון */
199
- CHESHVAN: 8,
200
- /** Kislev / כסלו */
201
- KISLEV: 9,
202
- /** Tevet / טבת */
203
- TEVET: 10,
204
- /** Sh'vat / שבט */
205
- SHVAT: 11,
206
- /** Adar or Adar Rishon / אדר */
207
- ADAR_I: 12,
208
- /** Adar Sheini (only on leap years) / אדר ב׳ */
209
- ADAR_II: 13,
210
- };
211
-
212
- const monthNames0 = [
213
- '',
214
- 'Nisan',
215
- 'Iyyar',
216
- 'Sivan',
217
- 'Tamuz',
218
- 'Av',
219
- 'Elul',
220
- 'Tishrei',
221
- 'Cheshvan',
222
- 'Kislev',
223
- 'Tevet',
224
- 'Sh\'vat',
225
- ];
226
-
227
- /**
228
- * Transliterations of Hebrew month names.
229
- * Regular years are index 0 and leap years are index 1.
230
- * @private
231
- */
232
- const monthNames = [
233
- monthNames0.concat([
234
- 'Adar',
235
- 'Nisan',
236
- ]),
237
- monthNames0.concat([
238
- 'Adar I',
239
- 'Adar II',
240
- 'Nisan',
241
- ]),
242
- ];
243
-
244
- const edCache = Object.create(null);
245
-
246
- const EPOCH = -1373428;
247
- // Avg year length in the cycle (19 solar years with 235 lunar months)
248
- const AVG_HEBYEAR_DAYS = 365.24682220597794;
249
-
250
- /**
251
- * Converts Hebrew date to R.D. (Rata Die) fixed days.
252
- * R.D. 1 is the imaginary date Monday, January 1, 1 on the Gregorian
253
- * Calendar.
254
- * @private
255
- * @param {number} year Hebrew year
256
- * @param {number} month Hebrew month
257
- * @param {number} day Hebrew date (1-30)
258
- * @return {number}
259
- */
260
- function hebrew2abs(year, month, day) {
261
- let tempabs = day;
262
-
263
- if (month < TISHREI) {
264
- for (let m = TISHREI; m <= monthsInYear(year); m++) {
265
- tempabs += daysInMonth(m, year);
266
- }
267
- for (let m = NISAN; m < month; m++) {
268
- tempabs += daysInMonth(m, year);
269
- }
270
- } else {
271
- for (let m = TISHREI; m < month; m++) {
272
- tempabs += daysInMonth(m, year);
273
- }
274
- }
275
-
276
- return EPOCH + elapsedDays(year) + tempabs - 1;
277
- }
278
-
279
- /**
280
- * @private
281
- * @param {number} year
282
- * @return {number}
283
- */
284
- function newYear(year) {
285
- return EPOCH + elapsedDays(year);
286
- }
287
-
288
- /**
289
- * Converts absolute R.D. days to Hebrew date
290
- * @private
291
- * @param {number} abs absolute R.D. days
292
- * @return {SimpleHebrewDate}
293
- */
294
- function abs2hebrew(abs) {
295
- if (typeof abs !== 'number' || isNaN(abs)) {
296
- throw new TypeError(`invalid parameter to abs2hebrew ${abs}`);
297
- }
298
- abs = Math.trunc(abs);
299
-
300
- // first, quickly approximate year
301
- let year = Math.floor((abs - EPOCH) / AVG_HEBYEAR_DAYS);
302
- while (newYear(year) <= abs) {
303
- ++year;
304
- }
305
- --year;
306
-
307
- let month = abs < hebrew2abs(year, 1, 1) ? 7 : 1;
308
- while (abs > hebrew2abs(year, month, daysInMonth(month, year))) {
309
- ++month;
310
- }
311
-
312
- const day = 1 + abs - hebrew2abs(year, month, 1);
313
- return {yy: year, mm: month, dd: day};
314
- }
315
-
316
- /**
317
- * Returns true if Hebrew year is a leap year
318
- * @private
319
- * @param {number} year Hebrew year
320
- * @return {boolean}
321
- */
322
- function isLeapYear(year) {
323
- return (1 + year * 7) % 19 < 7;
324
- }
325
-
326
- /**
327
- * Number of months in this Hebrew year (either 12 or 13 depending on leap year)
328
- * @private
329
- * @param {number} year Hebrew year
330
- * @return {number}
331
- */
332
- function monthsInYear(year) {
333
- return 12 + isLeapYear(year); // boolean is cast to 1 or 0
334
- }
335
-
336
- /**
337
- * Number of days in Hebrew month in a given year (29 or 30)
338
- * @private
339
- * @param {number} month Hebrew month (e.g. months.TISHREI)
340
- * @param {number} year Hebrew year
341
- * @return {number}
342
- */
343
- function daysInMonth(month, year) {
344
- switch (month) {
345
- case IYYAR:
346
- case TAMUZ:
347
- case ELUL:
348
- case TEVET:
349
- case ADAR_II:
350
- return 29;
351
- }
352
- if ((month === ADAR_I && !isLeapYear(year)) ||
353
- (month === CHESHVAN && !longCheshvan(year)) ||
354
- (month === KISLEV && shortKislev(year))) {
355
- return 29;
356
- } else {
357
- return 30;
358
- }
359
- }
360
-
361
- /**
362
- * Returns a transliterated string name of Hebrew month in year,
363
- * for example 'Elul' or 'Cheshvan'.
364
- * @private
365
- * @param {number} month Hebrew month (e.g. months.TISHREI)
366
- * @param {number} year Hebrew year
367
- * @return {string}
368
- */
369
- function getMonthName(month, year) {
370
- if (typeof month !== 'number' || isNaN(month) || month < 1 || month > 14) {
371
- throw new TypeError(`bad month argument ${month}`);
372
- }
373
- return monthNames[+isLeapYear(year)][month];
374
- }
375
-
376
- /**
377
- * Days from sunday prior to start of Hebrew calendar to mean
378
- * conjunction of Tishrei in Hebrew YEAR
379
- * @private
380
- * @param {number} year Hebrew year
381
- * @return {number}
382
- */
383
- function elapsedDays(year) {
384
- const elapsed = edCache[year] = edCache[year] || elapsedDays0(year);
385
- return elapsed;
386
- }
387
-
388
- /**
389
- * Days from sunday prior to start of Hebrew calendar to mean
390
- * conjunction of Tishrei in Hebrew YEAR
391
- * @private
392
- * @param {number} year Hebrew year
393
- * @return {number}
394
- */
395
- function elapsedDays0(year) {
396
- const prevYear = year - 1;
397
- const mElapsed = 235 * Math.floor(prevYear / 19) + // Months in complete 19 year lunar (Metonic) cycles so far
398
- 12 * (prevYear % 19) + // Regular months in this cycle
399
- Math.floor(((prevYear % 19) * 7 + 1) / 19); // Leap months this cycle
400
-
401
- const pElapsed = 204 + 793 * (mElapsed % 1080);
402
-
403
- const hElapsed = 5 +
404
- 12 * mElapsed +
405
- 793 * Math.floor(mElapsed / 1080) +
406
- Math.floor(pElapsed / 1080);
407
-
408
- const parts = (pElapsed % 1080) + 1080 * (hElapsed % 24);
409
-
410
- const day = 1 + 29 * mElapsed + Math.floor(hElapsed / 24);
411
- const altDay = day +
412
- (parts >= 19440 ||
413
- (2 === day % 7 && parts >= 9924 && !isLeapYear(year)) ||
414
- (1 === day % 7 && parts >= 16789 && isLeapYear(prevYear)));
415
-
416
- return altDay + (altDay % 7 === 0 || altDay % 7 === 3 || altDay % 7 === 5);
417
- }
418
-
419
- /**
420
- * Number of days in the hebrew YEAR.
421
- * A common Hebrew calendar year can have a length of 353, 354 or 355 days
422
- * A leap Hebrew calendar year can have a length of 383, 384 or 385 days
423
- * @private
424
- * @param {number} year Hebrew year
425
- * @return {number}
426
- */
427
- function daysInYear(year) {
428
- return elapsedDays(year + 1) - elapsedDays(year);
429
- }
430
-
431
- /**
432
- * true if Cheshvan is long in Hebrew year
433
- * @private
434
- * @param {number} year Hebrew year
435
- * @return {boolean}
436
- */
437
- function longCheshvan(year) {
438
- return daysInYear(year) % 10 === 5;
439
- }
440
-
441
- /**
442
- * true if Kislev is short in Hebrew year
443
- * @private
444
- * @param {number} year Hebrew year
445
- * @return {boolean}
446
- */
447
- function shortKislev(year) {
448
- return daysInYear(year) % 10 === 3;
449
- }
450
-
451
- const hdate = {
452
- abs2hebrew, daysInMonth, daysInYear, getMonthName, hebrew2abs,
453
- isLeapYear, longCheshvan, months,
454
- monthsInYear, shortKislev,
455
- };
456
-
457
- export { abs2greg, greg2abs, hdate };