* Add enclair_db option to UserDB.pm. Allows logging of enclair password
[interchange.git] / code / Filter / datetime2epoch.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: datetime2epoch.filter,v 1.2 2007-03-30 23:40:44 pajamian Exp $
10
11 CodeDef datetime2epoch Filter
12 CodeDef datetime2epoch Description Date and optional time to seconds since the UNIX Epoch
13 CodeDef datetime2epoch Routine <<EOR
14 sub {
15     use Time::Local;
16
17     my ($year, $mon, $day, $hr, $min, $sec, $time);
18
19     my $val = shift;
20     $val =~ s/\0+//g;
21
22     $val =~ m%^\s*(\d\d)[-/]+(\d+)[-/]+(\d+)% and do {
23         ($year, $mon, $day) = ($3, $1, $2);
24
25         $val =~ /:(\d\d):?(\d\d)?:?(\d\d)?\s*$/
26             and $time = sprintf('T%02d:%02d:%02d', $1, $2 || 0, $3 || 0);
27
28         if (length($year) < 4) {
29             $year =~ s/^0//;
30             $year = $year < 50 ? $year + 2000 : $year + 1900;
31         }
32         $val = sprintf('%d-%02d-%02d%s', $year, $mon || 1, $day || 1, $time);
33     };
34
35     $val =~ /^\s*(\d\d\d\d)-(\d\d)-(\d\d)(?:[T\s](\d\d)?(?::(\d\d)?(?::(\d\d)?)?)?)?/;
36     ($year, $mon, $day, $hr, $min, $sec) = ($1, $2, $3, $4 || 0, $5 || 0,$6 || 0);
37     eval {
38         $time = timelocal($sec, $min, $hr, $day, --$mon, $year);
39     };
40     if ($@) {
41         logError("bad time value passed to datetime2epoch: %s", $@);
42         return 0;
43     }
44     return $time;
45 }
46 EOR