ModifyNearDSTChangeTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of the Carbon package.
  5. *
  6. * (c) Brian Nesbitt <brian@nesbot.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Tests\CarbonImmutable;
  12. use Carbon\CarbonImmutable as Carbon;
  13. use Generator;
  14. use Tests\AbstractTestCase;
  15. class ModifyNearDSTChangeTest extends AbstractTestCase
  16. {
  17. /**
  18. * Tests transition through DST change hour in non default timezone
  19. *
  20. * @param string $dateString
  21. * @param int $addHours
  22. * @param string $expected
  23. *
  24. * @dataProvider \Tests\CarbonImmutable\ModifyNearDSTChangeTest::dataForTransitionTests
  25. */
  26. public function testTransitionInNonDefaultTimezone($dateString, $addHours, $expected)
  27. {
  28. date_default_timezone_set('Europe/london');
  29. $date = Carbon::parse($dateString, 'America/New_York');
  30. $date = $date->addHours($addHours);
  31. $this->assertSame($expected, $date->format('c'));
  32. }
  33. /**
  34. * Tests transition through DST change hour in default timezone
  35. *
  36. * @param string $dateString
  37. * @param int $addHours
  38. * @param string $expected
  39. *
  40. * @dataProvider \Tests\CarbonImmutable\ModifyNearDSTChangeTest::dataForTransitionTests
  41. */
  42. public function testTransitionInDefaultTimezone($dateString, $addHours, $expected)
  43. {
  44. date_default_timezone_set('America/New_York');
  45. $date = Carbon::parse($dateString, 'America/New_York');
  46. $date = $date->addHours($addHours);
  47. $this->assertSame($expected, $date->format('c'));
  48. }
  49. public static function dataForTransitionTests(): Generator
  50. {
  51. // arguments:
  52. // - Date string to Carbon::parse in America/New_York.
  53. // - Hours to add
  54. // - Resulting string in 'c' format
  55. // testForwardTransition
  56. // When standard time was about to reach 2010-03-14T02:00:00-05:00 clocks were turned forward 1 hour to
  57. // 2010-03-14T03:00:00-04:00 local daylight time instead
  58. yield ['2010-03-14T00:00:00', 24, '2010-03-15T00:00:00-04:00'];
  59. // testBackwardTransition
  60. // When local daylight time was about to reach 2010-11-07T02:00:00-04:00 clocks were turned backward 1 hour
  61. // to 2010-11-07T01:00:00-05:00 local standard time instead
  62. yield ['2010-11-07T00:00:00', 24, '2010-11-08T00:00:00-05:00'];
  63. }
  64. }