File: 24from-object.t

package info (click to toggle)
libdatetime-perl 2%3A1.50-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,504 kB
  • sloc: perl: 2,964; makefile: 3
file content (103 lines) | stat: -rw-r--r-- 2,299 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
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
## no critic (Modules::ProhibitMultiplePackages)
use strict;
use warnings;

use Test::More;

use DateTime;

my $dt1 = DateTime->new( year => 1970, hour => 1, nanosecond => 100 );

my $dt2 = DateTime->from_object( object => $dt1 );

is( $dt1->year,       1970, 'year is 1970' );
is( $dt1->hour,       1,    'hour is 1' );
is( $dt1->nanosecond, 100,  'nanosecond is 100' );

{
    my $t1 = DateTime::Calendar::_Test::WithoutTZ->new(
        rd_days => 1,
        rd_secs => 0
    );

    # Tests creating objects from other calendars (without time zones)
    my $t2 = DateTime->from_object( object => $t1 );

    isa_ok( $t2, 'DateTime' );
    is(
        $t2->datetime, '0001-01-01T00:00:00',
        'convert from object without tz'
    );
    ok( $t2->time_zone->is_floating, 'time_zone is floating' );
}

{
    my $tz = DateTime::TimeZone->new( name => 'America/Chicago' );
    my $t1 = DateTime::Calendar::_Test::WithTZ->new(
        rd_days   => 1, rd_secs => 0,
        time_zone => $tz
    );

    # Tests creating objects from other calendars (with time zones)
    my $t2 = DateTime->from_object( object => $t1 );

    isa_ok( $t2, 'DateTime' );
    is( $t2->time_zone->name, 'America/Chicago', 'time_zone is preserved' );
}

{
    my $tz = DateTime::TimeZone->new( name => 'UTC' );
    my $t1 = DateTime::Calendar::_Test::WithTZ->new(
        rd_days => 720258,
        rd_secs => 86400, time_zone => $tz
    );

    my $t2 = DateTime->from_object( object => $t1 );

    isa_ok( $t2, 'DateTime' );
    is(
        $t2->second, 60,
        'new DateTime from_object with TZ which is a leap second'
    );
}

{
    for my $class (qw( DateTime::Infinite::Past DateTime::Infinite::Future ))
    {
        isa_ok(
            DateTime->from_object( object => $class->new ),
            $class,
            "from_object($class)"
        );
    }
}

done_testing();

# Set up two simple test packages

package DateTime::Calendar::_Test::WithoutTZ;

sub new {
    my $class = shift;
    bless {@_}, $class;
}

sub utc_rd_values {
    return $_[0]{rd_days}, $_[0]{rd_secs}, 0;
}

package DateTime::Calendar::_Test::WithTZ;

sub new {
    my $class = shift;
    bless {@_}, $class;
}

sub utc_rd_values {
    return $_[0]{rd_days}, $_[0]{rd_secs}, 0;
}

sub time_zone {
    return $_[0]{time_zone};
}