Skip to content

Concept: Utils\DateTime deprecation & replacement #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 180 additions & 0 deletions src/Utils/DateHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php

namespace Nette\Utils;

use Nette;

/**
* DateTime manipulation helpers.
*/
class DateHelpers
{
/** minute in seconds */
const MINUTE = 60;

/** hour in seconds */
const HOUR = 60 * self::MINUTE;

/** day in seconds */
const DAY = 24 * self::HOUR;

/** week in seconds */
const WEEK = 7 * self::DAY;

/** average month in seconds */
const MONTH = 2629800;

/** average year in seconds */
const YEAR = 31557600;


public function __construct()
{
throw new Nette\StaticClassException;
}


/**
* @param string|int|\DateTimeInterface $time
* @return \DateTime
*/
public static function createMutable($time)
{
if ($time instanceof \DateTime) {
return clone $time;

} elseif ($time instanceof \DateTimeImmutable) {
return \DateTime::createFromImmutable($time);

} elseif (is_numeric($time)) {
if ($time <= self::YEAR) {
$time += time();
}
return (new \DateTime('@' . $time))->setTimeZone(new \DateTimeZone(date_default_timezone_get()));

} elseif ($time) {
return new \DateTime($time);

} else {
return new \DateTime();
}
}


/**
* @param string|int|\DateTimeInterface $time
* @return \DateTimeImmutable
*/
public static function createImmutable($time)
{
if ($time instanceof \DateTimeImmutable) {
return clone $time;

} elseif ($time instanceof \Datetime) {
return \DateTimeImmutable::createFromMutable($time);

} elseif (is_numeric($time)) {
if ($time <= self::YEAR) {
$time += time();
}
return (new \DateTimeImmutable('@' . $time))->setTimeZone(new \DateTimeZone(date_default_timezone_get()));

} elseif ($time) {
return new \DateTimeImmutable($time);

} else {
return new \DateTimeImmutable();
}
}


/**
* Returns new \DateTime object formatted according to the specified format or FALSE on failure.
* @param string The format the $time parameter should be in
* @param string String representing the time
* @param string|\DateTimeZone desired timezone (default timezone is used if NULL is passed)
* @return \DateTime|FALSE
*/
public static function createMutableFromFormat($format, $time, $timezone = NULL)
{
$timezone = self::checkTimezone($timezone);
return \DateTime::createFromFormat($format, $time, $timezone);
}


/**
* Returns new \DateTimeImmutable object formatted according to the specified format or FALSE on failure.
* @param string The format the $time parameter should be in
* @param string String representing the time
* @param string|\DateTimeZone desired timezone (default timezone is used if NULL is passed)
* @return \DateTimeImmutable|FALSE
*/
public static function createImmutableFromFormat($format, $time, $timezone = NULL)
{
$timezone = self::checkTimezone($timezone);
return \DateTimeImmutable::createFromFormat($format, $time, $timezone);
}


/**
* @param string
* @return \DateTimeZone
*/
private static function checkTimezone($timezone = NULL)
{
if ($timezone === NULL) {
return new \DateTimeZone(date_default_timezone_get());

} elseif (is_string($timezone)) {
return new \DateTimeZone($timezone);

} elseif (!$timezone instanceof \DateTimeZone) {
throw new Nette\InvalidArgumentException('Invalid timezone given');
}
}


/**
* @param \DateTimeInterface
* @param string
* @return \DateTimeInterface
*/
public static function modifyClone(\DateTimeInterface $dateTime, $modify = '')
{
if ($dateTime instanceof \DateTimeImmutable) {
return $modify ? $dateTime->modify($modify) : clone $dateTime;
}

$dateTime = clone $dateTime;
return $modify ? $dateTime->modify($modify) : $dateTime;
}


/**
* @param \DateTimeInterface
* @param int
* @return \DateTimeInterface
*/
public static function setTimestamp(\DateTimeInterface $dateTime, $timestamp)
{
$zone = $dateTime->getTimezone();

if ($dateTime instanceof \DateTimeImmutable) {
return (new \DateTimeImmutable('@' . $timestamp))->setTimezone($zone);
}

$dateTime->__construct('@' . $timestamp);
return $dateTime->setTimeZone($zone);
}


/**
* @param \DateTimeInterface
* @return int|string
*/
public static function getTimestamp(\DateTimeInterface $dateTime)
{
$ts = $dateTime->format('U');
return is_float($tmp = $ts * 1) ? $ts : $tmp;
}
}
29 changes: 16 additions & 13 deletions src/Utils/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,39 @@

namespace Nette\Utils;

use DateTimeZone;
use Nette;


