Skip to content

Commit

Permalink
Add timezones support, fix create from format methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Mistrfilda committed Dec 24, 2020
1 parent 1617c45 commit c3dfe1e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/DatetimeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Mistrfilda\Datetime;

use Mistrfilda\Datetime\Timezone\Timezone;
use Mistrfilda\Datetime\Types\DatetimeImmutable;
use Throwable;

Expand All @@ -17,14 +18,14 @@ class DatetimeFactory

public static function createFromFormat(
string $datetime,
string $mysqlDatetimeFormat = self::DEFAULT_MYSQL_DATETIME_FORMAT
string $format = self::DEFAULT_MYSQL_DATETIME_FORMAT
): DateTimeImmutable {
$parsedDatetime = DateTimeImmutable::createFromFormat($mysqlDatetimeFormat, $datetime);
$parsedDatetime = DateTimeImmutable::createFromFormat($format, $datetime);
if ($parsedDatetime === false) {
throw new DatetimeException('Can\'t create datetime from specified value and format');
}

return new DatetimeImmutable('@' . $parsedDatetime->getTimestamp());
return (new DatetimeImmutable('@' . $parsedDatetime->getTimestamp()))->setTimezone($parsedDatetime->getTimezone());
}

public function createNow(): DatetimeImmutable
Expand All @@ -37,10 +38,10 @@ public function createToday(): DateTimeImmutable
return (new DateTimeImmutable())->setTime(0, 0, 0);
}

public function createFromTimestamp(int $timestamp): DateTimeImmutable
public function createFromTimestamp(int $timestamp, string $timezone = Timezone::UTC): DateTimeImmutable
{
try {
return new DateTimeImmutable('@' . $timestamp);
return (new DateTimeImmutable('@' . $timestamp))->setTimezone(Timezone::createTimezone($timezone));
} catch (Throwable $e) {
throw new DatetimeException($e->getMessage(), $e->getCode(), $e);
}
Expand All @@ -55,6 +56,6 @@ public function createDatetimeFromMysqlFormat(
throw new DatetimeException('Can\'t create datetime from specified value and format');
}

return new DatetimeImmutable('@' . $parsedDatetime->getTimestamp());
return (new DatetimeImmutable('@' . $parsedDatetime->getTimestamp()))->setTimezone($parsedDatetime->getTimezone());
}
}
29 changes: 29 additions & 0 deletions src/Timezone/Timezone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Mistrfilda\Datetime\Timezone;

use DateTimeZone;

class Timezone
{
public const UTC = 'UTC';

public const PRAGUE = 'Europe/Prague';

public static function createTimezone(string $zone): DateTimeZone
{
return new DateTimeZone($zone);
}

public static function getUTCTimezone(): DateTimeZone
{
return new DateTimeZone(self::UTC);
}

public static function getPragueTimezone(): DateTimeZone
{
return new DateTimeZone(self::PRAGUE);
}
}
24 changes: 24 additions & 0 deletions tests/Unit/Timezone/TimezoneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Mistrfilda\Datetime\Tests\Unit\Timezone;

use Mistrfilda\Datetime\Tests\Unit\BaseUnitTest;
use Mistrfilda\Datetime\Timezone\Timezone;

class TimezoneTest extends BaseUnitTest
{
public function testTimezones(): void
{
self::assertSame(
'Europe/Prague',
Timezone::getPragueTimezone()->getName()
);

self::assertSame(
'UTC',
Timezone::getUTCTimezone()->getName()
);
}
}
1 change: 1 addition & 0 deletions tests/UpdatedTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ protected static function assertDatetimeImmutable(
DateTimeImmutable $actual
): void {
self::assertSame($expected->getTimestamp(), $actual->getTimestamp());
self::assertSame($expected->getTimezone()->getName(), $actual->getTimezone()->getName());
}
}

0 comments on commit c3dfe1e

Please sign in to comment.