|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: custom properties of Nette\Utils\DateTime. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +use Nette\Utils\DateTime; |
| 10 | +use Tester\Assert; |
| 11 | + |
| 12 | +require __DIR__ . '/../bootstrap.php'; |
| 13 | + |
| 14 | + |
| 15 | +date_default_timezone_set('Europe/Prague'); |
| 16 | + |
| 17 | +Assert::same(1978, DateTime::from('1978-01-23 11:40:00.86')->year); |
| 18 | +Assert::same(1, DateTime::from('1978-01-23 11:40:00.86')->month); |
| 19 | +Assert::same(23, DateTime::from('1978-01-23 11:40:00.86')->day); |
| 20 | +Assert::same(11, DateTime::from('1978-01-23 11:40:00.86')->hour); |
| 21 | +Assert::same(40, DateTime::from('1978-01-23 11:40:00.86')->minute); |
| 22 | +Assert::same(0, DateTime::from('1978-01-23 11:40:00.86')->second); |
| 23 | +Assert::same(863500, DateTime::from('1978-01-23 11:40:00.8635')->microsecond); |
| 24 | +Assert::same(863, DateTime::from('1978-01-23 11:40:00.8635')->millisecond); |
| 25 | +Assert::same('1978-01-23', DateTime::from('1978-01-23 11:40:00.86')->date); |
| 26 | +Assert::same('1978-01-23 11:40:00', DateTime::from('1978-01-23 11:40:00.86')->dateTime); |
| 27 | +Assert::same('1978-01-23 11:40:00+0100', DateTime::from('1978-01-23 11:40:00.86')->dateTimeTz); |
| 28 | +Assert::same('1978-01-23 11:40:00.86', DateTime::from('1978-01-23 11:40:00.860000')->dateTimeMicro); |
| 29 | +Assert::same('1978-01-23 11:40:00.0', DateTime::from('1978-01-23 11:40:00.000000')->dateTimeMicro); |
| 30 | +Assert::same('1978-01-23 11:40:00.86+0100', DateTime::from('1978-01-23 11:40:00.860000')->dateTimeMicroTz); |
| 31 | +Assert::same(254_400_000.86, DateTime::from('1978-01-23 11:40:00.860000')->timestampMicro); |
| 32 | +Assert::same(254_400_000, DateTime::from('1978-01-23 11:40:00.860000')->timestamp); |
| 33 | +Assert::same(1, DateTime::from('1978-01-23 11:40:00.860000')->dayOfWeek); |
| 34 | + |
| 35 | +/** |
| 36 | + * reverse engineering does not work, it is reason why keep zero for microsecond |
| 37 | + * @see \Nette\Utils\DateTimeFormat::getDateTimeMicro() |
| 38 | + */ |
| 39 | +Assert::type(DateTime::class, DateTime::createFromFormat('Y-m-d H:i:s.u', '1978-01-23 11:40:00.0')); |
| 40 | +Assert::false(DateTime::createFromFormat('Y-m-d H:i:s.u', '1978-01-23 11:40:00.')); |
| 41 | +Assert::false(DateTime::createFromFormat('Y-m-d H:i:s.u', '1978-01-23 11:40:00')); |
0 commit comments