Skip to content

Commit

Permalink
* added time_locale() and date_locale() to (hopefully) fix the bug in…
Browse files Browse the repository at this point in the history
… test 25

git-svn-id: svn://svn.tt2.org/tt/Template2/trunk@792 d5a88997-0a34-4036-9ed2-92fb5d660d91
  • Loading branch information
abw committed Jan 13, 2004
1 parent 57bf045 commit 0cffbc2
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions t/date.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -49,26 +49,43 @@ my $params = {
daystr => &POSIX::strftime($format->{ timeday }, @ltime), daystr => &POSIX::strftime($format->{ timeday }, @ltime),
defstr => &POSIX::strftime($format->{ default }, @ltime), defstr => &POSIX::strftime($format->{ default }, @ltime),
now => sub { now => sub {
&POSIX::strftime(shift || $format->{ default }, localtime(time)); &POSIX::strftime(shift || $format->{ default }, localtime(time));
}, },
nowloc => sub { my ($time, $format, $locale) = @_; time_locale => \&time_locale,
my $old_locale = &POSIX::setlocale(&POSIX::LC_ALL); date_locale => \&date_locale,
date_calc => $got_date_calc,
};


# some systems expect locales to have a particular suffix sub time_locale {
for my $suffix ('', @Template::Plugin::Date::LOCALE_SUFFIX) { my ($time, $format, $locale) = @_;
my $try_locale = $locale.$suffix; my $old_locale = &POSIX::setlocale(&POSIX::LC_ALL);

# some systems expect locales to have a particular suffix
for my $suffix ('', @Template::Plugin::Date::LOCALE_SUFFIX) {
my $try_locale = $locale.$suffix;
my $setlocale = &POSIX::setlocale(&POSIX::LC_ALL, $try_locale); my $setlocale = &POSIX::setlocale(&POSIX::LC_ALL, $try_locale);
if (defined $setlocale && $try_locale eq $setlocale) { if (defined $setlocale && $try_locale eq $setlocale) {
$locale = $try_locale; $locale = $try_locale;
last; last;
}
} }
my $datestr = &POSIX::strftime($format, localtime($time)); }
&POSIX::setlocale(&POSIX::LC_ALL, $old_locale); my $datestr = &POSIX::strftime($format, localtime($time));
return $datestr; &POSIX::setlocale(&POSIX::LC_ALL, $old_locale);
}, return $datestr;
date_calc => $got_date_calc, }
};
sub date_locale {
my ($time, $format, $locale) = @_;
my @date = (split(/(?:\/| |:|-)/, $time))[2,1,0,3..5];
return (undef, Template::Exception->new('date',
"bad time/date string: expects 'h:m:s d:m:y' got: '$time'"))
unless @date >= 6 && defined $date[5];
$date[4] -= 1; # correct month number 1-12 to range 0-11
$date[5] -= 1900; # convert absolute year to years since 1900
$time = &POSIX::mktime(@date);
return time_locale($time, $format, $locale);
}



# force second to rollover so that we reliably see any tests failing. # force second to rollover so that we reliably see any tests failing.
# lesson learnt from 2.07b where I broke the Date plugin's handling of a # lesson learnt from 2.07b where I broke the Date plugin's handling of a
Expand Down Expand Up @@ -162,8 +179,8 @@ In French, today's day is: [% french.format +%]
-- expect -- -- expect --
-- process -- -- process --
In English, today's day is: [% nowloc(time, '%A', 'en_GB') +%] In English, today's day is: [% time_locale(time, '%A', 'en_GB') +%]
In French, today's day is: [% nowloc(time, '%A', 'fr_FR') +%] In French, today's day is: [% time_locale(time, '%A', 'fr_FR') +%]
-- test -- -- test --
[% USE english = date(format => '%A') %] [% USE english = date(format => '%A') %]
Expand All @@ -175,8 +192,8 @@ In French, today's day is:
-- expect -- -- expect --
-- process -- -- process --
In English, today's day is: [% nowloc(time, '%A', 'en_GB') +%] In English, today's day is: [% time_locale(time, '%A', 'en_GB') +%]
In French, today's day is: [% nowloc(time, '%A', 'fr_FR') +%] In French, today's day is: [% time_locale(time, '%A', 'fr_FR') +%]
-- test -- -- test --
[% USE date %] [% USE date %]
Expand All @@ -190,12 +207,9 @@ In French, today's day is: [% nowloc(time, '%A', 'fr_FR') +%]
[% USE day = date(format => '%A', locale => 'en_GB') %] [% USE day = date(format => '%A', locale => 'en_GB') %]
[% day.format('4:20:00 13-9-2000') %] [% day.format('4:20:00 13-9-2000') %]
-- expect -- -- expect --
-- process -- -- process --
[% # 4:20:00 9-13-2000 equals 968818800 seconds since the epoch [% date_locale('4:20:00 13-9-2000', '%A', 'en_GB') %]
nowloc(968818800, '%A', 'en_GB')
%]
-- test -- -- test --
Expand Down

0 comments on commit 0cffbc2

Please sign in to comment.