Skip to content

Commit 5b9d8ff

Browse files
authored
feat(datetime): support tempest datetime in validator and mapper (#1257)
1 parent 076653a commit 5b9d8ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1301
-286
lines changed

packages/clock/src/Clock.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Psr\Clock\ClockInterface;
88
use Tempest\DateTime\DateTimeInterface;
99
use Tempest\DateTime\Duration;
10+
use Tempest\DateTime\Timestamp;
1011

1112
interface Clock
1213
{
@@ -21,14 +22,19 @@ public function now(): DateTimeInterface;
2122
public function toPsrClock(): ClockInterface;
2223

2324
/**
24-
* Returns the current timestamp in seconds.
25+
* Returns the current timestamp.
2526
*/
26-
public function timestamp(): int;
27+
public function timestamp(): Timestamp;
28+
29+
/**
30+
* Returns the current UNIX timestamp in seconds.
31+
*/
32+
public function seconds(): int;
2733

2834
/**
2935
* Returns the current timestamp in milliseconds.
3036
*/
31-
public function timestampMs(): int;
37+
public function milliseconds(): int;
3238

3339
/**
3440
* Sleeps for the given number of milliseconds.

packages/clock/src/GenericClock.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@ public function now(): DateTimeInterface
2424
return DateTime::now();
2525
}
2626

27-
public function timestamp(): int
27+
public function timestamp(): Timestamp
28+
{
29+
return Timestamp::monotonic();
30+
}
31+
32+
public function seconds(): int
2833
{
2934
return Timestamp::monotonic()->getSeconds();
3035
}
3136

32-
public function timestampMs(): int
37+
public function milliseconds(): int
3338
{
3439
return Timestamp::monotonic()->getMilliseconds();
3540
}

packages/clock/src/MockClock.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@ final class MockClock implements Clock
1717

1818
public function __construct(DateTimeImmutable|DateTimeInterface|string $now = 'now')
1919
{
20-
if ($now instanceof DateTimeImmutable) {
21-
$this->now = DateTime::fromTimestamp(
22-
Timestamp::fromParts($now->getTimestamp()),
23-
);
24-
} else {
25-
$this->now = DateTime::parse($now);
26-
}
20+
$this->setNow($now);
2721
}
2822

2923
public function toPsrClock(): ClockInterface
@@ -36,53 +30,61 @@ public function now(): DateTimeInterface
3630
return $this->now;
3731
}
3832

39-
public function setNow(DateTimeInterface|string $now): void
33+
/**
34+
* Globally sets the current time to the specified value.
35+
*/
36+
public function setNow(DateTimeImmutable|DateTimeInterface|string $now): void
4037
{
41-
if ($now instanceof DateTimeInterface) {
42-
$this->now = $now;
43-
} else {
44-
$this->now = DateTime::parse($now);
45-
}
38+
$this->now = DateTime::parse($now);
4639
}
4740

48-
public function timestamp(): int
41+
public function timestamp(): Timestamp
42+
{
43+
return $this->now->getTimestamp();
44+
}
45+
46+
public function seconds(): int
4947
{
5048
return $this->now->getTimestamp()->getSeconds();
5149
}
5250

53-
public function timestampMs(): int
51+
public function milliseconds(): int
5452
{
5553
return $this->now->getTimestamp()->getMilliseconds();
5654
}
5755

5856
public function sleep(int|Duration $milliseconds): void
5957
{
6058
if ($milliseconds instanceof Duration) {
61-
$this->addInterval($milliseconds);
59+
$this->plus($milliseconds);
6260
return;
6361
}
6462

6563
$this->now = $this->now->plusMilliseconds($milliseconds);
6664
}
6765

68-
public function addInterval(Duration $duration): void
66+
/**
67+
* Adds the given duration. Providing an integer value adds the corresponding seconds to the current time.
68+
*/
69+
public function plus(int|Duration $duration): void
6970
{
70-
$this->now = $this->now->plus($duration);
71-
}
71+
if (is_int($duration)) {
72+
$duration = Duration::seconds($duration);
73+
}
7274

73-
public function subInternal(Duration $duration): void
74-
{
75-
$this->now = $this->now->minus($duration);
75+
$this->now = $this->now->plus($duration);
7676
}
7777

78-
public function changeTime(int $seconds): void
78+
/**
79+
* Removes the given duration. Providing an integer value removes the corresponding seconds to the current time.
80+
*/
81+
public function minus(int|Duration $duration): void
7982
{
80-
if ($seconds < 0) {
81-
$seconds = abs($seconds);
82-
$this->now = $this->now->minusSeconds($seconds);
83-
} else {
84-
$this->now = $this->now->plusSeconds($seconds);
83+
if (is_int($duration)) {
84+
$duration = Duration::seconds($duration);
8585
}
86+
87+
$this->now = $this->now->minus($duration);
8688
}
8789

8890
public function dd(): void

packages/clock/src/PsrClock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public function __construct(
1313

1414
public function now(): DateTimeImmutable
1515
{
16-
return DateTimeImmutable::createFromTimestamp($this->clock->timestamp());
16+
return DateTimeImmutable::createFromTimestamp($this->clock->seconds());
1717
}
1818
}

packages/clock/tests/GenericClockTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function test_that_generic_clock_returns_the_current_date_time(): void
2929
public function test_that_generic_clock_returns_the_current_time(): void
3030
{
3131
$timeBefore = new DateTimeImmutable()->getTimestamp();
32-
$clockTime = new GenericClock()->timestamp();
32+
$clockTime = new GenericClock()->seconds();
3333
$timeAfter = new DateTimeImmutable()->getTimestamp();
3434

3535
$this->assertGreaterThanOrEqual($timeBefore, $clockTime);

packages/clock/tests/MockClockTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function test_mock_clock_returns_the_time_we_want(): void
4141
$time = DateTime::parse('2024-09-11 13:54:23');
4242
$clock = new MockClock($time);
4343

44-
$this->assertEquals($time->getTimestamp()->getSeconds(), $clock->timestamp());
44+
$this->assertEquals($time->getTimestamp()->getSeconds(), $clock->seconds());
4545
}
4646

4747
public function test_mock_clock_sleeps_time(): void
@@ -52,7 +52,7 @@ public function test_mock_clock_sleeps_time(): void
5252
$clock = new MockClock($oldTime);
5353
$clock->sleep(2_000);
5454

55-
$this->assertSame($expectedTime->getTimestamp()->getSeconds(), $clock->timestamp());
55+
$this->assertSame($expectedTime->getTimestamp()->getSeconds(), $clock->seconds());
5656
}
5757

5858
public function test_mock_clock_can_change_time(): void
@@ -62,14 +62,14 @@ public function test_mock_clock_can_change_time(): void
6262
$addedTime = DateTime::parse('2024-09-11 13:54:25');
6363
$clock = new MockClock($dateTime);
6464

65-
$clock->changeTime(-2);
65+
$clock->minus(2);
6666

6767
$this->assertEquals($subtractedTime, $clock->now());
68-
$this->assertEquals($subtractedTime->getTimestamp()->getSeconds(), $clock->timestamp());
68+
$this->assertEquals($subtractedTime->getTimestamp()->getSeconds(), $clock->seconds());
6969

70-
$clock->changeTime(4);
70+
$clock->plus(4);
7171

7272
$this->assertEquals($addedTime, $clock->now());
73-
$this->assertEquals($addedTime->getTimestamp()->getSeconds(), $clock->timestamp());
73+
$this->assertEquals($addedTime->getTimestamp()->getSeconds(), $clock->seconds());
7474
}
7575
}

0 commit comments

Comments
 (0)