Skip to content

Commit

Permalink
Merge pull request #799 from PhilippeThouvenot/main
Browse files Browse the repository at this point in the history
Provide a default timezone for casting date
  • Loading branch information
rubenvanassche authored Jun 25, 2024
2 parents 38d4049 + 83b4c59 commit 89b9d54
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
*/
'date_format' => DATE_ATOM,

/**
* Set a default timezone for casting date
*/
'default_timezone' => null,

/**
* It is possible to enable certain features of the package, these would otherwise
* be breaking changes, and thus they are disabled by default. In the next major
Expand Down
2 changes: 2 additions & 0 deletions src/Casts/DateTimeInterfaceCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ protected function castValue(
throw CannotCastDate::create($formats->toArray(), $type, $value);
}

$this->setTimeZone ??= config('data.default_timezone');

if ($this->setTimeZone) {
return $datetime->setTimezone(new DateTimeZone($this->setTimeZone));
}
Expand Down
2 changes: 2 additions & 0 deletions src/Transformers/DateTimeInterfaceTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public function __construct(

public function transform(DataProperty $property, mixed $value, TransformationContext $context): string
{
$this->setTimeZone ??= config('data.default_timezone');

/** @var DateTimeInterface $value */
if ($this->setTimeZone) {
$value = (clone $value)->setTimezone(new DateTimeZone($this->setTimeZone));
Expand Down

0 comments on commit 89b9d54

Please sign in to comment.