1#============================================================= -*-perl-*-
2#
3# t/date.t
4#
5# Tests the 'Date' plugin.
6#
7# Written by Andy Wardley <abw@kfs.org>
8#
9# Copyright (C) 2000 Andy Wardley. All Rights Reserved.
10#
11# This is free software; you can redistribute it and/or modify it
12# under the same terms as Perl itself.
13#
14# $Id$
15#
16#========================================================================
17
18use strict;
19use lib qw( ./lib ../lib );
20use Template;
21use Template::Test;
22use Template::Plugin::Date;
23use POSIX;
24$^W = 1;
25
26eval "use Date::Calc";
27
28my $got_date_calc = 0;
29$got_date_calc++ unless $@;
30
31
32$Template::Test::DEBUG = 0;
33
34my $format = {
35    'default' => $Template::Plugin::Date::FORMAT,
36    'time'    => '%H:%M:%S',
37    'date'    => '%d-%b-%Y',
38    'timeday' => 'the time is %H:%M:%S on %A',
39};
40
41my $time = time;
42my @ltime = localtime($time);
43
44my $params = { 
45    time    => $time,
46    format  => $format,
47    timestr => &POSIX::strftime($format->{ time }, @ltime),
48    datestr => &POSIX::strftime($format->{ date }, @ltime),
49    daystr  => &POSIX::strftime($format->{ timeday }, @ltime),
50    defstr  => &POSIX::strftime($format->{ default }, @ltime),
51    now     => sub { 
52        &POSIX::strftime(shift || $format->{ default }, localtime(time));
53    },
54    time_locale => \&time_locale,
55    date_locale => \&date_locale,    
56    date_calc   => $got_date_calc,
57};
58
59sub time_locale { 
60    my ($time, $format, $locale) = @_;
61    my $old_locale = &POSIX::setlocale(&POSIX::LC_ALL);
62    
63    # some systems expect locales to have a particular suffix
64    for my $suffix ('', @Template::Plugin::Date::LOCALE_SUFFIX) {
65        my $try_locale = $locale.$suffix;
66	    my $setlocale = &POSIX::setlocale(&POSIX::LC_ALL, $try_locale);
67        if (defined $setlocale && $try_locale eq $setlocale) {
68            $locale = $try_locale;
69            last;
70        }
71    }
72    my $datestr = &POSIX::strftime($format, localtime($time));
73    &POSIX::setlocale(&POSIX::LC_ALL, $old_locale);
74    return $datestr;
75}
76
77sub date_locale {
78    my ($time, $format, $locale) = @_;
79    my @date = (split(/(?:\/| |:|-)/, $time))[2,1,0,3..5];
80    return (undef, Template::Exception->new('date',
81                   "bad time/date string:  expects 'h:m:s d:m:y'  got: '$time'"))
82        unless @date >= 6 && defined $date[5];
83    $date[4] -= 1;     # correct month number 1-12 to range 0-11
84    $date[5] -= 1900;  # convert absolute year to years since 1900
85    $time = &POSIX::mktime(@date);
86    return time_locale($time, $format, $locale);
87}
88
89
90# force second to rollover so that we reliably see any tests failing.
91# lesson learnt from 2.07b where I broke the Date plugin's handling of a
92# 'time' parameter, but which didn't immediately come to light because the
93# script could run before the second rolled over and not expose the bug
94
95sleep 1;
96
97test_expect(\*DATA, { POST_CHOMP => 1 }, $params);
98 
99
100#------------------------------------------------------------------------
101# test input
102#
103# NOTE: these tests check that the Date plugin is behaving as expected
104# but don't attempt to validate that the output returned from strftime()
105# is semantically correct.  It's a closed loop (aka "vicious circle" :-)
106# in which we compare what date.format() returns to what we get by 
107# calling strftime() directly.  Despite this, we can rest assured that
108# the plugin is correctly parsing the various parameters and passing 
109# them to strftime() as expected.
110#------------------------------------------------------------------------
111
112__DATA__
113-- test --
114[% USE date %]
115Let's hope the year doesn't roll over in between calls to date.format()
116and now()...
117Year: [% date.format(format => '%Y') %]
118
119-- expect --
120-- process --
121Let's hope the year doesn't roll over in between calls to date.format()
122and now()...
123Year: [% now('%Y') %]
124
125-- test --
126[% USE date(time => time) %]
127default: [% date.format %]
128
129-- expect --
130-- process --
131default: [% defstr %]
132
133-- test --
134[% USE date(time => time) %]
135[% date.format(format => format.timeday) %]
136
137-- expect --
138-- process --
139[% daystr %]
140
141-- test --
142[% USE date(time => time, format = format.date) %]
143Date: [% date.format %]
144
145-- expect --
146-- process --
147Date: [% datestr %]
148
149-- test --
150[% USE date(format = format.date) %]
151Time: [% date.format(time, format.time) %]
152
153-- expect --
154-- process --
155Time: [% timestr %]
156
157-- test --
158[% USE date(format = format.date) %]
159Time: [% date.format(time, format = format.time) %]
160
161-- expect --
162-- process --
163Time: [% timestr %]
164
165
166-- test --
167[% USE date(format = format.date) %]
168Time: [% date.format(time = time, format = format.time) %]
169
170-- expect --
171-- process --
172Time: [% timestr %]
173
174-- test --
175[% USE english = date(format => '%A', locale => 'en_GB') %]
176[% USE french  = date(format => '%A', locale => 'fr_FR') %]
177In English, today's day is: [% english.format +%]
178In French, today's day is: [% french.format +%]
179
180-- expect --
181-- process --
182In English, today's day is: [% time_locale(time, '%A', 'en_GB') +%]
183In French, today's day is: [% time_locale(time, '%A', 'fr_FR') +%]
184
185-- test --
186[% USE english = date(format => '%A') %]
187[% USE french  = date() %]
188In English, today's day is: 
189[%- english.format(locale => 'en_GB') +%]
190In French, today's day is: 
191[%- french.format(format => '%A', locale => 'fr_FR') +%]
192
193-- expect --
194-- process --
195In English, today's day is: [% time_locale(time, '%A', 'en_GB') +%]
196In French, today's day is: [% time_locale(time, '%A', 'fr_FR') +%]
197
198-- test --
199[% USE date %]
200[% date.format('4:20:00 13-6-2000', '%H') %]
201-- expect --
20204
203
204-- test --
205[% USE date %]
206[% date.format('2000-6-13 4:20:00', '%H') %]
207-- expect --
20804
209
210-- test --
211-- name September 13th 2000 --
212[% USE day = date(format => '%A', locale => 'en_GB') %]
213[% day.format('4:20:00 13-9-2000') %]
214
215-- expect --
216-- process --
217[% date_locale('4:20:00 13-9-2000', '%A', 'en_GB') %]
218
219
220-- test --
221[% TRY %]
222[% USE date %]
223[% date.format('some stupid date') %]
224[% CATCH date %]
225Bad date: [% e.info %]
226[% END %]
227-- expect --
228Bad date: bad time/date string:  expects 'h:m:s d:m:y'  got: 'some stupid date'
229
230-- test --
231[% USE date %]
232[% template.name %] [% date.format(template.modtime, format='%Y') %]
233-- expect --
234-- process --
235input text [% now('%Y') %]
236
237-- test --
238[% IF date_calc -%]
239[% USE date; calc = date.calc; calc.Monday_of_Week(22, 2001).join('/') %]
240[% ELSE -%]
241not testing
242[% END -%]
243-- expect --
244-- process --
245[% IF date_calc -%]
2462001/5/28
247[% ELSE -%]
248not testing
249[% END -%]
250
251-- test --
252[% USE date;
253   date.format('12:59:00 30/09/2001', '%H:%M')
254-%]
255-- expect --
25612:59
257
258-- test --
259[% USE date;
260   date.format('2001/09/30 12:59:00', '%H:%M')
261-%]
262-- expect --
26312:59
264