Skip to content

Commit 3f5c633

Browse files
committed
fix: complexe calendar system conversions are now working, example: persian to islamic to gregory to persian....
1 parent 82243c2 commit 3f5c633

File tree

2 files changed

+72
-8
lines changed

2 files changed

+72
-8
lines changed

Diff for: src/index.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,12 @@ export default (options, dayjsClass, dayjsFactory) => {
322322
// If the instance is not already in the required calendar system, initialize it
323323
// You would also convert the date to the specified calendar here.
324324
if (newInstance.$C !== calendar) {
325-
// Store the current calendar system in $C
326-
newInstance.$C = calendar;
327-
// if calendar is gregorian, convert to gregorian, otherwise convert to calendar
325+
// if target calendar is gregorian, convert to gregorian, otherwise convert to calendar
328326
if (calendar === "gregory") {
329-
const gregoryDate = newInstance.toDate();
330-
const convertedDate = calendarSystems[calendar].convertToGregorian(
331-
gregoryDate.getFullYear(),
332-
gregoryDate.getMonth(),
333-
gregoryDate.getDate()
327+
const convertedDate = calendarSystems[newInstance.$C || "gregory"].convertToGregorian(
328+
newInstance.$y,
329+
newInstance.$M,
330+
newInstance.$D
334331
);
335332
newInstance.$G_y = convertedDate.year;
336333
newInstance.$G_M = convertedDate.month;
@@ -355,6 +352,9 @@ export default (options, dayjsClass, dayjsFactory) => {
355352
newInstance.$M = convertedDate.month;
356353
newInstance.$D = convertedDate.day;
357354
}
355+
356+
// Store the target calendar system in $C
357+
newInstance.$C = calendar;
358358
}
359359
// Update the locale to reflect the new calendar system
360360
dayjsFactory.updateLocale(

Diff for: test/calendarSystemConversions.test.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import dayjs from "dayjs";
2+
import calendarSystems from "../src/index";
3+
import GregoryCalendarSystem from "../src/calendarSystems/GregoryCalendarSystem";
4+
import PersianCalendarSystem from "../src/calendarSystems/PersianCalendarSystem";
5+
import HijriCalendarSystem from "../src/calendarSystems/HijriCalendarSystem";
6+
7+
describe('Calendar Systems Conversion', () => {
8+
const targetDate = "2023-05-24";
9+
const targetPersianDate = "1402-03-03";
10+
const targetIslamicDate = "1444-11-04";
11+
12+
beforeAll(() => {
13+
dayjs.extend(calendarSystems);
14+
dayjs.registerCalendarSystem("gregory", new GregoryCalendarSystem());
15+
dayjs.registerCalendarSystem("persian", new PersianCalendarSystem());
16+
dayjs.registerCalendarSystem("islamic", new HijriCalendarSystem());
17+
});
18+
19+
test('Convert to Persian', () => {
20+
const persianDate = dayjs(targetDate).toCalendarSystem("persian").format("YYYY-MM-DD");
21+
expect(persianDate).toEqual(targetPersianDate);
22+
});
23+
24+
test('Convert to Gregorian', () => {
25+
const gregorianDate = dayjs(targetDate).toCalendarSystem("gregory").format("YYYY-MM-DD");
26+
expect(gregorianDate).toEqual(targetDate); // Gregorian date should be the same as targetDate
27+
});
28+
29+
test('Convert to Islamic', () => {
30+
const hijriDate = dayjs(targetDate).toCalendarSystem("islamic").format("YYYY-MM-DD");
31+
expect(hijriDate).toEqual(targetIslamicDate);
32+
});
33+
34+
test('Convert Persian to Gregorian', () => {
35+
const persianToGregorian = dayjs(targetDate).toCalendarSystem("persian").toCalendarSystem("gregory").format("YYYY-MM-DD");
36+
expect(persianToGregorian).toEqual(targetDate);
37+
});
38+
39+
test('Convert Persian to Islamic', () => {
40+
const persianToIslamic = dayjs(targetDate).toCalendarSystem("persian").toCalendarSystem("islamic").format("YYYY-MM-DD");
41+
expect(persianToIslamic).toEqual(targetIslamicDate);
42+
});
43+
44+
test('Convert Gregorian to Persian', () => {
45+
const gregorianToPersian = dayjs(targetDate).toCalendarSystem("gregory").toCalendarSystem("persian").format("YYYY-MM-DD");
46+
expect(gregorianToPersian).toEqual(targetPersianDate);
47+
});
48+
49+
test('Convert Gregorian to Islamic', () => {
50+
const gregorianToIslamic = dayjs(targetDate).toCalendarSystem("gregory").toCalendarSystem("islamic").format("YYYY-MM-DD");
51+
expect(gregorianToIslamic).toEqual(targetIslamicDate);
52+
});
53+
54+
test('Convert Islamic to Persian', () => {
55+
const islamicToPersian = dayjs(targetDate).toCalendarSystem("islamic").toCalendarSystem("persian").format("YYYY-MM-DD");
56+
expect(islamicToPersian).toEqual(targetPersianDate);
57+
});
58+
59+
test('Convert Islamic to Gregorian', () => {
60+
const islamicToGregorian = dayjs(targetDate).toCalendarSystem("islamic").toCalendarSystem("gregory").format("YYYY-MM-DD");
61+
expect(islamicToGregorian).toEqual(targetDate);
62+
});
63+
64+
});

0 commit comments

Comments
 (0)