We have Kernel.to_timeout, which returns this in milliseconds, but we don't
have a general API. The implementation raises if the duration has months or
years in it, because it is impossible to convert it to an absolute value
without having a reference datetime attached to it. In such cases, you can
achieve what you want using "DateTime.diff(DateTime.shift(dt, duration),
dt)", so there is probably not a big reason for exposing an API for this.

On Sun, May 26, 2024 at 8:02 AM Wígny Almeida <wignyb...@gmail.com> wrote:

> Following the recent proposals and work done around the new `Duration`
> API, I wanted to propose having a function available for converting a
> `Duration` into a specific time unit, returning a float value.
>
> I see this function being useful where in your system you want to work
> with durations, but on inputs and outputs, you are required to convert this
> duration into a more plain value. The examples I can think of are 1) having
> an Ecto custom type that loads a value in seconds (or another unit) from
> the database, converting it into a duration using `Duration.new!(second:
> value)` and dumps a duration again into a value in seconds by calling
> `Duration.to_float(duration, :second)` and 2) transforming a duration into
> a plain number in Phoenix HTML inputs.
>
> Examples of similar APIs I could find are in the JavaScript
> Temporal.Duration
> <https://tc39.es/proposal-temporal/docs/duration.html#total> API or Ruby
> ActiveSupport::Duration
> <https://api.rubyonrails.org/classes/ActiveSupport/Duration.html#method-i-in_days:~:text=in_days,%20in_hours,%20in_minutes,%20in_months,%20in_seconds,%20in_weeks,%20in_years>
> .
>
> One challenge we may encounter when implementing this function is
> calculating, for example, how many seconds there are in a year. For this, I
> think we can rely on the Calendar APIs?
>
> Thoughts?
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "elixir-lang-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to elixir-lang-core+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/f11b73c0-b709-4fbe-94fe-f76412f7a14cn%40googlegroups.com
> <https://groups.google.com/d/msgid/elixir-lang-core/f11b73c0-b709-4fbe-94fe-f76412f7a14cn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4KQ0wJ5PH%3DcOn6ev1Nr0iFZFC4gb3OjL65qKot0tXTQjg%40mail.gmail.com.

Reply via email to