comparison core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
328 // On 32-bit systems, timestamps are limited to 1901-2038. 328 // On 32-bit systems, timestamps are limited to 1901-2038.
329 if (PHP_INT_SIZE > 4) { 329 if (PHP_INT_SIZE > 4) {
330 // Create a date object in the distant past. 330 // Create a date object in the distant past.
331 // @see https://www.drupal.org/node/2795489#comment-12127088 331 // @see https://www.drupal.org/node/2795489#comment-12127088
332 if (version_compare(PHP_VERSION, '5.6.15', '>=')) { 332 if (version_compare(PHP_VERSION, '5.6.15', '>=')) {
333 $dates[] = ['1809-02-12 10:30', 'America/Chicago', '1809-02-12T10:30:00-06:00']; 333 // Note that this date is after the United States standardized its
334 // timezones.
335 $dates[] = ['1883-11-19 10:30', 'America/Chicago', '1883-11-19T10:30:00-06:00'];
334 } 336 }
335 // Create a date object in the far future. 337 // Create a date object in the far future.
336 $dates[] = ['2345-01-02 02:04', 'UTC', '2345-01-02T02:04:00+00:00']; 338 $dates[] = ['2345-01-02 02:04', 'UTC', '2345-01-02T02:04:00+00:00'];
337 } 339 }
338 340
364 // On 32-bit systems, timestamps are limited to 1901-2038. 366 // On 32-bit systems, timestamps are limited to 1901-2038.
365 if (PHP_INT_SIZE > 4) { 367 if (PHP_INT_SIZE > 4) {
366 // Create a date object in the distant past. 368 // Create a date object in the distant past.
367 // @see https://www.drupal.org/node/2795489#comment-12127088 369 // @see https://www.drupal.org/node/2795489#comment-12127088
368 if (version_compare(PHP_VERSION, '5.6.15', '>=')) { 370 if (version_compare(PHP_VERSION, '5.6.15', '>=')) {
369 $dates[] = [['year' => 1809, 'month' => 2, 'day' => 12], 'America/Chicago', '1809-02-12T00:00:00-06:00']; 371 // Note that this date is after the United States standardized its
372 // timezones.
373 $dates[] = [['year' => 1883, 'month' => 11, 'day' => 19], 'America/Chicago', '1883-11-19T00:00:00-06:00'];
370 } 374 }
371 // Create a date object in the far future. 375 // Create a date object in the far future.
372 $dates[] = [['year' => 2345, 'month' => 1, 'day' => 2], 'UTC', '2345-01-02T00:00:00+00:00']; 376 $dates[] = [['year' => 2345, 'month' => 1, 'day' => 2], 'UTC', '2345-01-02T00:00:00+00:00'];
373 } 377 }
374 378
805 * Tests the $settings['validate_format'] parameter in ::createFromFormat(). 809 * Tests the $settings['validate_format'] parameter in ::createFromFormat().
806 */ 810 */
807 public function testValidateFormat() { 811 public function testValidateFormat() {
808 // Check that an input that does not strictly follow the input format will 812 // Check that an input that does not strictly follow the input format will
809 // produce the desired date. In this case the year string '11' doesn't 813 // produce the desired date. In this case the year string '11' doesn't
810 // precisely match the 'Y' formater parameter, but PHP will parse it 814 // precisely match the 'Y' formatter parameter, but PHP will parse it
811 // regardless. However, when formatted with the same string, the year will 815 // regardless. However, when formatted with the same string, the year will
812 // be output with four digits. With the ['validate_format' => FALSE] 816 // be output with four digits. With the ['validate_format' => FALSE]
813 // $settings, this will not thrown an exception. 817 // $settings, this will not thrown an exception.
814 $date = DateTimePlus::createFromFormat('Y-m-d H:i:s', '11-03-31 17:44:00', 'UTC', ['validate_format' => FALSE]); 818 $date = DateTimePlus::createFromFormat('Y-m-d H:i:s', '11-03-31 17:44:00', 'UTC', ['validate_format' => FALSE]);
815 $this->assertEquals('0011-03-31 17:44:00', $date->format('Y-m-d H:i:s')); 819 $this->assertEquals('0011-03-31 17:44:00', $date->format('Y-m-d H:i:s'));
889 } 893 }
890 $date = new DateTimePlus('now', 'Australia/Sydney'); 894 $date = new DateTimePlus('now', 'Australia/Sydney');
891 $date->setTimezone(new \DateTimeZone('America/New_York'))->nonexistent(); 895 $date->setTimezone(new \DateTimeZone('America/New_York'))->nonexistent();
892 } 896 }
893 897
898 /**
899 * @covers ::getPhpDateTime
900 */
901 public function testGetPhpDateTime() {
902 $new_york = new \DateTimeZone('America/New_York');
903 $berlin = new \DateTimeZone('Europe/Berlin');
904
905 // Test retrieving a cloned copy of the wrapped \DateTime object, and that
906 // altering it does not change the DateTimePlus object.
907 $datetimeplus = DateTimePlus::createFromFormat('Y-m-d H:i:s', '2017-07-13 22:40:00', $new_york, ['langcode' => 'en']);
908 $this->assertEquals(1500000000, $datetimeplus->getTimestamp());
909 $this->assertEquals('America/New_York', $datetimeplus->getTimezone()->getName());
910
911 $datetime = $datetimeplus->getPhpDateTime();
912 $this->assertInstanceOf('DateTime', $datetime);
913 $this->assertEquals(1500000000, $datetime->getTimestamp());
914 $this->assertEquals('America/New_York', $datetime->getTimezone()->getName());
915
916 $datetime->setTimestamp(1400000000)->setTimezone($berlin);
917 $this->assertEquals(1400000000, $datetime->getTimestamp());
918 $this->assertEquals('Europe/Berlin', $datetime->getTimezone()->getName());
919 $this->assertEquals(1500000000, $datetimeplus->getTimestamp());
920 $this->assertEquals('America/New_York', $datetimeplus->getTimezone()->getName());
921 }
922
894 } 923 }