Skip to content

Commit 1c40b28

Browse files
AlfGototim-smart
andauthored
Add DateTime.toEpochSeconds and fromEpochSeconds (#6909)
Co-authored-by: Tim Smart <[email protected]>
1 parent c0a1534 commit 1c40b28

4 files changed

Lines changed: 75 additions & 0 deletions

File tree

‎.changeset/bright-clocks-count.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Add `DateTime.toEpochSeconds` and `DateTime.fromEpochSeconds` for converting date-time values to and from Unix epoch seconds.

‎packages/effect/src/DateTime.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,22 @@ export const fromDateUnsafe: (date: Date) => Utc = Internal.fromDateUnsafe
652652
*/
653653
export const makeUnsafe: <A extends DateTime.Input>(input: A) => DateTime.PreserveZone<A> = Internal.makeUnsafe
654654

655+
/**
656+
* Creates a `DateTime.Utc` from the number of seconds since the Unix epoch.
657+
*
658+
* **Example** (Creating from epoch seconds)
659+
*
660+
* ```ts import.meta.vitest
661+
* import { DateTime } from "effect"
662+
*
663+
* DateTime.fromEpochSeconds(1704067200).toJSON() // => "2024-01-01T00:00:00.000Z"
664+
* ```
665+
*
666+
* @category constructors
667+
* @since 4.0.0
668+
*/
669+
export const fromEpochSeconds: (seconds: number) => Utc = Internal.fromEpochSeconds
670+
655671
/**
656672
* Create a `DateTime.Zoned` using `DateTime.makeUnsafe` and a time zone.
657673
*
@@ -1600,6 +1616,28 @@ export const zonedOffsetIso: (self: Zoned) => string = Internal.zonedOffsetIso
16001616
*/
16011617
export const toEpochMillis: (self: DateTime) => number = Internal.toEpochMillis
16021618

1619+
/**
1620+
* Converts a `DateTime` to the number of seconds since the Unix epoch.
1621+
*
1622+
* **Details**
1623+
*
1624+
* This returns the UTC timestamp regardless of any time zone information.
1625+
* The result is floored to the nearest second.
1626+
*
1627+
* **Example** (Reading epoch seconds)
1628+
*
1629+
* ```ts import.meta.vitest
1630+
* import { DateTime } from "effect"
1631+
*
1632+
* const dt = DateTime.makeUnsafe("2024-01-01T00:00:00Z")
1633+
* DateTime.toEpochSeconds(dt) // => 1704067200
1634+
* ```
1635+
*
1636+
* @category converting
1637+
* @since 4.0.0
1638+
*/
1639+
export const toEpochSeconds: (self: DateTime) => number = Internal.toEpochSeconds
1640+
16031641
/**
16041642
* Removes the time aspect of a `DateTime`, first adjusting for the time
16051643
* zone. It will return a `DateTime.Utc` only containing the date.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,12 @@ export const zonedOffsetIso = (self: DateTime.Zoned): string => offsetToString(z
600600
/** @internal */
601601
export const toEpochMillis = (self: DateTime.DateTime): number => self.epochMilliseconds
602602

603+
/** @internal */
604+
export const toEpochSeconds = (self: DateTime.DateTime): number => Math.floor(self.epochMilliseconds / 1000)
605+
606+
/** @internal */
607+
export const fromEpochSeconds = (seconds: number): DateTime.Utc => makeUtc(seconds * 1000)
608+
603609
/** @internal */
604610
export const removeTime = (self: DateTime.DateTime): DateTime.Utc =>
605611
withDate(self, (date) => {

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,32 @@ describe("DateTime", () => {
381381
})
382382
})
383383

384+
describe("toEpochSeconds", () => {
385+
it("returns epoch seconds", () => {
386+
const dt = DateTime.makeUnsafe("2024-01-01T00:00:00Z")
387+
strictEqual(DateTime.toEpochSeconds(dt), 1704067200)
388+
})
389+
390+
it("floors to nearest second", () => {
391+
const dt = DateTime.makeUnsafe("2024-01-01T00:00:00.999Z")
392+
strictEqual(DateTime.toEpochSeconds(dt), 1704067200)
393+
})
394+
})
395+
396+
describe("fromEpochSeconds", () => {
397+
it("creates DateTime from epoch seconds", () => {
398+
const dt = DateTime.fromEpochSeconds(1704067200)
399+
strictEqual(dt.toJSON(), "2024-01-01T00:00:00.000Z")
400+
})
401+
402+
it("roundtrips with toEpochSeconds", () => {
403+
const original = DateTime.makeUnsafe("2024-06-15T12:30:00Z")
404+
const seconds = DateTime.toEpochSeconds(original)
405+
const restored = DateTime.fromEpochSeconds(seconds)
406+
strictEqual(DateTime.toEpochSeconds(restored), seconds)
407+
})
408+
})
409+
384410
describe("makeZonedFromString", () => {
385411
it.effect("parses an instant with an offset and IANA zone", () =>
386412
Effect.gen(function*() {

0 commit comments

Comments
 (0)