Skip to content

Commit c6f5c3b

Browse files
committed
fix: utc and timezone plugins are now compatible with our plugin. more tests are welcome
1 parent ec77233 commit c6f5c3b

File tree

3 files changed

+97
-19
lines changed

3 files changed

+97
-19
lines changed

Diff for: dev/__wip__/overwrites.dayjs.function.js

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/** This is a draft sandbox. DO NOT USE.
2+
3+
dayjsClass.prototype.tz = function (timezone = defaultTimezone, keepLocalTime) {
4+
const oldOffset = this.utcOffset()
5+
const date = this.toDate()
6+
const target = date.toLocaleString('en-US', { timeZone: timezone })
7+
const diff = Math.round((date - new Date(target)) / 1000 / 60)
8+
let ins = dayjsFactory(target).$set("millisecond", this.$ms)
9+
.utcOffset((-Math.round(date.getTimezoneOffset() / 15) * 15) - diff, true)
10+
if (keepLocalTime) {
11+
const newOffset = ins.utcOffset()
12+
ins = ins.add(oldOffset - newOffset, "minute")
13+
}
14+
ins.$x.$timezone = timezone
15+
return ins
16+
}
17+
18+
dayjsClass.prototype.utcOffset = function (input, keepLocalTime) {
19+
const { u } = this.$utils()
20+
if (u(input)) {
21+
if (this.$u) {
22+
return 0
23+
}
24+
if (!u(this.$offset)) {
25+
return this.$offset
26+
}
27+
return -Math.round(this.$d.getTimezoneOffset() / 15) * 15;
28+
}
29+
30+
if (typeof input === 'string') {
31+
input = offsetFromString(input)
32+
if (input === null) {
33+
return this
34+
}
35+
}
36+
const offset = Math.abs(input) <= 16 ? input * 60 : input
37+
let ins = this.clone();
38+
if (keepLocalTime) {
39+
ins.$offset = offset
40+
ins.$u = input === 0
41+
return ins
42+
}
43+
if (input !== 0) {
44+
const localTimezoneOffset = this.$u
45+
? this.toDate().getTimezoneOffset() : -1 * this.utcOffset()
46+
47+
ins = this.local().add(offset + localTimezoneOffset, MIN)
48+
ins.$offset = offset
49+
ins.$x.$localOffset = localTimezoneOffset
50+
} else {
51+
ins = this.utc()
52+
}
53+
return ins
54+
}
55+
56+
dayjsClass.prototype.utc = function (keepLocalTime) {
57+
let y = this.$y;
58+
let m = this.$M;
59+
let d = this.$D;
60+
// if calendar system is not gregorian, convert the date to gregorian
61+
if ("$C" in this && this.$C !== "gregory") {
62+
const convertedDate = calendarSystems[this.$C].convertToGregorian(
63+
this.$y,
64+
this.$M,
65+
this.$D
66+
);
67+
y = convertedDate.year;
68+
m = convertedDate.month;
69+
d = convertedDate.day;
70+
}
71+
const ins = wrapper(Date.UTC(y, m, d), { ...this, locale: this.$L, utc: true, $u: true })
72+
if (keepLocalTime) {
73+
return ins.add(this.utcOffset(), "minute")
74+
}
75+
return ins
76+
}
77+
*/

