Hyperlinkv0.8.0-beta.28

DateTime

DateTime.matchconsteffect/DateTime.ts:2243
<A, B>(options: {
  readonly onUtc: (_: Utc) => A
  readonly onZoned: (_: Zoned) => B
}): (self: DateTime) => A | B
<A, B>(
  self: DateTime,
  options: {
    readonly onUtc: (_: Utc) => A
    readonly onZoned: (_: Zoned) => B
  }
): A | B

Pattern match on a DateTime to handle Utc and Zoned cases differently.

Example (Pattern matching DateTime variants)

import { DateTime } from "effect"

const dt1 = DateTime.makeUnsafe("2024-01-01T12:00:00Z") // Utc
const dt2 = DateTime.makeZonedUnsafe("2024-06-15T14:30:00Z", {
  timeZone: "Europe/London"
}) // Zoned

const result1 = DateTime.match(dt1, {
  onUtc: (utc) => `UTC: ${DateTime.formatIso(utc)}`,
  onZoned: (zoned) => `Zoned: ${DateTime.formatIsoZoned(zoned)}`
})

const result2 = DateTime.match(dt2, {
  onUtc: (utc) => `UTC: ${DateTime.formatIso(utc)}`,
  onZoned: (zoned) => `Zoned: ${DateTime.formatIsoZoned(zoned)}`
})

console.log(result1) // "UTC: 2024-01-01T12:00:00.000Z"
console.log(result2) // "Zoned: 2024-06-15T15:30:00.000+01:00[Europe/London]"
mapping
export const match: {
  <A, B>(options: {
    readonly onUtc: (_: Utc) => A
    readonly onZoned: (_: Zoned) => B
  }): (self: DateTime) => A | B
  <A, B>(self: DateTime, options: {
    readonly onUtc: (_: Utc) => A
    readonly onZoned: (_: Zoned) => B
  }): A | B
} = Internal.match