From c39fb1f1e9338f8e7135070dff5082b66fbd4d59 Mon Sep 17 00:00:00 2001 From: co-sic Date: Wed, 23 Oct 2024 10:36:10 +0200 Subject: [PATCH 1/2] chore: use node v22 in github pipelines --- .github/workflows/main.yml | 6 +++--- .github/workflows/pr.yml | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2d648e7..9f99ef7 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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' @@ -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 @@ -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 diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 000a3e3..43a9156 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -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' @@ -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 From 1507c3fdd2584dd4683bac742554ccce11b48906 Mon Sep 17 00:00:00 2001 From: co-sic Date: Wed, 23 Oct 2024 10:58:40 +0200 Subject: [PATCH 2/2] fix: add caching for intl formatter --- src/CalendarDate.ts | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/CalendarDate.ts b/src/CalendarDate.ts index fa3a016..080cd69 100644 --- a/src/CalendarDate.ts +++ b/src/CalendarDate.ts @@ -34,6 +34,12 @@ export class CalendarDate { */ readonly weekday!: number; + /** + * cache for Intl.DateTimeFormat instances + * @private + */ + private static dateTimeFormatterByTimezone = new Map(); + /** * Customizes the default string description for instances of `CalendarDate`. */ @@ -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 */ @@ -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)); } /**