From 53f003decc2e4a8c4eb4ff1db223670db36d7c89 Mon Sep 17 00:00:00 2001 From: Alex Gubarev Date: Fri, 24 Jul 2026 05:33:18 +0300 Subject: [PATCH] Fix Calendar.strftime/3 width calculations --- lib/elixir/lib/calendar.ex | 10 +++++----- lib/elixir/test/elixir/calendar_test.exs | 10 ++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/elixir/lib/calendar.ex b/lib/elixir/lib/calendar.ex index 954ed394713..0bcd2193fde 100644 --- a/lib/elixir/lib/calendar.ex +++ b/lib/elixir/lib/calendar.ex @@ -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 diff --git a/lib/elixir/test/elixir/calendar_test.exs b/lib/elixir/test/elixir/calendar_test.exs index 038c8d80508..ab5310172f7 100644 --- a/lib/elixir/test/elixir/calendar_test.exs +++ b/lib/elixir/test/elixir/calendar_test.exs @@ -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