Changeset 1744

Show
Ignore:
Timestamp:
04/03/08 08:14:20 (15 months ago)
Author:
fumiakiy
Message:

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

Location:
branches/release-33
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • branches/release-33/lib/MT/AtomServer.pm

    r1174 r1744  
    557557    if (my $iso = $atom->issued) { 
    558558        my $pub_ts = MT::Util::iso2ts($blog, $iso); 
    559         my @ts = MT::Util::offset_time_list(time, $blog->id); 
    560         my $ts = sprintf '%04d%02d%02d%02d%02d%02d', 
    561             $ts[5]+1900, $ts[4]+1, @ts[3,2,1,0]; 
    562559        $entry->authored_on($pub_ts); 
    563         if ($pub_ts > $ts) { 
     560        if ( 0 < MT::DateTime->compare( blog => $blog, 
     561                a => $pub_ts, 
     562                b => { value => time(), type => 'epoch' } ) 
     563           ) 
     564        { 
    564565            $entry->status(MT::Entry::FUTURE()) 
    565566        } 
     
    647648    if (my $iso = $atom->issued) { 
    648649        my $pub_ts = MT::Util::iso2ts($blog, $iso); 
    649         my @ts = MT::Util::offset_time_list(time, $blog->id); 
    650         my $ts = sprintf '%04d%02d%02d%02d%02d%02d', 
    651             $ts[5]+1900, $ts[4]+1, @ts[3,2,1,0]; 
    652650        $entry->authored_on($pub_ts); 
    653         if ($pub_ts > $ts) { 
     651        if ( 0 < MT::DateTime->compare( blog => $blog, 
     652                a => $pub_ts, 
     653                b => { value => time(), type => 'epoch' } ) 
     654           ) 
     655        { 
    654656            $entry->status(MT::Entry::FUTURE()) 
    655657        } 
  • branches/release-33/lib/MT/DateTime.pm

    r1174 r1744  
    1313use vars qw( @EXPORT_OK ); 
    1414@EXPORT_OK = qw( ymd2rd tz_offset_as_seconds ); 
     15 
     16use MT::Util qw( epoch2ts ); 
    1517 
    1618sub new { 
     
    170172} 
    171173 
     174sub _param2ts { 
     175    my ( $param, $blog ) = @_; 
     176 
     177    my ( $type, $value ); 
     178    if ( 'HASH' eq ref($param) ) { 
     179        $type = $param->{type}; 
     180        $value = $param->{value}; 
     181    } 
     182    else { 
     183        $type = 'ts'; 
     184        $value = $param; 
     185    } 
     186    if ( 'CODE' eq ref($value) ) { 
     187        $value = $value->(); 
     188    } 
     189 
     190    my $ts; 
     191    if ( 'epoch' eq $type ) { 
     192        $ts = epoch2ts( $blog, $value ); 
     193    } 
     194    elsif ( 'datetime' eq $type ) { 
     195        $ts = sprintf "%04d%02d%02d%02d%02d%02d", 
     196            $value->year, 
     197            $value->month, 
     198            $value->day, 
     199            $value->hour, 
     200            $value->minute, 
     201            $value->second; 
     202    } 
     203    else { 
     204        $ts = $value; 
     205    } 
     206    $ts; 
     207} 
     208 
     209sub compare { 
     210    my $self = shift; 
     211    my %param = @_; 
     212    # a => $ts | CODE | { value => CODE|$v, type => ts|epoch|datetime } 
     213    # b => $ts | CODE | { value => CODE|$v, type => ts|epoch|datetime } 
     214    # blog => ref|N|undef 
     215    # comparer => CODE|undef 
     216 
     217    my $blog = $param{blog}; 
     218    if ( defined($blog) && !ref($blog) ) { 
     219        $blog = MT->model('blog')->load( $blog ); 
     220        $blog = undef unless ref($blog); 
     221    } 
     222 
     223    if ( !exists($param{a}) && ref($self) ) { 
     224        $param{a} = { value => $self, type => 'datetime' }; 
     225    } 
     226    my $ts_a = _param2ts( $param{a}, $blog ); 
     227 
     228    if ( !exists($param{b}) && ref($self) ) { 
     229        $param{b} = { value => $self, type => 'datetime' }; 
     230    } 
     231    my $ts_b = _param2ts( $param{b}, $blog ); 
     232 
     233    my $comparer = $param{code}; 
     234    if ( 'CODE' eq ref($comparer) ) { 
     235        return $comparer->( $ts_a, $ts_b ); 
     236    } 
     237    else { 
     238        return $ts_a - $ts_b; 
     239    } 
     240} 
     241 
    1722421; 
    173243__END__ 
  • branches/release-33/lib/MT/Util.pm

    r1616 r1744  
    119119sub epoch2ts { 
    120120    my ($blog, $epoch) = @_; 
    121     $epoch = offset_time($epoch, $blog) if ref $blog; 
     121    $epoch = offset_time($epoch, $blog) if defined $blog; 
    122122    my ($s, $m, $h, $d, $mo, $y) = gmtime($epoch); 
    123123    sprintf("%04d%02d%02d%02d%02d%02d", 
  • branches/release-33/lib/MT/XMLRPCServer.pm

    r1724 r1744  
    361361        $entry->authored_on(MT::XMLRPCServer::Util::iso2ts($blog, $iso)) 
    362362            || die MT::XMLRPCServer::_fault(MT->translate("Invalid timestamp format")); 
    363         my @ts = MT::Util::offset_time_list(time, $blog_id); 
    364         my $ts = sprintf '%04d%02d%02d%02d%02d%02d', 
    365             $ts[5]+1900, $ts[4]+1, @ts[3,2,1,0]; 
     363        require MT::DateTime; 
    366364        $entry->status(MT::Entry::FUTURE()) 
    367             if ($entry->authored_on > $ts); 
     365            if MT::DateTime->compare( 
     366                blog => $blog, 
     367                a => $entry->authored_on, 
     368                b => { value => time(), type => 'epoch' } ) > 0; 
    368369    } 
    369370    $entry->discover_tb_from_entry(); 
     
    487488        $entry->authored_on(MT::XMLRPCServer::Util::iso2ts($entry->blog_id, $iso)) 
    488489           || die MT::XMLRPCServer::_fault(MT->translate("Invalid timestamp format")); 
     490        require MT::DateTime; 
     491        $entry->status(MT::Entry::FUTURE()) 
     492            if MT::DateTime->compare( 
     493                a => $entry->authored_on, 
     494                b => { value => time(), type => 'epoch' } ) > 0; 
    489495    } 
    490496    $entry->discover_tb_from_entry(); 
  • branches/release-33/t/45-datetime.t

    r1098 r1744  
    2424 
    2525my $num_tests = 3; 
    26 plan tests => (scalar @dates) * $num_tests; 
     26plan tests => 9 + (scalar @dates) * $num_tests; 
    2727 
    2828foreach my $dh (@dates) { 
     
    6767} 
    6868 
     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);