File: 002_dates.t

package info (click to toggle)
libdatetime-format-strptime-perl 1.5600-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 284 kB
  • ctags: 15
  • sloc: perl: 1,453; makefile: 2
file content (104 lines) | stat: -rw-r--r-- 2,727 bytes parent folder | download
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!perl -w

# t/002_basic.t - check module dates in various formats

use Test::More 0.88;
use DateTime::Format::Strptime;
use DateTime;
use DateTime::TimeZone;

my $object = DateTime::Format::Strptime->new(
    pattern => '%D',

    #	time_zone => 'Australia/Melbourne',
    diagnostic => 1,
    on_error   => 'croak',
);

my @tests = (

    # Simple dates
    [ '%Y-%m-%d',          '1998-12-31' ],
    [ '%y-%m-%d',          '98-12-31' ],
    [ '%Y years, %j days', '1998 years, 312 days' ],
    [ '%b %d, %Y',         'Jan 24, 2003' ],
    [ '%B %d, %Y',         'January 24, 2003' ],

    # Simple times
    [ '%H:%M:%S',    '23:45:56' ],
    [ '%l:%M:%S %p', '11:34:56 PM' ],

    # With Nanoseconds
    [ '%H:%M:%S.%N',  '23:45:56.123456789' ],
    [ '%H:%M:%S.%6N', '23:45:56.123456' ],
    [ '%H:%M:%S.%3N', '23:45:56.123' ],

    # Complex dates
    [ '%Y;%j = %Y-%m-%d',      '2003;056 = 2003-02-25' ],
    [ q|%d %b '%y = %Y-%m-%d|, q|25 Feb '03 = 2003-02-25| ],

    # Leading spaces
    [ '%e-%b-%Y %T %z', '13-Jun-2010 09:20:47 -0400' ],
    [ '%e-%b-%Y %T %z', ' 3-Jun-2010 09:20:47 -0400' ],
);

foreach (@tests) {
    my ( $pattern, $data, $expect ) = @$_;
    $expect ||= $data;
    $object->pattern($pattern);
    is(
        $object->format_datetime( $object->parse_datetime($data) ), $expect,
        $pattern
    );
}

SKIP: {
    skip
        "You don't have the latest DateTime. Older version have a bug whereby 12am and 12pm are shown as 0am and 0pm. You should upgrade.",
        1
        unless $DateTime::VERSION >= 0.11;

    $object->pattern('%l:%M:%S %p');
    is(
        $object->format_datetime( $object->parse_datetime('12:34:56 AM') ),
        '12:34:56 AM', '%l:%M:%S %p'
    );
}

# Timezones
SKIP: {
    skip
        "You don't have the latest DateTime::TimeZone. Older versions don't display all time zone information. You should upgrade.",
        3
        unless $DateTime::TimeZone::VERSION >= 0.13;

    $object->pattern('%H:%M:%S %z');
    is(
        $object->format_datetime( $object->parse_datetime('23:45:56 +1000') ),
        '23:45:56 +1000', '%H:%M:%S %z'
    );

    $object->pattern('%H:%M:%S %Z');
    is(
        $object->format_datetime( $object->parse_datetime('23:45:56 AEST') ),
        '23:45:56 +1000', '%H:%M:%S %Z'
    );

    $object->pattern('%H:%M:%S %z %Z');
    is(
        $object->format_datetime(
            $object->parse_datetime('23:45:56 +1000 AEST')
        ),
        '23:45:56 +1000 +1000',
        '%H:%M:%S %z %Z'
    );
}

$object->time_zone('America/New_York');
$object->pattern('%Y %H:%M:%S %Z');
is(
    $object->format_datetime( $object->parse_datetime('2003 23:45:56 MDT') ),
    '2003 00:45:56 EST', $object->pattern
);

done_testing();