date_change — convert MMDDYYYY date (possibly with specified time) to YYYYMMDDhhmm
The filter replaces date specification in form of
MM
[/-]DD
[/-]YY(YY)?
(:hh
(mm
)?)?
or
YYYY
[/-]MM
[/-]DD
(:hh
(mm
)?)?
to
.
YYYYMMDD
((hh
)?(mm
)?)?
If the year specification contains 2 digits only and is less than
50
, as is say, 02
,
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 year
1980
.
Time specification unspecified in input get omitted from output as well.
The filter optionally accepts three arguments:
iso
- output date in ISO format
undef
- don't default to current date if no date is specified
no_time
- output only date, without time portion
Example: Using date_change
[filter date_change]2005-01-01[/filter] [filter date_change]2005/01/01[/filter] [filter date_change]2005-01-01:10[/filter] [filter date_change]2005/05/29:1536[/filter] [filter date_change]05-29-2005:1536[/filter] [filter date_change]05-29-05:1536[/filter] [filter date_change]0000-00-00[/filter] [filter date_change][/filter]
For more information on Perl Regular Expressions, pattern matching and character classes, see perlre(1).
The filter is most commonly used with date selection fields.
If only two out of four time digits are specified, then due to the operation
of the sprintf()
function, they tend to indicate minutes
and not hours!
When the input data starts with year specification, the year must have
4-digit format (i.e. 2005
and
not just 05
)!
If the date is empty, the filter defaults to current date. To
force an absence of a date (as required by some SQL databases),
use the undef
filter option.
Interchange 5.9.0:
Source: code/Filter/date_change.filter
Lines: 84
# Copyright 2002-2016 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. CodeDef date_change Filter CodeDef date_change Description Date widget CodeDef date_change Routine <<EOR sub { my $val = shift; shift; # discard tag my $opt = { map { $_ => 1 } @_ }; HTML::Entities::decode_entities($val) if $val =~ /&/; $val =~ s/\0+//g; my $re = $opt->{undef} ? qr:^(\d*)[-/]+(\d*)[-/]+(\d*)(.*)$: : qr:^(\d+)[-/]+(\d+)[-/]+(\d+)(.*)$: ; return $val unless $val =~ /$re/; my ($year, $month, $day, $timeval); if (length($1) == 4) { # ISO date style 2003-03-20 ($year, $month, $day) = ($1, $2, $3); } else { # U.S. date style 3/20/2003 or 3/20/03 ($year, $month, $day) = ($3, $1, $2); } $timeval = $4; if ($opt->{undef}) { # return nothing (undef, which DBI treats as SQL NULL) for an # empty date (all zeroes or nothing at all) return unless grep /[1-9]/, ($year, $month, $day); } # Y2K fun: Try to guess intent of year "03" as "2003" if (length($year) < 4) { $year = $year < 50 ? $year + 2000 : $year + 1900; } my ($date_format, $time_format); if ($opt->{iso}) { $date_format = '%04d-%02d-%02d'; $time_format = 'T%02d:%02d:%02d'; } elsif ($opt->{common}) { $date_format = '%04d-%02d-%02d'; $time_format = ' %02d:%02d:%02d'; } else { $date_format = '%04d%02d%02d'; $time_format = '%02d%02d'; } my $time; if ($timeval =~ /^:(\d{1,4})\s*$/) { # accept traditional Interchange date_time widget times # of format '0130', e.g. '20080201:0130' $time = sprintf('%04d', $1); $time = sprintf($time_format, substr($time, 0, 2), substr($time, 2, 2)); } elsif ( # accept times of format '1:30', '1:30:05', # to support PostgreSQL's timestamp with time zone format # e.g. '2008-02-01 01:30:05-07' my ($hours, $minutes, $seconds) = ($timeval =~ /\s(\d\d?):(\d\d?)(?::(\d\d+))/) ) { $time = sprintf($time_format, $hours, $minutes, $seconds); } my $out = sprintf($date_format, $year, $month, $day); $out .= $time if $time and not $opt->{no_time}; return $out; } EOR