/**
* @deprecated Use Nette\Utils\DateHelpers.
* DateTime.
*/
class DateTime extends \DateTime implements \JsonSerializable
{
/** minute in seconds */
const MINUTE = 60;
const MINUTE = DateHelpers::MINUTE;

/** hour in seconds */
const HOUR = 60 * self::MINUTE;
const HOUR = DateHelpers::HOUR;

/** day in seconds */
const DAY = 24 * self::HOUR;
const DAY = DateHelpers::DAY;

/** week in seconds */
const WEEK = 7 * self::DAY;
const WEEK = DateHelpers::WEEK;

/** average month in seconds */
const MONTH = 2629800;
const MONTH = DateHelpers::MONTH;

/** average year in seconds */
const YEAR = 31557600;
const YEAR = DateHelpers::YEAR;

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

/**
* DateTime object factory.
Expand Down Expand Up @@ -71,8 +78,7 @@ public function __toString()
*/
public function modifyClone($modify = '')
{
$dolly = clone $this;
return $modify ? $dolly->modify($modify) : $dolly;
return DateHelpers::modifyClone($this, $modify);
}


Expand All @@ -82,9 +88,7 @@ public function modifyClone($modify = '')
*/
public function setTimestamp($timestamp)
{
$zone = $this->getTimezone();
$this->__construct('@' . $timestamp);
return $this->setTimeZone($zone);
return DateHelpers::setTimestamp($this, $timestamp);
}


Expand All @@ -93,8 +97,7 @@ public function setTimestamp($timestamp)
*/
public function getTimestamp()
{
$ts = $this->format('U');
return is_float($tmp = $ts * 1) ? $ts : $tmp;
return DateHelpers::getTimestamp($this);
}


Expand Down
24 changes: 20 additions & 4 deletions tests/Utils/Arrays.associate().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,30 @@ Assert::same(

// allowes objects in keys
Assert::equal(
['2014-02-05 00:00:00' => new DateTime('2014-02-05')],
['2014-02-05 nette' => new Foo('2014-02-05')],
Arrays::associate($arr = [
['date' => new DateTime('2014-02-05')],
['date' => new Foo('2014-02-05')],
], 'date=date')
);
Assert::equal(
(object) ['2014-02-05 00:00:00' => new DateTime('2014-02-05')],
(object) ['2014-02-05 nette' => new Foo('2014-02-05')],
Arrays::associate($arr = [
['date' => new DateTime('2014-02-05')],
['date' => new Foo('2014-02-05')],
], '->date=date')
);


class Foo
{
private $value;

public function __construct($value)
{
$this->value = $value;
}

public function __toString()
{
return $this->value . ' nette';
}
}
27 changes: 27 additions & 0 deletions tests/Utils/DateHelpers.createImmutable.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* Test: Nette\Utils\DateHelpers::createImmutable().
*/

use Tester\Assert;
use Nette\Utils\DateHelpers;

require __DIR__ . '/../bootstrap.php';


date_default_timezone_set('Europe/Prague');

const FORMAT = 'Y-m-d H:i:s';

Assert::same('1978-01-23 11:40:00', DateHelpers::createImmutable(254400000)->format(FORMAT));
Assert::same(254400000, DateHelpers::createImmutable(254400000)->getTimestamp());

Assert::same('2050-08-13 11:40:00', DateHelpers::createImmutable(2544000000)->format(FORMAT));
Assert::same(is_int(2544000000) ? 2544000000 : '2544000000', DateHelpers::createImmutable(2544000000)->getTimestamp()); // 64 bit

Assert::same('1978-05-05 00:00:00', DateHelpers::createImmutable('1978-05-05')->format(FORMAT));

Assert::type(DateTimeImmutable::class, DateHelpers::createImmutable(new DateTime('1978-05-05')));

Assert::same('1978-05-05 00:00:00', DateHelpers::createImmutable(new DateTime('1978-05-05'))->format(FORMAT));
27 changes: 27 additions & 0 deletions tests/Utils/DateHelpers.createMutable.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* Test: Nette\Utils\DateHelpers::createMutable().
*/

use Tester\Assert;
use Nette\Utils\DateHelpers;

require __DIR__ . '/../bootstrap.php';


date_default_timezone_set('Europe/Prague');

const FORMAT = 'Y-m-d H:i:s';

Assert::same('1978-01-23 11:40:00', DateHelpers::createMutable(254400000)->format(FORMAT));
Assert::same(254400000, DateHelpers::createMutable(254400000)->getTimestamp());

Assert::same('2050-08-13 11:40:00', DateHelpers::createMutable(2544000000)->format(FORMAT));
Assert::same(is_int(2544000000) ? 2544000000 : '2544000000', DateHelpers::createMutable(2544000000)->getTimestamp()); // 64 bit

Assert::same('1978-05-05 00:00:00', DateHelpers::createMutable('1978-05-05')->format(FORMAT));

Assert::type(DateTime::class, DateHelpers::createMutable(new DateTime('1978-05-05')));

Assert::same('1978-05-05 00:00:00', DateHelpers::createMutable(new DateTime('1978-05-05'))->format(FORMAT));
21 changes: 21 additions & 0 deletions tests/Utils/DateHelpers.getTimestamp.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* Test: Nette\DateHelpers::getTimestamp().
*/

use Tester\Assert;
use Nette\Utils\DateHelpers;

require __DIR__ . '/../bootstrap.php';


date_default_timezone_set('Europe/Prague');

$timestamp = 123456789;

$mutable = DateHelpers::createMutable($timestamp);
Assert::same($timestamp, $mutable->getTimestamp());

$immutable = DateHelpers::createImmutable($timestamp);
Assert::same($timestamp, $immutable->getTimestamp());
Loading