Skip to content

Commit

Permalink
Merge pull request #168 from cakephp/overwrite-us
Browse files Browse the repository at this point in the history
Clear out the microseconds when setting time.
  • Loading branch information
markstory committed May 23, 2018
2 parents ef9d2a1 + badbdfd commit 5b9d0ba
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Expand Up @@ -42,14 +42,14 @@ install:
- travis_retry composer install --no-interaction --prefer-dist

script:
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then export CODECOVERAGE=1; vendor/bin/phpunit --coverage-clover=clover.xml; fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.0 ]]; then vendor/bin/phpunit; fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.2 ]]; then export CODECOVERAGE=1; vendor/bin/phpunit --coverage-clover=clover.xml; fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.2 ]]; then vendor/bin/phpunit; fi

- if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi
- if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs ./src ./tests; fi
- if [[ $PHPSTAN = 1 ]]; then composer phpstan; fi

after_success:
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then bash <(curl -s https://codecov.io/bash); fi
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.2 ]]; then bash <(curl -s https://codecov.io/bash); fi

notifications:
email: false
4 changes: 4 additions & 0 deletions phpcs.xml.dist
Expand Up @@ -3,6 +3,10 @@
<description>Default configuration</description>

<rule ref="vendor/cakephp/cakephp-codesniffer/CakePHP"/>
<!-- Necessary for class aliases used for backwards compat -->
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
<severity>0</severity>
</rule>

<file>./src</file>
<file>./tests</file>
Expand Down
4 changes: 4 additions & 0 deletions src/Traits/FrozenTimeTrait.php
Expand Up @@ -61,6 +61,10 @@ protected function stripTime($time)
*/
public function setTime($hours, $minutes, $seconds = null, $microseconds = null)
{
if (CHRONOS_SUPPORTS_MICROSECONDS) {
return parent::setTime(0, 0, 0, 0);
}

return parent::setTime(0, 0, 0);
}

Expand Down
1 change: 1 addition & 0 deletions src/carbon_compat.php
Expand Up @@ -10,6 +10,7 @@
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
define('CHRONOS_SUPPORTS_MICROSECONDS', version_compare(PHP_VERSION, '7.1.0', '>='));

// Create class aliases for Carbon so applications
// can upgrade more easily.
Expand Down
19 changes: 19 additions & 0 deletions tests/Date/TimeMutateTest.php
Expand Up @@ -79,9 +79,28 @@ public function testSetterMethodIsIgnored($method, $value)
$this->assertEquals(0, $new->hour);
$this->assertEquals(0, $new->minute);
$this->assertEquals(0, $new->second);
$this->assertEquals(0, $new->format('u'));
$this->assertEquals(0, $date->hour);
$this->assertEquals(0, $date->minute);
$this->assertEquals(0, $date->second);
$this->assertEquals(0, $date->format('u'));
}

/**
* Test that setTime() ignores microseconds
*/
public function testSetTimeIgnored()
{
$date = new Date();
$new = $date->setTime(1, 2, 3, 4);
$this->assertEquals(0, $new->hour);
$this->assertEquals(0, $new->minute);
$this->assertEquals(0, $new->second);
$this->assertEquals(0, $new->format('u'));
$this->assertEquals(0, $date->hour);
$this->assertEquals(0, $date->minute);
$this->assertEquals(0, $date->second);
$this->assertEquals(0, $date->format('u'));
}

/**
Expand Down

0 comments on commit 5b9d0ba

Please sign in to comment.