Skip to content
Closed
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/add-datetime-epoch-seconds.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Add `DateTime.toEpochSeconds` for whole Unix timestamps.
8 changes: 8 additions & 0 deletions packages/effect/src/DateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,14 @@ export const zonedOffsetIso: (self: Zoned) => string = Internal.zonedOffsetIso
*/
export const toEpochMillis: (self: DateTime) => number = Internal.toEpochMillis

/**
* Get the whole seconds since the Unix epoch of a `DateTime`.
*
* @since 3.22.0
* @category conversions
*/
export const toEpochSeconds: (self: DateTime) => number = Internal.toEpochSeconds

/**
* Remove the time aspect of a `DateTime`, first adjusting for the time
* zone. It will return a `DateTime.Utc` only containing the date.
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 @@ -613,6 +613,9 @@ export const zonedOffsetIso = (self: DateTime.Zoned): string => offsetToString(z
/** @internal */
export const toEpochMillis = (self: DateTime.DateTime): number => self.epochMillis

/** @internal */
export const toEpochSeconds = (self: DateTime.DateTime): number => Math.floor(self.epochMillis / 1000)

/** @internal */
export const removeTime = (self: DateTime.DateTime): DateTime.Utc =>
withDate(self, (date) => {
Expand Down
10 changes: 10 additions & 0 deletions packages/effect/test/DateTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,16 @@ describe("DateTime", () => {
))
})

describe("toEpochSeconds", () => {
it("converts milliseconds to whole seconds", () => {
strictEqual(DateTime.toEpochSeconds(DateTime.unsafeMake(1_999)), 1)
})

it("floors timestamps before the Unix epoch", () => {
strictEqual(DateTime.toEpochSeconds(DateTime.unsafeMake(-1)), -1)
})
})

describe("removeTime", () => {
it("removes time", () => {
const dt = DateTime.unsafeMakeZoned("2024-01-01T01:00:00Z", {
Expand Down