Skip to content

Commit 0165413

Browse files
committed
Deprecated DateTime, added DateHelpers as a replacement [BC break]
1 parent 55ffc78 commit 0165413

14 files changed

+363
-17
lines changed

src/Utils/DateHelpers.php

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
<?php
2+
3+
namespace Nette\Utils;
4+
5+
use Nette;
6+
7+
/**
8+
* DateTime manipulation helpers.
9+
*/
10+
class DateHelpers
11+
{
12+
/** minute in seconds */
13+
const MINUTE = 60;
14+
15+
/** hour in seconds */
16+
const HOUR = 60 * self::MINUTE;
17+
18+
/** day in seconds */
19+
const DAY = 24 * self::HOUR;
20+
21+
/** week in seconds */
22+
const WEEK = 7 * self::DAY;
23+
24+
/** average month in seconds */
25+
const MONTH = 2629800;
26+
27+
/** average year in seconds */
28+
const YEAR = 31557600;
29+
30+
31+
public function __construct()
32+
{
33+
throw new Nette\StaticClassException;
34+
}
35+
36+
37+
/**
38+
* @param string|int|\DateTimeInterface $time
39+
* @return \DateTime
40+
*/
41+
public static function createMutable($time)
42+
{
43+
if ($time instanceof \DateTime) {
44+
return clone $time;
45+
46+
} elseif ($time instanceof \DateTimeImmutable) {
47+
return \DateTime::createFromImmutable($time);
48+
49+
} elseif (is_numeric($time)) {
50+
if ($time <= self::YEAR) {
51+
$time += time();
52+
}
53+
return (new \DateTime('@' . $time))->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
54+
55+
} elseif ($time) {
56+
return new \DateTime($time);
57+
58+
} else {
59+
return new \DateTime();
60+
}
61+
}
62+
63+
64+
/**
65+
* @param string|int|\DateTimeInterface $time
66+
* @return \DateTimeImmutable
67+
*/
68+
public static function createImmutable($time)
69+
{
70+
if ($time instanceof \DateTimeImmutable) {
71+
return clone $time;
72+
73+
} elseif ($time instanceof \Datetime) {
74+
return \DateTimeImmutable::createFromMutable($time);
75+
76+
} elseif (is_numeric($time)) {
77+
if ($time <= self::YEAR) {
78+
$time += time();
79+
}
80+
return (new \DateTimeImmutable('@' . $time))->setTimeZone(new \DateTimeZone(date_default_timezone_get()));
81+
82+
} elseif ($time) {
83+
return new \DateTimeImmutable($time);
84+
85+
} else {
86+
return new \DateTimeImmutable();
87+
}
88+
}
89+
90+
91+
/**
92+
* Returns new \DateTime object formatted according to the specified format or FALSE on failure.
93+
* @param string The format the $time parameter should be in
94+
* @param string String representing the time
95+
* @param string|\DateTimeZone desired timezone (default timezone is used if NULL is passed)
96+
* @return \DateTime|FALSE
97+
*/
98+
public static function createMutableFromFormat($format, $time, $timezone = NULL)
99+
{
100+
$timezone = self::checkTimezone($timezone);
101+
return \DateTime::createFromFormat($format, $time, $timezone);
102+
}
103+
104+
105+
/**
106+
* Returns new \DateTimeImmutable object formatted according to the specified format or FALSE on failure.
107+
* @param string The format the $time parameter should be in
108+
* @param string String representing the time
109+
* @param string|\DateTimeZone desired timezone (default timezone is used if NULL is passed)
110+
* @return \DateTimeImmutable|FALSE
111+
*/
112+
public static function createImmutableFromFormat($format, $time, $timezone = NULL)
113+
{
114+
$timezone = self::checkTimezone($timezone);
115+
return \DateTimeImmutable::createFromFormat($format, $time, $timezone);
116+
}
117+
118+
119+
/**
120+
* @param string
121+
* @return \DateTimeZone
122+
*/
123+
private static function checkTimezone($timezone = NULL)
124+
{
125+
if ($timezone === NULL) {
126+
return new \DateTimeZone(date_default_timezone_get());
127+
128+
} elseif (is_string($timezone)) {
129+
return new \DateTimeZone($timezone);
130+
131+
} elseif (!$timezone instanceof \DateTimeZone) {
132+
throw new Nette\InvalidArgumentException('Invalid timezone given');
133+
}
134+
}
135+
136+
137+
/**
138+
* @param \DateTimeInterface
139+
* @param string
140+
* @return \DateTimeInterface
141+
*/
142+
public static function modifyClone(\DateTimeInterface $dateTime, $modify = '')
143+
{
144+
if ($dateTime instanceof \DateTimeImmutable) {
145+
return $modify ? $dateTime->modify($modify) : clone $dateTime;
146+
}
147+
148+
$dateTime = clone $dateTime;
149+
return $modify ? $dateTime->modify($modify) : $dateTime;
150+
}
151+
152+
153+
/**
154+
* @param \DateTimeInterface
155+
* @param int
156+
* @return \DateTimeInterface
157+
*/
158+
public static function setTimestamp(\DateTimeInterface $dateTime, $timestamp)
159+
{
160+
$zone = $dateTime->getTimezone();
161+
162+
if ($dateTime instanceof \DateTimeImmutable) {
163+
return (new \DateTimeImmutable('@' . $timestamp))->setTimezone($zone);
164+
}
165+
166+
$dateTime->__construct('@' . $timestamp);
167+
return $dateTime->setTimeZone($zone);
168+
}
169+
170+
171+
/**
172+
* @param \DateTimeInterface
173+
* @return int|string
174+
*/
175+
public static function getTimestamp(\DateTimeInterface $dateTime)
176+
{
177+
$ts = $dateTime->format('U');
178+
return is_float($tmp = $ts * 1) ? $ts : $tmp;
179+
}
180+
}

