1#!/usr/bin/perl -w
2
3use strict;
4
5use Test::More tests => 10;
6
7use DateTime;
8
9my $dt1 = DateTime->new( year => 1970, hour => 1, nanosecond => 100 );
10
11my $dt2 = DateTime->from_object( object => $dt1 );
12
13is( $dt1->year, 1970, 'year is 1970' );
14is( $dt1->hour, 1, 'hour is 1' );
15is( $dt1->nanosecond, 100, 'nanosecond is 100' );
16
17{
18    my $t1 =
19	DateTime::Calendar::_Test::WithoutTZ->new
20	    ( rd_days => 1, rd_secs => 0 );
21
22    # Tests creating objects from other calendars (without time zones)
23    my $t2 = DateTime->from_object( object => $t1 );
24
25    isa_ok( $t2, 'DateTime' );
26    is( $t2->datetime, '0001-01-01T00:00:00', 'convert from object without tz');
27    ok( $t2->time_zone->is_floating, 'time_zone is floating');
28}
29
30
31{
32    my $tz = DateTime::TimeZone->new( name => 'America/Chicago');
33    my $t1 =
34	DateTime::Calendar::_Test::WithTZ->new
35	    ( rd_days => 1, rd_secs => 0, time_zone => $tz );
36
37    # Tests creating objects from other calendars (with time zones)
38    my $t2 = DateTime->from_object( object => $t1 );
39
40    isa_ok( $t2, 'DateTime' );
41    is( $t2->time_zone->name, 'America/Chicago', 'time_zone is preserved');
42}
43
44{
45    my $tz = DateTime::TimeZone->new( name => 'UTC' );
46    my $t1 =
47	DateTime::Calendar::_Test::WithTZ->new
48	    ( rd_days => 720258, rd_secs => 86400, time_zone => $tz );
49
50    my $t2 = DateTime->from_object( object => $t1 );
51
52    isa_ok( $t2, 'DateTime' );
53    is( $t2->second, 60, 'new DateTime from_object with TZ which is a leap second' );
54}
55
56
57
58# Set up two simple test packages
59
60package DateTime::Calendar::_Test::WithoutTZ;
61
62sub new
63{
64    my $class = shift;
65    bless {@_}, $class;
66}
67
68sub utc_rd_values
69{
70    return $_[0]{rd_days}, $_[0]{rd_secs}, 0;
71}
72
73package DateTime::Calendar::_Test::WithTZ;
74
75sub new
76{
77    my $class = shift;
78    bless {@_}, $class;
79}
80
81sub utc_rd_values
82{
83    return $_[0]{rd_days}, $_[0]{rd_secs}, 0;
84}
85
86sub time_zone
87{
88    return $_[0]{time_zone};
89}
90
91