No Description

ConstructTest.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /*
  3. * This file is part of the Carbon package.
  4. *
  5. * (c) Brian Nesbitt <brian@nesbot.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use Carbon\Carbon;
  11. class ConstructTest extends TestFixture
  12. {
  13. public function testCreatesAnInstanceDefaultToNow()
  14. {
  15. $c = new Carbon();
  16. $now = Carbon::now();
  17. $this->assertInstanceOfCarbon($c);
  18. $this->assertSame($now->tzName, $c->tzName);
  19. $this->assertCarbon($c, $now->year, $now->month, $now->day, $now->hour, $now->minute, $now->second);
  20. }
  21. public function testParseCreatesAnInstanceDefaultToNow()
  22. {
  23. $c = Carbon::parse();
  24. $now = Carbon::now();
  25. $this->assertInstanceOfCarbon($c);
  26. $this->assertSame($now->tzName, $c->tzName);
  27. $this->assertCarbon($c, $now->year, $now->month, $now->day, $now->hour, $now->minute, $now->second);
  28. }
  29. public function testWithFancyString()
  30. {
  31. $c = new Carbon('first day of January 2008');
  32. $this->assertCarbon($c, 2008, 1, 1, 0, 0, 0);
  33. }
  34. public function testParseWithFancyString()
  35. {
  36. $c = Carbon::parse('first day of January 2008');
  37. $this->assertCarbon($c, 2008, 1, 1, 0, 0, 0);
  38. }
  39. public function testDefaultTimezone()
  40. {
  41. $c = new Carbon('now');
  42. $this->assertSame('America/Toronto', $c->tzName);
  43. }
  44. public function testParseWithDefaultTimezone()
  45. {
  46. $c = Carbon::parse('now');
  47. $this->assertSame('America/Toronto', $c->tzName);
  48. }
  49. public function testSettingTimezone()
  50. {
  51. $timezone = 'Europe/London';
  52. $dtz = new \DateTimeZone($timezone);
  53. $dt = new \DateTime('now', $dtz);
  54. $dayLightSavingTimeOffset = $dt->format('I');
  55. $c = new Carbon('now', $dtz);
  56. $this->assertSame($timezone, $c->tzName);
  57. $this->assertSame(0 + $dayLightSavingTimeOffset, $c->offsetHours);
  58. }
  59. public function testParseSettingTimezone()
  60. {
  61. $timezone = 'Europe/London';
  62. $dtz = new \DateTimeZone($timezone);
  63. $dt = new \DateTime('now', $dtz);
  64. $dayLightSavingTimeOffset = $dt->format('I');
  65. $c = Carbon::parse('now', $dtz);
  66. $this->assertSame($timezone, $c->tzName);
  67. $this->assertSame(0 + $dayLightSavingTimeOffset, $c->offsetHours);
  68. }
  69. public function testSettingTimezoneWithString()
  70. {
  71. $timezone = 'Asia/Tokyo';
  72. $dtz = new \DateTimeZone($timezone);
  73. $dt = new \DateTime('now', $dtz);
  74. $dayLightSavingTimeOffset = $dt->format('I');
  75. $c = new Carbon('now', $timezone);
  76. $this->assertSame($timezone, $c->tzName);
  77. $this->assertSame(9 + $dayLightSavingTimeOffset, $c->offsetHours);
  78. }
  79. public function testParseSettingTimezoneWithString()
  80. {
  81. $timezone = 'Asia/Tokyo';
  82. $dtz = new \DateTimeZone($timezone);
  83. $dt = new \DateTime('now', $dtz);
  84. $dayLightSavingTimeOffset = $dt->format('I');
  85. $c = Carbon::parse('now', $timezone);
  86. $this->assertSame($timezone, $c->tzName);
  87. $this->assertSame(9 + $dayLightSavingTimeOffset, $c->offsetHours);
  88. }
  89. }