Name

datetime2epoch — convert date (possibly with specified time) to number of seconds since Epoch

DESCRIPTION

The filter replaces date specification with optional time, in format of

MM[/-]DD[/-]YY(YY)?(:hh(:?mm(:ss)?)?)?, or
YYYY-MM-DD([T ]hh(:mm(:ss)?)?)?

to a number of seconds since Unix epoch.

If the year specification contains 2 digits only and is less than 50, then it is treated as an offset from year 2000, and not 1900. In other words, 05 is understood as year 2005, 80 is understood as 1980.

Unspecified month or day default to 01, unspecified hours or minutes default to 00.

EXAMPLES

Example: Converting dates and times to seconds since Epoch

[filter datetime2epoch]01/01/2008[/filter]
[filter datetime2epoch]01/01/08[/filter]

[filter datetime2epoch]01-01-2008[/filter]
[filter datetime2epoch]01-01-08[/filter]

[filter datetime2epoch]01-01-2008:10[/filter]
[filter datetime2epoch]01-01-08:10:45[/filter]
[filter datetime2epoch]01-01-08:10:45:00[/filter]
Example in action:
              
1199145600
1199145600

1199145600
1199145600

1199181600
1199184300
1199184300

            

Example: Converting ISO/MySQL dates and times to seconds since Epoch

[filter datetime2epoch]2008-01-01 10[/filter]
[filter datetime2epoch]2008-01-01 10:45[/filter]
[filter datetime2epoch]2008-01-01 10:45:00[/filter]
Example in action:
              
1199181600
1199184300
1199184300

            

NOTES

This filter is designed to replace date2time, which can be considered deprecated.

For more information on Perl Regular Expressions, pattern matching and character classes, see perlre(1).

The timelocal() function used in the filter comes from the Time::Local Perl module.

AVAILABILITY

datetime2epoch is available in Interchange versions:

4.6.0-5.9.0 (git-head)

SOURCE

Interchange 5.9.0:

Source: code/Filter/datetime2epoch.filter
Lines: 46


# Copyright 2002-2007 Interchange Development Group and others
# Copyright 1996-2002 Red Hat, Inc.
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.  See the LICENSE file for details.
# 
# $Id: datetime2epoch.filter,v 1.2 2007-03-30 23:40:44 pajamian 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

AUTHORS

Kevin Walsh

SEE ALSO

date2time(7ic)

DocBook! Interchange!