The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

MooseX::Types::DateTime::W3C - W3C DateTime format type constraint

VERSION

version 1.103360

SYNOPSIS

    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() );

DESCRIPTION

This class provides W3C date/time format type constraint.

TYPES

DateTimeW3C

    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.

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.

EXPORTS

In addition to type constraint following functions are exported:

is_DateTimeW3C

    if ( is_DateTimeW3C($w3cdtf_string) ) {
        ...
    }

Tests if given value is a valid W3C DateTime string.

to_DateTimeW3C

    # 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.

SEE ALSO

AUTHOR

Alex J. G. Burzyński <ajgb@cpan.org>

COPYRIGHT AND LICENSE

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.