Skip to content

Commit

Permalink
Sometimes a token will look the same for two dates where the underlyi…
Browse files Browse the repository at this point in the history
…ng entity is in fact different. For instance consider: 2015-02-11 and 2015-02-18 - both are Wednesdays, but they represent different Wednesday so we should represent this as "Wednesday 11th - Wednesday 18th, February" and not "Wednesday 11th - 18th, February". Fixes #359.
  • Loading branch information
stephenharris committed May 11, 2016
1 parent f081419 commit 991350a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
44 changes: 34 additions & 10 deletions includes/event-organiser-utility-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,27 +205,27 @@ function eo_format_event_occurrence( $event_id = false, $occurrence_id = false,
* @param bool $is_rtl Whether the formatted date should be written right-to-left. Defaults to is_rtl().
* @return string|dateTime The formatted date range
*/
function _eo_format_datetime_range( $datetime1, $datetime2, $format, $is_rtl = null ){
function _eo_format_datetime_range( $datetime1, $datetime2, $format, $is_rtl = null ) {

if( is_null( $is_rtl ) ){
if ( is_null( $is_rtl ) ) {
$is_rtl = is_rtl();
}

$formatted1 = eo_format_datetime( $datetime1, $format );
$formatted2 = eo_format_datetime( $datetime2, $format );

if( $formatted1 === $formatted2 ){
if ( $formatted1 === $formatted2 ) {
return $formatted1;
}

//we include jS as a token to ensure correct positioning of suffix: 4th-5th not 4-5th
$date = array(
'jS', 'j', 'd', 'D', 'l', 'S', 'w', 'N', 'z',//Day
'W', //Week
'F', 'm', 'M', 'n', 't', //Month
'Y', 'y', 'o', 'L', //Year
'e', 'P', 'O', 'T', 'Z', 'I', //Timezone
'c', 'R', 'U', 'u', 'e','r', //Full date time
array( 'c', 'R', 'U', 'u', 'e','r' ), //Full date time
array( 'e', 'P', 'O', 'T', 'Z', 'I' ), //Timezone
array( 'Y', 'y', 'o', 'L' ), //Year
array( 'F', 'm', 'M', 'n', 't' ), //Month
array( 'W' ), //Week
array( 'jS', 'j', 'd', 'D', 'l', 'S', 'w', 'N', 'z' ),//Day
);
$time = array(
'g', 'G', 'h', 'H', //Hour
Expand All @@ -235,7 +235,7 @@ function _eo_format_datetime_range( $datetime1, $datetime2, $format, $is_rtl = n
);

//Include time with (:?) to ensure we don't split at : in time fragment.
$regexp = '/(\\\\\S|' . implode( '(:?)|', $time ) . '(:?)|' . implode( '|', $date ) . '|.)/';
$regexp = '/(\\\\\S|' . implode( '(:?)|', $time ) . '(:?)|' . implode( '|', call_user_func_array( 'array_merge', $date ) ) . '|.)/';

preg_match_all( $regexp, $format, $matches );
$tokens = $matches[0];
Expand All @@ -248,6 +248,16 @@ function _eo_format_datetime_range( $datetime1, $datetime2, $format, $is_rtl = n
$middle2 = false;
$right = false;

//Collect the tokens which represent entities for which the two dates differ
$break_at_tokens = array();
if ( $datetime1->format( 'Y' ) !== $datetime2->format( 'Y' ) ) {
$break_at_tokens = call_user_func_array( 'array_merge', array_slice( $date, -4 ) );
} elseif ( $datetime1->format( 'Ym' ) !== $datetime2->format( 'Ym' ) ) {
$break_at_tokens = call_user_func_array( 'array_merge', array_slice( $date, -3 ) );
} elseif ( $datetime1->format( 'Ymd' ) !== $datetime2->format( 'Ymd' ) ) {
$break_at_tokens = call_user_func_array( 'array_merge', array_slice( $date, -1 ) );
}

while( $left_counter < count( $tokens ) ){

$parsed_token_1 = eo_format_datetime( $datetime1, $tokens[$left_counter] );
Expand All @@ -258,6 +268,13 @@ function _eo_format_datetime_range( $datetime1, $datetime2, $format, $is_rtl = n
break;
}

//If token is indicated as representing entity that is different, split even though
//they look the same e.g. 'l' with in Saturday 2nd and Saturday 9th
//@see https://github.com/stephenharris/Event-Organiser/issues/359
if ( in_array( $tokens[$left_counter], $break_at_tokens ) ) {
break;
}

$left .= $parsed_token_1;

$left_counter++;
Expand All @@ -274,6 +291,13 @@ function _eo_format_datetime_range( $datetime1, $datetime2, $format, $is_rtl = n
break;
}

//If token is indicated as representing entity that is different, split even though
//they look the same e.g. 'l' with in Saturday 2nd and Saturday 9th
//@see https://github.com/stephenharris/Event-Organiser/issues/359
if ( in_array( $tokens[$right_counter], $break_at_tokens ) ) {
break;
}

$right = $parsed_token_1 . $right;

$right_counter--;
Expand Down
20 changes: 19 additions & 1 deletion tests/unit-tests/dateFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public function testDateRangeFormat(){

$datetime1 = new DateTime( '2015-02-16' );
$datetime2 = new DateTime( '2015-03-16' );
$this->assertEquals( '16th February-March, 2015', eo_format_datetime_range( $datetime1, $datetime2, 'jS F, Y', '-' ) );
$this->assertEquals( '16th February - 16th March, 2015', eo_format_datetime_range( $datetime1, $datetime2, 'jS F, Y', ' - ' ) );

}

Expand All @@ -352,6 +352,24 @@ public function testDateRangeFormatOrdinalSuffix(){
$this->assertEquals( '16th-17th February 2015', eo_format_datetime_range( $datetime1, $datetime2, 'jS F Y', '-' ) );
}

/**
* @see https://github.com/stephenharris/Event-Organiser/issues/359
*/
public function testDateSameDateDifferentMonth() {

$datetime1 = new DateTime( '2016-04-09' );
$datetime2 = new DateTime( '2016-05-09' );
$this->assertEquals( 'Saturday, April 9–Monday, May 9, 2016', eo_format_datetime_range( $datetime1, $datetime2, 'l, F j, Y', '' ) );

$datetime1 = new DateTime( '2015-02-11' );
$datetime2 = new DateTime( '2015-03-11' );
$this->assertEquals( 'Wednesday, February 11–Wednesday, March 11, 2015', eo_format_datetime_range( $datetime1, $datetime2, 'l, F j, Y', '' ) );

$datetime1 = new DateTime( '2015-02-11' );
$datetime2 = new DateTime( '2015-02-18' );
$this->assertEquals( 'Wednesday 11th – Wednesday 18th, February', eo_format_datetime_range( $datetime1, $datetime2, 'l jS, F', '' ) );
}

public function testDateRangeFormatTime(){

$datetime1 = new DateTime( '2015-02-16 13:30:00' );
Expand Down

0 comments on commit 991350a

Please sign in to comment.