Skip to content

Commit 76be065

Browse files
committed
feat(datetime): introduce DateTime component
Signed-off-by: azjezz <azjezz@protonmail.com>
1 parent 6e23573 commit 76be065

File tree

102 files changed

+5902
-246
lines changed

Some content is hidden

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

102 files changed

+5902
-246
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ compare-benchmark-to-reference:
2222
./vendor/bin/phpbench run --config config/phpbench.json --ref=benchmark_reference
2323

2424
static-analysis: ## run static analysis checks
25-
./vendor/bin/psalm -c config/psalm.xml --show-info=true --no-cache --threads=1
26-
./vendor/bin/psalm -c config/psalm.xml tests/static-analysis --no-cache --threads=1
25+
./vendor/bin/psalm -c config/psalm.xml --show-info=true --no-cache --threads=2
26+
./vendor/bin/psalm -c config/psalm.xml tests/static-analysis --no-cache --threads=2
2727

2828
type-coverage: ## send static analysis type coverage metrics to https://shepherd.dev/
2929
./vendor/bin/psalm -c config/psalm.xml --shepherd --stats --threads=1

config/.phpcs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
<rule ref="PSR2">
3232
<type>error</type>
33+
<exclude name="PSR2.ControlStructures.ControlStructureSpacing.SpacingAfterOpenBrace"/>
3334
</rule>
3435

3536
<rule ref="PSR12">

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- [Psl\Collection](./component/collection.md)
2121
- [Psl\Comparison](./component/comparison.md)
2222
- [Psl\DataStructure](./component/data-structure.md)
23+
- [Psl\DateTime](./component/date-time.md)
2324
- [Psl\Dict](./component/dict.md)
2425
- [Psl\Encoding\Base64](./component/encoding-base64.md)
2526
- [Psl\Encoding\Hex](./component/encoding-hex.md)

