Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Duration: add toSeconds #504

Merged
merged 2 commits into from
Sep 28, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/odd-bears-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/data": patch
---

Duration: add toSeconds, closes #503
11 changes: 11 additions & 0 deletions docs/modules/Duration.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Added in v1.0.0
- [toHrTime](#tohrtime)
- [toMillis](#tomillis)
- [toNanos](#tonanos)
- [toSeconds](#toseconds)
- [unsafeToNanos](#unsafetonanos)
- [guards](#guards)
- [isDuration](#isduration)
Expand Down Expand Up @@ -199,6 +200,16 @@ export declare const toNanos: (self: DurationInput) => Option.Option<bigint>

Added in v1.0.0

## toSeconds

**Signature**

```ts
export declare const toSeconds: (self: DurationInput) => number
```

Added in v1.0.0

## unsafeToNanos

Get the duration in nanoseconds as a bigint.
Expand Down
6 changes: 6 additions & 0 deletions src/Duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ export const toMillis = (self: DurationInput): number => {
}
}

/**
* @since 1.0.0
* @category getters
*/
export const toSeconds = (self: DurationInput): number => toMillis(self) / 1_000

/**
* Get the duration in nanoseconds as a bigint.
*
Expand Down
10 changes: 10 additions & 0 deletions test/Duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,16 @@ describe.concurrent("Duration", () => {
expect(Duration.toMillis("1 millis")).toBe(1)
})

it("toSeconds", () => {
expect(Duration.millis(1).pipe(Duration.toSeconds)).toBe(0.001)
expect(Duration.nanos(1n).pipe(Duration.toSeconds)).toBe(9.999999999999999e-10)
expect(Duration.infinity.pipe(Duration.toSeconds)).toBe(Infinity)

expect(Duration.toSeconds("1 seconds")).toBe(1)
expect(Duration.toSeconds("3 seconds")).toBe(3)
expect(Duration.toSeconds("3 minutes")).toBe(180)
})

it("toNanos", () => {
expect(Duration.nanos(1n).pipe(Duration.toNanos)).toEqual(Option.some(1n))
expect(Duration.infinity.pipe(Duration.toNanos)).toEqual(Option.none())
Expand Down