Skip to:
Content

BuddyPress.org

Changeset 8676


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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-functions.php

    r8579 r8676  
    772772 *
    773773 * @param bool $gmt True to use GMT (rather than local) time. Default: true.
     774 * @param string $type See the 'type' parameter in {@link current_time()}.
     775          Default: 'mysql'.
    774776 * @return string Current time in 'Y-m-d h:i:s' format.
    775777 */
    776 function bp_core_current_time( $gmt = true ) {
    777         // Get current time in MYSQL format
    778         $current_time = current_time( 'mysql', $gmt );
     778function bp_core_current_time( $gmt = true, $type = 'mysql' ) {
     779        // Get current time
     780        $current_time = current_time( $type, $gmt );
    779781
    780782        return apply_filters( 'bp_core_current_time', $current_time );
     
    841843         * work out time elapsed between two known dates.
    842844         */
    843         $newer_date = ( !$newer_date ) ? strtotime( bp_core_current_time() ) : $newer_date;
     845        $newer_date = ( !$newer_date ) ? bp_core_current_time( true, 'timestamp' ) : $newer_date;
    844846
    845847        // Difference in seconds
  • 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.