[interchange-cvs] interchange - kwalsh modified code/Filter/datetime2epoch.filter

interchange-cvs at icdevgroup.org interchange-cvs at icdevgroup.org
Fri Feb 23 10:59:19 EST 2007


User:      kwalsh
Date:      2007-02-23 15:59:18 GMT
Added:     code/Filter datetime2epoch.filter
Log:
    * New filter, designed to replace the date2time filter, which I suggest
      should now be deprecated.

      The date/time is expected to have one of the following formats:

	MM[/-]DD[/-]YY(YY)?(:hh(:?mm(:ss)?)?)?

      or (ISO/MySQL date/time):

	YYYY-MM-DD([T ]hh(:mm(:ss)?)?)?

      First format:  if the year specification contains only two digits,
      and is less than 50, then it is treated as an offset from the year
      2000, rather than from 1900.  In other words, 07 is understood as
      year 2007, and 80 is understood as year 1980.  An unspecified day
      or month will default to 01.  Unspecified hours or minutes default
      to 00.

      Second (ISO/MySQL date/time) format:  The year must be specified
      using four digits, and the day and month are not optional.

      In both cases, the hours, minutes and seconds will default to 00,
      unless specified otherwise.

Revision  Changes    Path
1.1                  interchange/code/Filter/datetime2epoch.filter


rev 1.1, prev_rev 1.0
Index: datetime2epoch.filter
===================================================================
# Copyright 2002-2005 Interchange Development Group (http://www.icdevgroup.org/)
# Copyright 1996-2002 Red Hat, Inc.
# Licensed under the GNU GPL v2 or later. See file LICENSE for details.
# $Id: datetime2epoch.filter,v 1.1 2007/02/23 15:59:18 kwalsh Exp $

CodeDef datetime2epoch Filter
CodeDef datetime2epoch Description Date and optional time to seconds since the UNIX Epoch
CodeDef datetime2epoch Routine <<EOR
sub {
    use Time::Local;

    my ($year, $mon, $day, $hr, $min, $sec, $time);

    my $val = shift;
    $val =~ s/\0+//g;

    $val =~ m%^\s*(\d\d)[-/]+(\d+)[-/]+(\d+)% and do {
	($year, $mon, $day) = ($3, $1, $2);

	$val =~ /:(\d\d):?(\d\d)?:?(\d\d)?\s*$/
	    and $time = sprintf('T%02d:%02d:%02d', $1, $2 || 0, $3 || 0);

	if (length($year) < 4) {
	    $year =~ s/^0//;
	    $year = $year < 50 ? $year + 2000 : $year + 1900;
	}
	$val = sprintf('%d-%02d-%02d%s', $year, $mon || 1, $day || 1, $time);
    };

    $val =~ /^\s*(\d\d\d\d)-(\d\d)-(\d\d)(?:[T\s](\d\d)?(?::(\d\d)?(?::(\d\d)?)?)?)?/;
    ($year, $mon, $day, $hr, $min, $sec) = ($1, $2, $3, $4 || 0, $5 || 0,$6 || 0);
    eval {
	$time = timelocal($sec, $min, $hr, $day, --$mon, $year);
    };
    if ($@) {
	logError("bad time value passed to datetime2epoch: %s", $@);
	return 0;
    }
    return $time;
}
EOR








More information about the interchange-cvs mailing list