Skip to content

Commit

Permalink
Implement LocalTime::fromDateTime()
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Mar 14, 2018
1 parent ae0555d commit ee8c984
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/LocalTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ private function __construct(int $hour, int $minute, int $second, int $nano)
}

/**
* @param int $hour
* @param int $minute
* @param int $second
* @param int $nano
* @param int $hour The hour, from 0 to 23.
* @param int $minute The minute, from 0 to 59.
* @param int $second The second, from 0 to 59.
* @param int $nano The nano-of-second, from 0 to 999,999,999.
*
* @return LocalTime
*
Expand Down Expand Up @@ -157,6 +157,23 @@ public static function parse(string $text, DateTimeParser $parser = null) : Loca
return LocalTime::from($parser->parse($text));
}

/**
* Creates a LocalTime from a native DateTime object.
*
* @param \DateTimeInterface $dateTime
*
* @return LocalTime
*/
public static function fromDateTime(\DateTimeInterface $dateTime) : LocalTime
{
return new LocalTime(
(int) $dateTime->format('G'),
(int) $dateTime->format('i'),
(int) $dateTime->format('s'),
1000 * (int) $dateTime->format('u')
);
}

/**
* Returns the current local time in the given time-zone, according to the given clock.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/LocalTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ public function providerParseInvalidStringThrowsException() : array
];
}

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

/**
* @dataProvider providerNow
*
Expand Down

0 comments on commit ee8c984

Please sign in to comment.