Skip to content
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
11 changes: 11 additions & 0 deletions ark/type/__tests__/keywords/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,15 @@ contextualize(() => {
'must be an ISO 8601 (YYYY-MM-DDTHH:mm:ss.sssZ) date (was "05-21-1993")'
)
})

it("string.date.epoch.parse", () => {
const parseEpoch = type("string.date.epoch.parse")
const toIso = (s: string) => parseEpoch.assert(s).toISOString()
attest(toIso("0")).equals("1970-01-01T00:00:00.000Z")
attest(toIso("1700000000000")).equals("2023-11-14T22:13:20.000Z")
attest(toIso("-86400000")).equals("1969-12-31T00:00:00.000Z")
attest(parseEpoch("1.5").toString()).snap(
'must be a well-formed integer string (was "1.5")'
)
})
})
4 changes: 3 additions & 1 deletion ark/type/keywords/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ const epoch = Scope.module(
root: epochRoot,
parse: rootSchema({
in: epochRoot,
morphs: (s: string) => new Date(s),
// parse as a number so the string is treated as milliseconds
// rather than passed to the Date string parser
morphs: (s: string) => new Date(Number.parseInt(s)),
declaredOut: intrinsic.Date
})
},
Expand Down
Loading