File: 13_datetime_tz.t

package info (click to toggle)
libmoosex-types-iso8601-perl 0.20-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 352 kB
  • sloc: perl: 500; makefile: 2
file content (73 lines) | stat: -rw-r--r-- 2,282 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use strict;
use warnings;

use MooseX::Types::DateTime;
use MooseX::Types::ISO8601 qw/
    ISO8601DateTimeTZStr
    ISO8601StrictDateTimeTZStr
/;

use Test::More 0.88;
use Test::Deep;
use if $ENV{AUTHOR_TESTING}, 'Test::Warnings';

{
    note "String with offset into datetime";
    my $datetime = MooseX::Types::DateTime::to_DateTime('2011-02-01T00:05:06+01:30');
    cmp_deeply(
        $datetime,
        all(
            isa('DateTime'),
            methods(
                offset => 3600+1800,
                datetime => "2011-02-01T00:05:06",
                nanosecond => 0,
            ),
        ),
    );

    note "DateTime into string";
    is(to_ISO8601DateTimeTZStr($datetime), "2011-02-01T00:05:06+01:30");
}

{
    note "String with offset into datetime, with precision";
    my $datetime = MooseX::Types::DateTime::to_DateTime('2011-02-03T01:05:06.000000001+01:30');
    cmp_deeply(
        $datetime,
        all(
            isa('DateTime'),
            methods(
                offset => 3600+1800,
                datetime => "2011-02-03T01:05:06",
                nanosecond => '000000001',
            ),
        ),
    );

    # XXX - currently we don't generate nanosecond offsets for compatibility.
    note "DateTime into string";
    is(to_ISO8601DateTimeTZStr($datetime), "2011-02-03T01:05:06+01:30");
}

{
    ok(is_ISO8601DateTimeTZStr('2013-02-21T02:00:00Z'),
        'String with Z for zero UTC offset');
    ok(is_ISO8601StrictDateTimeTZStr('2013-02-21T02:00:00Z'),
        'String with Z for zero UTC offset with DateTime check');

    ok(!is_ISO8601DateTimeTZStr('2013-02-21T02:00:00'),
        'String without Z');
    ok(!is_ISO8601StrictDateTimeTZStr('2013-02-21T02:00:00'),
        'String without Z');
}

{
    # it doesn't look like we can validate bad timezones, as it's just an arbitrary hour offset?
    ok(is_ISO8601DateTimeTZStr('2013-02-31T02:00:00+01:00'), 'bad datetime validates against our regexp');
    ok(!is_ISO8601StrictDateTimeTZStr('2013-02-31T03:00:00+01:00'), 'bad datetime is caught by strict type');
    ok(is_ISO8601StrictDateTimeTZStr('2013-02-01T04:00:00+01:00'), 'good datetime passes strict type');
    is(to_ISO8601StrictDateTimeTZStr('2013-02-01T05:00:00+01:00'), '2013-02-01T05:00:00+01:00');
}

done_testing;