1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
<?php
declare(strict_types=1);
/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <brian@nesbot.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\CarbonImmutable;
use Carbon\CarbonImmutable as Carbon;
use Tests\AbstractTestCase;
class ModifyTest extends AbstractTestCase
{
public function testSimpleModify()
{
$a = new Carbon('2014-03-30 00:00:00');
$b = $a->addHours(24);
$this->assertSame(24, $a->diffInHours($b));
}
public function testSimpleModifyWithNamedParameter()
{
if (version_compare(PHP_VERSION, '8.0.0-dev', '<')) {
$this->markTestSkipped('This tests needs PHP 8 named arguments syntax.');
}
$a = new Carbon('2014-03-30 00:00:00');
$b = eval('return $a->addHours(value: 24);');
$this->assertSame(24, $a->diffInHours($b));
}
public function testTimezoneModify()
{
// For daylight saving time reason 2014-03-30 0h59 is immediately followed by 2h00
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addHours(24);
$this->assertSame(24, $a->diffInHours($b));
$this->assertSame(24, $a->diffInHours($b, false));
$this->assertSame(24, $b->diffInHours($a));
$this->assertSame(-24, $b->diffInHours($a, false));
$this->assertSame(-23, $b->diffInRealHours($a, false));
$this->assertSame(-23 * 60, $b->diffInRealMinutes($a, false));
$this->assertSame(-23 * 60 * 60, $b->diffInRealSeconds($a, false));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealHours(24);
$this->assertSame(-24, $b->diffInRealHours($a, false));
$this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
$this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
$this->assertSame(-25, $b->diffInHours($a, false));
$b = $b->subRealHours(24);
$this->assertSame(0, $b->diffInRealHours($a, false));
$this->assertSame(0, $b->diffInHours($a, false));
$a = new Carbon('2014-03-30 00:59:00', 'Europe/London');
$a = $a->addRealHour();
$this->assertSame('02:59', $a->format('H:i'));
$a = $a->subRealHour();
$this->assertSame('00:59', $a->format('H:i'));
$a = new Carbon('2014-03-30 00:59:00', 'Europe/London');
$a = $a->addRealMinutes(2);
$this->assertSame('02:01', $a->format('H:i'));
$a = $a->subRealMinutes(2);
$this->assertSame('00:59', $a->format('H:i'));
$a = new Carbon('2014-03-30 00:59:30', 'Europe/London');
$a = $a->addRealMinute();
$this->assertSame('02:00:30', $a->format('H:i:s'));
$a = $a->subRealMinute();
$this->assertSame('00:59:30', $a->format('H:i:s'));
$a = new Carbon('2014-03-30 00:59:30', 'Europe/London');
$a = $a->addRealSeconds(40);
$this->assertSame('02:00:10', $a->format('H:i:s'));
$a = $a->subRealSeconds(40);
$this->assertSame('00:59:30', $a->format('H:i:s'));
$a = new Carbon('2014-03-30 00:59:59', 'Europe/London');
$a = $a->addRealSecond();
$this->assertSame('02:00:00', $a->format('H:i:s'));
$a = $a->subRealSecond();
$this->assertSame('00:59:59', $a->format('H:i:s'));
$a = new Carbon('2014-03-30 00:59:59.999990', 'Europe/London');
$a = $a->addRealMicroseconds(20);
$this->assertSame('02:00:00.000010', $a->format('H:i:s.u'));
$a = $a->subRealMicroseconds(20);
$this->assertSame('00:59:59.999990', $a->format('H:i:s.u'));
$a = new Carbon('2014-03-30 00:59:59.999999', 'Europe/London');
$a = $a->addRealMicrosecond();
$this->assertSame('02:00:00.000000', $a->format('H:i:s.u'));
$a = $a->subRealMicrosecond();
$this->assertSame('00:59:59.999999', $a->format('H:i:s.u'));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealDay();
$this->assertSame(-24, $b->diffInRealHours($a, false));
$this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
$this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
$this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false));
$this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false));
$this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
$this->assertSame(-25, $b->diffInHours($a, false));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealWeeks(1 / 7);
$this->assertSame(-24, $b->diffInRealHours($a, false));
$this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
$this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
$this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false));
$this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false));
$this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
$this->assertSame(-25, $b->diffInHours($a, false));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealMonths(1 / 30);
$this->assertSame(-24, $b->diffInRealHours($a, false));
$this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
$this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
$this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false));
$this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false));
$this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
$this->assertSame(-25, $b->diffInHours($a, false));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealQuarters(1 / 90);
$this->assertSame(-24, $b->diffInRealHours($a, false));
$this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
$this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
$this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false));
$this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false));
$this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
$this->assertSame(-25, $b->diffInHours($a, false));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealYears(1 / 365);
$this->assertSame(-24, $b->diffInRealHours($a, false));
$this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
$this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
$this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false));
$this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false));
$this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
$this->assertSame(-25, $b->diffInHours($a, false));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealDecades(1 / 3650);
$this->assertSame(-24, $b->diffInRealHours($a, false));
$this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
$this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
$this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false));
$this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false));
$this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
$this->assertSame(-25, $b->diffInHours($a, false));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealCenturies(1 / 36500);
$this->assertSame(-24, $b->diffInRealHours($a, false));
$this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
$this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
$this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false));
$this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false));
$this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
$this->assertSame(-25, $b->diffInHours($a, false));
$a = new Carbon('2014-03-30 00:00:00', 'Europe/London');
$b = $a->addRealMillennia(1 / 365000);
$this->assertSame(-24, $b->diffInRealHours($a, false));
$this->assertSame(-24 * 60, $b->diffInRealMinutes($a, false));
$this->assertSame(-24 * 60 * 60, $b->diffInRealSeconds($a, false));
$this->assertSame(-24 * 60 * 60 * 1000, $b->diffInRealMilliseconds($a, false));
$this->assertSame(-24 * 60 * 60 * 1000000, $b->diffInRealMicroseconds($a, false));
$this->assertSame(-25 * 60 * 60 * 1000000, $b->diffInMicroseconds($a, false));
$this->assertSame(-25, $b->diffInHours($a, false));
}
public function testNextAndPrevious()
{
Carbon::setTestNow('2019-06-02 13:27:09.816752');
$this->assertSame('2019-06-02 14:00:00', Carbon::now()->next('2pm')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-01 14:00:00', Carbon::now()->previous('2pm')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-02 14:00:00', Carbon::now()->next('14h')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-01 14:00:00', Carbon::now()->previous('14h')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-03 09:00:00', Carbon::now()->next('9am')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-02 09:00:00', Carbon::now()->previous('9am')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-02 14:00:00', Carbon::parse('next 2pm')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-01 14:00:00', Carbon::parse('previous 2pm')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-02 14:00:00', Carbon::parse('next 14h')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-01 14:00:00', Carbon::parse('previous 14h')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-03 09:00:00', Carbon::parse('next 9am')->format('Y-m-d H:i:s'));
$this->assertSame('2019-06-02 09:00:00', Carbon::parse('previous 9am')->format('Y-m-d H:i:s'));
}
}
|