Skip to content

Commit

Permalink
Merge pull request #82 from cakephp/issue-81
Browse files Browse the repository at this point in the history
Fix createFromTimestamp() acting incorrectly.
  • Loading branch information
markstory committed Feb 14, 2016
2 parents 14c54f9 + 41ecd2b commit 8f90c9b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 158 deletions.
148 changes: 1 addition & 147 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Date extends DateTimeImmutable implements ChronosInterface
use Traits\DifferenceTrait;
use Traits\FactoryTrait;
use Traits\FormattingTrait;
use Traits\FrozenTimeTrait;
use Traits\MagicPropertyTrait;
use Traits\ModifierTrait;
use Traits\RelativeKeywordTrait;
Expand Down Expand Up @@ -80,153 +81,6 @@ public function __construct($time = 'now', $tz = null)
parent::__construct($time, $tz);
}

/**
* Removes the time components from an input string.
*
* Used to ensure constructed objects always lack time.
*
* @param string|int $time The input time. Integer values will be assumed
* to be in UTC. The 'now' and '' values will use the current local time.
* @return string The date component of $time.
*/
protected function stripTime($time)
{
if (substr($time, 0, 1) === '@') {
return gmdate('Y-m-d 00:00:00', substr($time, 1));
}
if (is_int($time) || ctype_digit($time)) {
return gmdate('Y-m-d 00:00:00', $time);
}
if ($time === null || $time === 'now' || $time === '') {
return date('Y-m-d 00:00:00');
}
return preg_replace('/\d{1,2}:\d{1,2}:\d{1,2}/', '00:00:00', $time);
}

/**
* Modify the time on the Date.
*
* This method ignores all inputs and forces all inputs to 0.
*
* @param int $hours The hours to set (ignored)
* @param int $minutes The hours to set (ignored)
* @param int $seconds The hours to set (ignored)
* @return static A modified Date instance.
*/
public function setTime($hours, $minutes, $seconds = 0)
{
return parent::setTime(0, 0, 0);
}

/**
* Add an Interval to a Date
*
* Any changes to the time will be ignored and reset to 00:00:00
*
* @param \DateInterval $interval The interval to modify this date by.
* @return static A modified Date instance
*/
public function add($interval)
{
$date = parent::add($interval);
if ($date->format('H:i:s') !== '00:00:00') {
return $date->setTime(0, 0, 0);
}
return $date;
}

/**
* Subtract an Interval from a Date.
*
* Any changes to the time will be ignored and reset to 00:00:00
*
* @param \DateInterval $interval The interval to modify this date by.
* @return static A modified Date instance
*/
public function sub($interval)
{
$date = parent::sub($interval);
if ($date->format('H:i:s') !== '00:00:00') {
return $date->setTime(0, 0, 0);
}
return $date;
}

/**
* No-op method.
*
* Timezones have no effect on calendar dates.
*
* @param DateTimeZone|string $value The DateTimeZone object or timezone name to use.
* @return $this
*/
public function timezone($value)
{
return $this;
}

/**
* No-op method.
*
* Timezones have no effect on calendar dates.
*
* @param DateTimeZone|string $value The DateTimeZone object or timezone name to use.
* @return $this
*/
public function tz($value)
{
return $this;
}

/**
* No-op method.
*
* Timezones have no effect on calendar dates.
*
* @param DateTimeZone|string $value The DateTimeZone object or timezone name to use.
* @return $this
*/
public function setTimezone($value)
{
return $this;
}

/**
* Set the timestamp value and get a new object back.
*
* This method will discard the time aspects of the timestamp
* and only apply the date portions
*
* @param int $value The timestamp value to set.
* @return static
*/
public function setTimestamp($value)
{
$date = date('Y-m-d 00:00:00', $value);
return parent::setTimestamp(strtotime($date));
}

/**
* Overloaded to ignore time changes.
*
* Changing any aspect of the time will be ignored, and the resulting object
* will have its time frozen to 00:00:00.
*
* @param string $relative The relative change to make.
* @return static A new date with the applied date changes.
*/
public function modify($relative)
{
if (preg_match('/hour|minute|second/', $relative)) {
return $this;
}
$new = parent::modify($relative);
if ($new->format('H:i:s') !== '00:00:00') {
return $new->setTime(0, 0, 0);
}
return $new;
}

/**
* Create a new mutable instance from current immutable instance.
*
Expand Down
15 changes: 4 additions & 11 deletions src/Traits/FrozenTimeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
trait FrozenTimeTrait
{

/**
* Removes the time components from an input string.
*
Expand Down Expand Up @@ -66,11 +67,7 @@ public function setTime($hours, $minutes, $seconds = 0)
*/
public function add($interval)
{
$date = parent::add($interval);
if ($date->format('H:i:s') !== '00:00:00') {
return $date->setTime(0, 0, 0);
}
return $date;
return parent::add($interval)->setTime(0, 0, 0);
}

/**
Expand All @@ -83,11 +80,7 @@ public function add($interval)
*/
public function sub($interval)
{
$date = parent::sub($interval);
if ($date->format('H:i:s') !== '00:00:00') {
return $date->setTime(0, 0, 0);
}
return $date;
return parent::sub($interval)->setTime(0, 0, 0);
}

/**
Expand Down Expand Up @@ -140,7 +133,7 @@ public function setTimezone($value)
*/
public function setTimestamp($value)
{
return parent::setTimestamp(strtotime($value));
return parent::setTimestamp($value)->setTime(0, 0, 0);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/Date/ConstructTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@
*/
class ConstructTest extends TestCase
{

/**
* @dataProvider dateClassProvider
* @return void
*/
public function testCreateFromTimestamp($class)
{
$ts = 1454284800;
$date = $class::createFromTimestamp($ts);
$this->assertEquals('UTC', $date->tzName);
$this->assertEquals('2016-02-01', $date->format('Y-m-d'));
}

/**
* @dataProvider dateClassProvider
* @return void
*/
public function testCreateFromTimestampUtc($class)
{
$ts = 1454284800;
$date = $class::createFromTimestamp($ts);
$this->assertEquals('UTC', $date->tzName);
$this->assertEquals('2016-02-01', $date->format('Y-m-d'));
}

/**
* @dataProvider dateClassProvider
* @return void
Expand Down

0 comments on commit 8f90c9b

Please sign in to comment.