Skip to content

Commit

Permalink
Don't use date_diff, can result in buggy behaviour. Fixes #205. Add u…
Browse files Browse the repository at this point in the history
…nit-tests.
  • Loading branch information
stephenharris committed Jul 15, 2014
1 parent 07f76a2 commit 5e3e1f6
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 21 deletions.
25 changes: 4 additions & 21 deletions includes/event.php
Expand Up @@ -334,17 +334,8 @@ function _eventorganiser_insert_occurrences( $post_id, $event_data ){
extract( $event_data );
$tz = eo_get_blog_timezone();

//Get duration
$duration = false;
if( function_exists('date_diff') ){
$duration = date_diff( $start,$end );

/* Storing a DateInterval object can cause errors. Serialize it.
Thanks to Mathieu Parisot, Mathias & Dave Page */
$event_data['duration'] = maybe_serialize( $duration );
}

//Work around for PHP < 5.3
//Don't use date_diff (requires php 5.3+)
//Also see https://github.com/stephenharris/Event-Organiser/issues/205
$seconds = round( abs( $start->format('U') - $end->format('U') ) );
$days = floor( $seconds/86400 );// 86400 = 60*60*24 seconds in a normal day
$sec_diff = $seconds - $days*86400;
Expand Down Expand Up @@ -376,11 +367,7 @@ function _eventorganiser_insert_occurrences( $post_id, $event_data ){
foreach( $update as $occurrence_id => $occurrence ){

$occurrence_end = clone $occurrence;
if( $duration ){
$occurrence_end->add($duration);
}else{
$occurrence_end->modify($duration_str);
}
$occurrence_end->modify($duration_str);

$occurrence_input = array(
'StartDate' => $occurrence->format('Y-m-d'),
Expand All @@ -407,11 +394,7 @@ function _eventorganiser_insert_occurrences( $post_id, $event_data ){
if( $insert ){
foreach( $insert as $counter => $occurrence ):
$occurrence_end = clone $occurrence;
if( $duration ){
$occurrence_end->add($duration);
}else{
$occurrence_end->modify($duration_str);
}
$occurrence_end->modify($duration_str);

$occurrence_input =array(
'post_id' => $post_id,
Expand Down
35 changes: 35 additions & 0 deletions tests/unit-tests/eventTest.php
Expand Up @@ -270,5 +270,40 @@ function array_map_assoc( $callback, $arr1 ) {

return $results;
}


/**
* @see https://github.com/stephenharris/Event-Organiser/issues/205
* Tests event end date is created successfully.
*/
public function testEventAtEndOfMonth()
{

$original_tz = get_option( 'timezone_string' );
$original_offset = get_option( 'gmt_offset' );

update_option( 'timezone_string', '' );
update_option( 'gmt_offset', 10 );

$tz = eo_get_blog_timezone();

$event = array(
'post_title' => 'Test event',
'start' => new DateTime( '2014-07-01 00:00:00', $tz ),
'end' => new DateTime( '2014-07-31 23:59:00', $tz ),
'all_day' => 1,
);

$event_id = eo_insert_event( $event );
$occurrences = eo_get_the_occurrences( $event_id );
$occurrence_ids = array_keys( $occurrences );
$occurrence_id = array_shift( $occurrence_ids );

$this->assertEquals( '2014-07-31', eo_get_the_end( 'Y-m-d', $event_id, null, $occurrence_id ) );

update_option( 'timezone_string', $original_tz );
update_option( 'gmt_offset', $original_offset );
}

}

47 changes: 47 additions & 0 deletions tests/unit-tests/utilityFunctionsTest.php
Expand Up @@ -165,6 +165,53 @@ public function testCombineArraysAssoc(){
$this->assertEquals( $expected, eo_array_combine_assoc( $key_array, $value_array ) );
}

/**
* date_diff can cause some unexpected behaviour. This is a simple workaround.
* @see https://github.com/stephenharris/Event-Organiser/issues/205
*/
public function testDateDiffFallback()
{

$timezone = new DateTimeZone( 'Europe/London' );
$date1 = new DateTime( '2014-07-01 00:00:00', $timezone );
$date2 = new DateTime( '2014-07-31 23:59:00', $timezone );

//Work around for PHP < 5.3. Also see
$seconds = round( abs( $date1->format('U') - $date2->format('U') ) );
$days = floor( $seconds/86400 );// 86400 = 60*60*24 seconds in a normal day
$sec_diff = $seconds - $days*86400;

$this->assertEquals( 30, $days );
$this->assertEquals( 86340, $sec_diff);

}

public function testDateDiffFallbackDST()
{

$timezone = new DateTimeZone( 'Europe/London' );
$date1 = new DateTime( '2014-03-30 00:00:00', $timezone );
$date2 = new DateTime( '2014-03-30 04:00:00', $timezone );

//Work around for PHP < 5.3. Also see
$seconds = round( abs( $date1->format('U') - $date2->format('U') ) );
$days = floor( $seconds/86400 );// 86400 = 60*60*24 seconds in a normal day
$sec_diff = $seconds - $days*86400;

//$this->assertEquals( 3, $days );
$this->assertEquals( 10800, $sec_diff);

$date1 = new DateTime( '2014-10-26 00:00:00', $timezone );
$date2 = new DateTime( '2014-10-26 04:00:00', $timezone );

$seconds = round( abs( $date1->format('U') - $date2->format('U') ) );
$days = floor( $seconds/86400 );// 86400 = 60*60*24 seconds in a normal day
$sec_diff = $seconds - $days*86400;

$this->assertEquals( 18000, $sec_diff);

}


/**
* TODO eo_get_blog_timezone(): Why does +10 give Asia/Choibalsan timezone.
Expand Down

0 comments on commit 5e3e1f6

Please sign in to comment.