-
Notifications
You must be signed in to change notification settings - Fork 3.5k
dk_use_iodata_for_datetime_formatting #14130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
4f66340
621cef0
9303f84
75ed764
6f63ca3
7025133
e091108
09eb9bf
fbdf235
3a1a74e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1220,6 +1220,37 @@ defmodule Calendar.ISO do | |||||||||||
| :basic | :extended | ||||||||||||
| ) :: String.t() | ||||||||||||
| def time_to_string( | ||||||||||||
| hour, | ||||||||||||
| minute, | ||||||||||||
| second, | ||||||||||||
| microsecond, | ||||||||||||
| format \\ :extended | ||||||||||||
| ) do | ||||||||||||
| time_to_iodata(hour, minute, second, microsecond, format) | ||||||||||||
| |> IO.iodata_to_binary() | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| @doc """ | ||||||||||||
| Converts the given time into a iodata. | ||||||||||||
|
|
||||||||||||
| Look at time_to_string/5 for more information | ||||||||||||
|
|
||||||||||||
| ## Examples | ||||||||||||
|
|
||||||||||||
| iex> data = Calendar.ISO.time_to_iodata(2, 2, 2, {2, 6}) | ||||||||||||
| iex> IO.iodata_to_binary(data) | ||||||||||||
| "02:02:02.000002" | ||||||||||||
|
|
||||||||||||
| """ | ||||||||||||
| @doc since: "1.19.0" | ||||||||||||
josevalim marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
| @spec time_to_iodata( | ||||||||||||
| Calendar.hour(), | ||||||||||||
| Calendar.minute(), | ||||||||||||
| Calendar.second(), | ||||||||||||
| Calendar.microsecond(), | ||||||||||||
| :basic | :extended | ||||||||||||
| ) :: iodata | ||||||||||||
| def time_to_iodata( | ||||||||||||
| hour, | ||||||||||||
| minute, | ||||||||||||
| second, | ||||||||||||
|
|
@@ -1228,24 +1259,42 @@ defmodule Calendar.ISO do | |||||||||||
| ) | ||||||||||||
| when is_hour(hour) and is_minute(minute) and is_second(second) and | ||||||||||||
| is_microsecond(ms_value, ms_precision) and format in [:basic, :extended] do | ||||||||||||
| time_to_string_guarded(hour, minute, second, microsecond, format) | ||||||||||||
| time_to_iodata_guarded(hour, minute, second, microsecond, format) | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| defp time_to_string_guarded(hour, minute, second, {_, 0}, format) do | ||||||||||||
| time_to_string_format(hour, minute, second, format) | ||||||||||||
| defp time_to_iodata_guarded(hour, minute, second, {_, 0}, format) do | ||||||||||||
| time_to_iodata_format(hour, minute, second, format) | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| defp time_to_string_guarded(hour, minute, second, {microsecond, precision}, format) do | ||||||||||||
| time_to_string_format(hour, minute, second, format) <> | ||||||||||||
| "." <> (microsecond |> zero_pad(6) |> binary_part(0, precision)) | ||||||||||||
| defp time_to_iodata_guarded(hour, minute, second, {microsecond, precision}, format) do | ||||||||||||
| [ | ||||||||||||
| time_to_iodata_format(hour, minute, second, format), | ||||||||||||
| ?. | ||||||||||||
| | microseconds_to_iodata(microsecond, precision) | ||||||||||||
| ] | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| defp time_to_string_format(hour, minute, second, :extended) do | ||||||||||||
| zero_pad(hour, 2) <> ":" <> zero_pad(minute, 2) <> ":" <> zero_pad(second, 2) | ||||||||||||
| @doc false | ||||||||||||
| def microseconds_to_iodata(_microsecond, 0), do: [] | ||||||||||||
| def microseconds_to_iodata(microsecond, 6), do: zero_pad(microsecond, 6) | ||||||||||||
|
|
||||||||||||
| def microseconds_to_iodata(microsecond, precision) do | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need it in the next pr |
||||||||||||
| num = div(microsecond, div_factor(precision)) | ||||||||||||
| zero_pad(num, precision) | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| defp div_factor(1), do: 100_000 | ||||||||||||
| defp div_factor(2), do: 10_000 | ||||||||||||
| defp div_factor(3), do: 1_000 | ||||||||||||
| defp div_factor(4), do: 100 | ||||||||||||
| defp div_factor(5), do: 10 | ||||||||||||
|
|
||||||||||||
| defp time_to_iodata_format(hour, minute, second, :extended) do | ||||||||||||
| [zero_pad(hour, 2), ?:, zero_pad(minute, 2), ?: | zero_pad(second, 2)] | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| defp time_to_string_format(hour, minute, second, :basic) do | ||||||||||||
| zero_pad(hour, 2) <> zero_pad(minute, 2) <> zero_pad(second, 2) | ||||||||||||
| defp time_to_iodata_format(hour, minute, second, :basic) do | ||||||||||||
| [zero_pad(hour, 2), zero_pad(minute, 2) | zero_pad(second, 2)] | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| @doc """ | ||||||||||||
|
|
@@ -1273,18 +1322,35 @@ defmodule Calendar.ISO do | |||||||||||
| @doc since: "1.4.0" | ||||||||||||
| @spec date_to_string(year, month, day, :basic | :extended) :: String.t() | ||||||||||||
| @impl true | ||||||||||||
| def date_to_string(year, month, day, format \\ :extended) | ||||||||||||
| def date_to_string(year, month, day, format \\ :extended) do | ||||||||||||
| date_to_iodata(year, month, day, format) | ||||||||||||
| |> IO.iodata_to_binary() | ||||||||||||
| end | ||||||||||||
|
|
||||||||||||
| @doc """ | ||||||||||||
| Converts the given date into a iodata. | ||||||||||||
| Look at date_to_string/4 for more information | ||||||||||||
|
||||||||||||
| Converts the given date into a iodata. | |
| Look at date_to_string/4 for more information | |
| Converts the given date into an iodata. | |
| See `date_to_string/4` for more information. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Converts the given naive_datetime into a iodata. | |
| Look at naive_datetime_to_iodata/8 for more information | |
| Converts the given naive_datetime into an iodata. | |
| See `naive_datetime_to_iodata/8` for more information. |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Converts the given datetime into a iodata. | |
| Look at datetime_to_iodata/12 for more information | |
| Converts the given datetime into an iodata. | |
| See `datetime_to_iodata/12` for more information. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.