datetime2epoch
Convert a date (possibly with a time) to the number of seconds
since the Epoch (1970-01-01 00:00:00 UTC).
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 of the above cases,
and unless specified otherwise,
the hours, minutes and seconds will default to 00.
To convert the seconds-since-epoch value back into a date,
see the "strftime" filter.
|
Availability
This filter was introduced in version 5.5.0,
and is therefore not available for use with any earlier Interchange version.
This filter was designed to replace the "date2time" filter.
|
Example
- [filter datetime2epoch]01/01/2005[/filter]
- [filter datetime2epoch]01/01/05[/filter]
- [filter datetime2epoch]01-01-2005[/filter]
- [filter datetime2epoch]01-01-05[/filter]
- [filter datetime2epoch]01-01-2005:10[/filter]
- [filter datetime2epoch]01-01-05:1000[/filter]
- [filter datetime2epoch]01-01-05:1045[/filter]
- [filter datetime2epoch]01-01-05:10:45[/filter]
- [filter datetime2epoch]01-01-05:10:45:15[/filter]
- [filter datetime2epoch]2005-01-01[/filter]
- [filter datetime2epoch]2005-01-01 10:45[/filter]
- [filter datetime2epoch]2005-01-01 10:45:15[/filter]
- [filter datetime2epoch]2005-01-01T10:45:15[/filter]
|
Results in:
- 1104537600
- 1104537600
- 1104537600
- 1104537600
- 1104573600
- 1104573600
- 1104576300
- 1104576300
- 1104576315
- 1104537600
- 1104576300
- 1104576315
- 1104576315
|
Source code
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;
}
|