WordPress.org

Make WordPress Core

Ticket #28636: 28636.6.diff

File 28636.6.diff, 3.4 KB (added by MikeHansenMe, 6 years ago)

Refreshed with basic unit tests

  • src/wp-includes/formatting.php

     
    23432343}
    23442344
    23452345/**
     2346 * Returns a formatted date in the local timezone. This is a drop-in
     2347 * replacement for `date()`, except that the returned string will be formatted
     2348 * for the local timezone.
     2349 *
     2350 * If there is a timezone_string available, the date is assumed to be in that
     2351 * timezone, otherwise it simply subtracts the value of the 'gmt_offset'
     2352 * option.
     2353 *
     2354 * @uses get_option() to retrieve the value of 'gmt_offset'.
     2355 * @param string $format The format of the outputted date string.
     2356 * @param string $timestamp Optional. If absent, defaults to `time()`.
     2357 * @return string GMT version of the date provided.
     2358 */
     2359function wp_date( $format, $timestamp = false ) {
     2360        $tz = get_option( 'timezone_string' );
     2361        if ( ! $timestamp ) {
     2362                $timestamp = time();
     2363        }
     2364        if ( $tz ) {
     2365                $date = date_create( '@' . $timestamp );
     2366                if ( ! $date ) {
     2367                        return gmdate( $format, 0 );
     2368                }
     2369                $date->setTimezone( new DateTimeZone( $tz ) );
     2370                return $date->format( $format );
     2371        } else {
     2372                return date( $format, $timestamp + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
     2373        }
     2374}
     2375
     2376/**
     2377 * Converts a locally-formatted date to a unix timestamp. This is a drop-in
     2378 * replacement for `strtotime()`, except that where strtotime assumes GMT, this
     2379 * assumes local time (as described below). If a timezone is specified, this
     2380 * function defers to strtotime().
     2381 *
     2382 * If there is a timezone_string available, the date is assumed to be in that
     2383 * timezone, otherwise it simply subtracts the value of the 'gmt_offset'
     2384 * option.
     2385 *
     2386 * @see strtotime()
     2387 * @uses get_option() to retrieve the value of 'gmt_offset'.
     2388 * @param string $string A date/time string. See `strtotime` for valid formats.
     2389 * @return int UNIX timestamp.
     2390 */
     2391function wp_strtotime( $string ) {
     2392        // If there's a timezone specified, we shouldn't convert it
     2393        try {
     2394                $test_date = new DateTime( $string );
     2395                if ( 'UTC' != $test_date->getTimezone()->getName() ) {
     2396                        return strtotime( $string );
     2397                }
     2398        } catch ( Exception $e ) {
     2399                return strtotime( $string );
     2400        }
     2401
     2402        $tz = get_option( 'timezone_string' );
     2403        if ( $tz ) {
     2404                $date = date_create( $string, new DateTimeZone( $tz ) );
     2405                if ( ! $date ) {
     2406                        return strtotime( $string );
     2407                }
     2408                $date->setTimezone( new DateTimeZone( 'UTC' ) );
     2409                return $date->getTimestamp();
     2410        } else {
     2411                return strtotime( $string ) - ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
     2412        }
     2413}
     2414
     2415/**
    23462416 * Adds a element attributes to open links in new windows.
    23472417 *
    23482418 * Comment text in popup windows should be filtered through this. Right now it's
  • tests/phpunit/tests/formatting/date.php

     
    4747                $gmt = '2012-06-01 11:34:56';
    4848                $this->assertEquals( $gmt, get_gmt_from_date( $local ) );
    4949        }
     50
     51        /**
     52         * @ticket 28636
     53         */
     54        function test_wp_dateis_string() {
     55                $format = "Y-m-d H:i:s";
     56                $result = wp_date( $format );
     57                $this->assertEquals( true, is_string( $result ) );
     58        }
     59
     60        /**
     61         * @ticket 28636
     62         */
     63        function test_wp_strtotime_is_int() {
     64                $result = wp_strtotime( 'now' );
     65                $this->assertEquals( true, is_int( $result ) );
     66        }
    5067}