root/branches/boomer/t/45-datetime.t @ 1098

Revision 1098, 2.9 kB (checked in by hachi, 2 years ago)

Branching for boomer from release-19, rev 62318

  • 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 => (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
Note: See TracBrowser for help on using the browser.