Make WordPress Core

Ticket #24730: wp-timezone-simple-date-i18n.diff

File wp-timezone-simple-date-i18n.diff, 4.8 KB (added by remcotolsma, 7 years ago)

Introduced wp_timezone() function, simplified date_i18n() function.

  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index 3c4a301298..46d0c3b536 100644
    function current_time( $type, $gmt = 0 ) { 
    7373        }
    7474}
    7575
     76/**
     77 * Retrieve timezone.
     78 *
     79 * @return DateTimeZone
     80 */
     81function wp_timezone() {
     82        $timezone_string = get_option( 'timezone_string' );
     83       
     84        if ( ! empty( $timezone_string ) ) {
     85                return new DateTimeZone( $timezone_string );
     86        }
     87       
     88        $gmt_offset = get_option( 'gmt_offset' );
     89        $hours      = (int) $gmt_offset;
     90        $minutes    = abs( ( $gmt_offset - (int) $gmt_offset ) * 60 );
     91        $offset     = sprintf( '%+03d:%02d', $hours, $minutes );
     92       
     93        /**
     94         * Offset values as timezone parameter are supported since PHP 5.5.10.
     95         *
     96         * @link http://php.net/manual/en/datetimezone.construct.php
     97         */
     98        if ( version_compare( PHP_VERSION, '5.5.10', '<' ) ) {
     99                $date = new DateTime( $offset );
     100
     101                return $date->getTimezone();
     102        }
     103
     104        return new DateTimeZone( $offset );
     105}
     106
    76107/**
    77108 * Retrieve the date in localized format, based on a sum of Unix timestamp and
    78109 * timezone offset in seconds.
    function date_i18n( $dateformatstring, $timestamp_with_offset = false, $gmt = fa 
    106137         */
    107138        $req_format = $dateformatstring;
    108139
     140        $datetime = date_create( date( 'Y-m-d H:i:s', $timestamp_with_offset ), wp_timezone() );
     141       
     142        if ( false === $datetime ) {
     143                return false;
     144        }
     145
    109146        if ( ( ! empty( $wp_locale->month ) ) && ( ! empty( $wp_locale->weekday ) ) ) {
    110                 $datemonth            = $wp_locale->get_month( date( 'm', $i ) );
    111                 $datemonth_abbrev     = $wp_locale->get_month_abbrev( $datemonth );
    112                 $dateweekday          = $wp_locale->get_weekday( date( 'w', $i ) );
    113                 $dateweekday_abbrev   = $wp_locale->get_weekday_abbrev( $dateweekday );
    114                 $datemeridiem         = $wp_locale->get_meridiem( date( 'a', $i ) );
    115                 $datemeridiem_capital = $wp_locale->get_meridiem( date( 'A', $i ) );
    116                 $dateformatstring     = ' ' . $dateformatstring;
    117                 $dateformatstring     = preg_replace( '/([^\\\])D/', "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
    118                 $dateformatstring     = preg_replace( '/([^\\\])F/', "\\1" . backslashit( $datemonth ), $dateformatstring );
    119                 $dateformatstring     = preg_replace( '/([^\\\])l/', "\\1" . backslashit( $dateweekday ), $dateformatstring );
    120                 $dateformatstring     = preg_replace( '/([^\\\])M/', "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
    121                 $dateformatstring     = preg_replace( '/([^\\\])a/', "\\1" . backslashit( $datemeridiem ), $dateformatstring );
    122                 $dateformatstring     = preg_replace( '/([^\\\])A/', "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );
    123 
    124                 $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) - 1 );
    125         }
    126         $timezone_formats    = array( 'P', 'I', 'O', 'T', 'Z', 'e' );
    127         $timezone_formats_re = implode( '|', $timezone_formats );
    128         if ( preg_match( "/$timezone_formats_re/", $dateformatstring ) ) {
    129                 $timezone_string = get_option( 'timezone_string' );
    130                 if ( $timezone_string ) {
    131                         $timezone_object = timezone_open( $timezone_string );
    132                         $date_object     = date_create( null, $timezone_object );
    133                         foreach ( $timezone_formats as $timezone_format ) {
    134                                 if ( false !== strpos( $dateformatstring, $timezone_format ) ) {
    135                                         $formatted        = date_format( $date_object, $timezone_format );
    136                                         $dateformatstring = ' ' . $dateformatstring;
    137                                         $dateformatstring = preg_replace( "/([^\\\])$timezone_format/", "\\1" . backslashit( $formatted ), $dateformatstring );
    138                                         $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) - 1 );
    139                                 }
     147                $month   = $wp_locale->get_month( $datetime->format( 'm' ) );
     148                $weekday = $wp_locale->get_weekday( $datetime->format( 'w' ) );
     149
     150                $format_length = strlen( $req_format );
     151
     152                $new_format = '';
     153
     154                for ( $i = 0; $i < $format_length; $i++ ) {
     155                        switch ( $req_format[ $i ] ) {
     156                                case 'D':
     157                                        $new_format .= backslashit( $wp_locale->get_weekday_abbrev( $weekday ) );
     158                                        break;
     159                                case 'F':
     160                                        $new_format .= backslashit( $month );
     161                                        break;
     162                                case 'l':
     163                                        $new_format .= backslashit( $weekday );
     164                                        break;
     165                                case 'M':
     166                                        $new_format .= backslashit( $wp_locale->get_month_abbrev( $month ) );
     167                                        break;
     168                                case 'a':
     169                                        $new_format .= backslashit( $wp_locale->get_meridiem( $datetime->format( 'a' ) ) );
     170                                        break;
     171                                case 'A':
     172                                        $new_format .= backslashit( $wp_locale->get_meridiem( $datetime->format( 'A' ) ) );
     173                                        break;
     174                                case '\\':
     175                                        $new_format .= $format[ $i ];
     176
     177                                        if ( $i < $format_length ) {
     178                                                $i++;
     179                                        }
     180                                        // no break
     181                                default:
     182                                        $new_format .= $format[ $i ];
     183                                        break;
    140184                        }
    141185                }
    142186        }
    143         $j = @date( $dateformatstring, $i );
     187
     188        $j = $datetime->format( $new_format );
    144189
    145190        /**
    146191         * Filters the date formatted based on the locale.