src/Utils/DateTime.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,39 @@
77

88
namespace Nette\Utils;
99

10+
use DateTimeZone;
1011
use Nette;
1112

1213

1314
/**
15+
* @deprecated Use Nette\Utils\DateHelpers.
1416
* DateTime.
1517
*/
1618
class DateTime extends \DateTime implements \JsonSerializable
1719
{
1820
/** minute in seconds */
19-
const MINUTE = 60;
21+
const MINUTE = DateHelpers::MINUTE;
2022

2123
/** hour in seconds */
22-
const HOUR = 60 * self::MINUTE;
24+
const HOUR = DateHelpers::HOUR;
2325

2426
/** day in seconds */
25-
const DAY = 24 * self::HOUR;
27+
const DAY = DateHelpers::DAY;
2628

2729
/** week in seconds */
28-
const WEEK = 7 * self::DAY;
30+
const WEEK = DateHelpers::WEEK;
2931

3032
/** average month in seconds */
31-
const MONTH = 2629800;
33+
const MONTH = DateHelpers::MONTH;
3234

3335
/** average year in seconds */
34-
const YEAR = 31557600;
36+
const YEAR = DateHelpers::YEAR;
3537

38+
public function __construct($time = 'now', DateTimeZone $timezone = NULL)
39+
{
40+
trigger_error('Class ' . __CLASS__ . ' is deprecated, please use ' . DateHelpers::class . ' instead.', E_USER_DEPRECATED);
41+
parent::__construct($time, $timezone);
42+
}
3643

3744
/**
3845
* DateTime object factory.
@@ -71,8 +78,7 @@ public function __toString()
7178
*/
7279
public function modifyClone($modify = '')
7380
{
74-
$dolly = clone $this;
75-
return $modify ? $dolly->modify($modify) : $dolly;
81+
return DateHelpers::modifyClone($this, $modify);
7682
}
7783

7884

@@ -82,9 +88,7 @@ public function modifyClone($modify = '')
8288
*/
8389
public function setTimestamp($timestamp)
8490
{
85-
$zone = $this->getTimezone();
86-
$this->__construct('@' . $timestamp);
87-
return $this->setTimeZone($zone);
91+
return DateHelpers::setTimestamp($this, $timestamp);
8892
}
8993

9094

@@ -93,8 +97,7 @@ public function setTimestamp($timestamp)
9397
*/
9498
public function getTimestamp()
9599
{
96-
$ts = $this->format('U');
97-
return is_float($tmp = $ts * 1) ? $ts : $tmp;
100+
return DateHelpers::getTimestamp($this);
98101
}
99102

100103

tests/Utils/Arrays.associate().phpt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,30 @@ Assert::same(
193193

194194
// allowes objects in keys
195195
Assert::equal(
196-
['2014-02-05 00:00:00' => new DateTime('2014-02-05')],
196+
['2014-02-05 nette' => new Foo('2014-02-05')],
197197
Arrays::associate($arr = [
198-
['date' => new DateTime('2014-02-05')],
198+
['date' => new Foo('2014-02-05')],
199199
], 'date=date')
200200
);
201201
Assert::equal(
202-
(object) ['2014-02-05 00:00:00' => new DateTime('2014-02-05')],
202+
(object) ['2014-02-05 nette' => new Foo('2014-02-05')],
203203
Arrays::associate($arr = [
204-
['date' => new DateTime('2014-02-05')],
204+
['date' => new Foo('2014-02-05')],
205205
], '->date=date')
206206
);
207+
208+
209+
class Foo
210+
{
211+
private $value;
212+
213+
public function __construct($value)
214+
{
215+
$this->value = $value;
216+
}
217+
218+
public function __toString()
219+
{
220+
return $this->value . ' nette';
221+
}
222+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Utils\DateHelpers::createImmutable().
5+
*/
6+
7+
use Tester\Assert;
8+
use Nette\Utils\DateHelpers;
9+
10+
require __DIR__ . '/../bootstrap.php';
11+
12+
13+
date_default_timezone_set('Europe/Prague');
14+
15+
const FORMAT = 'Y-m-d H:i:s';
16+
17+
Assert::same('1978-01-23 11:40:00', DateHelpers::createImmutable(254400000)->format(FORMAT));
18+
Assert::same(254400000, DateHelpers::createImmutable(254400000)->getTimestamp());
19+
20+
Assert::same('2050-08-13 11:40:00', DateHelpers::createImmutable(2544000000)->format(FORMAT));
21+
Assert::same(is_int(2544000000) ? 2544000000 : '2544000000', DateHelpers::createImmutable(2544000000)->getTimestamp()); // 64 bit
22+
23+
Assert::same('1978-05-05 00:00:00', DateHelpers::createImmutable('1978-05-05')->format(FORMAT));
24+
25+
Assert::type(DateTimeImmutable::class, DateHelpers::createImmutable(new DateTime('1978-05-05')));
26+
27+
Assert::same('1978-05-05 00:00:00', DateHelpers::createImmutable(new DateTime('1978-05-05'))->format(FORMAT));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Utils\DateHelpers::createMutable().
5+
*/
6+
7+
use Tester\Assert;
8+
use Nette\Utils\DateHelpers;
9+
10+
require __DIR__ . '/../bootstrap.php';
11+
12+
13+
date_default_timezone_set('Europe/Prague');
14+
15+
const FORMAT = 'Y-m-d H:i:s';
16+
17+
Assert::same('1978-01-23 11:40:00', DateHelpers::createMutable(254400000)->format(FORMAT));
18+
Assert::same(254400000, DateHelpers::createMutable(254400000)->getTimestamp());
19+
20+
Assert::same('2050-08-13 11:40:00', DateHelpers::createMutable(2544000000)->format(FORMAT));
21+
Assert::same(is_int(2544000000) ? 2544000000 : '2544000000', DateHelpers::createMutable(2544000000)->getTimestamp()); // 64 bit
22+
23+
Assert::same('1978-05-05 00:00:00', DateHelpers::createMutable('1978-05-05')->format(FORMAT));
24+
25+
Assert::type(DateTime::class, DateHelpers::createMutable(new DateTime('1978-05-05')));
26+
27+
Assert::same('1978-05-05 00:00:00', DateHelpers::createMutable(new DateTime('1978-05-05'))->format(FORMAT));
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\DateHelpers::getTimestamp().
5+
*/
6+
7+
use Tester\Assert;
8+
use Nette\Utils\DateHelpers;
9+
10+
require __DIR__ . '/../bootstrap.php';
11+
12+
13+
date_default_timezone_set('Europe/Prague');
14+
15+
$timestamp = 123456789;
16+
17+
$mutable = DateHelpers::createMutable($timestamp);
18+
Assert::same($timestamp, $mutable->getTimestamp());
19+
20+
$immutable = DateHelpers::createImmutable($timestamp);
21+
Assert::same($timestamp, $immutable->getTimestamp());

0 commit comments

Comments
 (0)