DateTime
All checks were successful
Lint / lint (push) Successful in 1m12s

This commit is contained in:
Julien Valverdé
2024-08-20 01:46:00 +02:00
parent 94f16a3967
commit ad6255b5da
4 changed files with 59 additions and 6 deletions

52
src/Schema/DateTime.ts Normal file
View File

@@ -0,0 +1,52 @@
import { ParseResult, Schema } from "@effect/schema"
import { DateTime } from "effect"
export const DateTimeUtcFromDate = Schema.transformOrFail(
Schema.DateFromSelf,
Schema.DateTimeUtcFromSelf,
{
strict: true,
decode: (date, _, ast) => ParseResult.try({
try: () => DateTime.unsafeMake(date),
catch: e => new ParseResult.Type(
ast,
date,
e instanceof Error ? e.message : undefined,
),
}),
encode: dateTimeUtc => ParseResult.succeed(
DateTime.toDateUtc(dateTimeUtc)
),
},
)
export const DateTimeZonedFromDate = (options: {
readonly timeZone: number | string | DateTime.TimeZone
readonly adjustForTimeZone?: boolean | undefined
}) => Schema.transformOrFail(
Schema.DateFromSelf,
Schema.DateTimeZonedFromSelf,
{
strict: true,
decode: (date, _, ast) => ParseResult.try({
try: () => DateTime.unsafeMakeZoned(date, options),
catch: e => new ParseResult.Type(
ast,
date,
e instanceof Error ? e.message : undefined,
),
}),
encode: dateTime => ParseResult.succeed(
DateTime.toDate(dateTime)
),
},
)

View File

@@ -1,4 +1,5 @@
export * from "./Class"
export * from "./DateTime"
export * from "./Jsonifiable"
export * from "./Kind"
export * as MobX from "./MobX"