[wp-trac] [WordPress Trac] #40653: Use PHP `DateTime` class API in `current_time()`

WordPress Trac noreply at wordpress.org
Mon May 8 20:44:52 UTC 2017


#40653: Use PHP `DateTime` class API in `current_time()`
----------------------------------------+------------------------------
 Reporter:  jdgrimes                    |       Owner:
     Type:  enhancement                 |      Status:  new
 Priority:  normal                      |   Milestone:  Awaiting Review
Component:  Date/Time                   |     Version:  1.0
 Severity:  normal                      |  Resolution:
 Keywords:  has-patch needs-unit-tests  |     Focuses:
----------------------------------------+------------------------------

Comment (by jdgrimes):

 Replying to [comment:4 jdgrimes]:
 > Replying to [comment:3 Rarst]:
 > > That `Etc/*` conversion method for `gmt_offset` case is unreliable
 (won't handle half–hour cases for example).
 > >
 > > For my take on it see my `WpDateTime` lib:
 https://github.com/Rarst/wpdatetime/blob/master/php/WpDateTimeZone.php
 >
 > Thanks for pointing that out. I suspected that there was an issue there
 and was planing to take a closer look at that today. I've updated the
 patch, see [attachment:40653.diff].

 Unfortunately, this does not work on PHP versions prior to 5.5, because
 `DateTimeZone` doesn't accept offset strings in PHP 5.4 and before.

 I found [http://stackoverflow.com/a/11896631/1924128 an alternative
 solution using timezone_name_from_abbr()], but when running it over the
 offsets provided by WordPress on the General Settings screen, it doesn't
 work for all of them (and doesn't really work correctly in all cases
 anyway).

 Code used to test:

 {{{#!php
 <?php
 function convert_from_seconds( $seconds ) {

         // Get timezone name from seconds
         $tz = timezone_name_from_abbr( '', $seconds, 1 );
         // Workaround for bug #44780
         if ( $tz === false ) {
                 $tz = timezone_name_from_abbr( '', $seconds, 0 );
         }

         if ( ! $tz ) {
                 return false;
         }

         // Set timezone
         date_default_timezone_set( $tz );

         echo $tz . ': ' . date( 'r' );
         echo PHP_EOL;

         return true;
 }

 $offset_range = array (-12, -11.5, -11, -10.5, -10, -9.5, -9, -8.5, -8,
 -7.5, -7, -6.5, -6, -5.5, -5, -4.5, -4, -3.5, -3, -2.5, -2, -1.5, -1,
 -0.5,
         0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 5.75, 6, 6.5, 7,
 7.5, 8, 8.5, 8.75, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.75, 13, 13.75, 14);

 foreach ( $offset_range as $hours ) {
         echo $hours . ': ';
         if ( ! convert_from_seconds( $hours * 60 * 60 ) ) {
                 echo 'failed!' . PHP_EOL;
         }
 }

 }}}

 Output:

 {{{
 -12: failed!
 -11.5: failed!
 -11: Pacific/Apia: Tue, 09 May 2017 09:35:52 +1300
 -10.5: failed!
 -10: Pacific/Honolulu: Mon, 08 May 2017 10:35:52 -1000
 -9.5: failed!
 -9: America/Anchorage: Mon, 08 May 2017 12:35:52 -0800
 -8.5: failed!
 -8: America/Anchorage: Mon, 08 May 2017 12:35:52 -0800
 -7.5: failed!
 -7: America/Los_Angeles: Mon, 08 May 2017 13:35:52 -0700
 -6.5: failed!
 -6: America/Denver: Mon, 08 May 2017 14:35:52 -0600
 -5.5: failed!
 -5: America/Chicago: Mon, 08 May 2017 15:35:52 -0500
 -4.5: America/Caracas: Mon, 08 May 2017 16:35:52 -0400
 -4: America/New_York: Mon, 08 May 2017 16:35:52 -0400
 -3.5: failed!
 -3: America/Halifax: Mon, 08 May 2017 17:35:52 -0300
 -2.5: failed!
 -2: America/Sao_Paulo: Mon, 08 May 2017 17:35:52 -0300
 -1.5: failed!
 -1: Atlantic/Azores: Mon, 08 May 2017 20:35:52 +0000
 -0.5: failed!
 0: Atlantic/Azores: Mon, 08 May 2017 20:35:52 +0000
 0.5: failed!
 1: Europe/London: Mon, 08 May 2017 21:35:52 +0100
 1.5: failed!
 2: Europe/Paris: Mon, 08 May 2017 22:35:52 +0200
 2.5: failed!
 3: Europe/Helsinki: Mon, 08 May 2017 23:35:52 +0300
 3.5: failed!
 4: Europe/Moscow: Mon, 08 May 2017 23:35:52 +0300
 4.5: failed!
 5: Asia/Karachi: Tue, 09 May 2017 01:35:52 +0500
 5.5: Asia/Kolkata: Tue, 09 May 2017 02:05:52 +0530
 5.75: Asia/Katmandu: Tue, 09 May 2017 02:20:52 +0545
 6: Asia/Yekaterinburg: Tue, 09 May 2017 01:35:52 +0500
 6.5: failed!
 7: Asia/Novosibirsk: Tue, 09 May 2017 03:35:52 +0700
 7.5: failed!
 8: Asia/Krasnoyarsk: Tue, 09 May 2017 03:35:52 +0700
 8.5: failed!
 8.75: failed!
 9: Asia/Tokyo: Tue, 09 May 2017 05:35:52 +0900
 9.5: failed!
 10: Australia/Melbourne: Tue, 09 May 2017 06:35:52 +1000
 10.5: Australia/Adelaide: Tue, 09 May 2017 06:05:52 +0930
 11: Australia/Melbourne: Tue, 09 May 2017 06:35:52 +1000
 11.5: failed!
 12: Pacific/Auckland: Tue, 09 May 2017 08:35:52 +1200
 12.75: failed!
 13: Pacific/Auckland: Tue, 09 May 2017 08:35:52 +1200
 13.75: failed!
 14: failed!
 }}}

 '''In short, there is no way to use `DateTimeZone` with offsets in
 versions prior to PHP 5.5.'''

 I will be working on a new patch that avoids that.

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


More information about the wp-trac mailing list