Skip to content

Commit 5ab770f

Browse files
committed
DateTime::fromParts() uses setDate() & setTime()
1 parent 466a93d commit 5ab770f

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

src/Utils/DateTime.php

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

1010
namespace Nette\Utils;
1111

12-
use Nette;
1312
use function array_merge, checkdate, implode, is_numeric, is_string, preg_replace_callback, sprintf, time, trim;
1413

1514

@@ -61,7 +60,7 @@ public static function from(string|int|\DateTimeInterface|null $time): static
6160

6261
/**
6362
* Creates DateTime object.
64-
* @throws Nette\InvalidArgumentException if the date and time are not valid.
63+
* @throws \Exception if the date and time are not valid.
6564
*/
6665
public static function fromParts(
6766
int $year,
@@ -72,17 +71,10 @@ public static function fromParts(
7271
float $second = 0.0,
7372
): static
7473
{
75-
$s = sprintf('%04d-%02d-%02d %02d:%02d:%02.5F', $year, $month, $day, $hour, $minute, $second);
76-
if (
77-
!checkdate($month, $day, $year)
78-
|| $hour < 0 || $hour > 23
79-
|| $minute < 0 || $minute > 59
80-
|| $second < 0 || $second >= 60
81-
) {
82-
throw new Nette\InvalidArgumentException("Invalid date '$s'");
83-
}
84-
85-
return new static($s);
74+
$sec = (int) floor($second);
75+
return (new static(''))
76+
->setDate($year, $month, $day)
77+
->setTime($hour, $minute, $sec, (int) round(($second - $sec) * 1e6));
8678
}
8779

8880

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)