FormValidator::Simple::Plugin::DateTime::Format
MTのエントリーフォームのようにinput="text"に日付・時間を入力する場合のチェックに。
DateTime::Format::*とDateTime::Format::Strptimeを利用するメソッドの2つ
use FormValidator::Simple qw/DateTime::Format/; use CGI; my $q = CGI->new; $q->param( datetime1 => '2006-04-26T19:09:21+0900' ); $q->param( datetime2 => '2006-04-26 19:09:21' ); my $result = FormValidator::Simple->check( $q => [ datetime1 => [ [ 'DATETIME_STRPTIME', '%Y-%m-%dT%T%z' ] ], datetime2 => [ [ qw/DATETIME_FORMAT MySQL/ ] ], ] );
DateTime::Format::Strptimeはver. 1.0700にしないとtime_zoneがUTCになるバグがあります。
ソースは追記に
package FormValidator::Simple::Plugin::DateTime::Format; use strict; use warnings; use FormValidator::Simple::Exception; use FormValidator::Simple::Constants; use DateTime::Format::Strptime; use UNIVERSAL::require; sub DATETIME_FORMAT { my ( $self, $params, $args ) = @_; my $date = $params->[0]; my $format = $args->[0]; FormValidator::Simple::Exception->throw( qq/Validation DATETIME_FORMAT needs a format argument./) unless $format; my $module; if ( ref $format ) { $module = $format; } else { $module = "DateTime::Format::$format"; $module->require or FormValidator::Simple::Exception->throw( qq/Validation DATETIME_FORMAT: failed to require $module. "$@"/ ); } my $dt; eval { $dt = $module->parse_datetime($date); }; my $result = $dt ? TRUE : FALSE; if ( $dt && $self->options->{time_zone} ) { $dt->set_time_zone( $self->options->{time_zone} ); } return ($result, $dt); } sub DATETIME_STRPTIME { my ( $self, $params, $args ) = @_; my $date = $params->[0]; my $format = $args->[0]; FormValidator::Simple::Exception->throw( qq/Validation DATETIME_STRPTIME needs a format argument./) unless $format; my $dt; eval{ my $strp = DateTime::Format::Strptime->new( pattern => $format, on_error => 'croak' ); $dt = $strp->parse_datetime($date); }; my $result = $dt ? TRUE : FALSE; if ( $dt && $self->options->{time_zone} ) { $dt->set_time_zone( $self->options->{time_zone} ); } return ($result, $dt); } 1;
コメント
こっちも本体に登録しちゃっていいでしょうか
投稿者: kato | 2006年05月16日 17:24