diff --git a/src/dateutil.ts b/src/dateutil.ts index b2cd176a..dc78be07 100644 --- a/src/dateutil.ts +++ b/src/dateutil.ts @@ -204,11 +204,13 @@ export const untilStringToDate = function (until: string) { ) } -const dateTZtoISO8601 = function (date: Date, timeZone: string) { +export const dateTZtoISO8601 = function (date: Date, timeZone: string) { // date format for sv-SE is almost ISO8601 const dateStr = date.toLocaleString('sv-SE', { timeZone }) // '2023-02-07 10:41:36' - return dateStr.replace(' ', 'T') + 'Z' + return ( + dateStr.replace('em', '').replace('fm', '').trim().replace(' ', 'T') + 'Z' + ) } export const dateInTimeZone = function (date: Date, timeZone: string) { diff --git a/test/dateutil.test.ts b/test/dateutil.test.ts index b7787dd3..f673bf45 100644 --- a/test/dateutil.test.ts +++ b/test/dateutil.test.ts @@ -1,4 +1,4 @@ -import { datetime, untilStringToDate } from '../src/dateutil' +import { datetime, untilStringToDate, dateTZtoISO8601 } from '../src/dateutil' describe('untilStringToDate', () => { it('parses a date string', () => { @@ -6,3 +6,42 @@ describe('untilStringToDate', () => { expect(date.getTime()).toBe(datetime(1997, 9, 2, 9, 0, 0).getTime()) }) }) + +describe('dateTZtoISO8601', () => { + it('correctly formats a date with em suffix from toLocaleString', () => { + // Mock Date.prototype.toLocaleString to simulate iOS behavior + const originalToLocaleString = Date.prototype.toLocaleString + Date.prototype.toLocaleString = jest.fn(() => '2045-10-31 1:00:00 em') + + const date = new Date() + const timeZone = 'America/Los_Angeles' + + // Your patched function that handles the iOS date string correctly + expect(dateTZtoISO8601(date, timeZone)).toBe('2045-10-31T1:00:00Z') + + // Restore the original toLocaleString method after the test + Date.prototype.toLocaleString = originalToLocaleString + }) + + it('correctly formats a date with fm suffix from toLocaleString', () => { + // Mock Date.prototype.toLocaleString to simulate iOS behavior + const originalToLocaleString = Date.prototype.toLocaleString + Date.prototype.toLocaleString = jest.fn(() => '2045-10-31 1:00:00 fm') + + const date = new Date() + const timeZone = 'America/Los_Angeles' + + // Your patched function that handles the iOS date string correctly + expect(dateTZtoISO8601(date, timeZone)).toBe('2045-10-31T1:00:00Z') + + // Restore the original toLocaleString method after the test + Date.prototype.toLocaleString = originalToLocaleString + }) + + it('formats a date in ISO8601 format for non-iOS environments', () => { + const date = new Date(1704830400000) + const timeZone = 'America/Los_Angeles' + // This tests the function in environments that do not have the ' em' issue + expect(dateTZtoISO8601(date, timeZone)).toBe('2024-01-09T12:00:00Z') + }) +})