Skip to content

Commit 1cc92ca

Browse files
committed
DateTime::fromParts() uses setDate() & setTime()
1 parent ece1e8b commit 1cc92ca

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

src/Utils/DateTime.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
namespace Nette\Utils;
1111

12-
use Nette;
13-
1412

1513
/**
1614
* DateTime.
@@ -60,7 +58,7 @@ public static function from(string|int|\DateTimeInterface|null $time): static
6058

6159
/**
6260
* Creates DateTime object.
63-
* @throws Nette\InvalidArgumentException if the date and time are not valid.
61+
* @throws \Exception if the date and time are not valid.
6462
*/
6563
public static function fromParts(
6664
int $year,
@@ -71,17 +69,10 @@ public static function fromParts(
7169
float $second = 0.0,
7270
): static
7371
{
74-
$s = sprintf('%04d-%02d-%02d %02d:%02d:%02.5F', $year, $month, $day, $hour, $minute, $second);
75-
if (
76-
!checkdate($month, $day, $year)
77-
|| $hour < 0 || $hour > 23
78-
|| $minute < 0 || $minute > 59
79-
|| $second < 0 || $second >= 60
80-
) {
81-
throw new Nette\InvalidArgumentException("Invalid date '$s'");
82-
}
83-
84-
return new static($s);
72+
$sec = (int) floor($second);
73+
return (new static(''))
74+
->setDate($year, $month, $day)
75+
->setTime($hour, $minute, $sec, (int) round(($second - $sec) * 1e6));
8576
}
8677

8778

tests/Utils/DateTime.fromParts.phpt

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,61 +25,72 @@ Assert::same('1985-12-09 11:22:59.123000', DateTime::fromParts(1985, 12, 9, 11,
2525

2626
Assert::exception(
2727
fn() => DateTime::fromParts(1985, 2, 29),
28-
Nette\InvalidArgumentException::class,
29-
"Invalid date '1985-02-29 00:00:0.00000'",
28+
\Throwable::class,
29+
'The date 1985-02-29 is not valid.',
3030
);
3131

3232
Assert::exception(
3333
fn() => DateTime::fromParts(0, 12, 9),
34-
Nette\InvalidArgumentException::class,
34+
\Throwable::class,
35+
'The date 0000-12-09 is not valid.',
3536
);
3637

3738
Assert::exception(
3839
fn() => DateTime::fromParts(1985, 0, 9),
39-
Nette\InvalidArgumentException::class,
40+
\Throwable::class,
41+
'The date 1985-00-09 is not valid.',
4042
);
4143

4244
Assert::exception(
4345
fn() => DateTime::fromParts(1985, 13, 9),
44-
Nette\InvalidArgumentException::class,
46+
\Throwable::class,
47+
'The date 1985-13-09 is not valid.',
4548
);
4649

4750
Assert::exception(
4851
fn() => DateTime::fromParts(1985, 12, 0),
49-
Nette\InvalidArgumentException::class,
52+
\Throwable::class,
53+
'The date 1985-12-00 is not valid.',
5054
);
5155

5256
Assert::exception(
5357
fn() => DateTime::fromParts(1985, 12, 32),
54-
Nette\InvalidArgumentException::class,
58+
\Throwable::class,
59+
'The date 1985-12-32 is not valid.',
5560
);
5661

5762
Assert::exception(
5863
fn() => DateTime::fromParts(1985, 12, 9, -1),
59-
Nette\InvalidArgumentException::class,
64+
\Throwable::class,
65+
'The time -1:00:00.00000 is not valid.',
6066
);
6167

6268
Assert::exception(
6369
fn() => DateTime::fromParts(1985, 12, 9, 60),
64-
Nette\InvalidArgumentException::class,
70+
\Throwable::class,
71+
'The time 60:00:00.00000 is not valid.',
6572
);
6673

6774
Assert::exception(
6875
fn() => DateTime::fromParts(1985, 12, 9, 0, -1),
69-
Nette\InvalidArgumentException::class,
76+
\Throwable::class,
77+
'The time 00:-1:00.00000 is not valid.',
7078
);
7179

7280
Assert::exception(
7381
fn() => DateTime::fromParts(1985, 12, 9, 0, 60),
74-
Nette\InvalidArgumentException::class,
82+
\Throwable::class,
83+
'The time 00:60:00.00000 is not valid.',
7584
);
7685

7786
Assert::exception(
7887
fn() => DateTime::fromParts(1985, 12, 9, 0, 0, -1),
79-
Nette\InvalidArgumentException::class,
88+
\Throwable::class,
89+
'The time 00:00:-1.00000 is not valid.',
8090
);
8191

8292
Assert::exception(
8393
fn() => DateTime::fromParts(1985, 12, 9, 0, 0, 60),
84-
Nette\InvalidArgumentException::class,
94+
\Throwable::class,
95+
'The time 00:00:60.00000 is not valid.',
8596
);

0 commit comments

Comments
 (0)