Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/elixir/lib/calendar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -998,14 +998,14 @@ defmodule Calendar do
raise ArgumentError, "invalid strftime format: %#{next}"
end

defp pad_preferred(result, width, pad) when length(result) < width do
pad_preferred([pad | result], width, pad)
defp pad_preferred(result, width, pad) do
result
|> IO.iodata_to_binary()
|> pad_leading(width, pad)
end

defp pad_preferred(result, _width, _pad), do: result

defp pad_leading(string, count, padding) do
to_pad = count - byte_size(string)
to_pad = count - String.length(string)
if to_pad > 0, do: do_pad_leading(to_pad, padding, string), else: string
end

Expand Down
10 changes: 10 additions & 0 deletions lib/elixir/test/elixir/calendar_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,16 @@ defmodule CalendarTest do
assert Calendar.strftime(~N[2019-08-15 17:07:57], "%010A") == "00Thursday"
end

test "measures padding width in graphemes" do
assert Calendar.strftime(%{month: 9}, "%10B", month_names: fn _month -> "вересень" end) ==
" вересень"
end

test "measures preferred format padding width" do
assert Calendar.strftime(~N[2019-08-15 17:07:57], "%20c") ==
"02019-08-15 17:07:57"
end

test "limits width to at most 1024 characters" do
assert Calendar.strftime(~D[2019-08-15], "%1024d") |> byte_size() == 1024

Expand Down
Loading