MooseX::Types::DateTime::W3C - W3C DateTime format type constraint
version 1.103360
package My::Class; use Moose; use MooseX::Types::DateTime::W3C qw( DateTimeW3C ); has 'datetime' => ( is => 'rw', isa => DateTimeW3C, coerce => 1, ); package main; my $obj = My::Class->new(); # YYYY $obj->datetime('1997'); # YYYY-MM $obj->datetime('1997-07'); # YYYY-MM-DD $obj->datetime('1997-07-16'); # YYYY-MM-DDThh:mmTZD $obj->datetime('1997-07-16T19:20'); $obj->datetime('1997-07-16T19:20Z'); $obj->datetime('1997-07-16T19:20+01:00'); # YYYY-MM-DDThh:mm:ssTZD $obj->datetime('1997-07-16T19:20:30'); $obj->datetime('1997-07-16T19:20:30Z'); $obj->datetime('1997-07-16T19:20:30+01:00'); # YYYY-MM-DDThh:mm:ss.sTZD $obj->datetime('1997-07-16T19:20:30.45'); $obj->datetime('1997-07-16T19:20:30.45Z'); $obj->datetime('1997-07-16T19:20:30.45+01:00'); # coercion from DateTime objects use DateTime; $obj->datetime( DateTime->new( year => 1997, month => 7, day => 16, hour => 19, minute => 20, second => 30, time_zone => 'UTC' ) ); # same as 1997-07-16T19:20:30Z # coercion from Num $obj->datetime( time() ); # exported functions # is_DateTimeW3C - validate my $w3cdtf_string = '1997-07-16T19:20:30.45Z'; if ( is_DateTimeW3C($w3cdtf_string) ) { # yes, it is ... } # to_DateTimeW3C - coerce $w3cdtf_string = to_DateTimeW3C( DateTime->now ); $w3cdtf_string = to_DateTimeW3C( time() );
This class provides W3C date/time format type constraint.
has 'datetime' => ( is => 'rw', isa => DateTimeW3C, coerce => 1, );
DateTimeW3C is a subtype of Str validated against format described at http://www.w3.org/TR/NOTE-datetime.
DateTimeW3C
Str
Coercion is supported from DateTime objects and numbers (treated as time in seconds since unix epoch).
$obj->datetime( DateTime->now ); $obj->datetime( time() );
Please note that time coerced from Num will be in UTC time zone.
Num
UTC
In addition to type constraint following functions are exported:
if ( is_DateTimeW3C($w3cdtf_string) ) { ... }
Tests if given value is a valid W3C DateTime string.
# from DateTime object $w3cdtf_string = to_DateTimeW3C( DateTime->now ); # from number $w3cdtf_string = to_DateTimeW3C( time() );
Coerce given value into valid W3C DateTime string.
Note: nanoseconds are added only if != 0.
Note: numbers are converted into DateTime objects in UTC time zone.
http://www.w3.org/TR/NOTE-datetime
MooseX::Types
Alex J. G. Burzyński <ajgb@cpan.org>
This software is copyright (c) 2010 by Alex J. G. Burzyński <ajgb@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install MooseX::Types::DateTime::W3C, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::Types::DateTime::W3C
CPAN shell
perl -MCPAN -e shell install MooseX::Types::DateTime::W3C
For more information on module installation, please visit the detailed CPAN module installation guide.