[interchange-cvs] interchange - heins modified
code/Filter/duration.filter
interchange-cvs at icdevgroup.org
interchange-cvs at icdevgroup.org
Mon Feb 14 01:59:52 EST 2005
User: heins
Date: 2005-02-14 06:59:52 GMT
Added: code/Filter duration.filter
Log:
* Add filter that allows setting of an end date based on a start
date and a duration.
Accepts two parameters, the name of the CGI variables which
hold the start date/time and the duration value. With this:
[cgi name=start_date set=200502120800]
[cgi name=length set="12 hours"]
[filter op="duration.start_date.length"/]
The filter call will return 200502122000
Revision Changes Path
1.1 interchange/code/Filter/duration.filter
rev 1.1, prev_rev 1.0
Index: duration.filter
===================================================================
# Copyright 2002-2004 Interchange Development Group (http://www.icdevgroup.org/)
# Copyright 1996-2002 Red Hat, Inc.
# Licensed under the GNU GPL v2. See file LICENSE for details.
# $Id: duration.filter,v 1.1 2005/02/14 06:59:52 mheins Exp $
CodeDef duration Filter
CodeDef duration Routine <<EOR
sub {
my $val = shift;
shift;
## Accepts two parameters, the name of the CGI variables which
## hold the start date/time and the duration value. With this:
#
# [cgi name=start_date set=200502120800]
# [cgi name=length set="12 hours"]
# [filter op=duration.start_date.length /]
#
# The filter call will return 200502122000
#
my ($startvar, $durvar) = @_;
use vars qw/$CGI/;
my $start = $CGI->{$startvar};
my $durstring = $CGI->{$durvar};
my $duration = 0;
use Time::Local;
while($durstring =~ s/(\d+\s*[hmwd]\w*)\s*//) {
$duration += Vend::Config::time_to_seconds($1);
}
$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));
}
EOR
More information about the interchange-cvs
mailing list