Skip to content

Commit

Permalink
Merge pull request #38 from PcComponentes/bugfix/keep-microseconds-in…
Browse files Browse the repository at this point in the history
…-datetimevalueobject

fix: Keep microseconds in DateTimeValueObject
  • Loading branch information
zoilomora authored Sep 22, 2022
2 parents 9b64f2a + 4fab6b5 commit 46a57c2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Domain/Model/ValueObject/DateTimeValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class DateTimeValueObject extends \DateTimeImmutable implements ValueObject
{
private const TIME_ZONE = 'UTC';
private const TIME_FORMAT = 'Y-m-d\TH:i:s.uP';

final private function __construct($time, $timezone)
{
Expand All @@ -26,7 +27,7 @@ final public static function fromFormat(string $format, string $str): static
{
$dateTime = \DateTimeImmutable::createFromFormat($format, $str, new \DateTimeZone(self::TIME_ZONE));

return static::from($dateTime->format(\DATE_ATOM));
return static::from($dateTime->format(self::TIME_FORMAT));
}

final public static function fromTimestamp(int $timestamp): static
Expand Down
11 changes: 11 additions & 0 deletions tests/Domain/Model/ValueObject/DateTimeValueObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,15 @@ public function given_date_when_ask_to_get_info_then_return_expected_info()
$datetime = DateTimeValueObject::from('2000-01-02 03:04:05');
$this->assertEquals('2000-01-02T03:04:05+00:00', $datetime->jsonSerialize());
}

/**
* @test
*/
public function given_date_in_timestamp_with_microseconds_when_ask_to_get_info_then_return_expected_info()
{
$dateTimeMicroSeconds = '1663839726.123456';

$datetime = DateTimeValueObject::fromFormat('U.u', $dateTimeMicroSeconds);
$this->assertEquals($dateTimeMicroSeconds, $datetime->format('U.u'));
}
}

0 comments on commit 46a57c2

Please sign in to comment.