Skip to content

Commit

Permalink
Copy timezome offset with date and time
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Oct 7, 2024
1 parent 1108131 commit ec0288f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Carbon/Traits/Cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function cast(string $className): mixed
{
if (!method_exists($className, 'instance')) {
if (is_a($className, DateTimeInterface::class, true)) {
return new $className($this->rawFormat('Y-m-d H:i:s.u'), $this->getTimezone());
return $className::createFromFormat('U.u', $this->rawFormat('U.u'))
->setTimezone($this->getTimezone());
}

throw new InvalidCastException("$className has not the instance() method needed to cast the date.");
Expand Down
6 changes: 4 additions & 2 deletions src/Carbon/Traits/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ public function toJSON(): ?string
*/
public function toDateTime(): DateTime
{
return new DateTime($this->rawFormat('Y-m-d H:i:s.u'), $this->getTimezone());
return DateTime::createFromFormat('U.u', $this->rawFormat('U.u'))
->setTimezone($this->getTimezone());
}

/**
Expand All @@ -488,7 +489,8 @@ public function toDateTime(): DateTime
*/
public function toDateTimeImmutable(): DateTimeImmutable
{
return new DateTimeImmutable($this->rawFormat('Y-m-d H:i:s.u'), $this->getTimezone());
return DateTimeImmutable::createFromFormat('U.u', $this->rawFormat('U.u'))
->setTimezone($this->getTimezone());
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Carbon/Traits/Creator.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ public static function instance(DateTimeInterface $date): static
return clone $date;
}

$instance = new static($date->format('Y-m-d H:i:s.u'), $date->getTimezone());
$instance = static::createFromFormat('U.u', $date->format('U.u'))
->setTimezone($date->getTimezone());

if ($date instanceof CarbonInterface) {
$settings = $date->getSettings();
Expand Down
11 changes: 11 additions & 0 deletions tests/Carbon/ObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ public function testToDateTime()
$this->assertNotInstanceOf(CarbonInterface::class, $date);

$this->assertSame('2000-03-26', $date->format('Y-m-d'));

// Check it keeps timezone offset during DST
$date = Carbon::create(2290, 11, 2, 1, 10, 10 + 888480 / 1000000, 'America/Toronto');
$this->assertSame(
'2290-11-02 01:10:10.888480 America/Toronto -0400',
$date->toDateTime()->format('Y-m-d H:i:s.u e O'),
);
$this->assertSame(
'2290-11-02 01:10:10.888480 America/Toronto -0500',
$date->copy()->addHour()->toDateTime()->format('Y-m-d H:i:s.u e O'),
);
}

public function testToDateTimeImmutable()
Expand Down

0 comments on commit ec0288f

Please sign in to comment.