Skip to content

Commit

Permalink
ensure DateTime.Zoned produces valid dates (#4267)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Jan 16, 2025
1 parent 1fcbe55 commit 3179a9f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-garlics-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

ensure DateTime.Zoned produces valid dates
12 changes: 8 additions & 4 deletions packages/effect/src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6577,7 +6577,7 @@ export class DateTimeUtc extends transformOrFail(
).annotations({ identifier: "DateTimeUtc" }) {}

const timeZoneOffsetArbitrary = (): LazyArbitrary<dateTime.TimeZone.Offset> => (fc) =>
fc.integer({ min: -12 * 60 * 60 * 1000, max: 12 * 60 * 60 * 1000 }).map(dateTime.zoneMakeOffset)
fc.integer({ min: -12 * 60 * 60 * 1000, max: 14 * 60 * 60 * 1000 }).map(dateTime.zoneMakeOffset)

/**
* Describes a schema that represents a `TimeZone.Offset` instance.
Expand Down Expand Up @@ -6692,9 +6692,13 @@ export class DateTimeZonedFromSelf extends declare(
description: "a DateTime.Zoned instance",
pretty: (): pretty_.Pretty<dateTime.Zoned> => (dateTime) => dateTime.toString(),
arbitrary: (): LazyArbitrary<dateTime.Zoned> => (fc) =>
fc.date({ noInvalidDate: true }).chain((date) =>
timeZoneArbitrary(fc).map((timeZone) => dateTime.unsafeMakeZoned(date, { timeZone }))
),
fc.tuple(
fc.integer({
min: -8640000000000000 + (12 * 60 * 60 * 1000),
max: 8640000000000000 - (14 * 60 * 60 * 1000)
}),
timeZoneArbitrary(fc)
).map(([millis, timeZone]) => dateTime.unsafeMakeZoned(millis, { timeZone })),
equivalence: () => dateTime.Equivalence
}
) {}
Expand Down
6 changes: 6 additions & 0 deletions packages/effect/src/internal/dateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ export const unsafeMake = <A extends DateTime.DateTime.Input>(input: A): DateTim
return unsafeFromDate(new Date(input)) as DateTime.DateTime.PreserveZone<A>
}

const minEpochMillis = -8640000000000000 + (12 * 60 * 60 * 1000)
const maxEpochMillis = 8640000000000000 - (14 * 60 * 60 * 1000)

/** @internal */
export const unsafeMakeZoned = (input: DateTime.DateTime.Input, options?: {
readonly timeZone?: number | string | DateTime.TimeZone | undefined
Expand All @@ -243,6 +246,9 @@ export const unsafeMakeZoned = (input: DateTime.DateTime.Input, options?: {
return input
}
const self = unsafeMake(input)
if (self.epochMillis < minEpochMillis || self.epochMillis > maxEpochMillis) {
throw new IllegalArgumentException(`Epoch millis out of range: ${self.epochMillis}`)
}
let zone: DateTime.TimeZone
if (options?.timeZone === undefined) {
const offset = new Date(self.epochMillis).getTimezoneOffset() * -60 * 1000
Expand Down

0 comments on commit 3179a9f

Please sign in to comment.