Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-versions: ['8.2', '8.3']
php-versions: ['8.3']
dependency-versions: ['lowest', 'highest']
runs-on: ubuntu-latest
steps:
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
"assoconnect/php-quality-config": "^1.16"
},
"require": {
"php": "^8.2",
"php": "^8.3",
"ext-intl": "*",
"thecodingmachine/safe": "^3.0"
"thecodingmachine/safe": "^3.0",
"symfony/clock": "^6.4|^7.0"
},
"config": {
"allow-plugins": {
Expand Down
48 changes: 29 additions & 19 deletions src/AbsoluteDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
namespace AssoConnect\PHPDate;

use AssoConnect\PHPDate\Exception\ParsingException;
use DateTimeImmutable;
use DateTimeZone;
use Symfony\Component\Clock\Clock;
use Symfony\Component\Clock\DatePoint;

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

private DateTimeImmutable $datetime;
private DatePoint $datetime;

/**
* AbsoluteDate constructor from a date as string
Expand Down Expand Up @@ -80,22 +81,22 @@ public function modify(string $modifier): self
/**
* Return the DateTime for a given DateTimeZone
*/
public function startsAt(DateTimeZone $timezone): DateTimeImmutable
public function startsAt(DateTimeZone $timezone): DatePoint
{
return $this->getDateTimeFromFormatAndTimezone(self::DEFAULT_DATE_FORMAT, $timezone);
return $this->getDateTimeFromFormatAndTimezone(self::DEFAULT_DATE_FORMAT . ' 00:00:00', $timezone);
}

/**
* Return the DateTime at the end of the day for a given DateTimeZone
*/
public function endsAt(DateTimeZone $timezone): DateTimeImmutable
public function endsAt(DateTimeZone $timezone): DatePoint
{
return $this->getDateTimeFromFormatAndTimezone(self::DEFAULT_DATE_FORMAT . ' 23:59:59', $timezone);
}

private function getDateTimeFromFormatAndTimezone(string $format, DateTimeZone $timezone): DateTimeImmutable
private function getDateTimeFromFormatAndTimezone(string $format, DateTimeZone $timezone): DatePoint
{
return new DateTimeImmutable($this->format($format), $timezone);
return new DatePoint($this->format($format), $timezone);
}

/**
Expand Down Expand Up @@ -170,7 +171,7 @@ public function isBetweenOrEqualTo(self $other1, self $other2): bool
*/
public function __toString(): string
{
return $this->format(self::DEFAULT_DATE_FORMAT);
return $this->format();
}

/**
Expand All @@ -182,11 +183,22 @@ public function __toString(): string
*/
public static function createInTimezone(DateTimeZone $timezone, \DateTimeInterface $datetime = null): self
{
return new self(
(new DateTimeImmutable('@' . (null === $datetime ? time() : $datetime->getTimestamp())))
->setTimezone($timezone)
->format(self::DEFAULT_DATE_FORMAT)
$pointInTime = new DatePoint(
'@' . (null === $datetime ? Clock::get()->now()->getTimestamp() : $datetime->getTimestamp())
);

$pointInTime = $pointInTime->setTimezone($timezone);

$localMidnight = DatePoint::createFromFormat(
'Y-m-d H:i:s',
$pointInTime->format('Y-m-d 00:00:00'),
$timezone
);

$absolute = new self($localMidnight->format(self::DEFAULT_DATE_FORMAT));
$absolute->datetime = $localMidnight;

return $absolute;
}

/**
Expand All @@ -201,7 +213,7 @@ public static function createRelative(string $relative = 'now', DateTimeZone $ti
$timezone = new DateTimeZone('UTC');
}

$datetime = new DateTimeImmutable($relative, $timezone);
$datetime = new DatePoint($relative, $timezone);

return self::createInTimezone($timezone, $datetime);
}
Expand All @@ -212,13 +224,11 @@ private function initDatetime(string $date, string $format): void
$format .= 'H:i:s';
$date .= '00:00:00';

$datetime = DateTimeImmutable::createFromFormat($format, $date, $timezone);

if (false === $datetime) {
throw new ParsingException(sprintf('Cannot parse %s with format %s', $date, $format));
try {
$this->datetime = DatePoint::createFromFormat($format, $date, $timezone);
} catch (\DateMalformedStringException $e) {
throw new ParsingException(sprintf('Cannot parse %s with format %s', $date, $format), previous: $e);
}

$this->datetime = $datetime;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/LocalizedStringParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@

public function getPatternFromLocale(string $locale): string
{
$pattern = (new IntlDateFormatter(
$formatter = new IntlDateFormatter(
$locale,
IntlDateFormatter::SHORT,
IntlDateFormatter::NONE,
))->getPattern();
);

$pattern = $formatter->getPattern();

if (false === $pattern) {

Check failure on line 65 in src/LocalizedStringParser.php

View workflow job for this annotation

GitHub Actions / build (8.3, lowest)

Strict comparison using === between false and string will always evaluate to false.
throw new UnsupportedLocalException($locale);
}

Expand Down
41 changes: 21 additions & 20 deletions tests/AbsoluteDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use AssoConnect\PHPDate\AbsoluteDate;
use AssoConnect\PHPDate\Exception\ParsingException;
use DateTimeZone;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Clock\DatePoint;

class AbsoluteDateTest extends TestCase
{
Expand Down Expand Up @@ -79,70 +81,69 @@ public function testWithPointInTime(): void
$date = new AbsoluteDate('2020-01-02');
self::assertSame('2020-01-02', $date->format());

$datetime = \DateTime::createFromFormat(
$datetime = DatePoint::createFromFormat(
'Y-m-d H:i:s',
'2019-12-27 23:00:00',
new \DateTimeZone('UTC')
new DateTimeZone('UTC')
);
self::assertNotFalse($datetime);

$date = AbsoluteDate::createInTimezone(new \DateTimeZone('UTC'), $datetime);
$date = AbsoluteDate::createInTimezone(new DateTimeZone('UTC'), $datetime);
self::assertSame('2019-12-27', $date->format());

$date = AbsoluteDate::createInTimezone(new \DateTimeZone('Europe/Paris'), $datetime);
$date = AbsoluteDate::createInTimezone(new DateTimeZone('Europe/Paris'), $datetime);
self::assertSame('2019-12-28', $date->format());

$date = AbsoluteDate::createInTimezone(new \DateTimeZone('America/Los_Angeles'), $datetime);
$date = AbsoluteDate::createInTimezone(new DateTimeZone('America/Los_Angeles'), $datetime);
self::assertSame('2019-12-27', $date->format());
}

public function testStartsAt(): void
{
$date1 = \DateTime::createFromFormat(
$date1 = DatePoint::createFromFormat(
'Y-m-d H:i:s',
'2019-12-27 00:00:00',
new \DateTimeZone('Europe/Paris')
new DateTimeZone('Europe/Paris')
);

$date2 = \DateTime::createFromFormat(
$date2 = DatePoint::createFromFormat(
'Y-m-d H:i:s',
'2019-12-27 00:00:00',
new \DateTimeZone('America/Los_Angeles')
new DateTimeZone('America/Los_Angeles')
);

$date = new AbsoluteDate('2019-12-27');

self::assertEquals($date1, $date->startsAt(new \DateTimeZone('Europe/Paris')));
self::assertEquals($date2, $date->startsAt(new \DateTimeZone('America/Los_Angeles')));
self::assertEquals($date1, $date->startsAt(new DateTimeZone('Europe/Paris')));
self::assertEquals($date2, $date->startsAt(new DateTimeZone('America/Los_Angeles')));
}

public function testEndsAt(): void
{
$date1 = \DateTime::createFromFormat(
$date1 = DatePoint::createFromFormat(
'Y-m-d H:i:s',
'2019-12-27 23:59:59',
new \DateTimeZone('Europe/Paris')
new DateTimeZone('Europe/Paris')
);

$date2 = \DateTime::createFromFormat(
$date2 = DatePoint::createFromFormat(
'Y-m-d H:i:s',
'2019-12-27 23:59:59',
new \DateTimeZone('America/Los_Angeles')
new DateTimeZone('America/Los_Angeles')
);

$date = new AbsoluteDate('2019-12-27');

self::assertEquals($date1, $date->endsAt(new \DateTimeZone('Europe/Paris')));
self::assertEquals($date2, $date->endsAt(new \DateTimeZone('America/Los_Angeles')));
self::assertEquals($date1, $date->endsAt(new DateTimeZone('Europe/Paris')));
self::assertEquals($date2, $date->endsAt(new DateTimeZone('America/Los_Angeles')));
}

/**
* @throws \Exception
*/
public function testCreateRelative(): void
{
$date = AbsoluteDate::createRelative('yesterday', $timezone = new \DateTimeZone('America/Los_Angeles'));
$now = new \DateTimeImmutable('yesterday', $timezone);
$date = AbsoluteDate::createRelative('yesterday', $timezone = new DateTimeZone('America/Los_Angeles'));
$now = new DatePoint('yesterday', $timezone);
self::assertSame($now->format(AbsoluteDate::DEFAULT_DATE_FORMAT), $date->format());
}

Expand Down
Loading