Skip to content

Commit

Permalink
Merge pull request #12 from Innmind/increase-toString-precision
Browse files Browse the repository at this point in the history
Include microseconds in `PointInTime::toString()`
  • Loading branch information
Baptouuuu authored Nov 24, 2024
2 parents 492ae8b + 3cbf4a5 commit cf493cb
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- `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`
- `Innmind\TimeContinuum\PointInTime::toString()` now contains the microseconds

### Removed

Expand Down
2 changes: 1 addition & 1 deletion src/PointInTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function aheadOf(self $point): bool

public function toString(): string
{
return $this->date->format(\DateTime::ATOM);
return $this->date->format('Y-m-d\TH:i:s.uP');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Clock/LiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testAt()
$date = new \DateTimeImmutable('2016-10-08T16:08:30+02:00');
$date = $date->setTimezone(new \DateTimeZone(\date('P'))); //system timezone
$this->assertSame(
$date->format(\DateTime::ATOM),
$date->format('Y-m-d\TH:i:s.uP'),
$point->toString(),
);
}
Expand All @@ -62,7 +62,7 @@ public function testAtWithSpecificFormat()
$date = new \DateTimeImmutable('2016-10-08T16:08:30+02:00');
$date = $date->setTimezone(new \DateTimeZone(\date('P'))); //system timezone
$this->assertSame(
$date->format(\DateTime::ATOM),
$date->format('Y-m-d\TH:i:s.uP'),
$point->toString(),
);
}
Expand Down
12 changes: 7 additions & 5 deletions tests/Clock/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public function log($level, string|\Stringable $message, array $context = []): v
$clock = Clock::logger($concrete, $logger);

$this->assertSame(
$point->toString(),
$point->format(Format::iso8601()),
$clock->at($point->format(Format::iso8601()), Format::iso8601())->match(
static fn($found) => $found->toString(),
static fn($found) => $found->format(Format::iso8601()),
static fn() => null,
),
);
Expand All @@ -104,12 +104,14 @@ public function testAskedDateWithSpecificFormatIsLogged()
->forAll(
PointInTime::any(),
Set\Elements::of(
Format::cookie(),
Format::iso8601(),
Format::rfc1123(),
Format::rfc2822(),
Format::rss(),
Format::w3c(),
// problems with utc timezone as it sometimes return Z or
// GMT+0000
// Format::cookie(),
// the year is not precise enough to allow to correctly
// parse any date with these formats
// Format::rfc1036(),
Expand All @@ -133,9 +135,9 @@ public function log($level, string|\Stringable $message, array $context = []): v
$clock = Clock::logger($concrete, $logger);

$this->assertSame(
$point->toString(),
$point->format($format),
$clock->at($point->format($format), $format)->match(
static fn($point) => $point->toString(),
static fn($point) => $point->format($format),
static fn() => null,
),
);
Expand Down
8 changes: 4 additions & 4 deletions tests/ClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ public function testFrozenClockDoesntChangeItsTimezone()
$clock->now()->toString(),
);
$this->assertSame(
$frozen->now()->toString(),
$frozen->now()->format(Format::iso8601()),
$clock->at($point->format(Format::iso8601()), Format::iso8601())->match(
static fn($point) => $point->toString(),
static fn($point) => $point->format(Format::iso8601()),
static fn() => null,
),
);
Expand Down Expand Up @@ -151,11 +151,11 @@ public function log($level, string|\Stringable $message, array $context = []): v
$now2 = $new->now();

$this->assertSame(
$now1->toString(),
$now1->format(Format::iso8601()),
$gather->logs[0]['point'],
);
$this->assertSame(
$now2->toString(),
$now2->format(Format::iso8601()),
$gather->logs[1]['point'],
);
$this->assertNotSame(
Expand Down
1 change: 0 additions & 1 deletion tests/NowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public function testInterface()
$timezone = \date('P', $timestamp);
$timezone = $timezone === '+00:00' ? 'Z' : $timezone;
$this->assertSame($timezone, $point->offset()->toString());
$this->assertSame(\date('Y-m-d\TH:i:sP', $timestamp), $point->toString());
}

public function testFormat()
Expand Down
2 changes: 1 addition & 1 deletion tests/PointInTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testInterface()
$this->assertSame(30, $point->second()->toInt());
$this->assertSame(123, $point->millisecond()->toInt());
$this->assertSame('+02:00', $point->offset()->toString());
$this->assertSame('2016-10-05T08:01:30+02:00', $point->toString());
$this->assertSame('2016-10-05T08:01:30.123000+02:00', $point->toString());
}

public function testPreserveMillisecondsWhenNanosecondsInString()
Expand Down

0 comments on commit cf493cb

Please sign in to comment.