Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: corrects timezone setting in DateTimeRFC::UTCfrom #24

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
Loading