Ticket #35053: 35053-2.patch
File 35053-2.patch, 16.5 KB (added by , 9 years ago) |
---|
-
src/wp-includes/class-wp-xmlrpc-server.php
3427 3427 // We know this is supposed to be GMT, so we're going to slap that Z on there by force 3428 3428 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z'; 3429 3429 $comment_date = iso8601_to_datetime( $dateCreated ); 3430 $comment_date_gmt = get_gmt_from_date( $comment_date);3430 $comment_date_gmt = iso8601_to_datetime( $dateCreated, 'gmt' ); 3431 3431 } 3432 3432 3433 3433 if ( isset($content_struct['content']) ) … … 4998 4998 } 4999 4999 5000 5000 // Do some timestamp voodoo 5001 if ( !empty( $content_struct['date_created_gmt'] ) ) 5001 if ( !empty( $content_struct['date_created_gmt'] ) ) { 5002 5002 // We know this is supposed to be GMT, so we're going to slap that Z on there by force 5003 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z'; 5004 elseif ( !empty( $content_struct['dateCreated']) ) 5005 $dateCreated = $content_struct['dateCreated']->getIso(); 5003 $dateCreated = iso8601_to_datetime( rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z' ); 5004 } elseif ( !empty( $content_struct['dateCreated']) ) { 5005 $dateCreated = iso8601_to_datetime( $content_struct['dateCreated']->getIso() ); 5006 } 5006 5007 5007 5008 if ( !empty( $dateCreated ) ) { 5008 $post_date = iso8601_to_datetime( $dateCreated );5009 $post_date = $dateCreated; 5009 5010 $post_date_gmt = get_gmt_from_date( $post_date ); 5010 5011 } else { 5011 5012 $post_date = ''; … … 5355 5356 $to_ping = implode(' ', $to_ping); 5356 5357 } 5357 5358 5358 // Do some timestamp voodoo. 5359 if ( !empty( $content_struct['date_created_gmt'] ) ) 5360 // We know this is supposed to be GMT, so we're going to slap that Z on there by force. 5361 $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z'; 5362 elseif ( !empty( $content_struct['dateCreated']) ) 5363 $dateCreated = $content_struct['dateCreated']->getIso(); 5359 // Do some timestamp voodoo 5360 if ( !empty( $content_struct['date_created_gmt'] ) ) { 5361 // We know this is supposed to be GMT, so we're going to slap that Z on there by force 5362 $dateCreated = iso8601_to_datetime( rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z' ); 5363 } elseif ( !empty( $content_struct['dateCreated']) ) { 5364 $dateCreated = iso8601_to_datetime( $content_struct['dateCreated']->getIso() ); 5365 } 5364 5366 5365 5367 if ( !empty( $dateCreated ) ) { 5366 $post_date = iso8601_to_datetime( $dateCreated );5367 $post_date_gmt = get_gmt_from_date( $post_date , 'GMT');5368 $post_date = $dateCreated; 5369 $post_date_gmt = get_gmt_from_date( $post_date ); 5368 5370 } else { 5369 5371 $post_date = $postdata['post_date']; 5370 5372 $post_date_gmt = $postdata['post_date_gmt']; -
src/wp-includes/formatting.php
2673 2673 * @return string The date and time in MySQL DateTime format - Y-m-d H:i:s. 2674 2674 */ 2675 2675 function iso8601_to_datetime( $date_string, $timezone = 'user' ) { 2676 $timezone = strtolower( $timezone);2676 $timezone = strtolower( $timezone ); 2677 2677 2678 if ($timezone == 'gmt') {2678 $tzstring = get_option( 'timezone_string' ); 2679 2679 2680 preg_match('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', $date_string, $date_bits); 2680 if ( empty( $tzstring ) ) { 2681 $tzstring = date_default_timezone_get(); 2682 } 2681 2683 2682 if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset 2683 $offset = iso8601_timezone_to_offset($date_bits[7]); 2684 } else { // we don't have a timezone, so we assume user local timezone (not server's!) 2685 $offset = HOUR_IN_SECONDS * get_option('gmt_offset'); 2686 } 2684 $tz = timezone_open( $tzstring ); 2687 2685 2688 $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]); 2689 $timestamp -= $offset; 2686 if ( false === $tz ) { 2687 return '0000-00-00 00:00:00'; 2688 } 2690 2689 2691 return gmdate('Y-m-d H:i:s', $timestamp);2690 $datetime = date_create( $date_string, $tz ); 2692 2691 2693 } elseif ($timezone == 'user') {2694 return preg_replace('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', '$1-$2-$3 $4:$5:$6', $date_string);2692 if ( false === $datetime ) { 2693 return '0000-00-00 00:00:00'; 2695 2694 } 2695 2696 if ( 'gmt' === $timezone ) { 2697 date_timezone_set( $datetime, timezone_open( 'UTC' ) ); 2698 } else { 2699 date_timezone_set( $datetime, $tz ); 2700 } 2701 2702 return date_format( $datetime, 'Y-m-d H:i:s' ); 2696 2703 } 2697 2704 2698 2705 /** -
tests/phpunit/tests/formatting/DateTime.php
1 <?php 2 3 /** 4 * @group formatting 5 */ 6 class Tests_Formatting_DateTime extends WP_UnitTestCase { 7 8 /** 9 * @ticket 35053 10 */ 11 function test_bad_timezone_strings() { 12 $tz = get_option( 'timezone_string' ); 13 14 try { 15 16 update_option( 'timezone_string', '' ); 17 $this->assertEquals( '1984-01-11 05:00:00', iso8601_to_datetime( '1984-01-11T05:00:00', 'gmt' ) ); 18 19 update_option( 'timezone_string', 'garbage' ); // will get set to empty string 20 $this->assertEquals( '1984-01-11 05:00:00', iso8601_to_datetime( '1984-01-11T05:00:00', 'gmt' ) ); 21 22 } catch ( Exception $e ) {} 23 24 update_option( 'timezone_string', $tz ); 25 26 // no 'finally' until PHP 5.5+ 27 if ( isset( $e ) ) { 28 throw $e; 29 } 30 } 31 32 /** 33 * @ticket 35053 34 */ 35 function test_invalid_dates() { 36 $tz = get_option( 'timezone_string' ); 37 update_option( 'timezone_string', 'UTC' ); 38 39 try { 40 41 $this->assertEquals( '-0001-11-30 00:00:00', iso8601_to_datetime( '0000-00-00T00:00:00Z' ), 'all zeroes' ); 42 $this->assertEquals( '0000-00-00 00:00:00', iso8601_to_datetime( '1984-01-11T05:00:00+XYZ' ), 'invalid TZ' ); 43 44 } catch ( Exception $e ) {} 45 46 update_option( 'timezone_string', $tz ); 47 48 // no 'finally' until PHP 5.5+ 49 if ( isset( $e ) ) { 50 throw $e; 51 } 52 } 53 54 function test_utc() { 55 $tz = get_option( 'timezone_string' ); 56 update_option( 'timezone_string', 'UTC' ); 57 58 $date_string = '1984-01-11 05:00:00'; 59 60 try { 61 62 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T05:00:00Z', 'gmt' ), 'Z, gmt' ); 63 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T03:00:00-0200', 'gmt' ), '-0200, gmt' ); 64 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T04:00:00-0100', 'gmt' ), '-0100, gmt' ); 65 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T05:00:00+0000', 'gmt' ), '+0000, gmt' ); 66 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T06:00:00+0100', 'gmt' ), '+0100, gmt' ); 67 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T07:00:00+0200', 'gmt' ), '+0200, gmt' ); 68 69 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T05:00:00Z', 'user' ), 'Z, user' ); 70 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T03:00:00-0200', 'user' ), '-0200, user' ); 71 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T04:00:00-0100', 'user' ), '-0100, user' ); 72 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T05:00:00+0000', 'user' ), '+0000, user' ); 73 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T06:00:00+0100', 'user' ), '+0100, user' ); 74 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T07:00:00+0200', 'user' ), '+0200, user' ); 75 76 } catch ( Exception $e ) {} 77 78 update_option( 'timezone_string', $tz ); 79 80 // no 'finally' until PHP 5.5+ 81 if ( isset( $e ) ) { 82 throw $e; 83 } 84 } 85 86 /** 87 * @ticket 35053 88 */ 89 function test_america_newyork() { 90 // America/New_York is UTC-05:00 (UTC-04:00 DST) 91 $tz = get_option( 'timezone_string' ); 92 update_option( 'timezone_string', 'America/New_York' ); 93 94 $date_string = '1984-01-11 05:00:00'; 95 96 try { 97 98 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T05:00:00Z', 'gmt' ), 'Z, gmt' ); 99 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T03:00:00-0200', 'gmt' ), '-0200, gmt' ); 100 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T04:00:00-0100', 'gmt' ), '-0100, gmt' ); 101 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T05:00:00+0000', 'gmt' ), '+0000, gmt' ); 102 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T06:00:00+0100', 'gmt' ), '+0100, gmt' ); 103 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T07:00:00+0200', 'gmt' ), '+0200, gmt' ); 104 105 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T10:00:00Z', 'user' ), 'Z, user' ); 106 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T08:00:00-0200', 'user' ), '-0200, user' ); 107 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T09:00:00-0100', 'user' ), '-0100, user' ); 108 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T10:00:00+0000', 'user' ), '+0000, user' ); 109 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T11:00:00+0100', 'user' ), '+0100, user' ); 110 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T12:00:00+0200', 'user' ), '+0200, user' ); 111 112 } catch ( Exception $e ) {} 113 114 update_option( 'timezone_string', $tz ); 115 116 // no 'finally' until PHP 5.5+ 117 if ( isset( $e ) ) { 118 throw $e; 119 } 120 } 121 122 /** 123 * @ticket 35053 124 */ 125 function test_africa_johannesburg() { 126 // Africa/Johannesburg is UTC+02:00 127 $tz = get_option( 'timezone_string' ); 128 update_option( 'timezone_string', 'Africa/Johannesburg' ); 129 130 $date_string = '1984-01-11 05:00:00'; 131 132 try { 133 134 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T05:00:00Z', 'gmt' ), 'Z, gmt' ); 135 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T03:00:00-0200', 'gmt' ), '-0200, gmt' ); 136 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T04:00:00-0100', 'gmt' ), '-0100, gmt' ); 137 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T05:00:00+0000', 'gmt' ), '+0000, gmt' ); 138 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T06:00:00+0100', 'gmt' ), '+0100, gmt' ); 139 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T07:00:00+0200', 'gmt' ), '+0200, gmt' ); 140 141 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T03:00:00Z', 'user' ), 'Z, user' ); 142 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T01:00:00-0200', 'user' ), '-0200, user' ); 143 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T02:00:00-0100', 'user' ), '-0100, user' ); 144 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T03:00:00+0000', 'user' ), '+0000, user' ); 145 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T04:00:00+0100', 'user' ), '+0100, user' ); 146 $this->assertEquals( $date_string, iso8601_to_datetime( '19840111T05:00:00+0200', 'user' ), '+0200, user' ); 147 148 } catch ( Exception $e ) {} 149 150 update_option( 'timezone_string', $tz ); 151 152 // no 'finally' until PHP 5.5+ 153 if ( isset( $e ) ) { 154 throw $e; 155 } 156 } 157 158 } -
tests/phpunit/tests/xmlrpc/mw/editPost.php
Property changes on: tests/phpunit/tests/formatting/DateTime.php ___________________________________________________________________ Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property
246 246 } 247 247 248 248 /** 249 * @ticket 35053 250 */ 251 function test_post_date_dateCreated_gmt_timezone_conversion() { 252 $tz = get_option( 'timezone_string' ); 253 update_option( 'timezone_string', 'America/New_York' ); 254 255 $editor_id = $this->make_user_by_role( 'editor' ); 256 257 $post_id = self::factory()->post->create( array( 258 'post_author' => $editor_id 259 ) ); 260 261 $date_string = '1984-01-11 05:00:00'; 262 // America/New_York's time zone is -5:00 so add 5 hours 263 $date_string_gmt = '1984-01-11 10:00:00'; 264 265 $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'editor', 'editor', array( 266 'dateCreated' => new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date_string_gmt, false ) . 'Z' ), 267 ) ) ); 268 269 $fetched_post = get_post( $post_id ); 270 271 update_option( 'timezone_string', $tz ); 272 273 $this->assertTrue( $result ); 274 $this->assertEquals( $date_string, $fetched_post->post_date ); 275 } 276 277 /** 278 * @ticket 35053 279 */ 280 function test_post_date_gmt_timezone_conversion() { 281 $tz = get_option( 'timezone_string' ); 282 update_option( 'timezone_string', 'America/New_York' ); 283 284 $editor_id = $this->make_user_by_role( 'editor' ); 285 286 $post_id = self::factory()->post->create( array( 287 'post_author' => $editor_id 288 ) ); 289 290 $date_string = '1984-01-11 05:00:00'; 291 // America/New_York's time zone is -5:00 so add 5 hours 292 $date_string_gmt = '1984-01-11 10:00:00'; 293 294 $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'editor', 'editor', array( 295 'date_created_gmt' => new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date_string_gmt, false ) ), 296 ) ) ); 297 298 $fetched_post = get_post( $post_id ); 299 300 update_option( 'timezone_string', $tz ); 301 302 $this->assertTrue( $result ); 303 $this->assertEquals( $date_string, $fetched_post->post_date ); 304 } 305 306 /** 249 307 * @ticket 16980 250 308 */ 251 309 function test_empty_not_null() { -
tests/phpunit/tests/xmlrpc/mw/newPost.php
193 193 $this->assertStringMatchesFormat( '%d', $result ); 194 194 $this->assertEquals( $date_string , $fetched_post->post_date ); 195 195 } 196 197 /** 198 * @ticket 35053 199 */ 200 function test_post_date_dateCreated_gmt_timezone_conversion() { 201 $tz = get_option( 'timezone_string' ); 202 update_option( 'timezone_string', 'America/New_York' ); 203 204 $this->make_user_by_role( 'editor' ); 205 $date_string = '1984-01-11 05:00:00'; 206 // America/New_York's time zone is -5:00 so add 5 hours 207 $date_string_gmt = '1984-01-11 10:00:00'; 208 $post = array( 209 'title' => 'test', 210 'post_content' => 'test', 211 'dateCreated' => new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date_string_gmt, false ) . 'Z' ) 212 ); 213 $result = $this->myxmlrpcserver->mw_newPost( array( 1, 'editor', 'editor', $post ) ); 214 $fetched_post = get_post( $result ); 215 216 update_option( 'timezone_string', $tz ); 217 218 $this->assertStringMatchesFormat( '%d', $result ); 219 $this->assertEquals( $date_string , $fetched_post->post_date ); 220 } 221 222 /** 223 * @ticket 35053 224 */ 225 function test_post_date_gmt_timezone_conversion() { 226 $tz = get_option( 'timezone_string' ); 227 update_option( 'timezone_string', 'America/New_York' ); 228 229 $this->make_user_by_role( 'editor' ); 230 $date_string = '1984-01-11 05:00:00'; 231 // America/New_York's time zone is -5:00 so add 5 hours 232 $date_string_gmt = '1984-01-11 10:00:00'; 233 $post = array( 234 'title' => 'test', 235 'post_content' => 'test', 236 'date_created_gmt' => new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date_string_gmt, false ) ) 237 ); 238 $result = $this->myxmlrpcserver->mw_newPost( array( 1, 'editor', 'editor', $post ) ); 239 $fetched_post = get_post( $result ); 240 241 update_option( 'timezone_string', $tz ); 242 243 $this->assertStringMatchesFormat( '%d', $result ); 244 $this->assertEquals( $date_string , $fetched_post->post_date ); 245 } 246 196 247 } -
tests/phpunit/tests/xmlrpc/wp/editComment.php
91 91 $comment_id = wp_insert_comment( $comment_data ); 92 92 93 93 $date_string = '1984-01-11 05:00:00'; 94 // America/New_York's time zone is -5:00 so add 5 hours 95 $date_string_gmt = '1984-01-11 10:00:00'; 94 96 $result = $this->myxmlrpcserver->wp_editComment( array( 1, 'administrator', 'administrator', $comment_id, array( 95 'date_created_gmt' => new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date_string , false ) )97 'date_created_gmt' => new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date_string_gmt, false ) ) 96 98 ) ) ); 97 99 $fetched_comment = get_comment( $comment_id ); 98 100 … … 101 103 $this->assertTrue( $result ); 102 104 $this->assertEquals( $date_string, $fetched_comment->comment_date ); 103 105 } 104 } 105 No newline at end of file 106 }