Skip to content

Commit 28943ea

Browse files
committed
feat: add more constructors to durations
1 parent 468ea75 commit 28943ea

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

packages/datetime/src/Duration.php

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
use Tempest\Support\Str;
1111

1212
/**
13-
* Defines a representation of a time duration with specific hours, minutes, seconds,
14-
* and nanoseconds.
13+
* Defines a representation of a time duration with specific hours, minutes, seconds, and nanoseconds.
1514
*
1615
* All instances are normalized as follows:
1716
*
@@ -27,8 +26,7 @@
2726
final readonly class Duration implements Comparison\Comparable, Comparison\Equable, JsonSerializable, Stringable
2827
{
2928
/**
30-
* Initializes a new instance of Duration with specified hours, minutes, seconds, and
31-
* nanoseconds.
29+
* Initializes a new instance of Duration with specified hours, minutes, seconds, and nanoseconds.
3230
*
3331
* @param int<-59, 59> $minutes
3432
* @param int<-59, 59> $seconds
@@ -46,7 +44,6 @@ private function __construct(
4644
* optionally minutes, seconds, nanoseconds). Due to normalization, the
4745
* actual values in the returned instance may differ from the provided ones.
4846
*
49-
*
5047
* @mago-expect best-practices/no-else-clause
5148
*/
5249
public static function fromParts(int $hours, int $minutes = 0, int $seconds = 0, int $nanoseconds = 0): self
@@ -70,70 +67,102 @@ public static function fromParts(int $hours, int $minutes = 0, int $seconds = 0,
7067
}
7168

7269
/**
73-
* Returns an instance representing the specified number of weeks, in hours.
74-
*
75-
* For example, `Duration::weeks(1)` is equivalent to `Duration::hours(168)`.
76-
*
70+
* Returns an instance representing the specified number of weeks, in hours. For example, `Duration::weeks(1)` is equivalent to `Duration::hours(168)`.
7771
*/
7872
public static function weeks(int $weeks): self
7973
{
8074
return self::fromParts($weeks * HOURS_PER_WEEK);
8175
}
8276

77+
/**
78+
* Returns an instance representing exactly one week.
79+
*/
80+
public static function week(): self
81+
{
82+
return self::weeks(1);
83+
}
84+
8385
/**
8486
* Returns an instance representing the specified number of days, in hours.
8587
*
8688
* For example, `Duration::days(2)` is equivalent to `Duration::hours(48)`.
87-
*
8889
*/
8990
public static function days(int $days): self
9091
{
9192
return self::fromParts($days * HOURS_PER_DAY);
9293
}
9394

95+
/**
96+
* Returns an instance representing exactly one day.
97+
*/
98+
public static function day(): self
99+
{
100+
return self::days(1);
101+
}
102+
94103
/**
95104
* Returns an instance representing the specified number of hours.
96-
*
97105
*/
98106
public static function hours(int $hours): self
99107
{
100108
return self::fromParts($hours);
101109
}
102110

111+
/**
112+
* Returns an instance representing exactly one hour.
113+
*/
114+
public static function hour(): self
115+
{
116+
return self::hours(1);
117+
}
118+
103119
/**
104120
* Returns an instance representing the specified number of minutes. Due to
105121
* normalization, the actual value in the returned instance may differ from
106122
* the provided one, and the resulting instance may contain larger units.
107123
*
108124
* For example, `Duration::minutes(63)` normalizes to "1 hour(s), 3 minute(s)".
109-
*
110125
*/
111126
public static function minutes(int $minutes): self
112127
{
113128
return self::fromParts(0, $minutes);
114129
}
115130

131+
/**
132+
* Returns an instance representing exactly one minute.
133+
*/
134+
public static function minute(): self
135+
{
136+
return self::minutes(1);
137+
}
138+
116139
/**
117140
* Returns an instance representing the specified number of seconds. Due to
118141
* normalization, the actual value in the returned instance may differ from
119142
* the provided one, and the resulting instance may contain larger units.
120143
*
121144
* For example, `Duration::seconds(63)` normalizes to "1 minute(s), 3 second(s)".
122-
*
123145
*/
124146
public static function seconds(int $seconds): self
125147
{
126148
return self::fromParts(0, 0, $seconds);
127149
}
128150

151+
/**
152+
* Returns an instance representing exactly one second.
153+
*/
154+
public static function second(): self
155+
{
156+
return self::seconds(1);
157+
}
158+
129159
/**
130160
* Returns an instance representing the specified number of milliseconds (ms).
131161
* The value is converted and stored as nanoseconds, since that is the only
132162
* unit smaller than a second that we support. Due to normalization, the
133163
* resulting instance may contain larger units.
134164
*
135165
* For example, `Duration::milliseconds(8042)` normalizes to "8 second(s), 42000000 nanosecond(s)".
136-
*
137166
*/
138167
public static function milliseconds(int $milliseconds): self
139168
{

packages/datetime/tests/DurationTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,15 @@ public function test_getters(): void
3535
public function test_named_constructors(): void
3636
{
3737
$this->assertSame(168.0, DateTime\Duration::weeks(1)->getTotalHours());
38+
$this->assertSame(168.0, DateTime\Duration::week()->getTotalHours());
3839
$this->assertSame(24.0, DateTime\Duration::days(1)->getTotalHours());
40+
$this->assertSame(24.0, DateTime\Duration::day()->getTotalHours());
3941
$this->assertSame(1.0, DateTime\Duration::hours(1)->getTotalHours());
42+
$this->assertSame(1.0, DateTime\Duration::hour()->getTotalHours());
4043
$this->assertSame(1.0, DateTime\Duration::minutes(1)->getTotalMinutes());
44+
$this->assertSame(1.0, DateTime\Duration::minute()->getTotalMinutes());
4145
$this->assertSame(1.0, DateTime\Duration::seconds(1)->getTotalSeconds());
46+
$this->assertSame(1.0, DateTime\Duration::second()->getTotalSeconds());
4247
$this->assertSame(1.0, DateTime\Duration::milliseconds(1)->getTotalMilliseconds());
4348
$this->assertSame(1.0, DateTime\Duration::microseconds(1)->getTotalMicroseconds());
4449
$this->assertSame(1, DateTime\Duration::nanoseconds(1)->getNanoseconds());

0 commit comments

Comments
 (0)