Skip to content

Commit 35034bc

Browse files
committed
DateTimeFormat: support shortcut properties
1 parent 71017d2 commit 35034bc

File tree

3 files changed

+161
-1
lines changed

3 files changed

+161
-1
lines changed

src/Utils/DateTime.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,26 @@
1414

1515
/**
1616
* DateTime.
17+
* @property-read int $timestamp
18+
* @property-read int $year
19+
* @property-read int $month
20+
* @property-read int $day
21+
* @property-read int $hour
22+
* @property-read int $minute
23+
* @property-read int $second
24+
* @property-read int $microsecond
25+
* @property-read int $millisecond
26+
* @property-read string $date
27+
* @property-read string $dateTime
28+
* @property-read string $dateTimeTz
29+
* @property-read string $dateTimeMicro
30+
* @property-read string $dateTimeMicroTz
31+
* @property-read float $timestampMicro
1732
*/
1833
class DateTime extends \DateTime implements \JsonSerializable
1934
{
2035
use Nette\SmartObject;
36+
use DateTimeFormat;
2137

2238
/** minute in seconds */
2339
public const MINUTE = 60;
@@ -125,7 +141,7 @@ public function jsonSerialize(): string
125141
*/
126142
public function __toString(): string
127143
{
128-
return $this->format('Y-m-d H:i:s');
144+
return $this->getDateTime();
129145
}
130146

131147

src/Utils/DateTimeFormat.php

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\Utils;
11+
12+
/**
13+
* Date time format collection.
14+
*/
15+
trait DateTimeFormat
16+
{
17+
public function getYear(): int
18+
{
19+
return (int) $this->format('Y');
20+
}
21+
22+
23+
public function getMonth(): int
24+
{
25+
return (int) $this->format('n');
26+
}
27+
28+
29+
public function getDay(): int
30+
{
31+
return (int) $this->format('j');
32+
}
33+
34+
35+
public function getHour(): int
36+
{
37+
return (int) $this->format('G');
38+
}
39+
40+
41+
public function getMinute(): int
42+
{
43+
return (int) $this->format('i');
44+
}
45+
46+
47+
public function getSecond(): int
48+
{
49+
return (int) $this->format('s');
50+
}
51+
52+
53+
public function getMicrosecond(): int
54+
{
55+
return (int) $this->format('u');
56+
}
57+
58+
59+
public function getMillisecond(): int
60+
{
61+
return (int) $this->format('v');
62+
}
63+
64+
65+
public function getDate(): string
66+
{
67+
return $this->format('Y-m-d');
68+
}
69+
70+
71+
public function getDateTime(): string
72+
{
73+
return $this->format('Y-m-d H:i:s');
74+
}
75+
76+
77+
public function getDateTimeTz(): string
78+
{
79+
return $this->format('Y-m-d H:i:sO');
80+
}
81+
82+
83+
public function getDateTimeMicro(): string
84+
{
85+
$micro = rtrim($this->format('u'), '0');
86+
if ($micro === '') {
87+
$micro = '0';
88+
}
89+
90+
return $this->format('Y-m-d H:i:s.') . $micro;
91+
}
92+
93+
94+
public function getDateTimeMicroTz(): string
95+
{
96+
return $this->getDateTimeMicro() . $this->format('O');
97+
}
98+
99+
100+
public function getTimestampMicro(): float
101+
{
102+
return (float) $this->format('U.u');
103+
}
104+
}
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
34+
/**
35+
* reverse engineering does not work, it is reason why keep zero for microsecond
36+
* @see \Nette\Utils\DateTimeFormat::getDateTimeMicro()
37+
*/
38+
Assert::type(DateTime::class, DateTime::createFromFormat('Y-m-d H:i:s.u', '1978-01-23 11:40:00.0'));
39+
Assert::false(DateTime::createFromFormat('Y-m-d H:i:s.u', '1978-01-23 11:40:00.'));
40+
Assert::false(DateTime::createFromFormat('Y-m-d H:i:s.u', '1978-01-23 11:40:00'));

0 commit comments

Comments
 (0)