The problem is not so much with Template, but that the t/date.t test
case #25 is not quite valid.  See the example below.

#!/usr/bin/perl -w
                                                                                
use strict;
use Template::Plugin::Date;
use POSIX;
                                                                                
my $d = new Template::Plugin::Date(0, {format => '%A %s %Z', locale =>
'en_GB'});
my $string1 = $d->format('4:20:00 13-9-2000');
print "$string1\n";
my $string2 = $d->format(968818800);
print "$string2\n";

On my Fedora Linux box and Mac OS X box, I get the following results.

[EMAIL PROTECTED] perl]$ ./timetest.pl
Wednesday 968836800 CDT
Tuesday 968818800 CDT

In Template::Plugin::Date, if an epoch time is entered, that time is
used directly as a parameter to the Perl functions gmtime or localtime,
depending on whether the GMT flag is set or not.  If a date, such as
'4:20:00 13-9-2000', the format function picks apart the the date and
plugs it into mktime.  mktime returns a epoch time relative to the time
zone settings.  So, this test will work correctly for anyone in the with
a time zone offset between -400 and +1900.  This explains the test
failures within the United States, although it works fine in Europe,
Asia, and the Eastern US.

To make test 25 work correctly, there are a few possible solutions.
First, test with the epoch time in both cases.

[% USE day = date(format => '%A', locale => 'en_GB') %]
[% day.format(968818800) %]
                                                                                
                                                                                
-- expect --
-- process --
[% # 4:20:00 9-13-2000 equals 968818800 seconds since the epoch
   nowloc(968818800, '%A', 'en_GB')
%]

Second, you could create a new filter within the test script to test
this.  For example,

newnowloc => sub { 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);
        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);
            if (defined $setlocale && $try_locale eq $setlocale) {
                $locale = $try_locale;
                last;
            }
        }
        my $datestr = &POSIX::strftime($format, localtime($time));
        &POSIX::setlocale(&POSIX::LC_ALL, $old_locale);
        return $datestr;
    },

then the test case itself becomes

[% USE day = date(format => '%A', locale => 'en_GB') %]
[% day.format('4:20:00 13-9-2000') %]
                                                                                
                                                                                
-- expect --
-- process --
[% # 4:20:00 9-13-2000 equals 968818800 seconds since the epoch
   newnowloc('4:20:00 13-9-2000', '%A', 'en_GB')
%]

Now just reading the question regarding changing line 66 to gmtime from
localtime, it does work, although I'm not sure what it's testing then.

All three of the above cause the test to pass.  Another possible change
would be to mess with the TZ enivornment variables, although this might
not work on Win32.  Hacking other environment variables are also
possible, but they are also very platform specific.  

Anyway, I'm satisfied with the results I see above and have installed
it.

Thanks,

Steve Peters
[EMAIL PROTECTED]


_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to