Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it easy to convert a DateTime.Zoned to a DateTime.Utc #4375

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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