Name

duration — calculate end date from given start date and duration

DESCRIPTION

The filter takes a start date and a duration as inputs, and outputs the end date.

EXAMPLES

Example: Obtaining an end date string

The following example sets start date to Feb 12, 2005 (at 8:00 am), and the duration to 12 hours.
[cgi name=start_date set=200502120800 hide=1]
[cgi name=length     set="12 hours"   hide=1]
[filter op=duration.start_date.length /]

NOTES

For more information on Perl Regular Expressions, pattern matching and character classes, see perlre(1).

The timelocal() function used in the filter comes from the Time::Local Perl module.

AVAILABILITY

duration is available in Interchange versions:

4.6.0-5.7.0 (git-head)

SOURCE

Interchange 5.7.0:

Source: code/Filter/duration.filter
Lines: 76


# Copyright 2002-2007 Interchange Development Group and others
# Copyright 1996-2002 Red Hat, Inc.
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.  See the LICENSE file for details.
# 
# $Id: duration.filter,v 1.7 2009-05-01 13:50:00 pajamian Exp $

CodeDef duration Filter
CodeDef duration Description Duration
CodeDef duration Routine <<EOR
sub {
my ($val, undef, $startvar, $durvar, @extra) = @_;

## 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 20050212200000
#
## Can also be used like this, with the same output:
#
#  [filter duration.-dummy.12.hours]200502120800[/filter]
#
use vars qw/$CGI/;
my $start = $CGI->{$startvar} || $val;
my $durstring = $CGI->{$durvar};
use Time::Local;

if (!length($durstring) && $durvar =~ /^\d+$/) {
  $durstring = join(' ', $durvar, @extra);
}

## Want to allow setting the value directly
return $val unless $durstring;

$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 = adjust_time($durstring, $time);

return POSIX::strftime("%Y%m%d%H%M%S", localtime($time));
}
EOR

AUTHORS

Interchange Development Group

SEE ALSO

date2time(7ic), date_change(7ic)

DocBook! Interchange!