From a78126c55ecbe014ab2a214f324b32762e6a268d Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Mon, 30 Jan 2012 15:08:50 -0400 Subject: [PATCH] calendar, prettydate: Fix strftime encoding bug strftime is a C function, it does not return decoded utf8. Several places in ikiwiki manually decoded it, but at least two forgot to. Also, strftime might not return even encoded utf8, if LC_TIME is set to a non-utf8 value. Went ahead and supported decoding whatever encoding it uses. The remaining direct calls to strftime() are all ones that first set LC_TIME=C, in order to get times that are not for human display. --- IkiWiki.pm | 16 +++++++++++++--- IkiWiki/Plugin/calendar.pm | 15 +++++++-------- IkiWiki/Plugin/comments.pm | 3 +-- IkiWiki/Plugin/prettydate.pm | 4 ++-- debian/changelog | 1 + .../Encoding_problem_in_calendar_plugin.mdwn | 4 ++++ ..._problem_in_french_with_ikiwiki-calendar.mdwn | 3 +++ 7 files changed, 31 insertions(+), 15 deletions(-) diff --git a/IkiWiki.pm b/IkiWiki.pm index bc56501da..0a788f35b 100644 --- a/IkiWiki.pm +++ b/IkiWiki.pm @@ -20,7 +20,7 @@ use Exporter q{import}; our @EXPORT = qw(hook debug error htmlpage template template_depends deptype add_depends pagespec_match pagespec_match_list bestlink htmllink readfile writefile pagetype srcfile pagename - displaytime will_render gettext ngettext urlto targetpage + displaytime strftime_utf8 will_render gettext ngettext urlto targetpage add_underlay pagetitle titlepage linkpage newpagefile inject add_link add_autofile %config %links %pagestate %wikistate %renderedfiles @@ -1148,9 +1148,19 @@ sub formattime ($;$) { $format=$config{timeformat}; } + return strftime_utf8($format, localtime($time)); +} + +my $strftime_encoding; +sub strftime_utf8 { # strftime doesn't know about encodings, so make sure - # its output is properly treated as utf8 - return decode_utf8(POSIX::strftime($format, localtime($time))); + # its output is properly treated as utf8. + # Note that this does not handle utf-8 in the format string. + $strftime_encoding = POSIX::setlocale(&POSIX::LC_TIME) =~ m#\.([^@]+)# + unless defined $strftime_encoding; + $strftime_encoding + ? Encode::decode($strftime_encoding, POSIX::strftime(@_)) + : POSIX::strftime(@_); } sub date_3339 ($) { diff --git a/IkiWiki/Plugin/calendar.pm b/IkiWiki/Plugin/calendar.pm index c7d2b7c01..fc497b3c7 100644 --- a/IkiWiki/Plugin/calendar.pm +++ b/IkiWiki/Plugin/calendar.pm @@ -22,7 +22,6 @@ use warnings; use strict; use IkiWiki 3.00; use Time::Local; -use POSIX (); my $time=time; my @now=localtime($time); @@ -123,10 +122,10 @@ sub format_month (@) { } # Find out month names for this, next, and previous months - my $monthabbrev=POSIX::strftime("%b", @monthstart); - my $monthname=POSIX::strftime("%B", @monthstart); - my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900))); - my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900))); + my $monthabbrev=strftime_utf8("%b", @monthstart); + my $monthname=strftime_utf8("%B", @monthstart); + my $pmonthname=strftime_utf8("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900))); + my $nmonthname=strftime_utf8("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900))); my $archivebase = 'archives'; $archivebase = $config{archivebase} if defined $config{archivebase}; @@ -182,7 +181,7 @@ EOF my %dowabbr; for my $dow ($week_start_day..$week_start_day+6) { my @day=localtime(timelocal(0,0,0,$start_day++,$params{month}-1,$params{year}-1900)); - my $downame = POSIX::strftime("%A", @day); + my $downame = strftime_utf8("%A", @day); my $dowabbr = substr($downame, 0, 1); $downame{$dow % 7}=$downame; $dowabbr{$dow % 7}=$dowabbr; @@ -329,8 +328,8 @@ EOF for (my $month = 1; $month <= 12; $month++) { my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900)); my $murl; - my $monthname = POSIX::strftime("%B", @day); - my $monthabbr = POSIX::strftime("%b", @day); + my $monthname = strftime_utf8("%B", @day); + my $monthabbr = strftime_utf8("%b", @day); $calendar.=qq{\t\n} if ($month % $params{months_per_row} == 1); my $tag; my $mtag=sprintf("%02d", $month); diff --git a/IkiWiki/Plugin/comments.pm b/IkiWiki/Plugin/comments.pm index 3ad2a0e13..91a482ed6 100644 --- a/IkiWiki/Plugin/comments.pm +++ b/IkiWiki/Plugin/comments.pm @@ -9,7 +9,6 @@ use warnings; use strict; use IkiWiki 3.00; use Encode; -use POSIX qw(strftime); use constant PREVIEW => "Preview"; use constant POST_COMMENT => "Post comment"; @@ -460,7 +459,7 @@ sub editcomment ($$) { } $content .= " subject=\"$subject\"\n"; - $content .= " date=\"" . decode_utf8(strftime('%Y-%m-%dT%H:%M:%SZ', gmtime)) . "\"\n"; + $content .= " date=\"" . strftime_utf8('%Y-%m-%dT%H:%M:%SZ', gmtime) . "\"\n"; my $editcontent = $form->field('editcontent'); $editcontent="" if ! defined $editcontent; diff --git a/IkiWiki/Plugin/prettydate.pm b/IkiWiki/Plugin/prettydate.pm index 82d8a3df3..b0931cb55 100644 --- a/IkiWiki/Plugin/prettydate.pm +++ b/IkiWiki/Plugin/prettydate.pm @@ -118,10 +118,10 @@ sub IkiWiki::formattime ($;$) { } } - $t=~s{\%A-}{my @yest=@t; $yest[6]--; strftime("%A", \@yest)}eg; + $t=~s{\%A-}{my @yest=@t; $yest[6]--; strftime_utf8("%A", \@yest)}eg; $format=~s/\%X/$t/g; - return strftime($format, \@t); + return strftime_utf8($format, \@t); } 1 diff --git a/debian/changelog b/debian/changelog index 1765142a4..57fe7861c 100644 --- a/debian/changelog +++ b/debian/changelog @@ -5,6 +5,7 @@ ikiwiki (3.20120116) UNRELEASED; urgency=low * Switch to YAML::XS to work around insanity in YAML::Mo. Closes: #657533 * cvs: Ensure non-text files are added in binary mode. (Amitai Schlair) * cvs: Various cleanups and testing. (Amitai Schlair) + * calendar, prettydate: Fix strftime encoding bug. -- Joey Hess Mon, 16 Jan 2012 13:41:14 -0400 diff --git a/doc/bugs/Encoding_problem_in_calendar_plugin.mdwn b/doc/bugs/Encoding_problem_in_calendar_plugin.mdwn index 2ccc43c03..80e9f2c82 100644 --- a/doc/bugs/Encoding_problem_in_calendar_plugin.mdwn +++ b/doc/bugs/Encoding_problem_in_calendar_plugin.mdwn @@ -11,6 +11,10 @@ The problem is that I do not know Perl, encoding is one of the thing I would be Cheers, Louis +> Yes, this seems basically right. I've applied a modified version of this. +> [[done]] +> --[[Joey]] + diff --git a/IkiWiki/Plugin/calendar.pm b/IkiWiki/Plugin/calendar.pm index c7d2b7c..1345939 100644 diff --git a/doc/forum/Encoding_problem_in_french_with_ikiwiki-calendar.mdwn b/doc/forum/Encoding_problem_in_french_with_ikiwiki-calendar.mdwn index 7e21644ac..472412de1 100644 --- a/doc/forum/Encoding_problem_in_french_with_ikiwiki-calendar.mdwn +++ b/doc/forum/Encoding_problem_in_french_with_ikiwiki-calendar.mdwn @@ -15,3 +15,6 @@ Is someone could test it and verify if it works or not? Thanks. Zut + +> This was discussed in [[bugs/Encoding_problem_in_calendar_plugin]] +> and is now fixed. --[[Joey]] -- 2.26.2