Skip to content

Commit 76d70c1

Browse files
authored
feat(datetime): add datetime component (#1158)
1 parent 0980e3a commit 76d70c1

File tree

96 files changed

+10413
-119
lines changed

Some content is hidden

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

96 files changed

+10413
-119
lines changed

.github/workflows/integration-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
uses: shivammathur/setup-php@v2
2929
with:
3030
php-version: ${{ matrix.php }}
31-
extensions: dom, curl, libxml, mbstring, pcntl, fileinfo, pdo, sqlite, pdo_sqlite, pdo_mysql, ftp, zip
31+
extensions: dom, curl, libxml, mbstring, pcntl, fileinfo, pdo, sqlite, pdo_sqlite, pdo_mysql, intl, ftp, zip
3232
coverage: pcov
3333

3434
- name: Setup Bun
@@ -71,7 +71,7 @@ jobs:
7171
uses: shivammathur/setup-php@v2
7272
with:
7373
php-version: ${{ matrix.php }}
74-
extensions: dom, curl, libxml, mbstring, pcntl, fileinfo, pdo, sqlite, pdo_sqlite, pdo_mysql, ftp, zip
74+
extensions: dom, curl, libxml, mbstring, pcntl, fileinfo, pdo, sqlite, pdo_sqlite, pdo_mysql, intl, ftp, zip
7575
coverage: pcov
7676

7777
- name: Setup problem matchers

.github/workflows/isolated-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
uses: shivammathur/setup-php@v2
5656
with:
5757
php-version: ${{ matrix.php }}
58-
extensions: dom, curl, libxml, mbstring, pcntl, fileinfo
58+
extensions: dom, curl, libxml, mbstring, pcntl, fileinfo, intl
5959
coverage: pcov
6060

6161
- name: Install PHPUnit

.github/workflows/validate-packages.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
uses: shivammathur/setup-php@v2
1717
with:
1818
php-version: 8.4
19-
extensions: dom, curl, libxml, mbstring, pcntl, fileinfo
19+
extensions: dom, curl, libxml, mbstring, pcntl, fileinfo, intl
2020
coverage: none
2121

2222
- name: Validate framework composer file

composer.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"egulias/email-validator": "^4.0.2",
88
"ext-dom": "*",
99
"ext-fileinfo": "*",
10+
"ext-intl": "*",
1011
"ext-libxml": "*",
1112
"ext-mbstring": "*",
1213
"ext-pdo": "*",
@@ -75,6 +76,7 @@
7576
"tempest/container": "self.version",
7677
"tempest/core": "self.version",
7778
"tempest/database": "self.version",
79+
"tempest/datetime": "self.version",
7880
"tempest/debug": "self.version",
7981
"tempest/discovery": "self.version",
8082
"tempest/event-bus": "self.version",
@@ -108,6 +110,7 @@
108110
"Tempest\\Container\\": "src/Tempest/Container/src",
109111
"Tempest\\Core\\": "src/Tempest/Core/src",
110112
"Tempest\\Database\\": "src/Tempest/Database/src",
113+
"Tempest\\DateTime\\": "src/Tempest/DateTime/src",
111114
"Tempest\\Debug\\": "src/Tempest/Debug/src",
112115
"Tempest\\Discovery\\": "src/Tempest/Discovery/src",
113116
"Tempest\\EventBus\\": "src/Tempest/EventBus/src",
@@ -127,22 +130,30 @@
127130
"Tempest\\Vite\\": "src/Tempest/Vite/src"
128131
},
129132
"files": [
133+
"src/Tempest/Clock/src/functions.php",
130134
"src/Tempest/CommandBus/src/functions.php",
131135
"src/Tempest/Container/src/functions.php",
132136
"src/Tempest/Core/src/functions.php",
133137
"src/Tempest/Database/src/functions.php",
138+
"src/Tempest/DateTime/src/constants.php",
139+
"src/Tempest/DateTime/src/functions.php",
134140
"src/Tempest/Debug/src/functions.php",
135141
"src/Tempest/EventBus/src/functions.php",
136142
"src/Tempest/Mapper/src/functions.php",
137143
"src/Tempest/Reflection/src/functions.php",
138144
"src/Tempest/Router/src/functions.php",
139145
"src/Tempest/Support/src/Arr/functions.php",
146+
"src/Tempest/Support/src/Comparison/functions.php",
140147
"src/Tempest/Support/src/Html/functions.php",
148+
"src/Tempest/Support/src/Json/functions.php",
141149
"src/Tempest/Support/src/Language/functions.php",
150+
"src/Tempest/Support/src/Math/constants.php",
151+
"src/Tempest/Support/src/Math/functions.php",
142152
"src/Tempest/Support/src/Namespace/functions.php",
143153
"src/Tempest/Support/src/Path/functions.php",
144154
"src/Tempest/Support/src/Random/functions.php",
145155
"src/Tempest/Support/src/Regex/functions.php",
156+
"src/Tempest/Support/src/Str/constants.php",
146157
"src/Tempest/Support/src/Str/functions.php",
147158
"src/Tempest/Support/src/functions.php",
148159
"src/Tempest/View/src/functions.php",
@@ -159,6 +170,7 @@
159170
"Tempest\\Container\\Tests\\": "src/Tempest/Container/tests",
160171
"Tempest\\Core\\Tests\\": "src/Tempest/Core/tests",
161172
"Tempest\\Database\\Tests\\": "src/Tempest/Database/tests",
173+
"Tempest\\DateTime\\Tests\\": "src/Tempest/DateTime/tests",
162174
"Tempest\\Discovery\\Tests\\": "src/Tempest/Discovery/tests",
163175
"Tempest\\EventBus\\Tests\\": "src/Tempest/EventBus/tests",
164176
"Tempest\\Filesystem\\Tests\\": "src/Tempest/Filesystem/tests",

src/Tempest/Cache/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"psr/cache": "^3.0",
77
"symfony/cache": "^7.2",
88
"tempest/core": "dev-main",
9+
"tempest/clock": "dev-main",
910
"tempest/container": "dev-main"
1011
},
1112
"require-dev": {

src/Tempest/Cache/src/Cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace Tempest\Cache;
66

77
use Closure;
8-
use DateTimeInterface;
98
use Psr\Cache\CacheItemInterface;
9+
use Tempest\DateTime\DateTimeInterface;
1010

1111
interface Cache
1212
{

src/Tempest/Cache/src/IsCache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
namespace Tempest\Cache;
66

77
use Closure;
8-
use DateTimeInterface;
98
use Psr\Cache\CacheItemInterface;
109
use Psr\Cache\CacheItemPoolInterface;
10+
use Tempest\DateTime\DateTimeInterface;
1111

1212
trait IsCache
1313
{
@@ -20,7 +20,7 @@ public function put(string $key, mixed $value, ?DateTimeInterface $expiresAt = n
2020
->set($value);
2121

2222
if ($expiresAt !== null) {
23-
$item = $item->expiresAt($expiresAt);
23+
$item = $item->expiresAt($expiresAt->toNativeDateTime());
2424
}
2525

2626
if ($this->isEnabled()) {

src/Tempest/Clock/composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
"description": "A clock component that handle few simple clock operations.",
44
"require": {
55
"php": "^8.4",
6-
"psr/clock": "^1.0.0"
6+
"psr/clock": "^1.0.0",
7+
"tempest/datetime": "dev-main"
78
},
89
"autoload": {
910
"psr-4": {
1011
"Tempest\\Clock\\": "src"
11-
}
12+
},
13+
"files": [
14+
"src/functions.php"
15+
]
1216
},
1317
"autoload-dev": {
1418
"psr-4": {

src/Tempest/Clock/src/Clock.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,34 @@
44

55
namespace Tempest\Clock;
66

7-
use DateTimeImmutable;
87
use Psr\Clock\ClockInterface;
8+
use Tempest\DateTime\DateTimeInterface;
9+
use Tempest\DateTime\Duration;
910

10-
interface Clock extends ClockInterface
11+
interface Clock
1112
{
12-
public function now(): DateTimeImmutable;
13+
/**
14+
* Returns the current date and time.
15+
*/
16+
public function now(): DateTimeInterface;
1317

14-
public function time(): int;
18+
/**
19+
* Returns the same instance as a PSR-7 Clock Interface.
20+
*/
21+
public function toPsrClock(): ClockInterface;
1522

16-
public function sleep(int $seconds): void;
23+
/**
24+
* Returns the current timestamp in seconds.
25+
*/
26+
public function timestamp(): int;
27+
28+
/**
29+
* Returns the current timestamp in milliseconds.
30+
*/
31+
public function timestampMs(): int;
32+
33+
/**
34+
* Sleeps for the given number of milliseconds.
35+
*/
36+
public function sleep(int|Duration $milliseconds): void;
1737
}

src/Tempest/Clock/src/GenericClock.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,42 @@
44

55
namespace Tempest\Clock;
66

7-
use DateTimeImmutable;
7+
use Psr\Clock\ClockInterface;
8+
use Tempest\DateTime\DateTime;
9+
use Tempest\DateTime\DateTimeInterface;
10+
use Tempest\DateTime\Duration;
11+
use Tempest\DateTime\Timestamp;
12+
13+
use const Tempest\DateTime\MILLISECONDS_PER_SECOND;
814

915
final class GenericClock implements Clock
1016
{
11-
public function now(): DateTimeImmutable
17+
public function toPsrClock(): ClockInterface
18+
{
19+
return new PsrClock($this);
20+
}
21+
22+
public function now(): DateTimeInterface
1223
{
13-
return new DateTimeImmutable('now');
24+
return DateTime::now();
1425
}
1526

16-
public function time(): int
27+
public function timestamp(): int
1728
{
18-
return hrtime(true);
29+
return Timestamp::monotonic()->getSeconds();
1930
}
2031

21-
public function sleep(int $seconds): void
32+
public function timestampMs(): int
2233
{
23-
sleep($seconds);
34+
return Timestamp::monotonic()->getMilliseconds();
35+
}
36+
37+
public function sleep(int|Duration $milliseconds): void
38+
{
39+
if ($milliseconds instanceof Duration) {
40+
$milliseconds = (int) $milliseconds->getTotalMilliseconds();
41+
}
42+
43+
usleep($milliseconds * MILLISECONDS_PER_SECOND);
2444
}
2545
}

0 commit comments

Comments
 (0)