From 707b965d2136c7980098dcebbb26b788354c99dc Mon Sep 17 00:00:00 2001 From: Alexei Sholik Date: Thu, 22 Sep 2016 23:28:13 +0300 Subject: [PATCH] Use singular names for time units, as in Erlang/OTP 19.1. Fix #5236 (#5244) --- lib/elixir/lib/calendar.ex | 8 ++--- lib/elixir/lib/calendar/iso.ex | 4 +-- lib/elixir/lib/system.ex | 40 ++++++++++++++++++------- lib/elixir/test/elixir/process_test.exs | 2 +- lib/elixir/test/elixir/system_test.exs | 18 +++++------ lib/ex_unit/lib/ex_unit/server.ex | 2 +- lib/logger/lib/logger.ex | 6 ++-- 7 files changed, 49 insertions(+), 31 deletions(-) diff --git a/lib/elixir/lib/calendar.ex b/lib/elixir/lib/calendar.ex index 62c333edc3..924d01f24c 100644 --- a/lib/elixir/lib/calendar.ex +++ b/lib/elixir/lib/calendar.ex @@ -666,7 +666,7 @@ defmodule NaiveDateTime do iex> NaiveDateTime.from_unix(1464096368) {:ok, ~N[2016-05-24 13:26:08]} - iex> NaiveDateTime.from_unix(1432560368868569, :microseconds) + iex> NaiveDateTime.from_unix(1432560368868569, :microsecond) {:ok, ~N[2015-05-25 13:26:08.868569]} The unit can also be an integer as in `System.time_unit`: @@ -684,7 +684,7 @@ defmodule NaiveDateTime do When a Unix time before that moment is passed to `from_unix/2`, `:error` will be returned. """ @spec from_unix(integer, :native | System.time_unit) :: {:ok, NaiveDateTime.t} - def from_unix(integer, unit \\ :seconds) when is_integer(integer) do + def from_unix(integer, unit \\ :second) when is_integer(integer) do case Calendar.ISO.from_unix(integer, unit) do {:ok, {year, month, day}, {hour, minute, second}, microsecond} -> {:ok, %NaiveDateTime{year: year, month: month, day: day, @@ -711,7 +711,7 @@ defmodule NaiveDateTime do iex> NaiveDateTime.from_unix!(1464096368) ~N[2016-05-24 13:26:08] - iex> NaiveDateTime.from_unix!(1432560368868569, :microseconds) + iex> NaiveDateTime.from_unix!(1432560368868569, :microsecond) ~N[2015-05-25 13:26:08.868569] Negative Unix times are supported, up to -#{@unix_epoch} seconds, @@ -723,7 +723,7 @@ defmodule NaiveDateTime do When a Unix time before that moment is passed to `from_unix/2`, `:error` will be returned. """ @spec from_unix!(non_neg_integer, :native | System.time_unit) :: NaiveDateTime.t - def from_unix!(integer, unit \\ :seconds) when is_atom(unit) do + def from_unix!(integer, unit \\ :second) when is_atom(unit) do case from_unix(integer, unit) do {:ok, datetime} -> datetime diff --git a/lib/elixir/lib/calendar/iso.ex b/lib/elixir/lib/calendar/iso.ex index 7f91b84bb0..4bc29972a7 100644 --- a/lib/elixir/lib/calendar/iso.ex +++ b/lib/elixir/lib/calendar/iso.ex @@ -151,7 +151,7 @@ defmodule Calendar.ISO do @doc false def from_unix(integer, unit) when is_integer(integer) do - total = System.convert_time_unit(integer, unit, :microseconds) + total = System.convert_time_unit(integer, unit, :microsecond) if total < -@unix_epoch * 1_000_000 do {:error, :invalid_unix_time} else @@ -163,7 +163,7 @@ defmodule Calendar.ISO do end defp precision_for_unit(unit) do - subseconds = div System.convert_time_unit(1, :seconds, unit), 10 + subseconds = div System.convert_time_unit(1, :second, unit), 10 precision_for_unit(subseconds, 0) end diff --git a/lib/elixir/lib/system.ex b/lib/elixir/lib/system.ex index 436a294ba6..9998fe16d1 100644 --- a/lib/elixir/lib/system.ex +++ b/lib/elixir/lib/system.ex @@ -53,7 +53,7 @@ defmodule System do the time, all calculations are done in the `:native` unit, to avoid loss of precision, with `convert_time_unit/3` being invoked at the end to convert to a specific time unit like - milliseconds or microseconds. See the `t:time_unit/0` type for + `:millisecond` or `:microsecond`. See the `t:time_unit/0` type for more information. For a more complete rundown on the VM support for different @@ -65,25 +65,30 @@ defmodule System do @typedoc """ The time unit to be passed to functions like `monotonic_time/1` and others. - The `:seconds`, `:milliseconds`, `:microseconds` and `:nanoseconds` time + The `:second`, `:millisecond`, `:microsecond` and `:nanosecond` time units controls the return value of the functions that accept a time unit. A time unit can also be a strictly positive integer. In this case, it represents the "parts per second": the time will be returned in `1 / - parts_per_second` seconds. For example, using the `:milliseconds` time unit + parts_per_second` seconds. For example, using the `:millisecond` time unit is equivalent to using `1000` as the time unit (as the time will be returned in 1/1000 seconds - milliseconds). - Keep in mind the Erlang API will use `:milli_seconds`, `:micro_seconds` - and `:nano_seconds` as time units although Elixir normalizes their spelling - to match the SI convention. + Keep in mind the Erlang API prior to version 19.1 will use `:milli_seconds`, + `:micro_seconds` and `:nano_seconds` as time units although Elixir normalizes + their spelling to match the SI convention. """ @type time_unit :: - :seconds + :second + | :millisecond + | :microsecond + | :nanosecond + | pos_integer + # TODO: Deprecate these in Elixir 2.0 + | :seconds | :milliseconds | :microseconds | :nanoseconds - | pos_integer @base_dir :filename.join(__DIR__, "../../..") @version_file :filename.join(@base_dir, "VERSION") @@ -674,7 +679,7 @@ defmodule System do starts and stays the same until the runtime is stopped. To determine what the `:native` unit amounts to in a system, you can call this function to convert 1 second to the `:native` time unit (i.e., - `System.convert_time_unit(1, :seconds, :native)`). + `System.convert_time_unit(1, :second, :native)`). """ @spec convert_time_unit(integer, time_unit | :native, time_unit | :native) :: integer def convert_time_unit(time, from_unit, to_unit) do @@ -790,6 +795,18 @@ defmodule System do defp normalize_time_unit(:native), do: :native + + # TODO: Remove these mappings once Elixir requires Erlang/OTP 19.1 + defp normalize_time_unit(:second), + do: :seconds + defp normalize_time_unit(:millisecond), + do: :milli_seconds + defp normalize_time_unit(:microsecond), + do: :micro_seconds + defp normalize_time_unit(:nanosecond), + do: :nano_seconds + + # TODO: Warn on Elixir 1.5 defp normalize_time_unit(:seconds), do: :seconds defp normalize_time_unit(:milliseconds), @@ -798,6 +815,7 @@ defmodule System do do: :micro_seconds defp normalize_time_unit(:nanoseconds), do: :nano_seconds + defp normalize_time_unit(unit) when is_integer(unit) and unit > 0, do: unit @@ -809,8 +827,8 @@ defmodule System do defp normalize_time_unit(other) do raise ArgumentError, - "unsupported time unit. Expected :seconds, :milliseconds, " <> - ":microseconds, :nanoseconds, or a positive integer, " <> + "unsupported time unit. Expected :second, :millisecond, " <> + ":microsecond, :nanosecond, or a positive integer, " <> "got #{inspect other}" end end diff --git a/lib/elixir/test/elixir/process_test.exs b/lib/elixir/test/elixir/process_test.exs index 8c5d441ed3..6d7456fe74 100644 --- a/lib/elixir/test/elixir/process_test.exs +++ b/lib/elixir/test/elixir/process_test.exs @@ -69,7 +69,7 @@ defmodule ProcessTest do end test "send_after/4 with absolute time sends message once expired" do - time = System.monotonic_time(:milliseconds) + 10 + time = System.monotonic_time(:millisecond) + 10 Process.send_after(self(), :hello, time, abs: true) assert_receive :hello end diff --git a/lib/elixir/test/elixir/system_test.exs b/lib/elixir/test/elixir/system_test.exs index f6b3f1ef26..b2e6e162f3 100644 --- a/lib/elixir/test/elixir/system_test.exs +++ b/lib/elixir/test/elixir/system_test.exs @@ -150,8 +150,8 @@ defmodule SystemTest do end test "monotonic_time/1" do - assert is_integer(System.monotonic_time(:nanoseconds)) - assert abs(System.monotonic_time(:microseconds)) < abs(System.monotonic_time(:nanoseconds)) + assert is_integer(System.monotonic_time(:nanosecond)) + assert abs(System.monotonic_time(:microsecond)) < abs(System.monotonic_time(:nanosecond)) end test "system_time/0" do @@ -159,13 +159,13 @@ defmodule SystemTest do end test "system_time/1" do - assert is_integer(System.system_time(:nanoseconds)) - assert abs(System.system_time(:microseconds)) < abs(System.system_time(:nanoseconds)) + assert is_integer(System.system_time(:nanosecond)) + assert abs(System.system_time(:microsecond)) < abs(System.system_time(:nanosecond)) end test "time_offset/0 and time_offset/1" do assert is_integer(System.time_offset()) - assert is_integer(System.time_offset(:seconds)) + assert is_integer(System.time_offset(:second)) end test "os_time/0" do @@ -173,8 +173,8 @@ defmodule SystemTest do end test "os_time/1" do - assert is_integer(System.os_time(:nanoseconds)) - assert abs(System.os_time(:microseconds)) < abs(System.os_time(:nanoseconds)) + assert is_integer(System.os_time(:nanosecond)) + assert abs(System.os_time(:microsecond)) < abs(System.os_time(:nanosecond)) end test "unique_integer/0 and unique_integer/1" do @@ -184,8 +184,8 @@ defmodule SystemTest do end test "convert_time_unit/3" do - time = System.monotonic_time(:nanoseconds) - assert abs(System.convert_time_unit(time, :nanoseconds, :microseconds)) < abs(time) + time = System.monotonic_time(:nanosecond) + assert abs(System.convert_time_unit(time, :nanosecond, :microsecond)) < abs(time) end test "schedulers/0" do diff --git a/lib/ex_unit/lib/ex_unit/server.ex b/lib/ex_unit/lib/ex_unit/server.ex index 845141d1e4..a5f399683a 100644 --- a/lib/ex_unit/lib/ex_unit/server.ex +++ b/lib/ex_unit/lib/ex_unit/server.ex @@ -52,7 +52,7 @@ defmodule ExUnit.Server do end def handle_call(:cases_loaded, _from, %{loaded: loaded} = state) when is_integer(loaded) do - diff = System.convert_time_unit(System.monotonic_time - loaded, :native, :microseconds) + diff = System.convert_time_unit(System.monotonic_time - loaded, :native, :microsecond) {:reply, diff, take_cases(%{state | loaded: :done})} end diff --git a/lib/logger/lib/logger.ex b/lib/logger/lib/logger.ex index e4925dc1cf..5430caf3f7 100644 --- a/lib/logger/lib/logger.ex +++ b/lib/logger/lib/logger.ex @@ -254,7 +254,7 @@ defmodule Logger do * the first element is always the atom `Logger` * `message` is the actual message (as chardata) * `timestamp` is the timestamp for when the message was logged, as a - `{{year, month, day}, {hour, minute, second, milliseconds}}` tuple + `{{year, month, day}, {hour, minute, second, millisecond}}` tuple * `metadata` is a keyword list of metadata used when logging the message It is recommended that handlers ignore messages where @@ -306,8 +306,8 @@ defmodule Logger do @doc """ Alters the current process metadata according the given keyword list. - This will merge the given keyword list into the existing metadata. With - the exception of setting a key to nil will remove a key from the metadata. + This will merge the given keyword list into the existing metadata. With + the exception of setting a key to nil will remove a key from the metadata. """ def metadata(keywords) do {enabled?, metadata} = __metadata__()