Skip to content

Commit

Permalink
Improve factory timezone handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Aug 14, 2021
1 parent 6d59b9f commit a1bd865
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Carbon/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Carbon;

use Closure;
use DateTimeInterface;
use ReflectionMethod;

/**
Expand Down Expand Up @@ -289,7 +290,13 @@ public function __call($name, $arguments)
return \in_array($parameter->getName(), ['tz', 'timezone'], true);
});

if (\count($tzParameters)) {
if (isset($arguments[0]) && in_array($name, ['instance', 'make', 'create', 'parse'], true)) {
if ($arguments[0] instanceof DateTimeInterface) {
$settings['innerTimezone'] = $settings['timezone'];
} elseif (\is_string($arguments[0]) && date_parse($arguments[0])['is_localtime']) {
unset($settings['timezone'], $settings['innerTimezone']);
}
} elseif (\count($tzParameters)) {
array_splice($arguments, key($tzParameters), 0, [$settings['timezone']]);
unset($settings['timezone']);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Carbon/Traits/Creator.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ private static function createNowInstance($tz)
*/
public static function create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null)
{
if (\is_string($year) && !is_numeric($year)) {
if (\is_string($year) && !is_numeric($year) || $year instanceof DateTimeInterface) {
return static::parse($year, $tz ?: (\is_string($month) || $month instanceof DateTimeZone ? $month : null));
}

Expand Down
4 changes: 4 additions & 0 deletions src/Carbon/Traits/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,10 @@ public function settings(array $settings)
$this->locale(...$locales);
}

if (isset($settings['innerTimezone'])) {
return $this->setTimezone($settings['innerTimezone']);
}

if (isset($settings['timezone'])) {
return $this->shiftTimezone($settings['timezone']);
}
Expand Down
36 changes: 36 additions & 0 deletions tests/Factory/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,41 @@ public function testFactoryTimezone()
$this->assertSame('2020-09-03 00:00:00.000000 America/Toronto', $factory->today()->format('Y-m-d H:i:s.u e'));
$this->assertSame('2020-09-04 00:00:00.000000 America/Toronto', $factory->tomorrow()->format('Y-m-d H:i:s.u e'));
$this->assertSame('2020-09-04 09:39:04.123456 America/Toronto', $factory->parse('2020-09-04 09:39:04.123456')->format('Y-m-d H:i:s.u e'));

$factory = new Factory([
'timezone' => 'Asia/Shanghai',
]);

$baseDate = Carbon::parse('2021-08-01 08:00:00', 'UTC');

$date = $factory->createFromTimestamp($baseDate->getTimestamp());
$this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));

$date = $factory->make('2021-08-01 08:00:00');
$this->assertSame('2021-08-01T08:00:00+08:00', $date->format('c'));

$date = $factory->make($baseDate);
$this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));

$date = $factory->create($baseDate);
$this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));

$date = $factory->parse($baseDate);
$this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));

$date = $factory->instance($baseDate);
$this->assertSame('2021-08-01T16:00:00+08:00', $date->format('c'));

$date = $factory->make('2021-08-01 08:00:00+00:20');
$this->assertSame('2021-08-01T08:00:00+00:20', $date->format('c'));

$date = $factory->parse('2021-08-01T08:00:00Z');
$this->assertSame('2021-08-01T08:00:00+00:00', $date->format('c'));

$date = $factory->create('2021-08-01 08:00:00 UTC');
$this->assertSame('2021-08-01T08:00:00+00:00', $date->format('c'));

$date = $factory->make('2021-08-01 08:00:00 Europe/Paris');
$this->assertSame('2021-08-01T08:00:00+02:00', $date->format('c'));
}
}

0 comments on commit a1bd865

Please sign in to comment.