Skip to content

Commit

Permalink
fix: corrects timezone setting in DateTimeRFC::UTCfrom
Browse files Browse the repository at this point in the history
  • Loading branch information
salvatore-pollaci-abilio authored and matiux committed Jun 28, 2024
1 parent 9ce6916 commit 2bf2d55
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Matiux/DDDStarterPack/Type/DateTimeRFC.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

/**
Expand All @@ -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);
}

Expand Down
35 changes: 35 additions & 0 deletions tests/Unit/DDDStarterPack/Type/DateTimeRFCTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\DDDStarterPack\Type;

use DDDStarterPack\Type\DateTimeRFC;
use PHPUnit\Framework\TestCase;

class DateTimeRFCTest extends TestCase
{
/**
* @test
*/
public function it_should_create_utc_date_time_from_different_timezone_date_time_string(): void
{
$now = new DateTimeRFC('2024-06-27T21:06:37.879786+02:00');
$nowToUTC = DateTimeRFC::UTCfrom($now->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());
}
}

0 comments on commit 2bf2d55

Please sign in to comment.