duration
This filter has two modes of operation, as follows:
First mode:
The filter takes the name of two CGI variables,
a date/time and an offset,
and uses them to calculate a new date and time.
Second mode:
The filter expects a date/time in its body text and uses an offset,
specified in the filter parameters,
to calculate a new date and time.
|
Availability
This filter was introduced in version 5.4.0,
and is therefore not available for use with any earlier Interchange version.
The second mode of operation was introduced in Interchange 5.5.0.
Prior to that,
only the first mode was available.
|
Example
[cgi name=start_date set=200502120800 hide=1]
[cgi name=offset set="12 hours 10 mins" hide=1]
1. [filter duration.start_date.offset][/filter]
2. [filter duration.-dummy.12.hours.5.mins]200502120800[/filter]
|
Results in:
1. 20050212201000
2. 20050212203000
|
Source code
sub {
my ($val, undef, $startvar, $durvar, @extra) = @_;
use vars qw/$CGI/;
my $start = $CGI->{$startvar} || $val;
my $durstring = $CGI->{$durvar};
my $duration = 0;
use Time::Local;
if (!length($durstring) && $durvar =~ /^\d+$/) {
$durstring = join(' ', $durvar, @extra);
}
while($durstring =~ s/(\d+\s*[hmwd]\w*)\s*//) {
$duration += Vend::Config::time_to_seconds($1);
}
return $val unless $duration;
$start =~ s/\0+//g;
if($start =~ m:(\d+)[-/]+(\d+)[-/]+(\d+):) {
my ($yr, $mon, $day) = ($3, $1, $2);
my $time;
$start =~ /:(\d+)$/
and $time = $1;
if(length($yr) < 4) {
$yr =~ s/^0//;
$yr = $yr < 50 ? $yr + 2000 : $yr + 1900;
}
$mon =~ s/^0//;
$day =~ s/^0//;
$start = sprintf("%d%02d%02d", $yr, $mon, $day);
return $val unless $time;
$start .= sprintf('%04d', $time);
}
my $time;
$start =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)?(\d\d)?/;
my ($yr, $mon, $day, $hr, $min) = ($1 || 0, $2 || 1, $3 || 1, $4 || 0, $5 || 0);
$mon--;
eval {
$time = timelocal(0, $min, $hr, $day, $mon, $yr);
};
if($@) {
logError("bad time value passed to duration filter: %s", $@);
return 0;
}
$time += $duration;
return POSIX::strftime("%Y%m%d%H%M%S", localtime($time));
}
|