Skip to content

Commit

Permalink
Make it easy to convert a DateTime.Zoned to a DateTime.Utc (#4375)
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilkybarkid authored and effect-bot committed Feb 4, 2025
1 parent b263499 commit c0e5bef
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/sixty-mice-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

Make it easy to convert a DateTime.Zoned to a DateTime.Utc
17 changes: 17 additions & 0 deletions packages/effect/src/DateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,23 @@ export const unsafeNow: LazyArg<Utc> = Internal.unsafeNow
// time zones
// =============================================================================

/**
* For a `DateTime` returns a new `DateTime.Utc`.
*
* @since 3.13.0
* @category time zones
* @example
* ```ts
* import { DateTime } from "effect"
*
* const now = DateTime.unsafeMakeZoned({ year: 2024 }, { timeZone: "Europe/London" })
*
* // set as UTC
* const utc: DateTime.Utc = DateTime.toUtc(now)
* ```
*/
export const toUtc: (self: DateTime) => Utc = Internal.toUtc

/**
* Set the time zone of a `DateTime`, returning a new `DateTime.Zoned`.
*
Expand Down
3 changes: 3 additions & 0 deletions packages/effect/src/internal/dateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ export const unsafeNow: LazyArg<DateTime.Utc> = () => makeUtc(Date.now())
// time zones
// =============================================================================

/** @internal */
export const toUtc = (self: DateTime.DateTime): DateTime.Utc => makeUtc(self.epochMillis)

/** @internal */
export const setZone: {
(zone: DateTime.TimeZone, options?: {
Expand Down
17 changes: 17 additions & 0 deletions packages/effect/test/DateTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,4 +421,21 @@ describe("DateTime", () => {
const dt = DateTime.unsafeMakeZoned(date)
deepStrictEqual(dt.zone, DateTime.zoneMakeOffset(60 * 60 * 1000))
})

describe("toUtc", () => {
it.effect("with a Utc", () =>
Effect.gen(function*() {
const dt = DateTime.unsafeMake("2024-01-01T01:00:00")
strictEqual(dt.toJSON(), "2024-01-01T01:00:00.000Z")
}))

it.effect("with a Zoned", () =>
Effect.gen(function*() {
const dt = DateTime.unsafeMakeZoned("2024-01-01T01:00:00Z", {
timeZone: "Pacific/Auckland",
adjustForTimeZone: true
})
strictEqual(dt.toJSON(), "2023-12-31T12:00:00.000Z")
}))
})
})
2 changes: 1 addition & 1 deletion packages/platform-node/test/HttpApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ const HttpUsersLive = HttpApiBuilder.group(
new User({
id: 1,
name: `page ${_.headers.page}`,
createdAt: DateTime.unsafeMake(now.epochMillis)
createdAt: DateTime.toUtc(now)
})
]))
.handle("upload", (_) =>
Expand Down

0 comments on commit c0e5bef

Please sign in to comment.