Skip to content

Commit

Permalink
fix(date): fixed date creation with wrong timezone (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
SVillette committed Nov 24, 2022
1 parent d55b83b commit 0f91374
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/Model/ArchivableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Symandy\Component\Resource\Model;

use DateTimeInterface;
use DateTimeZone;

interface ArchivableInterface
{
Expand All @@ -13,7 +14,7 @@ public function getArchivedAt(): ?DateTimeInterface;

public function setArchivedAt(?DateTimeInterface $archivedAt): void;

public function archive(): void;
public function archive(DateTimeZone $timezone = null): void;

public function restore(): void;

Expand Down
9 changes: 7 additions & 2 deletions src/Model/ArchivableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;

use function date_default_timezone_get;
use function time;

trait ArchivableTrait
Expand All @@ -24,9 +26,12 @@ public function setArchivedAt(?DateTimeInterface $archivedAt): void
$this->archivedAt = $archivedAt;
}

public function archive(): void
public function archive(DateTimeZone $timezone = null): void
{
$this->setArchivedAt(DateTimeImmutable::createFromFormat('U', (string) time()));
$timezone ??= new DateTimeZone(date_default_timezone_get());
$archivedAt = DateTimeImmutable::createFromFormat('U', (string) time());

$this->setArchivedAt($archivedAt->setTimezone($timezone));
}

public function restore(): void
Expand Down
3 changes: 2 additions & 1 deletion src/Model/CreatableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Symandy\Component\Resource\Model;

use DateTimeInterface;
use DateTimeZone;

interface CreatableInterface
{
Expand All @@ -13,6 +14,6 @@ public function getCreatedAt(): ?DateTimeInterface;

public function setCreatedAt(?DateTimeInterface $createdAt): void;

public function create(): void;
public function create(DateTimeZone $timezone = null): void;

}
9 changes: 7 additions & 2 deletions src/Model/CreatableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;

use function date_default_timezone_get;
use function time;

trait CreatableTrait
Expand All @@ -24,9 +26,12 @@ public function setCreatedAt(?DateTimeInterface $createdAt): void
$this->createdAt = $createdAt;
}

public function create(): void
public function create(DateTimeZone $timezone = null): void
{
$this->setCreatedAt(DateTimeImmutable::createFromFormat('U', (string) time()));
$timezone ??= new DateTimeZone(date_default_timezone_get());
$createdAt = DateTimeImmutable::createFromFormat('U', (string) time());

$this->setCreatedAt($createdAt->setTimezone($timezone));
}

}
6 changes: 4 additions & 2 deletions src/Model/TimestampableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Symandy\Component\Resource\Model;

use DateTimeZone;

trait TimestampableTrait
{

Expand All @@ -12,9 +14,9 @@ trait TimestampableTrait
}
use UpdatableTrait;

public function create(): void
public function create(DateTimeZone $timezone = null): void
{
$this->initializeCreatedAt();
$this->initializeCreatedAt($timezone);
$this->update();
}

Expand Down
3 changes: 2 additions & 1 deletion src/Model/UpdatableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Symandy\Component\Resource\Model;

use DateTimeInterface;
use DateTimeZone;

interface UpdatableInterface
{
Expand All @@ -13,6 +14,6 @@ public function getUpdatedAt(): ?DateTimeInterface;

public function setUpdatedAt(?DateTimeInterface $updatedAt): void;

public function update(): void;
public function update(DateTimeZone $timezone = null): void;

}
10 changes: 7 additions & 3 deletions src/Model/UpdatableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace Symandy\Component\Resource\Model;

use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;

use function date_default_timezone_get;
use function time;

trait UpdatableTrait
Expand All @@ -25,9 +26,12 @@ public function setUpdatedAt(?DateTimeInterface $updatedAt): void
$this->updatedAt = $updatedAt;
}

public function update(): void
public function update(DateTimeZone $timezone = null): void
{
$this->setUpdatedAt(DateTimeImmutable::createFromFormat('U', (string) time()));
$timezone ??= new DateTimeZone(date_default_timezone_get());
$updatedAt = DateTimeImmutable::createFromFormat('U', (string) time());

$this->setUpdatedAt($updatedAt->setTimezone($timezone));
}

}

0 comments on commit 0f91374

Please sign in to comment.