[wp-trac] [WordPress Trac] #49038: Timezone setting does not display correct time of next DST transition

WordPress Trac noreply at wordpress.org
Sun Dec 22 16:13:45 UTC 2019


#49038: Timezone setting does not display correct time of next DST transition
--------------------------+-----------------------
 Reporter:  autotutorial  |       Owner:  Rarst
     Type:  defect (bug)  |      Status:  assigned
 Priority:  normal        |   Milestone:  5.3.3
Component:  Date/Time     |     Version:  5.3
 Severity:  normal        |  Resolution:
 Keywords:  has-patch     |     Focuses:
--------------------------+-----------------------

Comment (by autotutorial):

 The bug has changed from the DateTime component to the logic structured in
 options-general.php.
 the syntax UTC, Etc/GMT or any other synonym that has an offset 0 is
 considered a manual offset and in this case a manual offset regardless of
 its value does not have a Daylight saving time (old list timezone
 https://www.php.net/manual/en/timezones.others.php ).
 I converted the page to a WordPress 5.3 syntax, where thanks to the
 creator of wp_timezone it was possible to apply other improvements.
 In this example I removed the redundant calls to timezone_string and
 stored the value in a container variable since the value is the one
 previously recovered from general settings it is useful to keep the value
 for later use if the context remains to the WordPress core, more
 specifically it works only in that page context and will not be used for
 other pages.
 I added wp_date with the timezone object created by wp_timezone or UTC, or
 UTC arithmetic operation, set UTC if $tzstring has an offset equal to 0, I
 added two unset to the wp_timezone_choice function ( use
 timezone_identifiers_list ),  if possible I would like to avoid
 timezone_identifiers_list() if the timezone is in the list you should
 retrieve the value set from the menu, added restore array in
 $allowed_zones to free up php memory
 {{{#!php
 <?php
 if ( empty( $tzstring ) ) { // Create a UTC+- zone if no timezone string
 exists
         $check_zone_info = false;
         if ( 0 == $current_offset ) {
                 $tzstring = 'UTC+0';
         } elseif ( $current_offset < 0 ) {
                 $tzstring = 'UTC' . $current_offset;
         } else {
                 $tzstring = 'UTC+' . $current_offset;
         }
 } elseif ( 0 == $current_offset ) {
     $check_zone_info = false;
     $tzstring = 'UTC';
 }

 ?>
 <th scope="row"><label for="timezone_string"><?php _e( 'Timezone' );
 ?></label></th>
 <td>

 <select id="timezone_string" name="timezone_string" aria-describedby
 ="timezone-description">
         <?php echo wp_timezone_choice( $tzstring, get_user_locale() ); ?>
 </select>

 <p class="description" id="timezone-description">
 <?php
         printf(
                 /* translators: %s: UTC abbreviation */
                 __( 'Choose either a city in the same timezone as you or a
 %s (Coordinated Universal Time) time offset.' ),
                 '<abbr>UTC</abbr>'
         );
         ?>
 </p>

 <p class="timezone-info">
         <span id="utc-time">
         <?php
                 printf(
                         /* translators: %s: UTC time. */
                         __( 'Universal time is %s.' ),
                         '<code>' . wp_date( $timezone_format, null, new
 DateTimeZone( 'UTC' ) ) . '</code>'
                 );
                 ?>
         </span>
 <?php if ( $current_offset ) : ?>
         <span id="local-time">
         <?php
         $timezone = wp_timezone();
                 printf(
                         /* translators: %s: Local time. */
                         __( 'Local time is %s.' ),
                         '<code>' . wp_date( $timezone_format, null,
 $timezone ) . '</code>'
                 );
         ?>
         </span>
 </p>
 <?php endif; ?>

 <?php if ( $check_zone_info && $current_offset ) : ?>
 <p class="timezone-info">
 <span>
         <?php
         $now = new DateTime( 'now', $timezone );
         $dst = (bool) $now->format( 'I' );

         if ( $dst ) {
                 _e( 'This timezone is currently in daylight saving time.'
 );
         } else {
                 _e( 'This timezone is currently in standard time.' );
         }
         ?>
         <br />
         <?php
     //timezone_identifiers_list Avoid if possible
         $allowed_zones = timezone_identifiers_list();
         if ( in_array( $tzstring, $allowed_zones ) ) {
                                 $transitions = $timezone->getTransitions(
 time(), time() + YEAR_IN_SECONDS );

                                 if ( ! empty( $transitions[1] ) ) {
                 echo ' ';
                             // Create timestamp UTC +- for display
 Daylight saving time or Standard time.
                             $utc = ( $transitions[0]['offset'] ) > 0 ?
                             $transitions[1]['ts'] +
 $transitions[0]['offset'] :
                             $transitions[1]['ts'] -
 $transitions[0]['offset'];
                                 $message = $transitions[1]['isdst'] ?
                                 /* translators: %s: Date and time. */
                                 __( 'Daylight saving time begins on: %s.'
 ) :
                                 /* translators: %s: Date and time. */
                                 __( 'Standard time begins on: %s.' );
                         printf(
                                 $message,
                                                                 '<code>' .
 wp_date( __( 'F j, Y' ) . ' ' . __( 'g:i a' ), $utc, new DateTimeZone(
 'UTC' ) ) . '</code>'
                         );
                 } else {
                         _e( 'This timezone does not observe daylight
 saving time.' );
                 }
         }
 //You now, not have 467 array in memory
 $allowed_zones = array( $tzstring );
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/49038#comment:15>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list