Diff for: src/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export default (options, dayjsClass, dayjsFactory) => {
2222
props["locale"] = instance[key];
2323
} else if (key === "$u") {
2424
props["utc"] = instance[key];
25+
} else if (key === "$offset") {
26+
// nothing here as it causes issues with the UTC and Timezone plugins
2527
} else if (instance[key] !== instance) {
2628
props[key] = instance[key];
2729
}
@@ -33,6 +35,10 @@ export default (options, dayjsClass, dayjsFactory) => {
3335
if ("$x" in instance) {
3436
newInstance.$x = instance.$x;
3537
}
38+
// Add the $u property (used for UTC) to the new instance:
39+
if ("$u" in instance && newInstance.$u !== instance.$u) {
40+
newInstance.$u = instance.$u;
41+
}
3642
if ("$C" in instance && instance.$C !== "gregory") {
3743
// If the calendar system is set, convert the date to the specified calendar system
3844
return newInstance.toCalendarSystem(instance.$C);

Diff for: test/daysInMonth.test.js

+14-19
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import PersianCalendarSystem from "../src/calendarSystems/PersianCalendarSystem"
88

99
describe("daysInMonth method with different calendar systems", () => {
1010
beforeAll(() => {
11+
dayjs.extend(utc);
12+
dayjs.extend(timeZone);
1113
dayjs.extend(calendarSystems);
1214
dayjs.registerCalendarSystem("gregory", new GregoryCalendarSystem());
1315
dayjs.registerCalendarSystem("persian", new PersianCalendarSystem());
14-
dayjs.extend(utc);
15-
dayjs.extend(timeZone);
1616
});
1717

1818
test("should return the correct number of days in a month for Gregorian calendar", () => {
@@ -24,56 +24,51 @@ describe("daysInMonth method with different calendar systems", () => {
2424
});
2525

2626
test("should return the correct number of days in a month for Persian calendar", () => {
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");
27+
const dateInFarvardin = dayjs("2023-03-24").utc().tz("Europe/Paris").toCalendarSystem("persian");
3328
const daysInFarvardin = dateInFarvardin.daysInMonth();
3429
expect(daysInFarvardin).toBe(31);
3530

36-
const dateInOrdibehesht = dayjs("2023-04-21").toCalendarSystem("persian");
31+
const dateInOrdibehesht = dayjs("2023-04-25").toCalendarSystem("persian");
3732
const daysInOrdibehesht = dateInOrdibehesht.daysInMonth();
3833
expect(daysInOrdibehesht).toBe(31);
3934

40-
const dateInKhordad = dayjs("2023-05-21").toCalendarSystem("persian");
35+
const dateInKhordad = dayjs("2023-05-30").toCalendarSystem("persian");
4136
const daysInKhordad = dateInKhordad.daysInMonth();
4237
expect(daysInKhordad).toBe(31);
4338

44-
const dateInTir = dayjs("2023-06-21").toCalendarSystem("persian");
39+
const dateInTir = dayjs("2023-06-27").toCalendarSystem("persian");
4540
const daysInTir = dateInTir.daysInMonth();
4641
expect(daysInTir).toBe(31);
4742

48-
const dateInMordad = dayjs("2023-07-21").toCalendarSystem("persian");
43+
const dateInMordad = dayjs("2023-07-23").toCalendarSystem("persian");
4944
const daysInMordad = dateInMordad.daysInMonth();
5045
expect(daysInMordad).toBe(31);
5146

52-
const dateInShahrivar = dayjs("2023-08-21").toCalendarSystem("persian");
47+
const dateInShahrivar = dayjs("2023-08-29").toCalendarSystem("persian");
5348
const daysInShahrivar = dateInShahrivar.daysInMonth();
5449
expect(daysInShahrivar).toBe(31);
5550

56-
const dateInMehr = dayjs("2023-09-23").toCalendarSystem("persian");
51+
const dateInMehr = dayjs("2023-09-26").toCalendarSystem("persian");
5752
const daysInMehr = dateInMehr.daysInMonth();
5853
expect(daysInMehr).toBe(30);
5954

60-
const dateInAban = dayjs("2023-10-21").toCalendarSystem("persian");
55+
const dateInAban = dayjs("2023-10-24").toCalendarSystem("persian");
6156
const daysInAban = dateInAban.daysInMonth();
6257
expect(daysInAban).toBe(30);
6358

64-
const dateInAzar = dayjs("2023-11-21").toCalendarSystem("persian");
59+
const dateInAzar = dayjs("2023-11-28").toCalendarSystem("persian");
6560
const daysInAzar = dateInAzar.daysInMonth();
6661
expect(daysInAzar).toBe(30);
6762

68-
const dateInDey = dayjs("2023-12-21").tz("Europe/Paris").toCalendarSystem("persian");
63+
const dateInDey = dayjs("2023-12-28").tz("Europe/Paris").utc().toCalendarSystem("persian");
6964
const daysInDey = dateInDey.daysInMonth();
7065
expect(daysInDey).toBe(30);
7166

72-
const dateInBahman = dayjs("2023-01-21").tz("Europe/Paris").toCalendarSystem("persian");
67+
const dateInBahman = dayjs("2024-01-23").utc().tz("Europe/Paris").toCalendarSystem("persian");
7368
const daysInBahman = dateInBahman.daysInMonth();
7469
expect(daysInBahman).toBe(30);
7570

76-
const dateInEsfand = dayjs("2024-02-20").toCalendarSystem("persian");
71+
const dateInEsfand = dayjs("2024-03-15").tz("Europe/Paris").toCalendarSystem("persian");
7772
const daysInEsfand = dateInEsfand.daysInMonth();
7873
expect(daysInEsfand).toBe(29); // Esfand in a leap year has 29 days
7974
});

0 commit comments

Comments
 (0)