Skip to content

Commit

Permalink
Add toDateTimeImmutable() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Oct 29, 2018
1 parent 588553c commit 91a0c9c
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/LocalDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,14 @@ public function toDateTime() : \DateTime
return $this->atTime(LocalTime::midnight())->toDateTime();
}

/**
* @return \DateTimeImmutable
*/
public function toDateTimeImmutable() : \DateTimeImmutable
{
return \DateTimeImmutable::createFromMutable($this->toDateTime());
}

/**
* Returns the ISO 8601 representation of this LocalDate.
*
Expand Down
8 changes: 8 additions & 0 deletions src/LocalDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,14 @@ public function toDateTime() : \DateTime
return $this->atTimeZone(TimeZone::utc())->toDateTime();
}

/**
* @return \DateTimeImmutable
*/
public function toDateTimeImmutable() : \DateTimeImmutable
{
return \DateTimeImmutable::createFromMutable($this->toDateTime());
}

/**
* @return string
*/
Expand Down
8 changes: 8 additions & 0 deletions src/LocalTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,14 @@ public function toDateTime() : \DateTime
return $this->atDate(LocalDate::of(0, 1, 1))->toDateTime();
}

/**
* @return \DateTimeImmutable
*/
public function toDateTimeImmutable() : \DateTimeImmutable
{
return \DateTimeImmutable::createFromMutable($this->toDateTime());
}

/**
* Returns this time as a string, such as 10:15.
*
Expand Down
8 changes: 8 additions & 0 deletions src/ZonedDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,14 @@ public function toDateTime() : \DateTime
return \DateTime::createFromFormat($format, $dateTime, $dateTimeZone);
}

/**
* @return \DateTimeImmutable
*/
public function toDateTimeImmutable() : \DateTimeImmutable
{
return \DateTimeImmutable::createFromMutable($this->toDateTime());
}

/**
* @return string
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/LocalDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,21 @@ public function testToDateTime(string $dateTime, string $expected)
$this->assertSame($expected, $dateTime->format('Y-m-d\TH:i:s.uO'));
}

/**
* @dataProvider providerToDateTime
*
* @param string $dateTime The date-time string that will be parse()d by LocalDate.
* @param string $expected The expected output from the native DateTime object.
*/
public function testToDateTimeImmutable(string $dateTime, string $expected)
{
$zonedDateTime = LocalDate::parse($dateTime);
$dateTime = $zonedDateTime->toDateTimeImmutable();

$this->assertInstanceOf(\DateTimeImmutable::class, $dateTime);
$this->assertSame($expected, $dateTime->format('Y-m-d\TH:i:s.uO'));
}

/**
* @return array
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/LocalDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,21 @@ public function testToDateTime(string $dateTime, string $expected)
$this->assertSame($expected, $dateTime->format('Y-m-d\TH:i:s.uO'));
}

/**
* @dataProvider providerToDateTime
*
* @param string $dateTime The date-time string that will be parse()d by LocalDateTime.
* @param string $expected The expected output from the native DateTime object.
*/
public function testToDateTimeImmutable(string $dateTime, string $expected)
{
$zonedDateTime = LocalDateTime::parse($dateTime);
$dateTime = $zonedDateTime->toDateTimeImmutable();

$this->assertInstanceOf(\DateTimeImmutable::class, $dateTime);
$this->assertSame($expected, $dateTime->format('Y-m-d\TH:i:s.uO'));
}

/**
* @return array
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/LocalTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,21 @@ public function testToDateTime(string $dateTime, string $expected)
$this->assertSame($expected, $dateTime->format('Y-m-d\TH:i:s.uO'));
}

/**
* @dataProvider providerToDateTime
*
* @param string $dateTime The date-time string that will be parse()d by LocalTime.
* @param string $expected The expected output from the native DateTime object.
*/
public function testToDateTimeImmutable(string $dateTime, string $expected)
{
$zonedDateTime = LocalTime::parse($dateTime);
$dateTime = $zonedDateTime->toDateTimeImmutable();

$this->assertInstanceOf(\DateTimeImmutable::class, $dateTime);
$this->assertSame($expected, $dateTime->format('Y-m-d\TH:i:s.uO'));
}

/**
* @return array
*/
Expand Down
15 changes: 15 additions & 0 deletions tests/ZonedDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,21 @@ public function testToDateTime(string $dateTime, string $expected)
$this->assertSame($expected, $dateTime->format('Y-m-d\TH:i:s.uO'));
}

/**
* @dataProvider providerToDateTime
*
* @param string $dateTime The date-time string that will be parse()d by ZonedDateTime.
* @param string $expected The expected output from the native DateTime object.
*/
public function testToDateTimeImmutable(string $dateTime, string $expected)
{
$zonedDateTime = ZonedDateTime::parse($dateTime);
$dateTime = $zonedDateTime->toDateTimeImmutable();

$this->assertInstanceOf(\DateTimeImmutable::class, $dateTime);
$this->assertSame($expected, $dateTime->format('Y-m-d\TH:i:s.uO'));
}

/**
* @return array
*/
Expand Down

0 comments on commit 91a0c9c

Please sign in to comment.