| 1 | /** |
| 2 | * @title Creating and formatting DateTime values |
| 3 | * |
| 4 | * Parse incoming date values safely, use Clock-powered current time, and format |
| 5 | * instants for API payloads or user-facing labels. |
| 6 | */ |
| 7 | import { DateTime, Effect, Option } from "effect" |
| 8 | |
| 9 | Effect.gen(function*() { |
| 10 | // Use DateTime.now to get the current time from Effect's Clock service. |
| 11 | // Using the Clock service ensures tests can use the `TestClock` module to |
| 12 | // control time. |
| 13 | const now = yield* DateTime.now |
| 14 | |
| 15 | // Use DateTime.make to parse a date input, such as a user-entered string or a |
| 16 | // epoch timestamp. It returns an Option depending on whether the input was |
| 17 | // valid. |
| 18 | const parsedOption: Option.Option<DateTime.Utc> = DateTime.make("2024-06-15T14:30:00.000Z") |
| 19 | |
| 20 | // you can then use the Option apis to unwrap the value |
| 21 | Option.getOrUndefined(parsedOption) |
| 22 | |
| 23 | // Calendar/date-time math returns a new DateTime value; the original value is |
| 24 | // immutable. |
| 25 | const endsAt = now.pipe(DateTime.add({ hours: 2 })) |
| 26 | |
| 27 | // The DateTime.format* functions can be used to convert a DateTime value to |
| 28 | // differen formats. |
| 29 | yield* Effect.log("ISO string:", DateTime.formatIso(endsAt)) |
| 30 | }) |