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 all commits
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
6 changes: 5 additions & 1 deletion docs/formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ The macro options available correspond one-to-one with the preset formats define
(Examples below given for `2014-08-06T13:07:04.054` considered as a local time in America/New_York).

| Standalone token | Format token | Description | Example |
| ---------------- | ------------ | -------------------------------------------------------------- | ------------------------------------------------------------- |
|------------------| ------------ |----------------------------------------------------------------| ------------------------------------------------------------- |
| S | | millisecond, no padding | `54` |
| SSS | | millisecond, padded to 3 | `054` |
| u | | fractional seconds, functionally identical to SSS | `054` |
Expand Down Expand Up @@ -219,6 +219,10 @@ The macro options available correspond one-to-one with the preset formats define
| kkkk | | ISO week year, padded to 4 | `2014` |
| W | | ISO week number, unpadded | `32` |
| WW | | ISO week number, padded to 2 | `32` |
| ii | | Local week year, unpadded | `14` |
| iiii | | Local week year, padded to 4 | `2014` |
| n | | Local week number, unpadded | `32` |
| nn | | Local week number, padded to 2 | `32` |
| o | | ordinal (day of year), unpadded | `218` |
| ooo | | ordinal (day of year), padded to 3 | `218` |
| q | | quarter, no padding | `3` |
Expand Down
68 changes: 68 additions & 0 deletions docs/intl.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,71 @@ Similar to `locale`, you can set the default numbering system for new instances:
```js
Settings.defaultNumberingSystem = "beng";
```

## Locale-based weeks

Most of Luxon uses the [ISO week date](https://en.wikipedia.org/wiki/ISO_week_date) system when working with week-related data.
This means that the week starts on Monday and the first week of the year is that week, which has 4 or more of its days in January.

This definition works for most use-cases, however locales can define different rules. For example, in many English-speaking countries
the week is said to start on Sunday and the 1 January always defines the first week of the year. This information is
available through the Luxon as well.

Note that your runtime needs to support [`Intl.Locale#getWeekInfo`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/getWeekInfo) for this to have an effect. If unsupported, Luxon will fall back
to using the ISO week dates.

### Accessing locale-based week info

The `Info` class exposes methods `getStartOfWeek`, `getMinimumDaysInFirstWeek` and `getWeekendWeekdays` for informational
purposes.

### Accessing locale-based week data

```js
const dt = DateTime.local(2022, 1, 1, { locale: "en-US" });
dt.localWeekday // 7, because Saturday is the 7th day of the week in en-US
dt.localWeekNumber // 1, because 1 January is always in the first week in en-US
dt.localWeekYear // 2022, because 1 January is always in the first week in en-US
dt.weeksInLocalWeekYear // 53, because 2022 has 53 weeks in en-US
```

### Using locale-based week data when creating DateTimes

When creating a DateTime instance using `fromObject`, you can use the `localWeekday`, `localWeekNumber` and `localWeekYear`
properties. They will use the same locale that the newly created DateTime will use, either explicitly provided or falling back
to the system default.

```js
const dt = DateTime.fromObject({localWeekYear: 2022, localWeekNumber: 53, localWeekday: 7});
dt.toISODate(); // 2022-12-31
```

### Setting locale-based week data

When modifying an existing DateTime instance using `set`, you can use the `localWeekday`, `localWeekNumber` and `localWeekYear`
properties. They will use the locale of the existing DateTime as reference.

```js
const dt = DateTime.local(2022, 1, 2, { locale: "en-US" });
const modified = dt.set({localWeekNumber: 2});
modified.toISODate(); // 2022-01-08
```

### Locale-based weeks with math

The methods `startOf`, `endOf`, `hasSame` of `DateTime` as well as `count` of `Interval` accept an option `useLocaleWeeks`.
If enabled, the methods will treat the `week` unit according to the locale, respecting the correct start of the week.

```js
const dt = DateTime.local(2022, 1, 6, { locale: "en-US" });
const startOfWeek = dt.startOf('week', {useLocaleWeeks: true});
startOfWeek.toISODate(); // 2022-01-02, a Sunday
```

### Overriding defaults

You can override the runtime-provided defaults for the week settings using `Settings.defaultWeekSettings`:

```js
Settings.defaultWeekSettings = { firstDay: 7, minimalDays: 1, weekend: [6, 7] }
```
Loading