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

Patch IOS native date locale appending em to the end of the date string in dateTZtoISO8601 #619

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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: 4 additions & 2 deletions src/dateutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
41 changes: 40 additions & 1 deletion test/dateutil.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,47 @@
import { datetime, untilStringToDate } from '../src/dateutil'
import { datetime, untilStringToDate, dateTZtoISO8601 } from '../src/dateutil'

describe('untilStringToDate', () => {
it('parses a date string', () => {
const date = untilStringToDate('19970902T090000')
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')
})
})