Skip to content

Commit

Permalink
fix: complexe calendar system conversions are now working, example: p…
Browse files Browse the repository at this point in the history
…ersian to islamic to gregory to persian....
  • Loading branch information
amirhmoradi committed May 23, 2023
1 parent 82243c2 commit 3f5c633
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,12 @@ export default (options, dayjsClass, dayjsFactory) => {
// If the instance is not already in the required calendar system, initialize it
// You would also convert the date to the specified calendar here.
if (newInstance.$C !== calendar) {
// Store the current calendar system in $C
newInstance.$C = calendar;
// if calendar is gregorian, convert to gregorian, otherwise convert to calendar
// if target calendar is gregorian, convert to gregorian, otherwise convert to calendar
if (calendar === "gregory") {
const gregoryDate = newInstance.toDate();
const convertedDate = calendarSystems[calendar].convertToGregorian(
gregoryDate.getFullYear(),
gregoryDate.getMonth(),
gregoryDate.getDate()
const convertedDate = calendarSystems[newInstance.$C || "gregory"].convertToGregorian(
newInstance.$y,
newInstance.$M,
newInstance.$D
);
newInstance.$G_y = convertedDate.year;
newInstance.$G_M = convertedDate.month;
Expand All @@ -355,6 +352,9 @@ export default (options, dayjsClass, dayjsFactory) => {
newInstance.$M = convertedDate.month;
newInstance.$D = convertedDate.day;
}

// Store the target calendar system in $C
newInstance.$C = calendar;
}
// Update the locale to reflect the new calendar system
dayjsFactory.updateLocale(
Expand Down
64 changes: 64 additions & 0 deletions test/calendarSystemConversions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import dayjs from "dayjs";
import calendarSystems from "../src/index";
import GregoryCalendarSystem from "../src/calendarSystems/GregoryCalendarSystem";
import PersianCalendarSystem from "../src/calendarSystems/PersianCalendarSystem";
import HijriCalendarSystem from "../src/calendarSystems/HijriCalendarSystem";

describe('Calendar Systems Conversion', () => {
const targetDate = "2023-05-24";
const targetPersianDate = "1402-03-03";
const targetIslamicDate = "1444-11-04";

beforeAll(() => {
dayjs.extend(calendarSystems);
dayjs.registerCalendarSystem("gregory", new GregoryCalendarSystem());
dayjs.registerCalendarSystem("persian", new PersianCalendarSystem());
dayjs.registerCalendarSystem("islamic", new HijriCalendarSystem());
});

test('Convert to Persian', () => {
const persianDate = dayjs(targetDate).toCalendarSystem("persian").format("YYYY-MM-DD");
expect(persianDate).toEqual(targetPersianDate);
});

test('Convert to Gregorian', () => {
const gregorianDate = dayjs(targetDate).toCalendarSystem("gregory").format("YYYY-MM-DD");
expect(gregorianDate).toEqual(targetDate); // Gregorian date should be the same as targetDate
});

test('Convert to Islamic', () => {
const hijriDate = dayjs(targetDate).toCalendarSystem("islamic").format("YYYY-MM-DD");
expect(hijriDate).toEqual(targetIslamicDate);
});

test('Convert Persian to Gregorian', () => {
const persianToGregorian = dayjs(targetDate).toCalendarSystem("persian").toCalendarSystem("gregory").format("YYYY-MM-DD");
expect(persianToGregorian).toEqual(targetDate);
});

test('Convert Persian to Islamic', () => {
const persianToIslamic = dayjs(targetDate).toCalendarSystem("persian").toCalendarSystem("islamic").format("YYYY-MM-DD");
expect(persianToIslamic).toEqual(targetIslamicDate);
});

test('Convert Gregorian to Persian', () => {
const gregorianToPersian = dayjs(targetDate).toCalendarSystem("gregory").toCalendarSystem("persian").format("YYYY-MM-DD");
expect(gregorianToPersian).toEqual(targetPersianDate);
});

test('Convert Gregorian to Islamic', () => {
const gregorianToIslamic = dayjs(targetDate).toCalendarSystem("gregory").toCalendarSystem("islamic").format("YYYY-MM-DD");
expect(gregorianToIslamic).toEqual(targetIslamicDate);
});

test('Convert Islamic to Persian', () => {
const islamicToPersian = dayjs(targetDate).toCalendarSystem("islamic").toCalendarSystem("persian").format("YYYY-MM-DD");
expect(islamicToPersian).toEqual(targetPersianDate);
});

test('Convert Islamic to Gregorian', () => {
const islamicToGregorian = dayjs(targetDate).toCalendarSystem("islamic").toCalendarSystem("gregory").format("YYYY-MM-DD");
expect(islamicToGregorian).toEqual(targetDate);
});

});

0 comments on commit 3f5c633

Please sign in to comment.