Skip to content

Commit

Permalink
Use their own classes for UTC correct date types
Browse files Browse the repository at this point in the history
  • Loading branch information
jbtronics committed Sep 10, 2024
1 parent d843fe5 commit ef8d108
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 2 deletions.
4 changes: 2 additions & 2 deletions config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ doctrine:
datetime:
class: App\Doctrine\Types\UTCDateTimeType
date:
class: App\Doctrine\Types\UTCDateTimeType
class: App\Doctrine\Types\UTCDateType

datetime_immutable:
class: App\Doctrine\Types\UTCDateTimeImmutableType
date_immutable:
class: App\Doctrine\Types\UTCDateTimeImmutableType
class: App\Doctrine\Types\UTCDateImmutableType

# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
Expand Down
74 changes: 74 additions & 0 deletions src/Doctrine/Types/UTCDateImmutableType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);


namespace App\Doctrine\Types;

use DateTime;
use DateTimeZone;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\DateImmutableType;
use Doctrine\DBAL\Types\DateType;
use Doctrine\DBAL\Types\Exception\InvalidFormat;

class UTCDateImmutableType extends DateImmutableType
{
private static ?DateTimeZone $utc_timezone = null;

/**
* {@inheritdoc}
*
* @param T $value
*
* @return (T is null ? null : string)
*
* @template T
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if (!self::$utc_timezone instanceof \DateTimeZone) {
self::$utc_timezone = new DateTimeZone('UTC');
}

if ($value instanceof \DateTimeImmutable) {
$value->setTimezone(self::$utc_timezone);
}

return parent::convertToDatabaseValue($value, $platform);
}

/**
* {@inheritDoc}
*
* @param T $value
*
* @template T
*/
public function convertToPHPValue($value, AbstractPlatform $platform): ?\DateTimeImmutable
{
if (!self::$utc_timezone instanceof \DateTimeZone) {
self::$utc_timezone = new DateTimeZone('UTC');
}

if (null === $value || $value instanceof \DateTimeImmutable) {
return $value;
}

$converted = \DateTimeImmutable::createFromFormat(
'!' . $platform->getDateFormatString(),
$value,
self::$utc_timezone
);

if (!$converted) {
throw InvalidFormat::new(
$value,
static::class,
$platform->getDateFormatString(),
);
}

return $converted;
}
}
73 changes: 73 additions & 0 deletions src/Doctrine/Types/UTCDateType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);


namespace App\Doctrine\Types;

use DateTime;
use DateTimeZone;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\DateType;
use Doctrine\DBAL\Types\Exception\InvalidFormat;

class UTCDateType extends DateType
{
private static ?DateTimeZone $utc_timezone = null;

/**
* {@inheritdoc}
*
* @param T $value
*
* @return (T is null ? null : string)
*
* @template T
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if (!self::$utc_timezone instanceof \DateTimeZone) {
self::$utc_timezone = new DateTimeZone('UTC');
}

if ($value instanceof DateTime) {
$value->setTimezone(self::$utc_timezone);
}

return parent::convertToDatabaseValue($value, $platform);
}

/**
* {@inheritDoc}
*
* @param T $value
*
* @template T
*/
public function convertToPHPValue($value, AbstractPlatform $platform): ?DateTime
{
if (!self::$utc_timezone instanceof \DateTimeZone) {
self::$utc_timezone = new DateTimeZone('UTC');
}

if (null === $value || $value instanceof DateTime) {
return $value;
}

$converted = DateTime::createFromFormat(
'!' . $platform->getDateFormatString(),
$value,
self::$utc_timezone
);

if (!$converted) {
throw InvalidFormat::new(
$value,
static::class,
$platform->getDateFormatString(),
);
}

return $converted;
}
}

0 comments on commit ef8d108

Please sign in to comment.