WordPress.org

Make WordPress Core

Ticket #28636: 28636.3.diff

File 28636.3.diff, 3.4 KB (added by mboynes, 6 years ago)

Leverage DateTime to parse the timezone from the string

  • wp-includes/formatting.php

     
    147147                $dynamic_characters['apos'] = array_keys( $dynamic );
    148148                $dynamic_replacements['apos'] = array_values( $dynamic );
    149149                $dynamic = array();
    150                
     150
    151151                // Quoted Numbers like "42"
    152152                if ( '"' !== $opening_quote && '"' !== $closing_quote ) {
    153153                        $dynamic[ '/(?<=\A|' . $spaces . ')"(\d[.,\d]*)"/' ] = $opening_quote . '$1' . $closing_quote;
     
    167167                if ( '"' !== $closing_quote ) {
    168168                        $dynamic[ '/"/' ] = $closing_quote;
    169169                }
    170                
     170
    171171                $dynamic_characters['quote'] = array_keys( $dynamic );
    172172                $dynamic_replacements['quote'] = array_values( $dynamic );
    173173                $dynamic = array();
    174                
     174
    175175                // Dashes and spaces
    176176                $dynamic[ '/---/' ] = $em_dash;
    177177                $dynamic[ '/(?<=' . $spaces . ')--(?=' . $spaces . ')/' ] = $em_dash;
     
    23272327}
    23282328
    23292329/**
     2330 * Returns a formatted date in the local timezone. This is a drop-in
     2331 * replacement for `date()`, except that the returned string will be formatted
     2332 * for the local timezone.
     2333 *
     2334 * If there is a timezone_string available, the date is assumed to be in that
     2335 * timezone, otherwise it simply subtracts the value of the 'gmt_offset'
     2336 * option.
     2337 *
     2338 * @uses get_option() to retrieve the value of 'gmt_offset'.
     2339 * @param string $format The format of the outputted date string.
     2340 * @param string $timestamp Optional. If absent, defaults to `time()`.
     2341 * @return string GMT version of the date provided.
     2342 */
     2343function wp_date( $format, $timestamp = false ) {
     2344        $tz = get_option( 'timezone_string' );
     2345        if ( ! $timestamp ) {
     2346                $timestamp = time();
     2347        }
     2348        if ( $tz ) {
     2349                $date = date_create( "@$timestamp" );
     2350                if ( ! $date ) {
     2351                        return gmdate( $format, 0 );
     2352                }
     2353                $date->setTimezone( new DateTimeZone( $tz ) );
     2354                return $date->format( $format );
     2355        } else {
     2356                return date( $format, $timestamp + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
     2357        }
     2358}
     2359
     2360/**
     2361 * Converts a locally-formatted date to a unix timestamp. This is a drop-in
     2362 * replacement for `strtotime()`, except that where strtotime assumes GMT, this
     2363 * assumes local time (as described below). If a timezone is specified, this
     2364 * function defers to strtotime().
     2365 *
     2366 * If there is a timezone_string available, the date is assumed to be in that
     2367 * timezone, otherwise it simply subtracts the value of the 'gmt_offset'
     2368 * option.
     2369 *
     2370 * @see strtotime()
     2371 * @uses get_option() to retrieve the value of 'gmt_offset'.
     2372 * @param string $string A date/time string. See `strtotime` for valid formats.
     2373 * @return int UNIX timestamp.
     2374 */
     2375function wp_strtotime( $string ) {
     2376        // If there's a timezone specified, we shouldn't convert it
     2377        try {
     2378                $test_date = new DateTime( $string );
     2379                if ( 'UTC' != $test_date->getTimezone()->getName() ) {
     2380                        return strtotime( $string );
     2381                }
     2382        } catch ( Exception $e ) {
     2383                return strtotime( $string );
     2384        }
     2385
     2386        $tz = get_option( 'timezone_string' );
     2387        if ( $tz ) {
     2388                $date = date_create( $string, new DateTimeZone( $tz ) );
     2389                if ( ! $date ) {
     2390                        return strtotime( $string );
     2391                }
     2392                $date->setTimezone( new DateTimeZone( 'UTC' ) );
     2393                return $date->getTimestamp();
     2394        } else {
     2395                return strtotime( $string ) - ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
     2396        }
     2397}
     2398
     2399/**
    23302400 * Adds a element attributes to open links in new windows.
    23312401 *
    23322402 * Comment text in popup windows should be filtered through this. Right now it's