diff --git a/src/Matiux/DDDStarterPack/Type/DateTimeRFC.php b/src/Matiux/DDDStarterPack/Type/DateTimeRFC.php index 9baf950..977bcf4 100644 --- a/src/Matiux/DDDStarterPack/Type/DateTimeRFC.php +++ b/src/Matiux/DDDStarterPack/Type/DateTimeRFC.php @@ -19,9 +19,9 @@ final public function __construct(string $datetime = 'now', ?\DateTimeZone $time parent::__construct($datetime, $timezone); } - public static function UTC(string $datetime = 'now'): static + public static function UTC(): static { - return new static($datetime,new \DateTimeZone('UTC')); + return new static(timezone: new \DateTimeZone('UTC')); } /** @@ -43,12 +43,14 @@ public static function from(string $dateTime, ?\DateTimeZone $timezone = null): public static function UTCfrom(string $dateTime): static { - $tz = new \DateTimeZone('UTC'); - - if (!$date = static::createFromFormat(self::FORMAT, $dateTime, $tz)) { + if (!$date = static::createFromFormat(self::FORMAT, $dateTime)) { throw new \InvalidArgumentException(sprintf('Data non valida: %s', $dateTime)); } + $tz = new \DateTimeZone('UTC'); + + $date = $date->setTimezone($tz); + return new static($date->format(self::FORMAT), $tz); } diff --git a/tests/Unit/DDDStarterPack/Type/DateTimeRFCTest.php b/tests/Unit/DDDStarterPack/Type/DateTimeRFCTest.php new file mode 100644 index 0000000..f9e5e6f --- /dev/null +++ b/tests/Unit/DDDStarterPack/Type/DateTimeRFCTest.php @@ -0,0 +1,35 @@ +value()); + + self::assertSame('2024-06-27T21:06:37.879786+02:00', $now->value()); + self::assertSame('2024-06-27T19:06:37.879786+00:00', $nowToUTC->value()); + } + + /** + * @test + */ + public function it_should_create_utc_date_time(): void + { + $utc = DateTimeRFC::UTC(); + + $timezone = $utc->getTimezone(); + self::assertInstanceOf(\DateTimeZone::class, $timezone); + self::assertSame('UTC', $timezone->getName()); + } +}