Skip to content

Commit 470b66c

Browse files
committed
fix: timezone plugin compatibility. Note: when extending dayjs with plugins, make sure to have utc and timeZone plugins AFTER our calendarSystems plugin
1 parent 1d06e2d commit 470b66c

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@
103103
"calendarSystems",
104104
"calendarUtils",
105105
"*.md",
106-
"*.min.js"
106+
"*.min.js",
107+
"index.d.ts"
107108
],
108109
"devDependencies": {
109110
"@babel/cli": "^7.21.5",

Diff for: src/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*
77
* This plugin allows Day.js to work with different calendar systems.
88
*/
9-
import CalendarSystemBase from "./calendarSystems/CalendarSystemBase";
109
import GregoryCalendarSystem from "./calendarSystems/GregoryCalendarSystem";
1110

1211
// The built-in calendar systems
@@ -30,6 +29,10 @@ export default (options, dayjsClass, dayjsFactory) => {
3029
}, {});
3130
// Create a new instance of the class with the given date:
3231
const newInstance = dayjsFactory(date, properties);
32+
// Add the $x property (used for timezone) to the new instance:
33+
if ("$x" in instance) {
34+
newInstance.$x = instance.$x;
35+
}
3336
if ("$C" in instance && instance.$C !== "gregory") {
3437
// If the calendar system is set, convert the date to the specified calendar system
3538
return newInstance.toCalendarSystem(instance.$C);

Diff for: test/daysInMonth.test.js

+51-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import dayjs from "dayjs";
2+
import timeZone from "dayjs/plugin/timezone";
3+
//import toArray from "dayjs/plugin/toArray";
4+
import utc from "dayjs/plugin/utc";
25
import calendarSystems from "../src/index";
36
import GregoryCalendarSystem from "../src/calendarSystems/GregoryCalendarSystem";
47
import PersianCalendarSystem from "../src/calendarSystems/PersianCalendarSystem";
@@ -8,6 +11,8 @@ describe("daysInMonth method with different calendar systems", () => {
811
dayjs.extend(calendarSystems);
912
dayjs.registerCalendarSystem("gregory", new GregoryCalendarSystem());
1013
dayjs.registerCalendarSystem("persian", new PersianCalendarSystem());
14+
dayjs.extend(utc);
15+
dayjs.extend(timeZone);
1116
});
1217

1318
test("should return the correct number of days in a month for Gregorian calendar", () => {
@@ -19,10 +24,55 @@ describe("daysInMonth method with different calendar systems", () => {
1924
});
2025

2126
test("should return the correct number of days in a month for Persian calendar", () => {
22-
const dateInFarvardin = dayjs("2023-03-21").toCalendarSystem("persian");
27+
// console.log("TEST_DAYS_IN_MONTH",
28+
// dayjs("2023-03-21").toDate().toLocaleString('en-US', { timeZone: "Europe/Paris" }),
29+
// dayjs("2023-03-21").toCalendarSystem("persian").toDate().toLocaleString('en-US', { timeZone: "Europe/Paris" }),
30+
// dayjs("2023-03-21").tz("Europe/Paris").toCalendarSystem("persian").toDate().toLocaleString('en-US', { timeZone: "Europe/Paris" })
31+
// );
32+
const dateInFarvardin = dayjs("2023-03-21").tz("Europe/Paris").toCalendarSystem("persian");
2333
const daysInFarvardin = dateInFarvardin.daysInMonth();
2434
expect(daysInFarvardin).toBe(31);
2535

36+
const dateInOrdibehesht = dayjs("2023-04-21").toCalendarSystem("persian");
37+
const daysInOrdibehesht = dateInOrdibehesht.daysInMonth();
38+
expect(daysInOrdibehesht).toBe(31);
39+
40+
const dateInKhordad = dayjs("2023-05-21").toCalendarSystem("persian");
41+
const daysInKhordad = dateInKhordad.daysInMonth();
42+
expect(daysInKhordad).toBe(31);
43+
44+
const dateInTir = dayjs("2023-06-21").toCalendarSystem("persian");
45+
const daysInTir = dateInTir.daysInMonth();
46+
expect(daysInTir).toBe(31);
47+
48+
const dateInMordad = dayjs("2023-07-21").toCalendarSystem("persian");
49+
const daysInMordad = dateInMordad.daysInMonth();
50+
expect(daysInMordad).toBe(31);
51+
52+
const dateInShahrivar = dayjs("2023-08-21").toCalendarSystem("persian");
53+
const daysInShahrivar = dateInShahrivar.daysInMonth();
54+
expect(daysInShahrivar).toBe(31);
55+
56+
const dateInMehr = dayjs("2023-09-23").toCalendarSystem("persian");
57+
const daysInMehr = dateInMehr.daysInMonth();
58+
expect(daysInMehr).toBe(30);
59+
60+
const dateInAban = dayjs("2023-10-21").toCalendarSystem("persian");
61+
const daysInAban = dateInAban.daysInMonth();
62+
expect(daysInAban).toBe(30);
63+
64+
const dateInAzar = dayjs("2023-11-21").toCalendarSystem("persian");
65+
const daysInAzar = dateInAzar.daysInMonth();
66+
expect(daysInAzar).toBe(30);
67+
68+
const dateInDey = dayjs("2023-12-21").tz("Europe/Paris").toCalendarSystem("persian");
69+
const daysInDey = dateInDey.daysInMonth();
70+
expect(daysInDey).toBe(30);
71+
72+
const dateInBahman = dayjs("2023-01-21").tz("Europe/Paris").toCalendarSystem("persian");
73+
const daysInBahman = dateInBahman.daysInMonth();
74+
expect(daysInBahman).toBe(30);
75+
2676
const dateInEsfand = dayjs("2024-02-20").toCalendarSystem("persian");
2777
const daysInEsfand = dateInEsfand.daysInMonth();
2878
expect(daysInEsfand).toBe(29); // Esfand in a leap year has 29 days
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)