Skip to content

Commit fd2e67c

Browse files
committed
added DateTime::fromParts()
1 parent 0b6bfc7 commit fd2e67c

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/Utils/DateTime.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ public static function from($time)
5858
}
5959

6060

61+
/**
62+
* Creates DateTime object.
63+
* @return static
64+
*/
65+
public static function fromParts($year, $month, $day, $hour = 0, $minute = 0, $second = 0)
66+
{
67+
$s = sprintf("%04d-%02d-%02d %02d:%02d:%02.5f", $year, $month, $day, $hour, $minute, $second);
68+
if (!checkdate($month, $day, $year) || $hour < 0 || $hour > 23 || $minute < 0 || $minute > 59 || $second < 0 || $second >= 60) {
69+
throw new Nette\InvalidArgumentException("Invalid date '$s'");
70+
}
71+
return new static($s);
72+
}
73+
74+
6175
/**
6276
* @return string
6377
*/
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Utils\DateTime::fromParts().
5+
*/
6+
7+
use Tester\Assert;
8+
use Nette\Utils\DateTime;
9+
10+
require __DIR__ . '/../bootstrap.php';
11+
12+
13+
date_default_timezone_set('Europe/Prague');
14+
15+
Assert::same('0001-12-09 00:00:00.000000', DateTime::fromParts(1, 12, 9)->format('Y-m-d H:i:s.u'));
16+
Assert::same('0085-12-09 00:00:00.000000', DateTime::fromParts(85, 12, 9)->format('Y-m-d H:i:s.u'));
17+
Assert::same('1985-01-01 00:00:00.000000', DateTime::fromParts(1985, 1, 1)->format('Y-m-d H:i:s.u'));
18+
Assert::same('1985-12-19 00:00:00.000000', DateTime::fromParts(1985, 12, 19)->format('Y-m-d H:i:s.u'));
19+
Assert::same('1985-12-09 01:02:00.000000', DateTime::fromParts(1985, 12, 9, 1, 2)->format('Y-m-d H:i:s.u'));
20+
Assert::same('1985-12-09 01:02:03.000000', DateTime::fromParts(1985, 12, 9, 1, 2, 3)->format('Y-m-d H:i:s.u'));
21+
Assert::same('1985-12-09 11:22:33.000000', DateTime::fromParts(1985, 12, 9, 11, 22, 33)->format('Y-m-d H:i:s.u'));
22+
Assert::same('1985-12-09 11:22:59.123000', DateTime::fromParts(1985, 12, 9, 11, 22, 59.123)->format('Y-m-d H:i:s.u'));
23+
24+
Assert::exception(function () {
25+
DateTime::fromParts(1985, 2, 29);
26+
}, Nette\InvalidArgumentException::class, "Invalid date '1985-02-29 00:00:0.00000'");
27+
28+
Assert::exception(function () { // year must be at least 1 due to limitation of checkdate()
29+
DateTime::fromParts(0, 12, 9);
30+
}, Nette\InvalidArgumentException::class);
31+
32+
Assert::exception(function () {
33+
DateTime::fromParts(1985, 0, 9);
34+
}, Nette\InvalidArgumentException::class);
35+
36+
Assert::exception(function () {
37+
DateTime::fromParts(1985, 13, 9);
38+
}, Nette\InvalidArgumentException::class);
39+
40+
Assert::exception(function () {
41+
DateTime::fromParts(1985, 12, 0);
42+
}, Nette\InvalidArgumentException::class);
43+
44+
Assert::exception(function () {
45+
DateTime::fromParts(1985, 12, 32);
46+
}, Nette\InvalidArgumentException::class);
47+
48+
Assert::exception(function () {
49+
DateTime::fromParts(1985, 12, 9, -1);
50+
}, Nette\InvalidArgumentException::class);
51+
52+
Assert::exception(function () {
53+
DateTime::fromParts(1985, 12, 9, 60);
54+
}, Nette\InvalidArgumentException::class);
55+
56+
Assert::exception(function () {
57+
DateTime::fromParts(1985, 12, 9, 0, -1);
58+
}, Nette\InvalidArgumentException::class);
59+
60+
Assert::exception(function () {
61+
DateTime::fromParts(1985, 12, 9, 0, 60);
62+
}, Nette\InvalidArgumentException::class);
63+
64+
Assert::exception(function () {
65+
DateTime::fromParts(1985, 12, 9, 0, 0, -1);
66+
}, Nette\InvalidArgumentException::class);
67+
68+
Assert::exception(function () {
69+
DateTime::fromParts(1985, 12, 9, 0, 0, 60);
70+
}, Nette\InvalidArgumentException::class);

0 commit comments

Comments
 (0)