Skip to content

Commit

Permalink
Implement LocalDateTime::fromDateTime()
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Mar 14, 2018
1 parent ee8c984 commit dccebea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/LocalDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ public static function parse(string $text, DateTimeParser $parser = null) : Loca
return LocalDateTime::from($parser->parse($text));
}

/**
* Creates a LocalDateTime from a native DateTime object.
*
* @param \DateTimeInterface $dateTime
*
* @return LocalDateTime
*/
public static function fromDateTime(\DateTimeInterface $dateTime) : LocalDateTime
{
return new LocalDateTime(
LocalDate::fromDateTime($dateTime),
LocalTime::fromDateTime($dateTime)
);
}

/**
* Returns the smallest possible value for LocalDateTime.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/LocalDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public function testOf()
$this->assertLocalDateTimeIs(2001, 12, 23, 12, 34, 56, 987654321, new LocalDateTime($date, $time));
}

public function testFromDateTime()
{
$dateTime = new \DateTime('2018-07-21 14:09:10.23456');
$this->assertLocalDateTimeIs(2018, 7, 21, 14, 9, 10, 234560000, LocalDateTime::fromDateTime($dateTime));
}

/**
* @dataProvider providerNow
*
Expand Down

0 comments on commit dccebea

Please sign in to comment.