Make WordPress Core

Ticket #40653: 40653.diff

File 40653.diff, 2.4 KB (added by jdgrimes, 8 years ago)

Fixes method of converting offsets to timezone strings

  • src/wp-includes/functions.php

     
    5959 * @return int|string Integer if $type is 'timestamp', string otherwise.
    6060 */
    6161function current_time( $type, $gmt = 0 ) {
     62        // Special handling for legacy offset timestamps.
     63        if ( 'timestamp' === $type && ! $gmt ) {
     64                return time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
     65        }
     66
    6267        switch ( $type ) {
    6368                case 'mysql':
    64                         return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ) );
     69                        $format ='Y-m-d H:i:s';
    6570                case 'timestamp':
    66                         return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
     71                        $format = 'U';
    6772                default:
    68                         return ( $gmt ) ? date( $type ) : date( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
     73                        $format = $type;
    6974        }
     75
     76        if ( $gmt ) {
     77                $timezone = timezone_open( 'UTC' );
     78        } else {
     79                $timezone = wp_get_site_timezone();
     80        }
     81
     82        $datetime = date_create( 'now', $timezone );
     83        return $datetime->format( $format );
    7084}
    7185
    7286/**
     87 * Creates a `DateTimeZone` object for the site's timezone.
     88 *
     89 * This function determines the site timezone as follows:
     90 *
     91 * - If the site uses a timezone identifier (i.e., 'timezone_string' option is set),
     92 *   that is used.
     93 * - If that's not set, we make up an identifier based on the 'gmt_offset'.
     94 * - If the GMT offset is 0, or the identifier is invalid, UTC is used.
     95 *
     96 * @see https://wordpress.stackexchange.com/a/198453/27757
     97 * @see https://us.php.net/manual/en/timezones.others.php
     98 *
     99 * @since 4.8.0
     100 *
     101 * @return DateTimeZone The site's timezone.
     102 */
     103function wp_get_site_timezone() {
     104        $timezone_string = get_option( 'timezone_string' );
     105
     106        // A direct offset may be used instead of a timezone identifier.
     107        if ( empty( $timezone_string ) ) {
     108                $offset = get_option( 'gmt_offset' );
     109
     110                if ( empty( $offset ) ) {
     111                        $timezone_string = 'UTC';
     112                } else {
     113                        $hours   = (int) $offset;
     114                        $minutes = ( $offset - floor( $offset ) ) * 60;
     115                        $timezone_string = sprintf( '%+03d:%02d', $hours, $minutes );
     116                }
     117        }
     118
     119        return new DateTimeZone( $timezone_string );
     120}
     121
     122/**
    73123 * Retrieve the date in localized format, based on timestamp.
    74124 *
    75125 * If the locale specifies the locale month and weekday, then the locale will