1use strict;
2BEGIN { $^W = 1 }
3
4use Test::More tests => 10;
5use DateTime;
6use DateTime::Format::Epoch;
7
8my $dt = DateTime->new( year  => 1970, month => 1, day   => 1 );
9
10my $f_with_leap = DateTime::Format::Epoch->new( epoch => $dt,
11                                                skip_leap_seconds => 0 );
12my $f_skip_leap = DateTime::Format::Epoch->new( epoch => $dt,
13                                                skip_leap_seconds => 1 );
14
15isa_ok($f_with_leap, 'DateTime::Format::Epoch' );
16
17is($f_with_leap->format_datetime($dt), 0, 'Epoch = 0');
18
19$dt->set( hour => 1 );
20is($f_with_leap->format_datetime($dt), 3600, 'Epoch + 1hour');
21
22$dt->set( day => 2, hour => 0 );
23is($f_with_leap->format_datetime($dt), 24*3600, 'Epoch + 1day');
24
25$dt = DateTime->new( year => 2003, month => 4, day => 27,
26                     hour => 21, minute => 9, second => 57,
27                     time_zone => 'Europe/Amsterdam' );
28
29is($f_with_leap->format_datetime($dt) - $f_skip_leap->format_datetime($dt),
30    22, '22 leap seconds until 2003');
31
32$dt = DateTime->new( year => 1994, month => 6, day => 30,
33                     hour => 23, minute => 59 );
34my $dt2 = DateTime->new( year => 1994, month => 7, day => 1,
35                         hour => 0, minute => 1 );
36is($f_with_leap->format_datetime($dt2) - $f_with_leap->format_datetime($dt),
37    121, '121 secs in 2 minutes');
38is($f_skip_leap->format_datetime($dt2) - $f_skip_leap->format_datetime($dt),
39    120, '120 secs counted in 2 minutes');
40
41$dt2 = DateTime->new( year => 1994, month => 6, day => 30,
42                      hour => 23, minute => 59, second => 60,
43                      time_zone => 'UTC' );
44
45is($f_with_leap->format_datetime($dt2) - $f_with_leap->format_datetime($dt),
46    60, 'correct value at leap second');
47
48# (epoch count at leap second is not specified if skip_leap_seconds is
49# true, so not tested)
50
51# epoch is leap second
52my $f = DateTime::Format::Epoch->new( epoch => $dt2,
53                                      skip_leap_seconds => 0 );
54is($f->format_datetime($dt), -60, 'epoch -60 before leap second');
55
56$dt = DateTime->new( year => 1994, month => 7, day => 1,
57                     hour => 0, minute => 1 );
58is($f->format_datetime($dt), 61, 'epoch 61 after leap second');
59