* Add enclair_db option to UserDB.pm. Allows logging of enclair password
[interchange.git] / code / Filter / duration.filter
1 # Copyright 2002-2007 Interchange Development Group and others
2 # Copyright 1996-2002 Red Hat, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.  See the LICENSE file for details.
8
9 # $Id: duration.filter,v 1.7 2009-05-01 13:50:00 pajamian Exp $
10
11 CodeDef duration Filter
12 CodeDef duration Description Duration
13 CodeDef duration Routine <<EOR
14 sub {
15         my ($val, undef, $startvar, $durvar, @extra) = @_;
16
17         ## Accepts two parameters, the name of the CGI variables which
18         ## hold the start date/time and the duration value. With this:
19         #
20         #  [cgi name=start_date set=200502120800]
21         #  [cgi name=length set="12 hours"]
22         #  [filter op=duration.start_date.length /]
23         #
24         # The filter call will return 20050212200000
25         #
26         ## Can also be used like this, with the same output:
27         #
28         #  [filter duration.-dummy.12.hours]200502120800[/filter]
29         #
30         use vars qw/$CGI/;
31         my $start = $CGI->{$startvar} || $val;
32         my $durstring = $CGI->{$durvar};
33         use Time::Local;
34
35         if (!length($durstring) && $durvar =~ /^\d+$/) {
36                 $durstring = join(' ', $durvar, @extra);
37         }
38
39         ## Want to allow setting the value directly
40         return $val unless $durstring;
41
42         $start =~ s/\0+//g;
43         if($start =~ m:(\d+)[-/]+(\d+)[-/]+(\d+):) {
44                 my ($yr, $mon, $day) = ($3, $1, $2);
45
46                 my $time;
47                 $start =~ /:(\d+)$/
48                         and $time = $1;
49                 if(length($yr) < 4) {
50                         $yr =~ s/^0//;
51                         $yr = $yr < 50 ? $yr + 2000 : $yr + 1900;
52                 }
53                 $mon =~ s/^0//;
54                 $day =~ s/^0//;
55                 $start = sprintf("%d%02d%02d", $yr, $mon, $day);
56                 return $val unless $time;
57                 $start .= sprintf('%04d', $time);
58         }
59
60         my $time;
61         $start =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)?(\d\d)?/;
62         my ($yr, $mon, $day, $hr, $min) = ($1 || 0, $2 || 1, $3 || 1, $4 || 0, $5 || 0);
63         $mon--;
64         eval {
65                 $time = timelocal(0, $min, $hr, $day, $mon, $yr);
66         };
67         if($@) {
68                 logError("bad time value passed to duration filter: %s", $@);
69                 return 0;
70         }
71
72         $time = adjust_time($durstring, $time);
73
74         return POSIX::strftime("%Y%m%d%H%M%S", localtime($time));
75 }
76 EOR