Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement localized week information #1454

Merged
merged 29 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
a47a704
begin adding support for locale-dependent start of week
diesieben07 Jun 13, 2023
0c395f8
Use an options parameter for locale week startOf
diesieben07 Jun 14, 2023
3e5aa7b
Add tests for locale week start and end
diesieben07 Jun 14, 2023
a5e7752
Add useLocaleWeeks option to DateTime.hasSame and add tests
diesieben07 Jun 14, 2023
87c90e0
Add useLocaleWeeks option to Interval.count
diesieben07 Jun 14, 2023
4df94ed
Add Intl.Locale.weekInfo as a reported optional feature and test for it
diesieben07 Jun 14, 2023
e330883
Add Info.getStartOfWeek method
diesieben07 Jun 14, 2023
28bb096
Add DateTime.isWeekend and DateTime.localDayOfWeek getters
diesieben07 Jun 22, 2023
c82ba21
Improve localized week code in DateTime#startOf
diesieben07 Jun 22, 2023
cfd79fd
Implement DateTime#localWeekYear and DateTime#localWeekNumber
diesieben07 Jun 25, 2023
69f8c1e
Add documentatino for DateTime#localWeekNumber and DateTime#localWeek…
diesieben07 Jun 25, 2023
d91e4ff
Merge branch 'master' into feature/locale-week-start
diesieben07 Aug 16, 2023
5839bac
Add Info.getMinimumDaysInFirstWeek and Info.getWeekendWeekdays and tests
diesieben07 Aug 16, 2023
17a0f39
Add DateTime#isWeekend test
diesieben07 Aug 16, 2023
10781a3
Add tests for DateTime#localWeekNumber and localWeekYear
diesieben07 Aug 16, 2023
7ae90ed
Add tests for localWeekday
diesieben07 Aug 16, 2023
313c31c
Add formatting tokens for localWeekYear and localWeekNumber
diesieben07 Aug 16, 2023
9d0620c
Add tests for formatting tokens n, nn, ii, iiii
diesieben07 Aug 16, 2023
7df2880
Merge branch 'master' into feature/locale-week-start
diesieben07 Sep 17, 2023
21af1ff
Implement DateTime#weeksInLocalWeekYear and proper week year length c…
diesieben07 Sep 17, 2023
fb9ffca
Support setting locale based week units with `DateTime#set`
diesieben07 Sep 17, 2023
1d128a6
Fix Typo
diesieben07 Sep 17, 2023
aa0e72c
Add tests for DateTime#set with locale based week units and DateTime#…
diesieben07 Sep 17, 2023
a88a997
Accept locale-based week units in DateTime#fromObject
diesieben07 Sep 17, 2023
18d0446
Add Settings.defaultWeekSettings
diesieben07 Sep 17, 2023
33b7259
Add documentation for locale-based weeks
diesieben07 Sep 17, 2023
d189b7d
Remove unused value from usesLocalWeekValues
diesieben07 Sep 17, 2023
6d1388c
Remove incorrect claim in documentation
diesieben07 Sep 17, 2023
bb4352c
Remove dumb import
diesieben07 Sep 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests for DateTime#set with locale based week units and DateTime#…
…weeksInLocalWeekYear
  • Loading branch information
diesieben07 committed Sep 17, 2023
commit aa0e72c7ba14e3251e0806841dfc63d280eb0666
2 changes: 1 addition & 1 deletion src/impl/conversions.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export function usesLocalWeekValues(obj, loc) {
delete obj.localWeekYear;
return {
minDaysInFirstWeek: loc.getMinDaysInFirstWeek(),
startOfWeek: loc.getMinDaysInFirstWeek(),
startOfWeek: loc.getStartOfWeek(),
};
} else {
return { minDaysInFirstWeek: 4, startOfWeek: 1 };
Expand Down
15 changes: 15 additions & 0 deletions test/datetime/localeWeek.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,18 @@ describe("localWeekday in locale de-DE", () => {
expect(dt.localWeekday).toBe(7);
});
});

describe("weeksInLocalWeekYear", () => {
test("2018 should have 53 weeks in en-US", () => {
expect(DateTime.local(2018, 6, 1, { locale: "en-US" }).weeksInLocalWeekYear).toBe(52);
});
test("2022 should have 53 weeks in en-US", () => {
expect(DateTime.local(2022, 6, 1, { locale: "en-US" }).weeksInLocalWeekYear).toBe(53);
});
test("2022 should have 52 weeks in de-DE", () => {
expect(DateTime.local(2022, 6, 1, { locale: "de-DE" }).weeksInLocalWeekYear).toBe(52);
});
test("2020 should have 53 weeks in de-DE", () => {
expect(DateTime.local(2020, 6, 1, { locale: "de-DE" }).weeksInLocalWeekYear).toBe(53);
});
});
118 changes: 118 additions & 0 deletions test/datetime/set.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,120 @@ test("DateTime#set({ weekday }) handles week year edge cases", () => {
endOfWeekIs("2028-01-01", "2028-01-02");
});

//------
// locale-based week units
//------

test("DateTime#set({ localWeekday }) sets the weekday to this week's matching day based on the locale (en-US)", () => {
const modified = dt.reconfigure({ locale: "en-US" }).set({ localWeekday: 1 });
expect(modified.localWeekday).toBe(1);
expect(modified.weekday).toBe(7);
expect(modified.year).toBe(1982);
expect(modified.month).toBe(5);
expect(modified.day).toBe(23);
expect(modified.hour).toBe(9);
expect(modified.minute).toBe(23);
expect(modified.second).toBe(54);
expect(modified.millisecond).toBe(123);
});

