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

From Discord: Request for Duration.toSeconds in the future #503

Closed
effect-bot opened this issue Sep 27, 2023 · 0 comments · Fixed by #505
Closed

From Discord: Request for Duration.toSeconds in the future #503

effect-bot opened this issue Sep 27, 2023 · 0 comments · Fixed by #505

Comments

@effect-bot
Copy link

Summary

The user is requesting a new feature called Duration.toSeconds in the future.

Example article

Request for Duration.toSeconds in the future

Introduction

In the Effect-TS ecosystem, there has been a request for a toSeconds function in the Duration module. This function would convert a duration value to seconds. This article will discuss the request and provide a possible implementation for the toSeconds function.

Background

The Effect-TS ecosystem is a collection of libraries that provide various functionalities for building scalable and performant applications. One of the libraries in this ecosystem is the @effect/data library, which includes the Duration module for working with time durations.

Currently, the Duration module provides functions for creating and manipulating duration values, such as fromMilliseconds, fromSeconds, add, and subtract. However, there is no built-in function to convert a duration value to seconds.

Request for toSeconds function

A user in the Effect-TS community has requested a toSeconds function in the Duration module. This function would be useful for scenarios where durations need to be represented in seconds, such as when working with external APIs or performing calculations based on time intervals.

Proposed Implementation

To fulfill the request for a toSeconds function, we can add a new function to the Duration module. Here's a possible implementation:

import { Duration } from '@effect/data';

Duration.prototype.toSeconds = function() {
  return this.toMilliseconds() / 1000;
};

In this implementation, we extend the Duration prototype to add a toSeconds method. This method calculates the duration in milliseconds using the existing toMilliseconds function and then divides it by 1000 to convert it to seconds.

Example Usage

Once the toSeconds function is implemented, it can be used as follows:

import { Duration } from '@effect/data';

const duration = Duration.fromSeconds(60);
const seconds = duration.toSeconds();

console.log(seconds); // Output: 60

In this example, we create a Duration value representing 60 seconds using the fromSeconds function. We then call the toSeconds function on this duration to obtain the duration in seconds.

Conclusion

The request for a toSeconds function in the Duration module of the Effect-TS ecosystem has been addressed with a proposed implementation. This function allows users to convert duration values to seconds, providing flexibility and convenience in various scenarios.

Discord thread

https://discord.com/channels/795981131316985866/1156566778957287484

gcanti added a commit that referenced this issue Sep 28, 2023
@gcanti gcanti closed this as completed in 0ff7653 Sep 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant