File: self.t

package info (click to toggle)
libdatetime-format-builder-perl 0.8300-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 504 kB
  • sloc: perl: 1,236; makefile: 2
file content (59 lines) | stat: -rw-r--r-- 1,789 bytes parent folder | download | duplicates (3)
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
use strict;
use warnings;

use Test::More;

use DateTime::Format::Builder;

{
    my $sample = 'SampleClassWithSelf';
    DateTime::Format::Builder->create_class(
        class   => $sample,
        parsers => {
            parse_datetime => [
                [
                    preprocess => sub {
                        my %p    = @_;
                        my $self = $p{self};
                        $p{parsed}->{time_zone} = $self->{global}
                            if $self->{global};
                        return $p{input};
                    },
                ],
                {
                    params => [qw( year month day hour minute second )],
                    regex  => qr/^(\d\d\d\d)(\d\d)(\d\d)T(\d\d)(\d\d)(\d\d)$/,
                    preprocess => sub {
                        my %p    = @_;
                        my $self = $p{self};
                        $p{parsed}->{time_zone} = $self->{pre}
                            if $self->{pre};
                        return $p{input};
                    },
                    postprocess => sub {
                        my %p    = @_;
                        my $self = $p{self};
                        $p{parsed}->{time_zone} = $self->{post}
                            if $self->{post};
                        return 1;
                    },
                },
            ],
        }
    );

    my %tests = (
        global => 'Africa/Cairo',
        pre    => 'Europe/London',
        post   => 'Australia/Sydney',
    );

    while ( my ( $callback, $value ) = each %tests ) {
        my $parser = $sample->new();
        $parser->{$callback} = $value;
        my $dt = $parser->parse_datetime("20030716T163245");
        is( $dt->time_zone->name, $value );
    }
}

done_testing();