Reputation: 38
If I want to specify an amount of time in C# (for example in a timer) I'd say TimeSpan.FromSeconds(20)
. It's explicit...a bit verbose, but there's no question about the length of time I'm specifying.
What's "the Elixir way" to accomplish the same thing? I could say 1_000 * 20
but that's implied, not explicit. There's no clear way to specify. I'm referring to seconds, microseconds, or even referring to time at all.
Upvotes: 1
Views: 334
Reputation: 23091
Elixir doesn't have a built-in duration/time-span type.
There is Timex.Duration
, but it's probably best to avoid adding Timex to your dependencies if you can get by with standard library code.
For working with interfaces that expect milliseconds, there are some built-in functions in the :timer
module to return milliseconds.
iex(1)> :timer.seconds(20)
20000
iex(2)> :timer.minutes(2)
120000
iex(3)> :timer.hours(4)
14400000
Upvotes: 2
Reputation: 121000
There is neither seconds not microseconds type in elixir. That said, the value in the vacuum cannot be “in seconds,” it’s just an integer.
Time fractions make sense only when you do math with times, and all the respective functions have an argument that allows you to specify it explicitly.
Consider Time.add/3
function, that adds a specified amount of time fractions to the time instance.
Here you explicitly specify the fraction to add.
iex(1)> t = Time.utc_now()
#⇒ ~T[20:37:01.699735]
iex(2)> Time.add(t, 20, :second)
#⇒ ~T[20:37:21.699735]
Upvotes: 2