* Add enclair_db option to UserDB.pm. Allows logging of enclair password
[interchange.git] / code / Filter / date_change.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: date_change.filter,v 1.8 2007-03-30 23:40:44 pajamian Exp $
10
11 CodeDef date_change Filter
12 CodeDef date_change Description Date widget
13 CodeDef date_change Routine <<EOR
14 sub {
15         my $val = shift; 
16         shift;  # discard tag
17         my $opt = { map { $_ => 1 } @_ };
18
19         HTML::Entities::decode_entities($val) if $val =~ /&/;
20         $val =~ s/\0+//g;
21         my $re = $opt->{undef}
22                 ? qr:^(\d*)[-/]+(\d*)[-/]+(\d*)(.*)$:
23                         : qr:^(\d+)[-/]+(\d+)[-/]+(\d+)(.*)$:
24                                 ;
25         return $val unless $val =~ /$re/;
26
27         my ($year, $month, $day, $timeval);
28
29         if (length($1) == 4) {
30                 # ISO date style 2003-03-20
31                 ($year, $month, $day) = ($1, $2, $3);
32         }
33         else {
34                 # U.S. date style 3/20/2003 or 3/20/03
35                 ($year, $month, $day) = ($3, $1, $2);
36         }
37
38         $timeval = $4;
39
40         if ($opt->{undef}) {
41                 # return nothing (undef, which DBI treats as SQL NULL) for an
42                 # empty date (all zeroes or nothing at all)
43                 return unless grep /[1-9]/, ($year, $month, $day);
44         }
45
46         # Y2K fun: Try to guess intent of year "03" as "2003"
47         if (length($year) < 4) {
48                 $year = $year < 50 ? $year + 2000 : $year + 1900;
49         }
50
51         my ($date_format, $time_format);
52         if ($opt->{iso}) {
53                 $date_format = '%04d-%02d-%02d';
54                 $time_format = 'T%02d:%02d:%02d';
55         }
56         else {
57                 $date_format = '%04d%02d%02d';
58                 $time_format = '%02d%02d';
59         }
60
61         my $time;
62         if ($timeval =~ /^:(\d{1,4})\s*$/) {
63                 # accept traditional Interchange date_time widget times
64                 # of format '0130', e.g. '20080201:0130'
65                 $time = sprintf('%04d', $1);
66                 $time = sprintf($time_format, substr($time, 0, 2), substr($time, 2, 2));
67         }
68         elsif (
69                 # accept times of format '1:30', '1:30:05',
70                 # to support PostgreSQL's timestamp with time zone format
71                 # e.g. '2008-02-01 01:30:05-07'
72                 my ($hours, $minutes, $seconds)
73                    = ($timeval =~ /\s(\d\d?):(\d\d?)(?::(\d\d+))/)
74                   ) {
75                 $time = sprintf($time_format, $hours, $minutes, $seconds);
76         }
77
78         my $out = sprintf($date_format, $year, $month, $day);
79         $out .= $time if $time and not $opt->{no_time};
80         return $out;
81 }
82 EOR