Skip to content

Commit

Permalink
Merge pull request #9 from Innmind/force-at-format
Browse files Browse the repository at this point in the history
Force the use of a format to parse a string
  • Loading branch information
Baptouuuu authored Nov 24, 2024
2 parents 4b49716 + 48a08ae commit 828bb7c
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
- `Innmind\TimeContinuum\PointInTime::timezone()` has been renamed `Innmind\TimeContinuum\PointInTime::offset()`
- `Innmind\TimeContinuum\PointInTime\Day::toInt()` has been renamed `Innmind\TimeContinuum\PointInTime\Day::ofMonth()`
- `Innmind\TimeContinuum\ElapsedPeriod::of()` is now an `internal` method
- `Innmind\TimeContinuum\Clock::at()` now requires a `Format`

### Removed

Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ composer install innmind/time-continuum
use Innmind\TimeContinuum\{
Clock,
PointInTime,
Format,
};
use Innmind\Immutable\Maybe;

$clock = Clock::live();
$now = $clock->now(); // return an instance of PointInTime
echo $now->toString(); // 2016-10-11T12:17:30+02:00

$epoch = $clock->at('1970-01-01T00:00:00.000000Z'); // Maybe<PointInTime>
$epoch = $clock->at(
'1970-01-01T00:00:00.000000+00:00',
Format::iso8601(),
); // Maybe<PointInTime>
```

Here we reference 2 points in time, the first is the exact moment we call `now` down to the millisecond and the second one is the epoch time (you can verify it via `$epoch->milliseconds()` that returns `0`).
Expand Down
1 change: 0 additions & 1 deletion proofs/clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ static function($assert, $point) {
given(
Set\Unicode::strings(),
Set\Elements::of(
null,
Format::cookie(),
Format::iso8601(),
Format::rfc1036(),
Expand Down
2 changes: 1 addition & 1 deletion src/Clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function switch(callable $changeTimezone): self
*
* @return Maybe<PointInTime>
*/
public function at(string $date, Format $format = null): Maybe
public function at(string $date, Format $format): Maybe
{
/**
* @psalm-suppress ImpureVariable
Expand Down
2 changes: 1 addition & 1 deletion src/Clock/Frozen.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function now(): PointInTime
*
* @return Maybe<PointInTime>
*/
public function at(string $date, Format $format = null): Maybe
public function at(string $date, Format $format): Maybe
{
/**
* @psalm-suppress ImpureVariable
Expand Down
42 changes: 15 additions & 27 deletions src/Clock/Live.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,36 +57,24 @@ public function now(): PointInTime
*
* @return Maybe<PointInTime>
*/
public function at(string $date, Format $format = null): Maybe
public function at(string $date, Format $format): Maybe
{
if ($format instanceof Format) {
try {
/** @psalm-suppress ImpureMethodCall */
$datetime = \DateTimeImmutable::createFromFormat($format->toString(), $date);
} catch (\Throwable) {
/** @var Maybe<PointInTime> */
return Maybe::nothing();
}

if ($datetime === false) {
/** @var Maybe<PointInTime> */
return Maybe::nothing();
}
try {
/** @psalm-suppress ImpureMethodCall */
$datetime = \DateTimeImmutable::createFromFormat($format->toString(), $date);
} catch (\Throwable) {
/** @var Maybe<PointInTime> */
return Maybe::nothing();
}

if ($datetime->format($format->toString()) !== $date) {
/** @var Maybe<PointInTime> */
return Maybe::nothing();
}
if ($datetime === false) {
/** @var Maybe<PointInTime> */
return Maybe::nothing();
}

$date = $datetime->format(\DateTime::ATOM);
} else {
try {
/** @psalm-suppress ImpureMethodCall */
$datetime = new \DateTimeImmutable($date);
} catch (\Throwable) {
/** @var Maybe<PointInTime> */
return Maybe::nothing();
}
if ($datetime->format($format->toString()) !== $date) {
/** @var Maybe<PointInTime> */
return Maybe::nothing();
}

/**
Expand Down
18 changes: 3 additions & 15 deletions src/Clock/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function now(): PointInTime
*
* @return Maybe<PointInTime>
*/
public function at(string $date, Format $format = null): Maybe
public function at(string $date, Format $format): Maybe
{
/**
* @psalm-suppress ImpureVariable
Expand All @@ -67,22 +67,10 @@ public function at(string $date, Format $format = null): Maybe
->map(fn($point) => $this->log($point, $date, $format));
}

/**
* @psalm-pure
*/
private function format(?Format $format): string
{
if (\is_null($format)) {
return 'unknown';
}

return $format->toString();
}

private function log(
PointInTime $point,
string $date,
?Format $format,
Format $format,
): PointInTime {
/**
* @psalm-suppress ImpureVariable
Expand All @@ -91,7 +79,7 @@ private function log(
*/
$this->logger->debug('Asked time {date} ({format}) resolved to {point}', [
'date' => $date,
'format' => $this->format($format),
'format' => $format->toString(),
'point' => $point->format(Format::iso8601()),
]);

Expand Down
2 changes: 1 addition & 1 deletion tests/Clock/FrozenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testAtReturnWithTheSameTimezoneAsNow()
->then(function($now, $at) {
$clock = Clock::frozen($now);

$point = $clock->at($at->format(Format::iso8601()))->match(
$point = $clock->at($at->format(Format::iso8601()), Format::iso8601())->match(
static fn($point) => $point,
static fn() => null,
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Clock/LiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testAt()
{
$this->assertInstanceOf(
PointInTime::class,
$point = Clock::live()->at('2016-10-08T16:08:30+02:00')->match(
$point = Clock::live()->at('2016-10-08T16:08:30+02:00', Format::iso8601())->match(
static fn($point) => $point,
static fn() => null,
),
Expand Down
6 changes: 3 additions & 3 deletions tests/Clock/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function log($level, string|\Stringable $message, array $context = []): v

$this->assertSame(
$point->toString(),
$clock->at($point->toString())->match(
$clock->at($point->format(Format::iso8601()), Format::iso8601())->match(
static fn($found) => $found->toString(),
static fn() => null,
),
Expand All @@ -88,8 +88,8 @@ public function log($level, string|\Stringable $message, array $context = []): v
'debug',
'Asked time {date} ({format}) resolved to {point}',
[
'date' => $point->toString(),
'format' => 'unknown',
'date' => $point->format(Format::iso8601()),
'format' => Format::iso8601()->toString(),
'point' => $point->format(Format::iso8601()),
],
],
Expand Down
2 changes: 1 addition & 1 deletion tests/ClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function testFrozenClockDoesntChangeItsTimezone()
);
$this->assertSame(
$frozen->now()->toString(),
$clock->at($point->toString())->match(
$clock->at($point->format(Format::iso8601()), Format::iso8601())->match(
static fn($point) => $point->toString(),
static fn() => null,
),
Expand Down

0 comments on commit 828bb7c

Please sign in to comment.