Skip to content

Commit 9b355bd

Browse files
author
Florian Guimier
committed
Use Symfony clock component in AbsolutDate
1 parent 59474f2 commit 9b355bd

4 files changed

Lines changed: 29 additions & 28 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
fail-fast: false
1313
matrix:
14-
php-versions: ['8.2', '8.3']
14+
php-versions: ['8.3']
1515
dependency-versions: ['lowest', 'highest']
1616
runs-on: ubuntu-latest
1717
steps:

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
"assoconnect/php-quality-config": "^1.16"
2626
},
2727
"require": {
28-
"php": "^8.2",
28+
"php": "^8.3",
2929
"ext-intl": "*",
30-
"thecodingmachine/safe": "^3.0"
30+
"thecodingmachine/safe": "^3.0",
31+
"symfony/clock": "^6.4|^7.0"
3132
},
3233
"config": {
3334
"allow-plugins": {

src/AbsoluteDate.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
namespace AssoConnect\PHPDate;
66

77
use AssoConnect\PHPDate\Exception\ParsingException;
8-
use DateTimeImmutable;
98
use DateTimeZone;
9+
use Symfony\Component\Clock\Clock;
10+
use Symfony\Component\Clock\DatePoint;
1011

1112
class AbsoluteDate implements \Stringable
1213
{
1314
public const DEFAULT_DATE_FORMAT = 'Y-m-d';
1415

15-
private DateTimeImmutable $datetime;
16+
private DatePoint $datetime;
1617

1718
/**
1819
* AbsoluteDate constructor from a date as string
@@ -80,22 +81,22 @@ public function modify(string $modifier): self
8081
/**
8182
* Return the DateTime for a given DateTimeZone
8283
*/
83-
public function startsAt(DateTimeZone $timezone): DateTimeImmutable
84+
public function startsAt(DateTimeZone $timezone): DatePoint
8485
{
8586
return $this->getDateTimeFromFormatAndTimezone(self::DEFAULT_DATE_FORMAT, $timezone);
8687
}
8788

8889
/**
8990
* Return the DateTime at the end of the day for a given DateTimeZone
9091
*/
91-
public function endsAt(DateTimeZone $timezone): DateTimeImmutable
92+
public function endsAt(DateTimeZone $timezone): DatePoint
9293
{
9394
return $this->getDateTimeFromFormatAndTimezone(self::DEFAULT_DATE_FORMAT . ' 23:59:59', $timezone);
9495
}
9596

96-
private function getDateTimeFromFormatAndTimezone(string $format, DateTimeZone $timezone): DateTimeImmutable
97+
private function getDateTimeFromFormatAndTimezone(string $format, DateTimeZone $timezone): DatePoint
9798
{
98-
return new DateTimeImmutable($this->format($format), $timezone);
99+
return new DatePoint($this->format($format), $timezone);
99100
}
100101

101102
/**
@@ -170,7 +171,7 @@ public function isBetweenOrEqualTo(self $other1, self $other2): bool
170171
*/
171172
public function __toString(): string
172173
{
173-
return $this->format(self::DEFAULT_DATE_FORMAT);
174+
return $this->format();
174175
}
175176

176177
/**
@@ -182,11 +183,12 @@ public function __toString(): string
182183
*/
183184
public static function createInTimezone(DateTimeZone $timezone, \DateTimeInterface $datetime = null): self
184185
{
185-
return new self(
186-
(new DateTimeImmutable('@' . (null === $datetime ? time() : $datetime->getTimestamp())))
187-
->setTimezone($timezone)
188-
->format(self::DEFAULT_DATE_FORMAT)
186+
$datetime = new DatePoint(
187+
'@' . (null === $datetime ? Clock::get()->now()->getTimestamp() : $datetime->getTimestamp())
189188
);
189+
$datetime->setTimezone($timezone);
190+
191+
return new self($datetime->format(self::DEFAULT_DATE_FORMAT));
190192
}
191193

192194
/**
@@ -201,7 +203,7 @@ public static function createRelative(string $relative = 'now', DateTimeZone $ti
201203
$timezone = new DateTimeZone('UTC');
202204
}
203205

204-
$datetime = new DateTimeImmutable($relative, $timezone);
206+
$datetime = new DatePoint($relative, $timezone);
205207

206208
return self::createInTimezone($timezone, $datetime);
207209
}
@@ -212,13 +214,11 @@ private function initDatetime(string $date, string $format): void
212214
$format .= 'H:i:s';
213215
$date .= '00:00:00';
214216

215-
$datetime = DateTimeImmutable::createFromFormat($format, $date, $timezone);
216-
217-
if (false === $datetime) {
218-
throw new ParsingException(sprintf('Cannot parse %s with format %s', $date, $format));
217+
try {
218+
$this->datetime = DatePoint::createFromFormat($format, $date, $timezone);
219+
} catch (\DateMalformedStringException $e) {
220+
throw new ParsingException(sprintf('Cannot parse %s with format %s', $date, $format), previous: $e);
219221
}
220-
221-
$this->datetime = $datetime;
222222
}
223223

224224
/**

tests/AbsoluteDateTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use AssoConnect\PHPDate\AbsoluteDate;
88
use AssoConnect\PHPDate\Exception\ParsingException;
99
use PHPUnit\Framework\TestCase;
10+
use Symfony\Component\Clock\DatePoint;
1011

1112
class AbsoluteDateTest extends TestCase
1213
{
@@ -79,12 +80,11 @@ public function testWithPointInTime(): void
7980
$date = new AbsoluteDate('2020-01-02');
8081
self::assertSame('2020-01-02', $date->format());
8182

82-
$datetime = \DateTime::createFromFormat(
83+
$datetime = DatePoint::createFromFormat(
8384
'Y-m-d H:i:s',
8485
'2019-12-27 23:00:00',
8586
new \DateTimeZone('UTC')
8687
);
87-
self::assertNotFalse($datetime);
8888

8989
$date = AbsoluteDate::createInTimezone(new \DateTimeZone('UTC'), $datetime);
9090
self::assertSame('2019-12-27', $date->format());
@@ -98,13 +98,13 @@ public function testWithPointInTime(): void
9898

9999
public function testStartsAt(): void
100100
{
101-
$date1 = \DateTime::createFromFormat(
101+
$date1 = DatePoint::createFromFormat(
102102
'Y-m-d H:i:s',
103103
'2019-12-27 00:00:00',
104104
new \DateTimeZone('Europe/Paris')
105105
);
106106

107-
$date2 = \DateTime::createFromFormat(
107+
$date2 = DatePoint::createFromFormat(
108108
'Y-m-d H:i:s',
109109
'2019-12-27 00:00:00',
110110
new \DateTimeZone('America/Los_Angeles')
@@ -118,13 +118,13 @@ public function testStartsAt(): void
118118

119119
public function testEndsAt(): void
120120
{
121-
$date1 = \DateTime::createFromFormat(
121+
$date1 = DatePoint::createFromFormat(
122122
'Y-m-d H:i:s',
123123
'2019-12-27 23:59:59',
124124
new \DateTimeZone('Europe/Paris')
125125
);
126126

127-
$date2 = \DateTime::createFromFormat(
127+
$date2 = DatePoint::createFromFormat(
128128
'Y-m-d H:i:s',
129129
'2019-12-27 23:59:59',
130130
new \DateTimeZone('America/Los_Angeles')
@@ -142,7 +142,7 @@ public function testEndsAt(): void
142142
public function testCreateRelative(): void
143143
{
144144
$date = AbsoluteDate::createRelative('yesterday', $timezone = new \DateTimeZone('America/Los_Angeles'));
145-
$now = new \DateTimeImmutable('yesterday', $timezone);
145+
$now = new DatePoint('yesterday', $timezone);
146146
self::assertSame($now->format(AbsoluteDate::DEFAULT_DATE_FORMAT), $date->format());
147147
}
148148

0 commit comments

Comments
 (0)