1 # Copyright 2002-2007 Interchange Development Group and others
2 # Copyright 1996-2002 Red Hat, Inc.
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.
9 # $Id: date_change.filter,v 1.8 2007-03-30 23:40:44 pajamian Exp $
11 CodeDef date_change Filter
12 CodeDef date_change Description Date widget
13 CodeDef date_change Routine <<EOR
17 my $opt = { map { $_ => 1 } @_ };
19 HTML::Entities::decode_entities($val) if $val =~ /&/;
21 my $re = $opt->{undef}
22 ? qr:^(\d*)[-/]+(\d*)[-/]+(\d*)(.*)$:
23 : qr:^(\d+)[-/]+(\d+)[-/]+(\d+)(.*)$:
25 return $val unless $val =~ /$re/;
27 my ($year, $month, $day, $timeval);
29 if (length($1) == 4) {
30 # ISO date style 2003-03-20
31 ($year, $month, $day) = ($1, $2, $3);
34 # U.S. date style 3/20/2003 or 3/20/03
35 ($year, $month, $day) = ($3, $1, $2);
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);
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;
51 my ($date_format, $time_format);
53 $date_format = '%04d-%02d-%02d';
54 $time_format = 'T%02d:%02d:%02d';
57 $date_format = '%04d%02d%02d';
58 $time_format = '%02d%02d';
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));
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+))/)
75 $time = sprintf($time_format, $hours, $minutes, $seconds);
78 my $out = sprintf($date_format, $year, $month, $day);
79 $out .= $time if $time and not $opt->{no_time};