Badger

 view release on metacpan or  search on metacpan

t/core/timestamp.t  view on Meta::CPAN

# test leap_year() and days_in_month() 
#-----------------------------------------------------------------------

$stamp = Timestamp('2008-08-01 06:00:04');

ok( ! $stamp->leap_year(1900), 'not leap year 1900' );
ok( ! $stamp->leap_year(1999), 'not leap year 1999' );
ok(   $stamp->leap_year(2000), 'leap year 2000' );
ok( ! $stamp->leap_year(2001), 'not leap year 2001' );
ok( ! $stamp->leap_year(2002), 'not leap year 2002' );
ok( ! $stamp->leap_year(2003), 'not leap year 2003' );
ok( $stamp->leap_year(2004), 'leap year 2004' );
ok( ! $stamp->leap_year(2005), 'leap year 2005' );

is( $stamp->days_in_month(), 31, 'august has 31 days' );
is( $stamp->days_in_month(1), 31, 'january has 31 days' );
is( $stamp->days_in_month(2, 2003), 28, 'january has 28 days in 2003' );
is( $stamp->days_in_month(2, 2004), 29, 'january has 29 days in 2004' );
is( $stamp->days_in_month(3), 31, 'march has 31 days' );
is( $stamp->days_in_month(4), 30, 'april has 30 days' );
is( $stamp->days_in_month(5), 31, 'may has 31 days' );
is( $stamp->days_in_month(6), 30, 'june has 30 days' );
is( $stamp->days_in_month(7), 31, 'july has 31 days' );
is( $stamp->days_in_month(8), 31, 'august has 31 days' );
is( $stamp->days_in_month(9), 30, 'september has 30 days' );
is( $stamp->days_in_month(10), 31, 'october has 31 days' );
is( $stamp->days_in_month(11), 30, 'november has 30 days' );
is( $stamp->days_in_month(12), 31, 'december has 31 days' );


#-----------------------------------------------------------------------
# test compare() method
#-----------------------------------------------------------------------

$stamp= Timestamp->new();
$stamp->adjust( second => -1 );
is( $stamp->compare(time()), -1, 'compare earlier than now' );
$stamp->adjust( minute => 1 );
is( $stamp->compare(time()), 1, 'compare later than now' );

$stamp = Timestamp->new();
my $compare = Timestamp->new($stamp);

is( $stamp->compare($compare), 0, 'compare the same' );
foreach my $item (qw(second minute hour day month year )) {
    $stamp->adjust( $item => -1 );
    is( $stamp->compare($compare), -1, "$stamp $item earlier $compare" );
    $stamp->adjust( $item => 2 );
    is( $stamp->compare($compare), 1, "$stamp $item later $compare" );
}


#-----------------------------------------------------------------------
# test before(), after() and equal()
#-----------------------------------------------------------------------

my $old = Timestamp->new('2009-07-05 12:47:42');
my $new = Timestamp->new('2009-07-05 16:20:00');
ok( $old->equal($old), 'old is equal to old' );
ok( $new->equal($new), 'new is equal to new' );
ok( $old->not_equal($new), 'old is not equal to new' );
ok( $new->not_equal($old), 'new is not equal to old' );

# before/after/compare/equal all accept another timestamp...
ok( $old->before($new), 'old is before new' );
ok( $new->after($old), 'new is after old' );

# ...or a time in epoch seconds...
ok( $old->before($new->epoch_time), 'old is before new epoch time' );
ok( $new->after($old->epoch_time), 'new is after old epoch time' );

# ...or a timestamp...
ok( $old->before($new->timestamp), 'old is before new epoch timestamp' );
ok( $new->after($old->timestamp), 'new is after old epoch timestamp' );

# ...or a set of named params
ok( $old->before( year => 2010 ), 'old is before new year' );
ok( $new->after( year => 1969 ), 'new is after old year' );

# test some negatives to make sure we're not using rose tinted methods
ok( ! $new->equal($old), 'new is not equal to old' );
ok( ! $old->equal($new), 'old is not equal to new' );
ok( ! $new->before($old), 'new is not before old' );
ok( ! $old->after($new), 'old is not after new' );
ok( ! $new->equal($old), 'new is not equal to old' );
ok( ! $old->equal($new), 'old is not equal to new' );
ok( ! $new->before($old->epoch_time), 'new is not before old epoch time' );
ok( ! $old->after($new->epoch_time), 'old is not after new epoch time' );
ok( ! $new->equal($old->epoch_time), 'new is not equal to old epoch time' );
ok( ! $old->equal($new->epoch_time), 'old is not equal to new epoch time' );


#-----------------------------------------------------------------------
# test comparison operators
#-----------------------------------------------------------------------

ok( $old == $old, 'old == old' );
ok( $new == $new, 'new == new' );
ok( $old != $new, 'old != new' );
ok( $new != $old, 'new != old' );
ok( $old < $new, 'old < new' );
ok( $new > $old, 'new > old' );
ok( $old <= $new, 'old <= new' );
ok( $new >= $old, 'new >= old' );
ok( $old <= $old, 'old <= old' );
ok( $new >= $new, 'new >= new' );

ok( ! ($old != $old), 'old != old is false' );
ok( ! ($new != $new), 'new != new is false' );
ok( ! ($old == $new), 'old == new is false' );
ok( ! ($new == $old), 'new == old is false' );
ok( ! ($old > $new), 'old > new is false' );
ok( ! ($new < $old), 'new < old is false' );
ok( ! ($old >= $new), 'old >= new is false' );
ok( ! ($new <= $old), 'new <= old is false' );


#-----------------------------------------------------------------------
# test epoch_seconds()
#-----------------------------------------------------------------------

$now   = time();



( run in 1.141 second using v1.01-cache-2.11-cpan-98e64b0badf )