1- # A really simple library for converting time
1+ # An advanced library offering type safety and identity with Durations
22
3- This is a simple library for interacting with time durations.
4- You can write your code to expect time and know that is what you have:
3+ ## Identity
4+
5+ This library uses some tricks to intern values so that two times are always equal to one another,
6+ no matter the distance in time or space.
7+
8+ ``` php
9+ use Withinboredom\Time;
10+ use Withinboredom\Time\Unit;
11+
12+ $hour = Time::from(Unit::Hours, 1);
13+ $minutes = Time::from(Unit::Minutes, 60);
14+
15+ echo $hour === $minutes ? 'true' : 'false'
16+ // outputs: true
17+ ```
18+
19+ ## Type Safety
20+
21+ You can ensure nobody will accidentally confuse seconds with milliseconds or minutes with seconds:
522
623``` php
7- function sleep(\Withinboredom\Time\ Time $time): void {
8- \sleep($time->as(\Withinboredom\Time\TimeUnit ::Seconds));
24+ function sleep(Time $time): void {
25+ \sleep($time->as(Unit ::Seconds));
926}
1027
11- sleep(\Withinboredom\Time\Minutes(5));
28+ // Helper functions are included so you can type less code:
29+ sleep(Minutes(5));
1230```
1331
14- ## Equality
32+ ## Conversions and Math
1533
16- All values of the same time are always strongly equaled to each other :
34+ You can easily convert between units and even perform operations, like sorting and arithmetic :
1735
1836``` php
19- \Withinboredom\Time\Minutes(60) === \Withinboredom\Time\Hours(1)
37+ // use the hour constant to get one hour
38+ $hour = Hour;
39+
40+ $hour = $hour->multiply(10)->add(Minutes(10)); // get 10:10 hours
41+
42+ $interval = $hour->toDateInterval();
43+
44+ echo Hours(10) < $hour ? 'true' : 'false';
45+ // output: true
2046```
2147
22- ## Utilities
48+ ## Support for Crell\Serde
2349
24- There are also a few utility methods:
50+ You cannot serialize/deserialize/clone ` Time ` objects.
51+ However, if you use something like Serde, you can still serialize your value objects:
2552
26- > ->add(Time)->subtract(Time): AnyTime
53+ ``` php
54+ class CacheItem {
55+ public function __construct(
56+ #[Field('expiration_in_seconds')]
57+ #[TimeAs(Unit::Seconds)]
58+ public Time $expiration,
59+ ) {}
60+ }
2761
28- Add and subtract durations.
62+ $serde = new SerdeCommon(handlers: new \Withinboredom\Time\SerdeExporter());
63+ $serde->serialize(new CacheItem(Minutes(5)), 'json');
64+ ```
2965
30- > ->toDateInterval(): DateInterval
66+ The above will be serialized (and deserialized) from:
3167
32- Creates a date interval for use in other things.
68+ ``` json
69+ {
70+ "expiration_in_seconds" : 300
71+ }
72+ ```
3373
3474## Units
3575
@@ -45,7 +85,7 @@ Creates a date interval for use in other things.
4585
4686> Why not months/years?
4787
48- There's no set days in a month/year, so it’s better to use ` DateInterval ` for those types of measures.
88+ There are no set days in a month/year, so it’s better to use ` DateInterval ` for those types of measures.
4989
5090> Why does this exist?
5191
0 commit comments