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

fix: add caching for intl formatter #335

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Setup Node.js environment
uses: actions/setup-node@v4.0.4
with:
node-version: '20'
node-version: '22'
- uses: actions/cache@v4
with:
path: '**/node_modules'
Expand All @@ -35,7 +35,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ 16, 18, 20 ]
node-version: [ 18, 20, 22 ]
steps:
- uses: actions/checkout@v4
- name: Setup Node.js environment
Expand Down Expand Up @@ -80,7 +80,7 @@ jobs:
- name: Setup Node.js environment
uses: actions/setup-node@v4.0.4
with:
node-version: '20'
node-version: '22'
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Build
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Setup Node.js environment
uses: actions/setup-node@v4.0.4
with:
node-version: '20'
node-version: '22'
- uses: actions/cache@v4
with:
path: '**/node_modules'
Expand All @@ -37,7 +37,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ 16, 18, 20 ]
node-version: [ 18, 20, 22 ]
steps:
- uses: actions/checkout@v4
- name: Setup Node.js environment
Expand Down
26 changes: 21 additions & 5 deletions src/CalendarDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export class CalendarDate {
*/
readonly weekday!: number;

/**
* cache for Intl.DateTimeFormat instances
* @private
*/
private static dateTimeFormatterByTimezone = new Map<string, Intl.DateTimeFormat>();

/**
* Customizes the default string description for instances of `CalendarDate`.
*/
Expand Down Expand Up @@ -108,6 +114,20 @@ export class CalendarDate {
return month === 2 && CalendarDate.isLeapYear(year) ? 29 : DAYS_IN_MONTH[month - 1];
}

private static getIntlDateTimeFormatter(timeZone: string): Intl.DateTimeFormat {
let formatter = CalendarDate.dateTimeFormatterByTimezone.get(timeZone);
if (!formatter) {
formatter = new Intl.DateTimeFormat('en-CA', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
timeZone,
});
CalendarDate.dateTimeFormatterByTimezone.set(timeZone, formatter);
}
return formatter;
}

/**
* returns a CalendarDate instance for the supplied Date, using UTC values
*/
Expand All @@ -126,11 +146,7 @@ export class CalendarDate {
* returns a CalendarDate instance for the supplied Date, using the supplied time zone string
*/
static fromDateWithTimeZone(date: Date, timeZone: string): CalendarDate {
const calendarValues = date
.toLocaleDateString('de-DE', { timeZone })
.split('.')
.map((value) => parseInt(value));
return new CalendarDate(calendarValues[2], calendarValues[1], calendarValues[0]);
return new CalendarDate(CalendarDate.getIntlDateTimeFormatter(timeZone).format(date));
}

/**
Expand Down