1/**
2 * @title Working with time zones
3 *
4 * Attach IANA zones to instants, render zoned ISO strings, and provide a
5 * CurrentTimeZone service for code that should use the workspace/user zone.
6 */
7import { NodeRuntime } from "@effect/platform-node"
8import { DateTime, Effect, Option } from "effect"
9
10Effect.gen(function*() {
11 // Use DateTime.now to get the current time from Effect's Clock service.
12 const now = yield* DateTime.now
13
14 // To attach a named IANA zone to a DateTime value
15 const nowInAuckland = now.pipe(
16 // Use DateTime.setZoneNamedUnsafe when you know the zone is valid.
17 DateTime.setZoneNamedUnsafe("Pacific/Auckland")
18 )
19 yield* Effect.log("Now in Auckland:", nowInAuckland)
20
21 // Use DateTime.setZoneNamed when you don't know the zone is valid.
22 const nowInSydneyOption: Option.Option<DateTime.Zoned> = now.pipe(
23 DateTime.setZoneNamed("Australia/Sydney")
24 )
25
26 yield* Effect.log("Now in Sydney:", Option.getOrUndefined(nowInSydneyOption))
27
28 // To generate a `DateTime.Zoned` in the `DateTime.CurrentTimeZone`
29 const nowInNewYork = yield* DateTime.nowInCurrentZone
30 yield* Effect.log("Now in New York:", nowInNewYork)
31
32 // If you have a date string that you know is in a particular IANA zone, you
33 // can convert it to a DateTime.Zoned to ensure the instant is correct
34 const dateInAuckland: DateTime.Zoned = DateTime.makeZonedUnsafe("2026-06-05", {
35 timeZone: "Pacific/Auckland",
36 // adjustForTimeZone will adjust the input to the given zone, otherwise it
37 // will be treated as UTC.
38 adjustForTimeZone: true
39 })
40 yield* Effect.log("Date in Auckland:", dateInAuckland)
41}).pipe(
42 Effect.provide(DateTime.layerCurrentZoneNamed("America/New_York")),
43 NodeRuntime.runMain
44)