Changeset 1744
- Timestamp:
- 04/03/08 08:14:20 (23 months ago)
- Location:
- branches/release-33
- Files:
-
- 5 modified
-
lib/MT/AtomServer.pm (modified) (2 diffs)
-
lib/MT/DateTime.pm (modified) (2 diffs)
-
lib/MT/Util.pm (modified) (1 diff)
-
lib/MT/XMLRPCServer.pm (modified) (2 diffs)
-
t/45-datetime.t (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/release-33/lib/MT/AtomServer.pm
r1174 r1744 557 557 if (my $iso = $atom->issued) { 558 558 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];562 559 $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 { 564 565 $entry->status(MT::Entry::FUTURE()) 565 566 } … … 647 648 if (my $iso = $atom->issued) { 648 649 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];652 650 $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 { 654 656 $entry->status(MT::Entry::FUTURE()) 655 657 } -
branches/release-33/lib/MT/DateTime.pm
r1174 r1744 13 13 use vars qw( @EXPORT_OK ); 14 14 @EXPORT_OK = qw( ymd2rd tz_offset_as_seconds ); 15 16 use MT::Util qw( epoch2ts ); 15 17 16 18 sub new { … … 170 172 } 171 173 174 sub _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 209 sub 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 172 242 1; 173 243 __END__ -
branches/release-33/lib/MT/Util.pm
r1616 r1744 119 119 sub epoch2ts { 120 120 my ($blog, $epoch) = @_; 121 $epoch = offset_time($epoch, $blog) if ref$blog;121 $epoch = offset_time($epoch, $blog) if defined $blog; 122 122 my ($s, $m, $h, $d, $mo, $y) = gmtime($epoch); 123 123 sprintf("%04d%02d%02d%02d%02d%02d", -
branches/release-33/lib/MT/XMLRPCServer.pm
r1724 r1744 361 361 $entry->authored_on(MT::XMLRPCServer::Util::iso2ts($blog, $iso)) 362 362 || 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; 366 364 $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; 368 369 } 369 370 $entry->discover_tb_from_entry(); … … 487 488 $entry->authored_on(MT::XMLRPCServer::Util::iso2ts($entry->blog_id, $iso)) 488 489 || 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; 489 495 } 490 496 $entry->discover_tb_from_entry(); -
branches/release-33/t/45-datetime.t
r1098 r1744 24 24 25 25 my $num_tests = 3; 26 plan tests => (scalar @dates) * $num_tests;26 plan tests => 9 + (scalar @dates) * $num_tests; 27 27 28 28 foreach my $dh (@dates) { … … 67 67 } 68 68 69 # compare tests 70 my $t = time(); 71 ok( (MT::DateTime->compare( a => '20080401123456', b => '20080401123456' ) == 0), 'the same time is equal to each other'); 72 ok( (MT::DateTime->compare( a => '20080401123457', b => '20080401123456' ) > 0), 'a > b returns positive'); 73 ok( (MT::DateTime->compare( a => '20080401123456', b => '20080501123456' ) < 0), 'a < b returns negative'); 74 ok( (MT::DateTime->compare( a => { value => $t, type => 'epoch' }, b => { value => $t, type => 'epoch' } ) == 0), 'the same time is equal to each other'); 75 ok( (MT::DateTime->compare( b => { value => $t, type => 'epoch' }, a => { value => $t, type => 'epoch' } ) == 0), 'the same time is equal to each other'); 76 ok( (MT::DateTime->compare( b => { value => time(), type => 'epoch' }, a => '20080101235959' ) < 0), 'today is larger than Jan 1st 23:59:59, 2008'); 77 ok( (MT::DateTime->compare( a => { value => time(), type => 'epoch' }, b => '20080101235959' ) > 0), 'today is larger than Jan 1st 23:59:59, 2008'); 78 my $dt1 = MT::DateTime->new( %{$dates[0]} ); 79 my $dt2 = MT::DateTime->new( %{$dates[$#dates]} ); 80 ok( ($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 ); 85 ok( ($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 );
