Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/23/2014 06:07:39 PM (12 years ago)
Author:
r-a-y
Message:

Fix issues with bp_core_time_since().

Previously, when the $newer_date parameter isn't passed, we generated a
UNIX timestamp using strtotime() and bp_core_current_time(). strtotime()
uses the default timezone, which can cause timezone discrepencies if the
server time is not UTC.

This commit removes the strtotime() usage and refactors bp_core_current_time()
to accept current_time()'s 'type' parameter. This allows us to correctly
grab the UTC timestamp when setting bp_core_current_time()'s second
argument to 'timestamp'.

Commit also adds some unit tests.

Fixes #4310.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/core/functions.php

    r8323 r8676  
    346346        $this->assertNotEmpty( buddypress()->loaded_components['foo'] );
    347347    }
     348
     349    /**
     350     * @group bp_core_time_since
     351     * @group bp_core_current_time
     352     */
     353    public function test_bp_core_time_since_timezone_right_now() {
     354        // backup timezone
     355        $tz_backup = date_default_timezone_get();
     356
     357        // set timezone to something other than UTC
     358        date_default_timezone_set( 'Europe/Paris' );
     359
     360        $this->assertSame( 'right now', bp_core_time_since( time() ) );
     361
     362        // revert timezone back to normal
     363        if ( $tz_backup ) {
     364            date_default_timezone_set( $tz_backup );
     365        }
     366    }
     367
     368    /**
     369     * @group bp_core_time_since
     370     * @group bp_core_current_time
     371     */
     372    public function test_bp_core_time_since_timezone() {
     373        // backup timezone
     374        $tz_backup = date_default_timezone_get();
     375
     376        // set timezone to something other than UTC
     377        date_default_timezone_set( 'Europe/Paris' );
     378
     379        $this->assertSame( '1 hour ago', bp_core_time_since( time() - 60*60 ) );
     380
     381        // revert timezone back to normal
     382        if ( $tz_backup ) {
     383            date_default_timezone_set( $tz_backup );
     384        }
     385    }
    348386}
Note: See TracChangeset for help on using the changeset viewer.