55namespace AssoConnect \PHPDate ;
66
77use AssoConnect \PHPDate \Exception \ParsingException ;
8- use DateTimeImmutable ;
98use DateTimeZone ;
9+ use Symfony \Component \Clock \Clock ;
10+ use Symfony \Component \Clock \DatePoint ;
1011
1112class 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 /**
0 commit comments