From d7fcb524f069d5ac7b51153ee9958d59360f46df Mon Sep 17 00:00:00 2001 From: Neil Lyons Date: Tue, 25 Mar 2025 12:33:56 +0000 Subject: [PATCH 1/6] Modify Time.utc_now/1 to allow truncation and add Time.utc_now/2 --- lib/elixir/lib/calendar/time.ex | 39 +++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/elixir/lib/calendar/time.ex b/lib/elixir/lib/calendar/time.ex index 322b655236..1307c3c11a 100644 --- a/lib/elixir/lib/calendar/time.ex +++ b/lib/elixir/lib/calendar/time.ex @@ -60,16 +60,49 @@ defmodule Time do @doc """ Returns the current time in UTC. + You can pass a time unit to automatically truncate the resulting time. + + The default unit if none gets passed is `:microseconds`. + ## Examples iex> time = Time.utc_now() iex> time.hour >= 0 true + iex> time = Time.utc_now(:second) + iex> time.microsecond + {0, 0} + """ @doc since: "1.4.0" @spec utc_now(Calendar.calendar()) :: t - def utc_now(calendar \\ Calendar.ISO) do + def utc_now(calendar_or_time_unit \\ Calendar.ISO) do + case calendar_or_time_unit do + unit when unit in [:microsecond, :millisecond, :second] -> + utc_now(unit, Calendar.ISO) + + calendar -> + utc_now(:microsecond, calendar) + end + end + + @doc """ + Returns the current time in UTC, supporting + a specific calendar and precision. + + ## Examples + iex> time = Time.utc_now(:microsecond, Calendar.ISO) + iex> time.hour >= 0 + + iex> time = Time.utc_now(:second, Calendar.ISO) + iex> time.microsecond + {0, 0} + + """ + @doc since: "1.9.0" + @spec utc_now(:microsecond | :millisecond | :second, Calendar.calendar()) :: t + def utc_now(time_unit, calendar) when time_unit in [:microsecond, :millisecond, :second] do {:ok, _, time, microsecond} = Calendar.ISO.from_unix(:os.system_time(), :native) {hour, minute, second} = time @@ -81,7 +114,9 @@ defmodule Time do calendar: Calendar.ISO } - convert!(iso_time, calendar) + iso_time + |> convert!(calendar) + |> truncate(time_unit) end @doc """ From 58eeba95cb25ca8480f140bee6b3128336d8c264 Mon Sep 17 00:00:00 2001 From: Neil Lyons Date: Tue, 25 Mar 2025 12:46:02 +0000 Subject: [PATCH 2/6] Update type specs and docs --- lib/elixir/lib/calendar/time.ex | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/elixir/lib/calendar/time.ex b/lib/elixir/lib/calendar/time.ex index 1307c3c11a..18394353fe 100644 --- a/lib/elixir/lib/calendar/time.ex +++ b/lib/elixir/lib/calendar/time.ex @@ -62,7 +62,7 @@ defmodule Time do You can pass a time unit to automatically truncate the resulting time. - The default unit if none gets passed is `:microseconds`. + The default unit if none gets passed is `:microsecond`. ## Examples @@ -76,7 +76,7 @@ defmodule Time do """ @doc since: "1.4.0" - @spec utc_now(Calendar.calendar()) :: t + @spec utc_now(Calendar.calendar() | :microsecond | :millisecond | :second) :: t def utc_now(calendar_or_time_unit \\ Calendar.ISO) do case calendar_or_time_unit do unit when unit in [:microsecond, :millisecond, :second] -> @@ -88,8 +88,7 @@ defmodule Time do end @doc """ - Returns the current time in UTC, supporting - a specific calendar and precision. + Returns the current time in UTC, supporting a precision and a specific calendar. ## Examples iex> time = Time.utc_now(:microsecond, Calendar.ISO) From c42bbef131dfb687646d3dad278dfd8ab30844de Mon Sep 17 00:00:00 2001 From: Neil Lyons Date: Tue, 25 Mar 2025 12:50:28 +0000 Subject: [PATCH 3/6] Add line break below heading in docs --- lib/elixir/lib/calendar/time.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/elixir/lib/calendar/time.ex b/lib/elixir/lib/calendar/time.ex index 18394353fe..3239244c95 100644 --- a/lib/elixir/lib/calendar/time.ex +++ b/lib/elixir/lib/calendar/time.ex @@ -91,6 +91,7 @@ defmodule Time do Returns the current time in UTC, supporting a precision and a specific calendar. ## Examples + iex> time = Time.utc_now(:microsecond, Calendar.ISO) iex> time.hour >= 0 From c35f228c16f46ab063e984294715aa4ea7c798f2 Mon Sep 17 00:00:00 2001 From: Neil Lyons Date: Tue, 25 Mar 2025 13:28:26 +0000 Subject: [PATCH 4/6] Fix @doc since: number MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: José Valim --- lib/elixir/lib/calendar/time.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/elixir/lib/calendar/time.ex b/lib/elixir/lib/calendar/time.ex index 3239244c95..750be9b75c 100644 --- a/lib/elixir/lib/calendar/time.ex +++ b/lib/elixir/lib/calendar/time.ex @@ -100,7 +100,7 @@ defmodule Time do {0, 0} """ - @doc since: "1.9.0" + @doc since: "1.19.0" @spec utc_now(:microsecond | :millisecond | :second, Calendar.calendar()) :: t def utc_now(time_unit, calendar) when time_unit in [:microsecond, :millisecond, :second] do {:ok, _, time, microsecond} = Calendar.ISO.from_unix(:os.system_time(), :native) From 5ca853e6f0e154890f5e4a3ff9757c8a3b541c63 Mon Sep 17 00:00:00 2001 From: Neil Lyons Date: Tue, 25 Mar 2025 13:43:51 +0000 Subject: [PATCH 5/6] Use :native as default precision for Time.utc_now/1 --- lib/elixir/lib/calendar/time.ex | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/elixir/lib/calendar/time.ex b/lib/elixir/lib/calendar/time.ex index 750be9b75c..af6d294bba 100644 --- a/lib/elixir/lib/calendar/time.ex +++ b/lib/elixir/lib/calendar/time.ex @@ -62,7 +62,7 @@ defmodule Time do You can pass a time unit to automatically truncate the resulting time. - The default unit if none gets passed is `:microsecond`. + The default unit if none gets passed is `:native` which results on a default resolution of microseconds. ## Examples @@ -76,14 +76,14 @@ defmodule Time do """ @doc since: "1.4.0" - @spec utc_now(Calendar.calendar() | :microsecond | :millisecond | :second) :: t + @spec utc_now(Calendar.calendar() | :native | :microsecond | :millisecond | :second) :: t def utc_now(calendar_or_time_unit \\ Calendar.ISO) do case calendar_or_time_unit do - unit when unit in [:microsecond, :millisecond, :second] -> + unit when unit in [:native, :microsecond, :millisecond, :second] -> utc_now(unit, Calendar.ISO) calendar -> - utc_now(:microsecond, calendar) + utc_now(:native, calendar) end end @@ -101,9 +101,10 @@ defmodule Time do """ @doc since: "1.19.0" - @spec utc_now(:microsecond | :millisecond | :second, Calendar.calendar()) :: t - def utc_now(time_unit, calendar) when time_unit in [:microsecond, :millisecond, :second] do - {:ok, _, time, microsecond} = Calendar.ISO.from_unix(:os.system_time(), :native) + @spec utc_now(:native | :microsecond | :millisecond | :second, Calendar.calendar()) :: t + def utc_now(time_unit, calendar) + when time_unit in [:native, :microsecond, :millisecond, :second] do + {:ok, _, time, microsecond} = Calendar.ISO.from_unix(System.os_time(time_unit), time_unit) {hour, minute, second} = time iso_time = %Time{ @@ -114,9 +115,7 @@ defmodule Time do calendar: Calendar.ISO } - iso_time - |> convert!(calendar) - |> truncate(time_unit) + convert!(iso_time, calendar) end @doc """ From fd446b6589976abf4359b6eb60857555bfc66929 Mon Sep 17 00:00:00 2001 From: Neil Lyons Date: Tue, 25 Mar 2025 14:39:03 +0000 Subject: [PATCH 6/6] Check result of expression is true Co-authored-by: Jean Klingler --- lib/elixir/lib/calendar/time.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/elixir/lib/calendar/time.ex b/lib/elixir/lib/calendar/time.ex index af6d294bba..e3b4fce96c 100644 --- a/lib/elixir/lib/calendar/time.ex +++ b/lib/elixir/lib/calendar/time.ex @@ -94,6 +94,7 @@ defmodule Time do iex> time = Time.utc_now(:microsecond, Calendar.ISO) iex> time.hour >= 0 + true iex> time = Time.utc_now(:second, Calendar.ISO) iex> time.microsecond