docs/component/date-time.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<!--
2+
This markdown file was generated using `docs/documenter.php`.
3+
4+
Any edits to it will likely be lost.
5+
-->
6+
7+
[*index](./../README.md)
8+
9+
---
10+
11+
### `Psl\DateTime` Component
12+
13+
#### `Constants`
14+
15+
- [DAYS_PER_WEEK](./../../src/Psl/DateTime/constants.php#L0)
16+
- [HOURS_PER_DAY](./../../src/Psl/DateTime/constants.php#L0)
17+
- [HOURS_PER_WEEK](./../../src/Psl/DateTime/constants.php#L0)
18+
- [MICROSECONDS_PER_MILLISECOND](./../../src/Psl/DateTime/constants.php#L0)
19+
- [MICROSECONDS_PER_SECOND](./../../src/Psl/DateTime/constants.php#L0)
20+
- [MILLISECONDS_PER_SECOND](./../../src/Psl/DateTime/constants.php#L0)
21+
- [MINUTES_PER_DAY](./../../src/Psl/DateTime/constants.php#L0)
22+
- [MINUTES_PER_HOUR](./../../src/Psl/DateTime/constants.php#L0)
23+
- [MINUTES_PER_WEEK](./../../src/Psl/DateTime/constants.php#L0)
24+
- [NANOSECONDS_PER_MICROSECOND](./../../src/Psl/DateTime/constants.php#L0)
25+
- [NANOSECONDS_PER_MILLISECOND](./../../src/Psl/DateTime/constants.php#L0)
26+
- [NANOSECONDS_PER_SECOND](./../../src/Psl/DateTime/constants.php#L0)
27+
- [SECONDS_PER_DAY](./../../src/Psl/DateTime/constants.php#L0)
28+
- [SECONDS_PER_HOUR](./../../src/Psl/DateTime/constants.php#L0)
29+
- [SECONDS_PER_MINUTE](./../../src/Psl/DateTime/constants.php#L0)
30+
- [SECONDS_PER_WEEK](./../../src/Psl/DateTime/constants.php#L0)
31+
32+
#### `Functions`
33+
34+
- [is_leap_year](./../../src/Psl/DateTime/is_leap_year.php#L17)
35+
36+
#### `Interfaces`
37+
38+
- [DateTimeInterface](./../../src/Psl/DateTime/DateTimeInterface.php#L9)
39+
- [TemporalInterface](./../../src/Psl/DateTime/TemporalInterface.php#L19)
40+
- [ZonedTemporalInterface](./../../src/Psl/DateTime/ZonedTemporalInterface.php#L16)
41+
42+
#### `Classes`
43+
44+
- [DateTime](./../../src/Psl/DateTime/DateTime.php#L15)
45+
- [Duration](./../../src/Psl/DateTime/Duration.php#L31)
46+
- [Timestamp](./../../src/Psl/DateTime/Timestamp.php#L17)
47+
48+
#### `Traits`
49+
50+
- [DateTimeConvenienceMethodsTrait](./../../src/Psl/DateTime/DateTimeConvenienceMethodsTrait.php#L12)
51+
- [TemporalConvenienceMethodsTrait](./../../src/Psl/DateTime/TemporalConvenienceMethodsTrait.php#L15)
52+
- [ZonedTemporalConvenienceMethodsTrait](./../../src/Psl/DateTime/ZonedTemporalConvenienceMethodsTrait.php#L12)
53+
54+
#### `Enums`
55+
56+
- [Era](./../../src/Psl/DateTime/Era.php#L14)
57+
- [FormatDateStyle](./../../src/Psl/DateTime/FormatDateStyle.php#L23)
58+
- [FormatPattern](./../../src/Psl/DateTime/FormatPattern.php#L15)
59+
- [FormatTimeStyle](./../../src/Psl/DateTime/FormatTimeStyle.php#L23)
60+
- [Meridiem](./../../src/Psl/DateTime/Meridiem.php#L14)
61+
- [Month](./../../src/Psl/DateTime/Month.php#L15)
62+
- [Timezone](./../../src/Psl/DateTime/Timezone.php#L21)
63+
- [Weekday](./../../src/Psl/DateTime/Weekday.php#L15)
64+
65+

docs/component/file.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
#### `Classes`
2929

3030
- [Lock](./../../src/Psl/File/Lock.php#L9)
31-
- [ReadHandle](./../../src/Psl/File/ReadHandle.php#L10)
32-
- [ReadWriteHandle](./../../src/Psl/File/ReadWriteHandle.php#L11)
33-
- [WriteHandle](./../../src/Psl/File/WriteHandle.php#L11)
31+
- [ReadHandle](./../../src/Psl/File/ReadHandle.php#L11)
32+
- [ReadWriteHandle](./../../src/Psl/File/ReadWriteHandle.php#L12)
33+
- [WriteHandle](./../../src/Psl/File/WriteHandle.php#L12)
3434

3535
#### `Enums`
3636

docs/component/io.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
- [input_handle](./../../src/Psl/IO/input_handle.php#L20)
1717
- [output_handle](./../../src/Psl/IO/output_handle.php#L20)
1818
- [pipe](./../../src/Psl/IO/pipe.php#L24)
19-
- [streaming](./../../src/Psl/IO/streaming.php#L38)
19+
- [streaming](./../../src/Psl/IO/streaming.php#L41)
2020
- [write](./../../src/Psl/IO/write.php#L21)
2121
- [write_error](./../../src/Psl/IO/write_error.php#L23)
2222
- [write_error_line](./../../src/Psl/IO/write_error_line.php#L23)
@@ -41,7 +41,7 @@
4141
- [CloseWriteHandleInterface](./../../src/Psl/IO/CloseWriteHandleInterface.php#L7)
4242
- [CloseWriteStreamHandleInterface](./../../src/Psl/IO/CloseWriteStreamHandleInterface.php#L9)
4343
- [HandleInterface](./../../src/Psl/IO/HandleInterface.php#L21)
44-
- [ReadHandleInterface](./../../src/Psl/IO/ReadHandleInterface.php#L10)
44+
- [ReadHandleInterface](./../../src/Psl/IO/ReadHandleInterface.php#L12)
4545
- [ReadStreamHandleInterface](./../../src/Psl/IO/ReadStreamHandleInterface.php#L9)
4646
- [ReadWriteHandleInterface](./../../src/Psl/IO/ReadWriteHandleInterface.php#L7)
4747
- [ReadWriteStreamHandleInterface](./../../src/Psl/IO/ReadWriteStreamHandleInterface.php#L9)
@@ -54,32 +54,32 @@
5454
- [SeekWriteHandleInterface](./../../src/Psl/IO/SeekWriteHandleInterface.php#L7)
5555
- [SeekWriteStreamHandleInterface](./../../src/Psl/IO/SeekWriteStreamHandleInterface.php#L9)
5656
- [StreamHandleInterface](./../../src/Psl/IO/StreamHandleInterface.php#L9)
57-
- [WriteHandleInterface](./../../src/Psl/IO/WriteHandleInterface.php#L10)
57+
- [WriteHandleInterface](./../../src/Psl/IO/WriteHandleInterface.php#L12)
5858
- [WriteStreamHandleInterface](./../../src/Psl/IO/WriteStreamHandleInterface.php#L9)
5959

6060
#### `Classes`
6161

62-
- [CloseReadStreamHandle](./../../src/Psl/IO/CloseReadStreamHandle.php#L12)
63-
- [CloseReadWriteStreamHandle](./../../src/Psl/IO/CloseReadWriteStreamHandle.php#L12)
64-
- [CloseSeekReadStreamHandle](./../../src/Psl/IO/CloseSeekReadStreamHandle.php#L12)
65-
- [CloseSeekReadWriteStreamHandle](./../../src/Psl/IO/CloseSeekReadWriteStreamHandle.php#L12)
62+
- [CloseReadStreamHandle](./../../src/Psl/IO/CloseReadStreamHandle.php#L13)
63+
- [CloseReadWriteStreamHandle](./../../src/Psl/IO/CloseReadWriteStreamHandle.php#L13)
64+
- [CloseSeekReadStreamHandle](./../../src/Psl/IO/CloseSeekReadStreamHandle.php#L13)
65+
- [CloseSeekReadWriteStreamHandle](./../../src/Psl/IO/CloseSeekReadWriteStreamHandle.php#L13)
6666
- [CloseSeekStreamHandle](./../../src/Psl/IO/CloseSeekStreamHandle.php#L10)
67-
- [CloseSeekWriteStreamHandle](./../../src/Psl/IO/CloseSeekWriteStreamHandle.php#L12)
67+
- [CloseSeekWriteStreamHandle](./../../src/Psl/IO/CloseSeekWriteStreamHandle.php#L13)
6868
- [CloseStreamHandle](./../../src/Psl/IO/CloseStreamHandle.php#L10)
69-
- [CloseWriteStreamHandle](./../../src/Psl/IO/CloseWriteStreamHandle.php#L12)
70-
- [MemoryHandle](./../../src/Psl/IO/MemoryHandle.php#L13)
71-
- [ReadStreamHandle](./../../src/Psl/IO/ReadStreamHandle.php#L12)
72-
- [ReadWriteStreamHandle](./../../src/Psl/IO/ReadWriteStreamHandle.php#L12)
73-
- [Reader](./../../src/Psl/IO/Reader.php#L16)
74-
- [SeekReadStreamHandle](./../../src/Psl/IO/SeekReadStreamHandle.php#L12)
75-
- [SeekReadWriteStreamHandle](./../../src/Psl/IO/SeekReadWriteStreamHandle.php#L12)
69+
- [CloseWriteStreamHandle](./../../src/Psl/IO/CloseWriteStreamHandle.php#L13)
70+
- [MemoryHandle](./../../src/Psl/IO/MemoryHandle.php#L14)
71+
- [ReadStreamHandle](./../../src/Psl/IO/ReadStreamHandle.php#L13)
72+
- [ReadWriteStreamHandle](./../../src/Psl/IO/ReadWriteStreamHandle.php#L13)
73+
- [Reader](./../../src/Psl/IO/Reader.php#L17)
74+
- [SeekReadStreamHandle](./../../src/Psl/IO/SeekReadStreamHandle.php#L13)
75+
- [SeekReadWriteStreamHandle](./../../src/Psl/IO/SeekReadWriteStreamHandle.php#L13)
7676
- [SeekStreamHandle](./../../src/Psl/IO/SeekStreamHandle.php#L10)
77-
- [SeekWriteStreamHandle](./../../src/Psl/IO/SeekWriteStreamHandle.php#L12)
78-
- [WriteStreamHandle](./../../src/Psl/IO/WriteStreamHandle.php#L12)
77+
- [SeekWriteStreamHandle](./../../src/Psl/IO/SeekWriteStreamHandle.php#L13)
78+
- [WriteStreamHandle](./../../src/Psl/IO/WriteStreamHandle.php#L13)
7979

8080
#### `Traits`
8181

82-
- [ReadHandleConvenienceMethodsTrait](./../../src/Psl/IO/ReadHandleConvenienceMethodsTrait.php#L15)
83-
- [WriteHandleConvenienceMethodsTrait](./../../src/Psl/IO/WriteHandleConvenienceMethodsTrait.php#L16)
82+
- [ReadHandleConvenienceMethodsTrait](./../../src/Psl/IO/ReadHandleConvenienceMethodsTrait.php#L16)
83+
- [WriteHandleConvenienceMethodsTrait](./../../src/Psl/IO/WriteHandleConvenienceMethodsTrait.php#L17)
8484

8585

docs/component/shell.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#### `Functions`
1414

15-
- [execute](./../../src/Psl/Shell/execute.php#L41)
15+
- [execute](./../../src/Psl/Shell/execute.php#L42)
1616
- [stream_unpack](./../../src/Psl/Shell/stream_unpack.php#L30)
1717
- [unpack](./../../src/Psl/Shell/unpack.php#L16)
1818

docs/component/tcp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#### `Functions`
1414

15-
- [connect](./../../src/Psl/TCP/connect.php#L18)
15+
- [connect](./../../src/Psl/TCP/connect.php#L19)
1616

1717
#### `Classes`
1818

docs/component/unix.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#### `Functions`
1414

15-
- [connect](./../../src/Psl/Unix/connect.php#L18)
15+
- [connect](./../../src/Psl/Unix/connect.php#L19)
1616

1717
#### `Classes`
1818

docs/documenter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ function get_all_components(): array
191191
'Psl\\Collection',
192192
'Psl\\Comparison',
193193
'Psl\\DataStructure',
194+
'Psl\\DateTime',
194195
'Psl\\Dict',
195196
'Psl\\Encoding\\Base64',
196197
'Psl\\Encoding\\Hex',

examples/async/usleep.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,27 @@
55
namespace Psl\Example\IO;
66

77
use Psl\Async;
8+
use Psl\DateTime;
89
use Psl\IO;
910

1011
require __DIR__ . '/../../vendor/autoload.php';
1112

1213
Async\main(static function (): int {
13-
$start = time();
14+
$start = DateTime\Timestamp::monotonic();
1415

1516
Async\concurrently([
16-
static fn() => Async\sleep(2.0),
17-
static fn() => Async\sleep(2.0),
18-
static fn() => Async\sleep(2.0),
17+
static fn() => Async\sleep(DateTime\Duration::hours(0)),
18+
static fn() => Async\sleep(DateTime\Duration::minutes(0)),
19+
static fn() => Async\sleep(DateTime\Duration::zero()),
20+
static fn() => Async\sleep(DateTime\Duration::seconds(2)),
21+
static fn() => Async\sleep(DateTime\Duration::nanoseconds(20000000)),
22+
static fn() => Async\sleep(DateTime\Duration::microseconds(200000)),
23+
static fn() => Async\sleep(DateTime\Duration::milliseconds(2000)),
1924
]);
2025

21-
$duration = time() - $start;
26+
$duration = DateTime\Timestamp::monotonic()->since($start);
2227

23-
IO\write_error_line("duration: %d.", $duration);
28+
IO\write_error_line("duration : %s.", $duration->toString(max_decimals: 5));
2429

2530
return 0;
2631
});

examples/channel/main.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Psl\Async;
88
use Psl\Channel;
9+
use Psl\DateTime\Duration;
910
use Psl\IO;
1011

1112
require __DIR__ . '/../../vendor/autoload.php';
@@ -16,7 +17,7 @@
1617
*/
1718
[$receiver, $sender] = Channel\unbounded();
1819

19-
Async\Scheduler::delay(1, static function () use ($sender) {
20+
Async\Scheduler::delay(Duration::seconds(1), static function () use ($sender) {
2021
$sender->send('Hello, World!');
2122
});
2223

examples/io/benchmark.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
namespace Psl\Example\IO;
66

77
use Psl\Async;
8+
use Psl\DateTime;
89
use Psl\IO;
10+
use Psl\Math;
911
use Psl\Regex;
12+
1013
use function fopen;
1114
use function getopt;
1215
use function memory_get_peak_usage;
13-
use function microtime;
14-
use function round;
16+
1517
use const PHP_OS_FAMILY;
1618

1719
require __DIR__ . '/../../vendor/autoload.php';
@@ -26,7 +28,7 @@
2628
$args = getopt('i:o:t:');
2729
$input_file = $args['i'] ?? '/dev/zero';
2830
$output_file = $args['o'] ?? '/dev/null';
29-
$seconds = (int)($args['t'] ?? 5);
31+
$seconds = DateTime\Duration::seconds((int)($args['t'] ?? 5));
3032

3133
// passing file descriptors requires mapping paths (https://bugs.php.net/bug.php?id=53465)
3234
$input_file = Regex\replace($input_file, '(^/dev/fd/)', 'php://fd/');
@@ -39,7 +41,7 @@
3941

4042
Async\Scheduler::delay($seconds, static fn() => $input->close());
4143

42-
$start = microtime(true);
44+
$start = DateTime\Timestamp::monotonic();
4345
$i = 0;
4446
try {
4547
while ($chunk = $input->read(65536)) {
@@ -51,12 +53,12 @@
5153
} catch (IO\Exception\AlreadyClosedException) {
5254
}
5355

54-
$seconds = microtime(true) - $start;
56+
$duration = DateTime\Timestamp::monotonic()->since($start);
5557
$bytes = $i * 65536;
56-
$bytes_formatted = round($bytes / 1024 / 1024 / $seconds, 1);
58+
$bytes_formatted = Math\round($bytes / 1024 / 1024 / $duration->getTotalSeconds(), 1);
5759

58-
IO\write_error_line('read %d byte(s) in %d second(s) => %dMiB/s', $bytes, round($seconds, 3), $bytes_formatted);
59-
IO\write_error_line('peak memory usage of %dMiB', round(memory_get_peak_usage(true) / 1024 / 1024, 1));
60+
IO\write_error_line('read %d byte(s) in %s => %dMiB/s', $bytes, $duration->toString(), $bytes_formatted);
61+
IO\write_error_line('peak memory usage of %dMiB', Math\round(memory_get_peak_usage(true) / 1024 / 1024, 1));
6062

6163
return 0;
6264
});

examples/io/pipe.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Psl\Example\IO;
66

77
use Psl\Async;
8+
use Psl\DateTime\Duration;
89
use Psl\IO;
910

1011
require __DIR__ . '/../../vendor/autoload.php';
@@ -16,7 +17,7 @@
1617
static function() use($read): void {
1718
IO\write_error_line("< sleeping.");
1819

19-
Async\sleep(0.01);
20+
Async\sleep(Duration::milliseconds(10));
2021

2122
IO\write_error_line("< waiting for content.");
2223

@@ -30,7 +31,7 @@ static function() use($read): void {
3031
static function() use($write): void {
3132
IO\write_error_line('> sleeping.');
3233

33-
Async\sleep(0.1);
34+
Async\sleep(Duration::milliseconds(100));
3435

3536
IO\write_error_line('> writing.');
3637

examples/io/queued.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
$he = Async\run(static fn(): string => $read->readFixedSize(2));
1717

18-
Async\sleep(0.001);
18+
Async\sleep(Psl\DateTime\Duration::milliseconds(200));
1919

2020
$write->write("hello");
2121

examples/run.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Psl\Example\IO;
77

88
use Psl\Async;
9+
use Psl\DateTime;
910
use Psl\Filesystem;
1011
use Psl\IO;
1112
use Psl\Shell;
@@ -34,9 +35,9 @@
3435
IO\write_error_line('- %s/%s -> started', $component, $script);
3536

3637
$awaitables[] = Async\run(static function() use($component, $script, $file): array {
37-
$start = microtime(true);
38+
$start = DateTime\Timestamp::monotonic();
3839
Shell\execute(PHP_BINARY, [$file]);
39-
$duration = microtime(true) - $start;
40+
$duration = DateTime\Timestamp::monotonic()->since($start);
4041

4142
return [$component, $script, $duration];
4243
});
@@ -46,7 +47,7 @@
4647
foreach (Async\Awaitable::iterate($awaitables) as $awaitable) {
4748
[$component, $script, $duration] = $awaitable->await();
4849

49-
IO\write_error_line('+ %s/%s -> finished in %ds', $component, $script, $duration);
50+
IO\write_error_line('+ %s/%s -> finished in %s', $component, $script, $duration->toString(10));
5051
}
5152

5253
return 0;

examples/shell/timeout.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
namespace Psl\Example\Shell;
66

77
use Psl\Async;
8+
use Psl\DateTime;
89
use Psl\IO;
910
use Psl\Shell;
1011

1112
require __DIR__ . '/../../vendor/autoload.php';
1213

1314
Async\main(static function (): void {
1415
try {
15-
Shell\execute('sleep', ['1'], timeout: 0.5);
16+
Shell\execute('sleep', ['1'], timeout: DateTime\Duration::milliseconds(500));
1617
} catch (Shell\Exception\TimeoutException $exception) {
1718
IO\write_error_line($exception->getMessage());
1819
}

0 commit comments

Comments
 (0)