Skip to content

Commit

Permalink
Implement UTC variants for add/sub methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Mar 27, 2024
1 parent d504c08 commit 41b550c
Show file tree
Hide file tree
Showing 12 changed files with 361 additions and 253 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ jobs:

- name: Select PHPUnit version
if: matrix.phpunit
shell: bash
run: composer require --no-update --no-interaction --dev "phpunit/phpunit:^${{ matrix.phpunit }}"

- name: Remove conflicting optional dependencies
Expand All @@ -71,6 +72,7 @@ jobs:

- name: Downgrade nikic/php-parser
if: matrix.phpunit < 11
shell: bash
run: composer require --no-update --no-interaction --dev "nikic/php-parser:^4"

- name: Install dependencies
Expand Down
6 changes: 6 additions & 0 deletions phpdoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,12 @@ function dumpParameter(string $method, ReflectionParameter $parameter): string
$plUnit.'Until($endDate = null, int|float $factor = 1)',
"Return an iterable period from current date to given end (string, DateTime or Carbon instance) for each $unitName or every X $plUnitName if a factor is given.",
];
$autoDocLines[] = [
'@method',
'float',
'diffInUTC'.ucFirst($plUnit).'(DateTimeInterface|string|null $date, bool $absolute = false)',
"Convert current and given date in UTC timezone and return a floating number of $plUnitName.",
];

break;

Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ parameters:
- '#^Call to an undefined static method#'
- '#^Call to an undefined method Carbon\\Carbon(Immutable)?::floatDiffIn([A-Za-z]+)\(\)\.$#'
- '#^Call to an undefined method Carbon\\Carbon(Immutable)?::diffInReal([A-Za-z]+)\(\)\.$#'
- '#^Call to an undefined method Carbon\\Carbon(Immutable)?::(add|sub)Real([A-Za-z]+)\(\)\.$#'
- '#^Unsafe usage of new static\(\)\.$#'
- '#^Method Carbon\\Carbon(Interface|Immutable)?::(add|sub)[A-Z][A-Za-z]+\(\) invoked with 1 parameter, 0 required\.$#'
- '#^Call to an undefined method Carbon\\Carbon(Interface|Immutable)?::(super|noThis|toAppTz|copyWithAppTz)\(\)\.$#'
Expand Down
135 changes: 75 additions & 60 deletions src/Carbon/Carbon.php

Large diffs are not rendered by default.

135 changes: 75 additions & 60 deletions src/Carbon/CarbonImmutable.php

Large diffs are not rendered by default.

161 changes: 101 additions & 60 deletions src/Carbon/CarbonInterface.php

Large diffs are not rendered by default.

153 changes: 91 additions & 62 deletions src/Carbon/Traits/Date.php

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions tests/Carbon/IsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Carbon\Carbon;
use DateTime;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use stdClass;
use Tests\AbstractTestCase;
use TypeError;
Expand Down Expand Up @@ -610,7 +609,6 @@ public function testIsCurrentSecondFalse()
$this->assertFalse(Carbon::now()->subDay()->isCurrentSecond());
}

#[Group('php-8.1')]
public function testIsSameMicrosecond()
{
$current = new Carbon('2018-05-06T13:30:54.123456');
Expand Down
13 changes: 10 additions & 3 deletions tests/Carbon/ModifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Closure;
use DateMalformedStringException;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\Group;
use Tests\AbstractTestCase;

class ModifyTest extends AbstractTestCase
Expand All @@ -31,7 +30,6 @@ public function testSimpleModify()
$this->assertSame(24.0, $a->diffInHours($b));
}

#[Group('php-8.1')]
public function testTimezoneModify()
{
$php81Fix = 1.0;
Expand All @@ -50,7 +48,7 @@ public function testTimezoneModify()

$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->copy();
$b->addRealHours(24);
$b->addUTCHours(24);
$this->assertSame(-24.0, $b->diffInHours($a, false));
$this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false));
$this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false));
Expand Down Expand Up @@ -117,6 +115,15 @@ public function testTimezoneModify()
$this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));

$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->copy();
$b->addUTCDay();
$this->assertSame(-24.0, $b->diffInHours($a, false));
$this->assertSame(-24.0 * 60, $b->diffInMinutes($a, false));
$this->assertSame(-24.0 * 60 * 60, $b->diffInSeconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000, $b->diffInMilliseconds($a, false));
$this->assertSame(-24.0 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));

$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->copy();
$b->addRealWeeks(1 / 7);
Expand Down
2 changes: 0 additions & 2 deletions tests/CarbonImmutable/IsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use DateTime;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use stdClass;
use Tests\AbstractTestCase;
use TypeError;
Expand Down Expand Up @@ -640,7 +639,6 @@ public function testIsCurrentSecondFalse()
$this->assertFalse(Carbon::now()->subDay()->isCurrentSecond());
}

#[Group('php-8.1')]
public function testIsSameMicrosecond()
{
$current = new Carbon('2018-05-06T13:30:54.123456');
Expand Down
2 changes: 0 additions & 2 deletions tests/CarbonImmutable/ModifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Tests\CarbonImmutable;

use Carbon\CarbonImmutable as Carbon;
use PHPUnit\Framework\Attributes\Group;
use Tests\AbstractTestCase;

class ModifyTest extends AbstractTestCase
Expand All @@ -33,7 +32,6 @@ public function testSimpleModifyWithNamedParameter()
$this->assertSame(24.0, $a->diffInHours($b));
}

#[Group('php-8.1')]
public function testTimezoneModify()
{
$php81Fix = 1.0;
Expand Down
2 changes: 0 additions & 2 deletions tests/CarbonInterval/AddTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use DateTime;
use DateTimeImmutable;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\TestWith;
use Tests\AbstractTestCase;
use Tests\Carbon\Fixtures\MyCarbon;
Expand Down Expand Up @@ -77,7 +76,6 @@ public function testAddWithNegativeDiffDateInterval()
$this->assertCarbonInterval($ci, 4, 3, 28, 8, 10, 11);
}

#[Group('php-8.1')]
public function testAddMicroseconds()
{
$diff = Carbon::now()->diff(Carbon::now()->addDays(3)->addMicroseconds(111222));
Expand Down

0 comments on commit 41b550c

Please sign in to comment.