File size: 3,689 Bytes
cf86710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { getMonthCode } from "./calendarMath.js";
import { toGregorianDate, toHebrewDate } from "./dateConversion.js";

describe("hebrew absolute conversions", () => {
  test("converts gregorian dates to hebrew and back", () => {
    const roshHashanah5785 = new Date(2024, 9, 3);
    const hebrew = toHebrewDate(roshHashanah5785);
    expect(hebrew.year).toBe(5785);
    expect(hebrew.monthIndex).toBe(0);
    expect(hebrew.day).toBe(1);

    const back = toGregorianDate(hebrew);
    expect(back.getFullYear()).toBe(2024);
    expect(back.getMonth()).toBe(9);
    expect(back.getDate()).toBe(3);
  });

  test("round-trips a leap month date", () => {
    const passover5784 = new Date(2024, 3, 23);
    const hebrew = toHebrewDate(passover5784);
    expect(hebrew.year).toBe(5784);
    expect(getMonthCode(hebrew.year, hebrew.monthIndex)).toBe("nisan");
    expect(hebrew.day).toBe(15);

    const back = toGregorianDate(hebrew);
    expect(back.getFullYear()).toBe(2024);
    expect(back.getMonth()).toBe(3);
    expect(back.getDate()).toBe(23);
  });

  test("rollover around rosh hashanah", () => {
    const roshHashanah5785 = new Date(2024, 9, 3);
    const previousDay = new Date(2024, 9, 2);
    const prevHebrew = toHebrewDate(previousDay);
    expect(getMonthCode(prevHebrew.year, prevHebrew.monthIndex)).toBe("elul");
    expect(prevHebrew.day).toBeGreaterThan(0);

    const nextHebrew = toHebrewDate(roshHashanah5785);
    expect(nextHebrew.year).toBe(prevHebrew.year + 1);
    expect(getMonthCode(nextHebrew.year, nextHebrew.monthIndex)).toBe(
      "tishrei",
    );
    expect(nextHebrew.day).toBe(1);
  });

  test("leap year transitions across adar months", () => {
    const adarI = new Date(2024, 1, 12);
    const hebrewAdarI = toHebrewDate(adarI);
    expect(getMonthCode(hebrewAdarI.year, hebrewAdarI.monthIndex)).toBe(
      "adarI",
    );

    const adarIi = new Date(2024, 2, 12);
    const hebrewAdarIi = toHebrewDate(adarIi);
    expect(getMonthCode(hebrewAdarIi.year, hebrewAdarIi.monthIndex)).toBe(
      "adar",
    );

    const nisan = new Date(2024, 3, 12);
    const hebrewNisan = toHebrewDate(nisan);
    expect(getMonthCode(hebrewNisan.year, hebrewNisan.monthIndex)).toBe(
      "nisan",
    );
  });

  test("conversion boundaries for past and future", () => {
    const farPast = new Date(1900, 0, 1);
    const pastHebrew = toHebrewDate(farPast);
    const pastBack = toGregorianDate(pastHebrew);
    expect(pastBack.getFullYear()).toBe(farPast.getFullYear());
    expect(pastBack.getMonth()).toBe(farPast.getMonth());
    expect(pastBack.getDate()).toBe(farPast.getDate());

    const farFuture = new Date(2300, 0, 1);
    const futureHebrew = toHebrewDate(farFuture);
    const futureBack = toGregorianDate(futureHebrew);
    expect(futureBack.getFullYear()).toBe(farFuture.getFullYear());
    expect(futureBack.getMonth()).toBe(farFuture.getMonth());
    expect(futureBack.getDate()).toBe(farFuture.getDate());
  });

  test("ignores time component when converting", () => {
    const withTime = new Date(2024, 3, 23, 15, 30, 45);
    const hebrew = toHebrewDate(withTime);
    const back = toGregorianDate(hebrew);
    expect(back.getFullYear()).toBe(2024);
    expect(back.getMonth()).toBe(3);
    expect(back.getDate()).toBe(23);
    expect(back.getHours()).toBe(0);
    expect(back.getMinutes()).toBe(0);
  });

  test("handles very early gregorian dates", () => {
    const early = new Date(0);
    early.setUTCFullYear(10, 0, 15);
    const hebrew = toHebrewDate(early);
    const back = toGregorianDate(hebrew);
    expect(back.getFullYear()).toBe(10);
    expect(back.getMonth()).toBe(0);
    expect(back.getDate()).toBe(15);
  });
});