test("DateTime#set({ localWeekday }) sets the weekday to this week's matching day based on the locale (de-DE)", () => {
const modified = dt.reconfigure({ locale: "de-DE" }).set({ localWeekday: 1 });
expect(modified.localWeekday).toBe(1);
expect(modified.weekday).toBe(1);
expect(modified.year).toBe(1982);
expect(modified.month).toBe(5);
expect(modified.day).toBe(24);
expect(modified.hour).toBe(9);
expect(modified.minute).toBe(23);
expect(modified.second).toBe(54);
expect(modified.millisecond).toBe(123);
});

test("DateTime#set({ localWeekday }) handles crossing over into the previous year", () => {
const modified = DateTime.local(2022, 1, 1, 9, 23, 54, 123, { locale: "en-US" }).set({
localWeekday: 2,
});
expect(modified.localWeekday).toBe(2);
expect(modified.weekday).toBe(1);
expect(modified.year).toBe(2021);
expect(modified.month).toBe(12);
expect(modified.day).toBe(27);
expect(modified.hour).toBe(9);
expect(modified.minute).toBe(23);
expect(modified.second).toBe(54);
expect(modified.millisecond).toBe(123);
});

test("DateTime#set({ localWeekday }) handles crossing over into the previous year", () => {
const modified = DateTime.local(2022, 1, 1, 9, 23, 54, 123, { locale: "en-US" }).set({
localWeekday: 2,
});
expect(modified.localWeekday).toBe(2);
expect(modified.weekday).toBe(1);
expect(modified.year).toBe(2021);
expect(modified.month).toBe(12);
expect(modified.day).toBe(27);
expect(modified.hour).toBe(9);
expect(modified.minute).toBe(23);
expect(modified.second).toBe(54);
expect(modified.millisecond).toBe(123);
});

test("DateTime#set({ localWeekNumber }) sets the date to the same weekday of the target weekNumber (en-US)", () => {
const modified = dt.reconfigure({ locale: "en-US" }).set({ localWeekNumber: 2 });
expect(modified.weekday).toBe(2); // still tuesday
expect(modified.localWeekNumber).toBe(2);
expect(modified.year).toBe(1982);
expect(modified.month).toBe(1);
expect(modified.day).toBe(5);
expect(modified.hour).toBe(9);
expect(modified.minute).toBe(23);
expect(modified.second).toBe(54);
expect(modified.millisecond).toBe(123);
});

test("DateTime#set({ localWeekNumber }) sets the date to the same weekday of the target weekNumber (de-DE)", () => {
const modified = dt.reconfigure({ locale: "de-DE" }).set({ localWeekNumber: 2 });
expect(modified.weekday).toBe(2); // still tuesday
expect(modified.localWeekNumber).toBe(2);
expect(modified.year).toBe(1982);
expect(modified.month).toBe(1);
expect(modified.day).toBe(12);
expect(modified.hour).toBe(9);
expect(modified.minute).toBe(23);
expect(modified.second).toBe(54);
expect(modified.millisecond).toBe(123);
});

test("DateTime#set({ localWeekYear }) sets the date to the same weekNumber/weekday of the target weekYear (en-US)", () => {
const modified = dt.reconfigure({ locale: "en-US" }).set({ localWeekYear: 2017 });
expect(modified.localWeekday).toBe(3); // still tuesday
expect(modified.localWeekNumber).toBe(22); // still week 22
expect(modified.localWeekYear).toBe(2017);
expect(modified.year).toBe(2017);
expect(modified.month).toBe(5);
expect(modified.day).toBe(30); // 2017-W22-3 is the 30
expect(modified.hour).toBe(9);
expect(modified.minute).toBe(23);
expect(modified.second).toBe(54);
expect(modified.millisecond).toBe(123);
});

test("DateTime#set({ localWeekYear }) sets the date to the same weekNumber/weekday of the target weekYear (de-DE)", () => {
const modified = dt.reconfigure({ locale: "de-DE" }).set({ localWeekYear: 2017 });
expect(modified.localWeekday).toBe(2); // still tuesday
expect(modified.localWeekNumber).toBe(21); // still week 21
expect(modified.localWeekYear).toBe(2017);
expect(modified.year).toBe(2017);
expect(modified.month).toBe(5);
expect(modified.day).toBe(23); // 2017-W21-2 is the 30
expect(modified.hour).toBe(9);
expect(modified.minute).toBe(23);
expect(modified.second).toBe(54);
expect(modified.millisecond).toBe(123);
});

//------
// year/ordinal
//------
Expand Down Expand Up @@ -127,6 +241,10 @@ test("DateTime#set throws for mixing incompatible units", () => {
expect(() => dt.set({ year: 2020, weekNumber: 22 })).toThrow();
expect(() => dt.set({ ordinal: 200, weekNumber: 22 })).toThrow();
expect(() => dt.set({ ordinal: 200, month: 8 })).toThrow();
expect(() => dt.set({ year: 2020, localWeekNumber: 22 })).toThrow();
expect(() => dt.set({ ordinal: 200, localWeekNumber: 22 })).toThrow();
expect(() => dt.set({ weekday: 2, localWeekNumber: 22 })).toThrow();
expect(() => dt.set({ weekday: 2, localWeekYear: 2022 })).toThrow();
});

test("DateTime#set maintains invalidity", () => {
Expand Down