root/branches/release-33/t/45-datetime.t @ 1744

Revision 1744, 4.9 kB (checked in by fumiakiy, 20 months ago)

Implemented MT::DateTime::compare. BugId:67917

  • Property svn:mime-type set to text/plain
  • Property svn:keywords set to Author Date Id Revision
Line 
1# $Id$
2
3use strict;
4use lib 't/lib', 'extlib', 'lib', '../lib', '../extlib';
5use Test::More;
6use MT::DateTime;
7use DateTime;
8use DateTime::TimeZone;
9use Time::Local qw(timegm);
10use MT::Util qw(week2ymd);
11
12my @dates;
13
14foreach my $year ( 2000..2001 ) {
15    foreach my $month (1..12) {
16        foreach my $day (1..28) {
17            my $zone = int(rand 10) - 5 + ( int(rand 1) / 2 );
18            my $tz = ($zone < 0 ? '-' : '+') . sprintf("%02d", int(abs($zone))) . ':' . sprintf("%02d", (abs($zone) - int(abs($zone))) * 60);
19            push @dates, { year => $year, month => $month, day => $day,
20                hour => int(rand 24), minute => int(rand 60), second => int(rand 60), time_zone => $tz };
21        }
22    }
23}
24
25my $num_tests = 3;
26plan tests => 9 + (scalar @dates) * $num_tests;
27
28foreach my $dh (@dates) {
29    my $mt_dt = MT::DateTime->new( %$dh );
30    my $dt = DateTime->new( %$dh );
31    my $the_date = sprintf("%04d-%02d-%02d %02d:%02d:%02d %s", $dh->{year}, $dh->{month}, $dh->{day}, $dh->{hour}, $dh->{minute}, $dh->{second}, $dh->{time_zone});
32
33    # testing week number calculation
34    # bchoate -- THESE NO LONGER MATCH; we calculate week number
35    # where Sunday is the start of the week.
36    # ok($mt_dt->week_number == $dt->week_number) or
37    #     print "week # is ", $mt_dt->week_number, "; expecting ",$dt->week_number,"\n";
38
39    # testing timezone offset to seconds calculation
40    ok($mt_dt->tz_offset_as_seconds == DateTime::TimeZone::offset_as_seconds($mt_dt->time_zone)) or
41        print "timezone seconds is ", $mt_dt->tz_offset_as_seconds, "; expecting ",DateTime::TimeZone::offset_as_seconds($mt_dt->time_zone),"; for time zone ",$mt_dt->time_zone,"\n";
42
43    # testing timezone translation (dt -> ts)
44    my $mt_ts = timegm( $dh->{second}, $dh->{minute}, $dh->{hour}, $dh->{day}, $dh->{month} - 1, $dh->{year} - 1900 );
45    my $dt_tz_secs = DateTime::TimeZone::offset_as_seconds($dh->{time_zone});
46    my $mt_tz_secs = $mt_dt->tz_offset_as_seconds;
47    $mt_ts -= $mt_tz_secs;
48    my ($s, $m, $h, $d, $mo, $y) = gmtime($mt_ts);
49    $y += 1900; $mo++;
50    my $mt_iso_date = sprintf("%04d%02d%02d%02d%02d%02d", $y, $mo, $d, $h, $m, $s);
51
52    $dt->subtract(seconds => $dt_tz_secs);
53    ($y, $mo, $d, $h, $m, $s) = (split('-', $dt->ymd), split(':', $dt->hms));
54    my $dt_iso_date = "$y$mo$d$h$m$s";
55    ok($mt_iso_date eq $dt_iso_date) or
56        print "date is $the_date\n\tmt iso date: $mt_iso_date\n\tdt iso date: $dt_iso_date\n";
57
58    my ($mt_yr, $mt_wk) = $mt_dt->week;
59    my ($wk_y, $wk_m, $wk_d) = week2ymd($mt_yr, $mt_wk);
60    my $wk_ymd = sprintf("%04d%02d%02d", $wk_y, $wk_m, $wk_d);
61    my $mt_wk_dt = MT::DateTime->new( year => substr($wk_ymd, 0, 4),
62        month => substr($wk_ymd, 4, 2),
63        day => substr($wk_ymd, 6, 2) );
64    my ($wk_yr, $wk_wk) = $mt_wk_dt->week;
65    ok(($wk_yr == $mt_yr) && ($wk_wk == $mt_wk))
66        or print "$wk_ymd :: $the_date -> $mt_yr/$mt_wk -> $wk_ymd -> $wk_yr/$wk_wk does not equate\n";
67}
68
69# compare tests
70my $t = time();
71ok( (MT::DateTime->compare( a => '20080401123456', b => '20080401123456' ) == 0), 'the same time is equal to each other');
72ok( (MT::DateTime->compare( a => '20080401123457', b => '20080401123456' ) > 0), 'a > b returns positive');
73ok( (MT::DateTime->compare( a => '20080401123456', b => '20080501123456' ) < 0), 'a < b returns negative');
74ok( (MT::DateTime->compare( a => { value => $t, type => 'epoch' }, b => { value => $t, type => 'epoch' } ) == 0), 'the same time is equal to each other');
75ok( (MT::DateTime->compare( b => { value => $t, type => 'epoch' }, a => { value => $t, type => 'epoch' } ) == 0), 'the same time is equal to each other');
76ok( (MT::DateTime->compare( b => { value => time(), type => 'epoch' }, a => '20080101235959' ) < 0), 'today is larger than Jan 1st 23:59:59, 2008');
77ok( (MT::DateTime->compare( a => { value => time(), type => 'epoch' }, b => '20080101235959' ) > 0), 'today is larger than Jan 1st 23:59:59, 2008');
78my $dt1 = MT::DateTime->new( %{$dates[0]} );
79my $dt2 = MT::DateTime->new( %{$dates[$#dates]} );
80ok( ($dt1->compare(a => { value => $dt2, type => 'datetime' }) > 0),
81    sprintf("%04d-%02d-%02d %02d:%02d:%02d %s", $dates[$#dates]{year}, $dates[$#dates]{month}, $dates[$#dates]{day}, $dates[$#dates]{hour}, $dates[$#dates]{minute}, $dates[$#dates]{second}, $dates[$#dates]{time_zone}) .
82    ' is the future from ' .
83    sprintf("%04d-%02d-%02d %02d:%02d:%02d %s", $dates[0]{year}, $dates[0]{month}, $dates[0]{day}, $dates[0]{hour}, $dates[0]{minute}, $dates[0]{second}, $dates[0]{time_zone})
84);
85ok( ($dt1->compare(b => { value => $dt2, type => 'datetime' }) < 0),
86    sprintf("%04d-%02d-%02d %02d:%02d:%02d %s", $dates[0]{year}, $dates[0]{month}, $dates[0]{day}, $dates[0]{hour}, $dates[0]{minute}, $dates[0]{second}, $dates[0]{time_zone}) .
87    ' is the past from ' .
88    sprintf("%04d-%02d-%02d %02d:%02d:%02d %s", $dates[$#dates]{year}, $dates[$#dates]{month}, $dates[$#dates]{day}, $dates[$#dates]{hour}, $dates[$#dates]{minute}, $dates[$#dates]{second}, $dates[$#dates]{time_zone})
89);
Note: See TracBrowser for help on using the browser.