Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting time zone when converting to DateTimeImmutable #430

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extend the test to ensure the time isn't warped.

$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());
}
}