Skip to content

Commit 6862444

Browse files
thewilkybarkidtim-smart
authored andcommittedFeb 14, 2025
Make it easy to convert a DateTime.Zoned to a DateTime.Utc (#4375)
1 parent cf8b2dd commit 6862444

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed
 

‎.changeset/sixty-mice-occur.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": minor
3+
---
4+
5+
Make it easy to convert a DateTime.Zoned to a DateTime.Utc

‎packages/effect/src/DateTime.ts

+17
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,23 @@ export const unsafeNow: LazyArg<Utc> = Internal.unsafeNow
437437
// time zones
438438
// =============================================================================
439439

440+
/**
441+
* For a `DateTime` returns a new `DateTime.Utc`.
442+
*
443+
* @since 3.13.0
444+
* @category time zones
445+
* @example
446+
* ```ts
447+
* import { DateTime } from "effect"
448+
*
449+
* const now = DateTime.unsafeMakeZoned({ year: 2024 }, { timeZone: "Europe/London" })
450+
*
451+
* // set as UTC
452+
* const utc: DateTime.Utc = DateTime.toUtc(now)
453+
* ```
454+
*/
455+
export const toUtc: (self: DateTime) => Utc = Internal.toUtc
456+
440457
/**
441458
* Set the time zone of a `DateTime`, returning a new `DateTime.Zoned`.
442459
*

‎packages/effect/src/internal/dateTime.ts

+3
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ export const unsafeNow: LazyArg<DateTime.Utc> = () => makeUtc(Date.now())
306306
// time zones
307307
// =============================================================================
308308

309+
/** @internal */
310+
export const toUtc = (self: DateTime.DateTime): DateTime.Utc => makeUtc(self.epochMillis)
311+
309312
/** @internal */
310313
export const setZone: {
311314
(zone: DateTime.TimeZone, options?: {

‎packages/effect/test/DateTime.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,21 @@ describe("DateTime", () => {
421421
const dt = DateTime.unsafeMakeZoned(date)
422422
deepStrictEqual(dt.zone, DateTime.zoneMakeOffset(60 * 60 * 1000))
423423
})
424+
425+
describe("toUtc", () => {
426+
it.effect("with a Utc", () =>
427+
Effect.gen(function*() {
428+
const dt = DateTime.unsafeMake("2024-01-01T01:00:00")
429+
strictEqual(dt.toJSON(), "2024-01-01T01:00:00.000Z")
430+
}))
431+
432+
it.effect("with a Zoned", () =>
433+
Effect.gen(function*() {
434+
const dt = DateTime.unsafeMakeZoned("2024-01-01T01:00:00Z", {
435+
timeZone: "Pacific/Auckland",
436+
adjustForTimeZone: true
437+
})
438+
strictEqual(dt.toJSON(), "2023-12-31T12:00:00.000Z")
439+
}))
440+
})
424441
})

‎packages/platform-node/test/HttpApi.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ const HttpUsersLive = HttpApiBuilder.group(
445445
new User({
446446
id: 1,
447447
name: `page ${_.headers.page}`,
448-
createdAt: DateTime.unsafeMake(now.epochMillis)
448+
createdAt: DateTime.toUtc(now)
449449
})
450450
]))
451451
.handle("upload", (_) =>

0 commit comments

Comments
 (0)
Please sign in to comment.