Skip to content

Commit 6c05a78

Browse files
authored
Add new methods to DateTimeHelper (#19)
1 parent 32612d0 commit 6c05a78

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

src/DateTimeHelper.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,34 @@ public function createDateTimeImmutableFromFormat(string $dateTimeAsString, stri
176176
return $result;
177177
}
178178

179+
/**
180+
* {@inheritdoc}
181+
*/
182+
public function createDateTimeImmutableFromTimestamp(int $timestamp): \DateTimeImmutable
183+
{
184+
$result = \DateTimeImmutable::createFromFormat('U', (string) $timestamp);
185+
186+
if (!$result instanceof \DateTimeImmutable) {
187+
throw new InvalidArgumentException(\sprintf('Could not create a \DateTimeImmutable object from string "%s" from format "%s".', (string) $timestamp, 'U'));
188+
}
189+
190+
return $result;
191+
}
192+
193+
/**
194+
* {@inheritdoc}
195+
*/
196+
public function createDateTimeFromTimestamp(int $timestamp): \DateTime
197+
{
198+
$result = \DateTime::createFromFormat('U', (string) $timestamp);
199+
200+
if (!$result instanceof \DateTime) {
201+
throw new InvalidArgumentException(\sprintf('Could not create a \DateTime object from string "%s" from format "%s".', (string) $timestamp, 'U'));
202+
}
203+
204+
return $result;
205+
}
206+
179207
/**
180208
* @param DateRangeInterface $dateRange
181209
*

tests/DateTimeHelperTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PHPUnit\Framework\Attributes\Test;
2121
use PHPUnit\Framework\MockObject\MockObject;
2222
use PHPUnit\Framework\TestCase;
23+
use TypeError;
2324

2425
/**
2526
* DateTimeHelperTest.
@@ -33,6 +34,7 @@ class DateTimeHelperTest extends TestCase
3334

3435
protected function setUp(): void
3536
{
37+
date_default_timezone_set('UTC');
3638
$this->dateRange = $this->createMock(DateRangeInterface::class);
3739
$this->dateTimeHelper = new DateTimeHelper();
3840
}
@@ -283,4 +285,52 @@ public function createDateTimeImmutableFromFormatWithException(): void
283285

284286
$this->dateTimeHelper->createDateTimeImmutableFromFormat(dateTimeAsString: 'fake');
285287
}
288+
289+
#[Test]
290+
public function createDateTimeImmutableFromTimestamp(): void
291+
{
292+
$timestamp = 1710183600;
293+
$dateTimeImmutable = $this->dateTimeHelper->createDateTimeImmutableFromTimestamp($timestamp);
294+
295+
$this->assertInstanceOf(\DateTimeImmutable::class, $dateTimeImmutable);
296+
297+
$this->assertSame(0, $dateTimeImmutable->getOffset());
298+
299+
$expectedTimezone = date_default_timezone_get();
300+
$this->assertContains($dateTimeImmutable->getTimezone()->getName(), [$expectedTimezone, '+00:00']);
301+
302+
$expectedDateTime = new \DateTimeImmutable('@' . $timestamp, new \DateTimeZone($expectedTimezone));
303+
$this->assertSame($expectedDateTime->format('Y-m-d H:i:s'), $dateTimeImmutable->format('Y-m-d H:i:s'));
304+
}
305+
306+
#[Test]
307+
public function createDateTimeImmutableFromInvalidTimestamp(): void
308+
{
309+
$this->expectException(TypeError::class);
310+
$this->dateTimeHelper->createDateTimeImmutableFromTimestamp(999999999999999999999999999);
311+
}
312+
313+
#[Test]
314+
public function createDateTimeFromTimestamp(): void
315+
{
316+
$timestamp = 1710183600; // 2024-03-11 12:00:00 UTC
317+
$dateTime = $this->dateTimeHelper->createDateTimeFromTimestamp($timestamp);
318+
319+
$this->assertInstanceOf(\DateTime::class, $dateTime);
320+
321+
$this->assertSame(0, $dateTime->getOffset());
322+
323+
$expectedTimezone = date_default_timezone_get();
324+
$this->assertContains($dateTime->getTimezone()->getName(), [$expectedTimezone, '+00:00']);
325+
326+
$expectedDateTime = new \DateTimeImmutable('@' . $timestamp, new \DateTimeZone($expectedTimezone));
327+
$this->assertSame($expectedDateTime->format('Y-m-d H:i:s'), $dateTime->format('Y-m-d H:i:s'));
328+
}
329+
330+
#[Test]
331+
public function createDateTimeFromInvalidTimestamp(): void
332+
{
333+
$this->expectException(TypeError::class);
334+
$this->dateTimeHelper->createDateTimeFromTimestamp(999999999999999999999999999);
335+
}
286336
}

0 commit comments

Comments
 (0)