use
5.008001;
BEGIN {
$ENV
{PERL_BSON_BACKEND} =
""
}
BEGIN {
$INC
{
"BSON/XS.pm"
} =
undef
}
binmode
( Test::More->builder->
$_
,
":utf8"
)
for
qw/output failure_output todo_output/
;
use
BSON
qw/encode decode/
;
my
(
$bson
,
$expect
,
$hash
);
my
$now
=
time
;
ok( bson_time() >=
$now
,
"empty bson_time() is current time (or so)"
);
ok( BSON::Time->new >=
$now
,
"empty BSON::Time constructor is curren time (or so)"
);
is( bson_time(
$now
),
$now
,
"BSON::Time string overload"
);
is( 0+ bson_time(
$now
),
$now
,
"BSON::Time string overload"
);
$bson
=
$expect
= encode( {
A
=> bson_time(
$now
) } );
$hash
= decode(
$bson
);
is(
ref
(
$hash
->{A} ),
'BSON::Time'
,
"BSON::Time->BSON::Time"
);
is(
"$hash->{A}"
,
$now
,
"value correct"
);
SKIP: {
skip(
"DateTime not installed"
, 1 )
unless
$INC
{
'DateTime.pm'
};
$bson
= encode( {
A
=> DateTime->from_epoch(
epoch
=>
$now
) } );
$hash
= decode(
$bson
);
is(
ref
(
$hash
->{A} ),
'BSON::Time'
,
"DateTime->BSON::Time"
);
is(
"$hash->{A}"
,
$now
,
"value correct"
);
is(
$bson
,
$expect
,
"BSON correct"
);
my
$obj
=
$hash
->{A}->as_datetime;
isa_ok(
$obj
,
'DateTime'
,
'as_datetime'
);
is(
$obj
->epoch,
$now
,
"epoch"
);
}
SKIP: {
skip(
"DateTime::Tiny not installed"
, 1 )
unless
$INC
{
'DateTime/Tiny.pm'
};
my
(
$s
,
$m
,
$h
,
$D
,
$M
,
$Y
) =
gmtime
(
$now
);
my
$dt
= DateTime::Tiny->new(
year
=>
$Y
+ 1900,
month
=>
$M
+ 1,
day
=>
$D
,
hour
=>
$h
,
minute
=>
$m
,
second
=>
$s
);
$bson
= encode( {
A
=>
$dt
} );
$hash
= decode(
$bson
);
is(
ref
(
$hash
->{A} ),
'BSON::Time'
,
"DateTime::Tiny->BSON::Time"
);
is(
"$hash->{A}"
,
$now
,
"value correct"
);
is(
$bson
,
$expect
,
"BSON correct"
);
my
$obj
=
$hash
->{A}->as_datetime_tiny;
isa_ok(
$obj
,
'DateTime::Tiny'
,
'as_datetime_tiny'
);
is(
$obj
->as_string .
"Z"
,
$hash
->{A}->as_iso8601,
"iso8601"
);
}
SKIP: {
skip(
"Time::Moment not installed"
, 1 )
unless
$INC
{
'Time/Moment.pm'
};
$bson
= encode( {
A
=> Time::Moment->from_epoch(
$now
) } );
$hash
= decode(
$bson
);
is(
ref
(
$hash
->{A} ),
'BSON::Time'
,
"Time::Moment->BSON::Time"
);
is(
"$hash->{A}"
,
$now
,
"value correct"
);
is(
$bson
,
$expect
,
"BSON correct"
);
my
$obj
=
$hash
->{A}->as_time_moment;
isa_ok(
$obj
,
'Time::Moment'
,
'as_time_moment'
);
is(
$obj
->epoch,
$now
,
"epoch"
);
}
is( to_myjson({
a
=>bson_time(0)}),
q[{"a":"1970-01-01T00:00:00Z"}]
,
'json: bson_time(0)'
);
is( to_myjson({
a
=>bson_time(1356351330.5)}),
q[{"a":"2012-12-24T12:15:30.500Z"}]
,
'json: bson_time(1356351330.5)'
);
is( to_extjson({
a
=>bson_time(0)}),
q[{"a":{"$date":{"$numberLong":"0"}}}]
,
'extjson: bson_time(0)'
);
is( to_extjson({
a
=>bson_time(1356351330.5)}),
q[{"a":{"$date":{"$numberLong":"1356351330500"}}}]
,
'extjson: bson_time(1356351330.5)'
);
done_testing;