Skip to content

Commit

Permalink
Add toDateTime() on LocalDate, LocalTime & LocalDateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Oct 18, 2018
1 parent 052c9d7 commit 588553c
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/LocalDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static function parse(string $text, DateTimeParser $parser = null) : Loca
}

/**
* Creates a LocalDate from a native DateTime object.
* Creates a LocalDate from a native DateTime or DateTimeImmutable object.
*
* @param \DateTimeInterface $dateTime
*
Expand Down Expand Up @@ -790,6 +790,18 @@ public function toEpochDay() : int
return $total - self::DAYS_0000_TO_1970;
}

/**
* Converts this LocalDate to a native DateTime object.
*
* The result is a DateTime with time 00:00 in the UTC time-zone.
*
* @return \DateTime
*/
public function toDateTime() : \DateTime
{
return $this->atTime(LocalTime::midnight())->toDateTime();
}

/**
* Returns the ISO 8601 representation of this LocalDate.
*
Expand Down
17 changes: 16 additions & 1 deletion src/LocalDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static function parse(string $text, DateTimeParser $parser = null) : Loca
}

/**
* Creates a LocalDateTime from a native DateTime object.
* Creates a LocalDateTime from a native DateTime or DateTimeImmutable object.
*
* @param \DateTimeInterface $dateTime
*
Expand Down Expand Up @@ -896,6 +896,21 @@ public function isAfterOrEqualTo(LocalDateTime $that) : bool
return $this->compareTo($that) >= 0;
}

/**
* Converts this LocalDateTime to a native DateTime object.
*
* The result is a DateTime in the UTC time-zone.
*
* Note that the native DateTime object supports a precision up to the microsecond,
* so the nanoseconds are rounded down to the nearest microsecond.
*
* @return \DateTime
*/
public function toDateTime() : \DateTime
{
return $this->atTimeZone(TimeZone::utc())->toDateTime();
}

/**
* @return string
*/
Expand Down
17 changes: 16 additions & 1 deletion src/LocalTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static function parse(string $text, DateTimeParser $parser = null) : Loca
}

/**
* Creates a LocalTime from a native DateTime object.
* Creates a LocalTime from a native DateTime or DateTimeImmutable object.
*
* @param \DateTimeInterface $dateTime
*
Expand Down Expand Up @@ -683,6 +683,21 @@ public function toSecondOfDay() : int
+ $this->second;
}

/**
* Converts this LocalTime to a native DateTime object.
*
* The result is a DateTime with date 0000-01-01 in the UTC time-zone.
*
* Note that the native DateTime object supports a precision up to the microsecond,
* so the nanoseconds are rounded down to the nearest microsecond.
*
* @return \DateTime
*/
public function toDateTime() : \DateTime
{
return $this->atDate(LocalDate::of(0, 1, 1))->toDateTime();
}

/**
* Returns this time as a string, such as 10:15.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/LocalDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1282,4 +1282,30 @@ public function testMaxOfZeroElementsThrowsException()
{
LocalDate::maxOf();
}

/**
* @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 testToDateTime(string $dateTime, string $expected)
{
$zonedDateTime = LocalDate::parse($dateTime);
$dateTime = $zonedDateTime->toDateTime();

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

/**
* @return array
*/
public function providerToDateTime()
{
return [
['2011-07-31', '2011-07-31T00:00:00.000000+0000'],
['2018-10-18', '2018-10-18T00:00:00.000000+0000'],
];
}
}
31 changes: 31 additions & 0 deletions tests/LocalDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1256,4 +1256,35 @@ public function providerCompareTo() : array
['0000-01-01T00:00:00.9', '9999-12-31T23:59:59.0', -1],
];
}

/**
* @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 testToDateTime(string $dateTime, string $expected)
{
$zonedDateTime = LocalDateTime::parse($dateTime);
$dateTime = $zonedDateTime->toDateTime();

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

/**
* @return array
*/
public function providerToDateTime()
{
return [
['2018-10-18T12:34', '2018-10-18T12:34:00.000000+0000'],
['2018-10-18T12:34:56', '2018-10-18T12:34:56.000000+0000'],
['2018-10-18T12:34:00.001', '2018-10-18T12:34:00.001000+0000'],
['2018-10-18T12:34:56.123002', '2018-10-18T12:34:56.123002+0000'],
['2011-07-31T23:59:59', '2011-07-31T23:59:59.000000+0000'],
['2011-07-31T23:59:59.02', '2011-07-31T23:59:59.020000+0000'],
['2011-07-31T23:59:59.000123456', '2011-07-31T23:59:59.000123+0000'],
];
}
}
31 changes: 31 additions & 0 deletions tests/LocalTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1158,4 +1158,35 @@ public function testMaxOfZeroElementsThrowsException()
{
LocalTime::maxOf();
}

/**
* @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 testToDateTime(string $dateTime, string $expected)
{
$zonedDateTime = LocalTime::parse($dateTime);
$dateTime = $zonedDateTime->toDateTime();

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

/**
* @return array
*/
public function providerToDateTime()
{
return [
['12:34', '0000-01-01T12:34:00.000000+0000'],
['12:34:56', '0000-01-01T12:34:56.000000+0000'],
['12:34:00.001', '0000-01-01T12:34:00.001000+0000'],
['12:34:56.123002', '0000-01-01T12:34:56.123002+0000'],
['23:59:59', '0000-01-01T23:59:59.000000+0000'],
['23:59:59.02', '0000-01-01T23:59:59.020000+0000'],
['23:59:59.000123456', '0000-01-01T23:59:59.000123+0000'],
];
}
}
2 changes: 1 addition & 1 deletion tests/ZonedDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ public function providerForPastFuture()
* @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 Date-Time object.
* @param string $expected The expected output from the native DateTime object.
*/
public function testToDateTime(string $dateTime, string $expected)
{
Expand Down

0 comments on commit 588553c

Please sign in to comment.