From 5f7bf7caaba4a4bdde15629598e1758ba5c11159 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C5=8Dan?= Date: Wed, 6 May 2026 04:32:13 -0600 Subject: [PATCH] fix: strftime class method uses wrong package for language dispatch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit strftime() used 'bless []' without specifying the package, causing class method calls like Date::Language::German->strftime(...) to always produce English output. The object was blessed into Date::Format::Generic instead of the calling package, so method dispatch resolved format_a/format_b/etc. to English formatters. Fix: change 'bless []' to 'bless [], $pkg' — matching what time2str() already does correctly on the adjacent line. Co-Authored-By: Claude Opus 4.6 --- lib/Date/Format/Generic.pm | 2 +- t/strftime-lang.t | 48 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 t/strftime-lang.t diff --git a/lib/Date/Format/Generic.pm b/lib/Date/Format/Generic.pm index b3b8bdf..25eb42d 100644 --- a/lib/Date/Format/Generic.pm +++ b/lib/Date/Format/Generic.pm @@ -44,7 +44,7 @@ sub strftime ($pkg,$fmt,$time,$tzname) = @_; - my $me = ref($pkg) ? $pkg : bless []; + my $me = ref($pkg) ? $pkg : bless [], $pkg; if(defined $tzname) { diff --git a/t/strftime-lang.t b/t/strftime-lang.t new file mode 100644 index 0000000..7c24f1a --- /dev/null +++ b/t/strftime-lang.t @@ -0,0 +1,48 @@ +use strict; +use warnings; +use Test::More; +use Date::Language; + +# Test that strftime produces language-specific output via both +# object method calls and class method calls. +# +# Bug: strftime() used 'bless []' without specifying the package, +# so class method calls like Date::Language::German->strftime(...) +# always produced English output instead of German. + +# Use a fixed time: Wednesday 2025-01-15 10:30:00 +# wday=3 (Wednesday), month=0 (January) +my @time = (0, 30, 10, 15, 0, 125, 3, 14, 0); + +# --- Object method (the common path) --- + +my $de = Date::Language->new('German'); +my $fmt_obj = $de->strftime("%A %B", \@time); +like($fmt_obj, qr/Mittwoch/i, "strftime object method: German weekday name"); +like($fmt_obj, qr/Januar/i, "strftime object method: German month name"); + +my $fr = Date::Language->new('French'); +$fmt_obj = $fr->strftime("%A %B", \@time); +like($fmt_obj, qr/mercredi/i, "strftime object method: French weekday name"); +like($fmt_obj, qr/janvier/i, "strftime object method: French month name"); + +# --- Class method (the buggy path before fix) --- + +require Date::Language::German; +my $fmt_cls = Date::Language::German->strftime("%A %B", \@time); +like($fmt_cls, qr/Mittwoch/i, "strftime class method: German weekday name"); +like($fmt_cls, qr/Januar/i, "strftime class method: German month name"); + +require Date::Language::French; +$fmt_cls = Date::Language::French->strftime("%A %B", \@time); +like($fmt_cls, qr/mercredi/i, "strftime class method: French weekday name"); +like($fmt_cls, qr/janvier/i, "strftime class method: French month name"); + +# --- Verify time2str also works (already correct, but let's confirm) --- + +my $epoch = 1736935800; # 2025-01-15 10:30:00 UTC +my $t2s = Date::Language::German->time2str("%A %B", $epoch, "UTC"); +like($t2s, qr/Mittwoch/i, "time2str class method: German weekday name"); +like($t2s, qr/Januar/i, "time2str class method: German month name"); + +done_testing;