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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
#============================================================= -*-perl-*-
#
# t/core/timestamp.t
#
# Test the Badger::Timestamp module.
#
# Copyright (C) 2006-2009 Andy Wardley. All Rights Reserved.
#
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
#========================================================================
use strict;
use warnings;
use lib qw( ./lib ../lib ../../lib );
use Badger::Test
tests => 168,
debug => 'Badger::Timestamp',
args => \@ARGV;
use Badger::Timestamp 'Timestamp TS Now';
use Badger::Utils 'refaddr';
#-----------------------------------------------------------------------
# check we barf on invalid dates
#-----------------------------------------------------------------------
eval { Timestamp->new('foobar') };
is( $@, 'timestamp error - Invalid timestamp: foobar', 'bad timestamp format');
my $n = 1;
#-----------------------------------------------------------------------
# check timestamp created now
#-----------------------------------------------------------------------
my $now = Timestamp->new();
my ($second, $minute, $hour, $day, $month, $year ) = localtime(time());
$year += 1900;
$month++;
is($now->day(), $day, 'day now' );
is($now->month(), $month, 'month now' );
is($now->year(), $year, 'year now' );
is($now->hour(), $hour, 'hour now' );
is($now->minute(), $minute, 'minute now' );
$now = Timestamp->now;
ok( $now, 'got now() timestamp' );
$now = Now;
ok( $now, 'got Now() timestamp' );
#-----------------------------------------------------------------------
# check timestamp parsing
#-----------------------------------------------------------------------
foreach my $timestamp (
'2006/08/04 21:22:23',
'2006-08-04 21:22:23',
'2006-08-04T21:22:23',
) {
my $stamp = Timestamp->new($timestamp);
ok( $stamp, "created timestamp $n" );
is( "$stamp", '2006-08-04 21:22:23', "timestamp $n string" );
is( $stamp->timestamp(), '2006-08-04 21:22:23', "timestamp() $n" );
is( $stamp->date(), '2006-08-04', "date() $n" );
is( $stamp->year(), '2006', "year() $n" );
is( $stamp->month(), '8', "month() $n" );
is( $stamp->day(), '4', "day() $n" );
is( $stamp->time(), '21:22:23', "time() $n" );
is( $stamp->hours(), '21', "hours() $n" );
is( $stamp->minutes(), '22', "hours() $n" );
is( $stamp->seconds(), '23', "minutes() $n" );
$n++;
}
#-----------------------------------------------------------------------
# check short numbers work
#-----------------------------------------------------------------------
my $short = Timestamp->new('2010/2/5 4:20:42');
ok( $short, 'created timestamp with short numbers');
is( $short->time, '04:20:42', 'got time' );
is( $short->date, '2010-02-05', 'got date' );
#-----------------------------------------------------------------------
# check named parameters work
#-----------------------------------------------------------------------
my $time = Timestamp->new( hour => 1, minute => 2, second => 3 );
is( $time->hour, 1, 'set 1 hour' );
is( $time->minute, 2, 'set 2 minute' );
is( $time->second, 3, 'set 3 second' );
$time = Timestamp->new( hours => 4, minutes => 5, seconds => 6 );
is( $time->hours, 4, 'set 4 hours' );
is( $time->minutes, 5, 'set 5 minutes' );
is( $time->seconds, 6, 'set 6 seconds' );
my $date = Timestamp->new( day => 7, month => 8, year => 2009 );
is( $date->date, '2009-08-07', 'set day' );
#-----------------------------------------------------------------------
# check we can change items
#-----------------------------------------------------------------------
my $stamp = Timestamp->new('2006-03-19 04:20:42');
ok( $stamp, 'created new timestamp' );
ok( $stamp->year(2007), 'changed year' );
is( $stamp, '2007-03-19 04:20:42', 'new year set' );
ok( $stamp->month(04), 'changed month' );
is( $stamp, '2007-04-19 04:20:42', 'new month set' );
ok( $stamp->day(20), 'changed year' );
is( $stamp, '2007-04-20 04:20:42', 'new day set' );
ok( $stamp->hours(05), 'changed hours' );
is( $stamp, '2007-04-20 05:20:42', 'new hours set' );
ok( $stamp->minutes(21), 'changed minutes' );
is( $stamp, '2007-04-20 05:21:42', 'new minutes set' );
ok( $stamp->seconds(43), 'changed seconds' );
is( $stamp, '2007-04-20 05:21:43', 'new seconds set' );
#-----------------------------------------------------------------------
# test the adjust() method
#-----------------------------------------------------------------------
is( $stamp->adjust( year => 1, month => 2, day => 3,
hours => 4, minutes => 5, seconds => 6 ), $stamp, 'adjusted time' );
is( $stamp, '2008-06-23 09:26:49', 'time adjusted' );
# roll over a minute, hour, day, and so on
is( $stamp->adjust( seconds => 12 ), '2008-06-23 09:27:01', 'rolled over minute' );
is( $stamp->adjust( minutes => 32, seconds => 63 ), '2008-06-23 10:00:04', 'rolled over hour' );
is( $stamp->adjust( hours => 20 ), '2008-06-24 06:00:04', 'rolled over day' );
is( $stamp->adjust( day => 8 ), '2008-07-02 06:00:04', 'rolled over 30 day month' );
is( $stamp->adjust( days => 30 ), '2008-08-01 06:00:04', 'rolled over 31 day month' );
# try with single argument
is( $stamp->adjust("3 days"), '2008-08-04 06:00:04', 'adjust 3 days' );
#is( $stamp->adjust("-1 month"), '2008-07-04 06:00:04', 'adjust -1 month' );
is( $stamp->adjust(month => -1), '2008-07-04 06:00:04', 'adjust -1 month' );
is( $stamp->adjust("-4 days"), '2008-06-30 06:00:04', 'adjust -4 days' );
#-----------------------------------------------------------------------
# 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();
$stamp = Timestamp->new($now);
my $epoch = $stamp->epoch_time();
is( $now, $epoch, 'epoch_time()' );
#-----------------------------------------------------------------------
# test month rollover
#-----------------------------------------------------------------------
$stamp = Timestamp->new();
#print "NOW: $stamp\n";
$stamp->adjust( months => 12 );
#print "ONE YEAR FROM NOW: $stamp (", $stamp->longmonth(), ")\n";
#-----------------------------------------------------------------------
# test format() method
#-----------------------------------------------------------------------
$stamp = Timestamp->new('2005-06-07 08:09:10');
is( $stamp->format('%Y/%m/%d'), '2005/06/07', 'format test date' );
is( $stamp->format('%Hh %Mm %Ss'), '08h 09m 10s', 'format test time' );
#-----------------------------------------------------------------------
# test copy constructor
#-----------------------------------------------------------------------
my $copy = Timestamp->new($stamp);
ok( $copy, 'created object from object' );
is( $copy->compare($stamp), 0, 'copy same as original' );
my $other = $copy->copy;
ok( $other, 'created new object from object new() method' );
is( $other->compare($copy), 0, 'new object same as original' );
isnt( refaddr($stamp), refaddr($copy), 'copy is new object' );
isnt( refaddr($stamp), refaddr($other), 'new is new object' );
#-----------------------------------------------------------------------
# check Timestamp also works as constructor subroutine
#-----------------------------------------------------------------------
my $substamp = Timestamp('2009/01/10 19:11:12');
ok( $substamp, 'created timestamp via Timestamp() subroutine' );
is( $substamp->date, '2009-01-10', "Timestamp() date" );
is( $substamp->year, 2009, "Timestamp() year" );
is( $substamp->month, 1, "Timestamp() month" );
#-----------------------------------------------------------------------
# check TS is an alias to module name
#-----------------------------------------------------------------------
my $tstamp = TS->new('2009/01/10 19:14:12');
ok( $tstamp, 'created timestamp via Timestamp() subroutine' );
is( $tstamp->date, '2009-01-10', "TS stamp date" );
is( $tstamp->year, 2009, "TS stamp year" );
is( $tstamp->minutes, 14, "TS stamp month" );
__END__
# Local Variables:
# mode: perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4:
|