Skip to content

Commit

Permalink
Merge pull request #73 from ConductionNL/development
Browse files Browse the repository at this point in the history
Datime to UTC
  • Loading branch information
rubenvdlinde authored Jan 21, 2020
2 parents a9aef16 + 2fce21f commit 7885fcb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ doctrine:
url: '%env(resolve:DATABASE_URL)%'
types:
uuid: Ramsey\Uuid\Doctrine\UuidType
datetime: App\Types\UTCDateTimeType #We override the default doctrine datetime with a UTC based version
incompleteDate: App\Types\IncompleteDateType
underInvestigation: App\Types\UnderInvestigationType
money: Tbbc\MoneyBundle\Type\MoneyType
Expand Down
46 changes: 46 additions & 0 deletions api/src/Types/UTCDateTimeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\DateTimeType;

class UTCDateTimeType extends DateTimeType
{
private static $utc = null;

public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return null;
}

if (is_null(self::$utc)) {
self::$utc = new \DateTimeZone('UTC');
}

$value->setTimeZone(self::$utc);

return $value->format($platform->getDateTimeFormatString());
}

public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return null;
}

if (is_null(self::$utc)) {
self::$utc = new \DateTimeZone('UTC');
}

$val = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value, self::$utc);

if (!$val) {
throw ConversionException::conversionFailed($value, $this->getName());
}

return $val;
}
}

0 comments on commit 7885fcb

Please sign in to comment.