Skip to content

Commit

Permalink
Allow setting time zone when converting to DateTimeImmutable
Browse files Browse the repository at this point in the history
  • Loading branch information
othercorey committed Sep 25, 2023
1 parent 900cfbd commit 8ca3ac2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
20 changes: 14 additions & 6 deletions src/ChronosDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -1578,25 +1578,33 @@ public function diffForHumans(?ChronosDate $other = null, bool $absolute = false
}

/**
* Returns the date as a `DateTimeImmutable` instance.
* Returns the date as a `DateTimeImmutable` instance at midnight.
*
* @param \DateTimeZone|string|null $timezone Time zone the DateTimeImmutable instance will be in
* @return \DateTimeImmutable
*/
public function toDateTimeImmutable(): DateTimeImmutable
public function toDateTimeImmutable(DateTimeZone|string|null $timezone = null): DateTimeImmutable
{
return $this->native;
if ($timezone === null) {
return $this->native;
}

$timezone = is_string($timezone) ? new DateTimeZone($timezone) : $timezone;

return new DateTimeImmutable($this->native->format('Y-m-d H:i:s.u'), $timezone);
}

/**
* Returns an `DateTimeImmutable` instance set to this clock time.
* Returns the date as a `DateTimeImmutable` instance at midnight.
*
* Alias of `toDateTimeImmutable()`.
*
* @param \DateTimeZone|string|null $timezone Time zone the DateTimeImmutable instance will be in
* @return \DateTimeImmutable
*/
public function toNative(): DateTimeImmutable
public function toNative(DateTimeZone|string|null $timezone = null): DateTimeImmutable
{
return $this->toDateTimeImmutable();
return $this->toDateTimeImmutable($timezone);
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/ChronosTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,14 @@ public function between(ChronosTime $start, ChronosTime $end, bool $equals = tru
/**
* Returns an `DateTimeImmutable` instance set to this clock time.
*
* @param \DateTimeZone|string|null $timezone Time zone the DateTimeImmutable instance will be in
* @return \DateTimeImmutable
*/
public function toDateTimeImmutable(): DateTimeImmutable
public function toDateTimeImmutable(DateTimeZone|string|null $timezone = null): DateTimeImmutable
{
return (new DateTimeImmutable())->setTime(
$timezone = is_string($timezone) ? new DateTimeZone($timezone) : $timezone;

return (new DateTimeImmutable(timezone: $timezone))->setTime(
$this->getHours(),
$this->getMinutes(),
$this->getSeconds(),
Expand All @@ -464,10 +467,11 @@ public function toDateTimeImmutable(): DateTimeImmutable
*
* Alias of `toDateTimeImmutable()`.
*
* @param \DateTimeZone|string|null $timezone Time zone the DateTimeImmutable instance will be in
* @return \DateTimeImmutable
*/
public function toNative(): DateTimeImmutable
public function toNative(DateTimeZone|string|null $timezone = null): DateTimeImmutable
{
return $this->toDateTimeImmutable();
return $this->toDateTimeImmutable($timezone);
}
}
13 changes: 11 additions & 2 deletions tests/TestCase/ChronosTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,20 @@ public function testComparisons(): void

public function testToDateTimeImmutable(): void
{
$native = ChronosTime::parse('23:59:59.999999')->toDateTimeImmutable();
$time = ChronosTime::parse('23:59:59.999999');
$native = $time->toDateTimeImmutable();
$this->assertSame('23:59:59.999999', $native->format('H:i:s.u'));

$native = ChronosTime::parse('23:59:59.999999')->toNative();
$native = $time->toNative();
$this->assertSame('23:59:59.999999', $native->format('H:i:s.u'));

$native = $time->toDateTimeImmutable('Asia/Tokyo');
$this->assertSame('23:59:59.999999', $native->format('H:i:s.u'));
$this->assertSame('Asia/Tokyo', $native->getTimezone()->getName());

$native = $time->toNative('Asia/Tokyo');
$this->assertSame('23:59:59.999999', $native->format('H:i:s.u'));
$this->assertSame('Asia/Tokyo', $native->getTimezone()->getName());
}

public function testToString(): void
Expand Down
12 changes: 9 additions & 3 deletions tests/TestCase/Date/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,15 @@ public function testToW3cString()
public function testToDateTimeImmutable(): void
{
$d = ChronosDate::now();
$this->assertSame($d->format('Y-m-d'), $d->toDateTimeImmutable()->format('Y-m-d'));
$this->assertSame($d->format('Y-m-d H:i:s.u'), $d->toDateTimeImmutable()->format('Y-m-d H:i:s.u'));
$this->assertSame($d->format('Y-m-d H:i:s.u'), $d->toNative()->format('Y-m-d H:i:s.u'));

$d = ChronosDate::now();
$this->assertSame($d->format('Y-m-d'), $d->toNative()->format('Y-m-d'));
$native = $d->toDateTimeImmutable('Asia/Tokyo');
$this->assertSame($d->format('Y-m-d H:i:s.u'), $native->format('Y-m-d H:i:s.u'));
$this->assertSame('Asia/Tokyo', $native->getTimezone()->getName());

$native = $d->toNative('Asia/Tokyo');
$this->assertSame($d->format('Y-m-d H:i:s.u'), $native->format('Y-m-d H:i:s.u'));
$this->assertSame('Asia/Tokyo', $native->getTimezone()->getName());
}
}

0 comments on commit 8ca3ac2

Please sign in to comment.