date2time
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 the following format:
|
MM[/-]DD[/-]YY(YY)?(:hh(mm)?)?
|
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.
To convert the seconds-since-epoch value back into a date,
see the "strftime" filter.
|
Warning
This filter can be considered to be broken,
given the above description.
If you omit the time,
then this filter will output a formatted date,
rather than the number of seconds since the UNIX Epoch.
|
|
Deprecation notice
Rather than fix this filter,
it has been deprecated in favour of the
"datetime2epoch" filter,
available in Interchange 5.0.0 and above.
This filter will remain, for a while, for backward compatibility,
as someone may be relying upon the current behaviour.
It will be deleted in a future Interchange release.
|
Example
- [filter date2time]01/01/2005[/filter]
- [filter date2time]01/01/05[/filter]
- [filter date2time]01-01-2005[/filter]
- [filter date2time]01-01-05[/filter]
- [filter date2time]01-01-2005:10[/filter]
- [filter date2time]01-01-05:1000[/filter]
- [filter date2time]01-01-05:1045[/filter]
|
Results in:
- 20050101
- 20050101
- 20050101
- 20050101
- 1104538200
- 1104573600
- 1104576300
|
Source code
sub {
my $val = shift;
use Time::Local;
$val =~ s/\0+//g;
if($val =~ m:(\d+)[-/]+(\d+)[-/]+(\d+):) {
my ($yr, $mon, $day) = ($3, $1, $2);
my $time;
$val =~ /:(\d+)$/
and $time = $1;
if(length($yr) < 4) {
$yr =~ s/^0//;
$yr = $yr < 50 ? $yr + 2000 : $yr + 1900;
}
$mon =~ s/^0//;
$day =~ s/^0//;
$val = sprintf("%d%02d%02d", $yr, $mon, $day);
return $val unless $time;
$val .= sprintf('%04d', $time);
}
my $time;
$val =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)?(\d\d)?/;
my ($yr, $mon, $day, $hr, $min) = ($1 || 0, $2 || 1, $3 || 1, $4 || 0, $5 || 0);
$mon--;
eval {
$time = timelocal(0, $min, $hr, $day, $mon, $yr);
};
if($@) {
logError("bad time value passed to date2time: %s", $@);
return 0;
}
return $time;
}
|