Skip to content

Commit

Permalink
Copy timezome offset with date and time
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Oct 6, 2024
1 parent 50c0e29 commit 6178543
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Carbon/Traits/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -1708,10 +1708,10 @@ public function setUnitNoOverflow(string $valueUnit, int $value, string $overflo
$start = $original->avoidMutation()->startOf($overflowUnit);
$end = $original->avoidMutation()->endOf($overflowUnit);

if ($date < $start) {
$date = $date->setDateTimeFrom($start);
} elseif ($date > $end) {
$date = $date->setDateTimeFrom($end);
$date = $date->modify($start->rawFormat('Y-m-d H:i:s.u e O'));
$date = $date->modify($end->rawFormat('Y-m-d H:i:s.u e O'));
}

return $date;
Expand Down
2 changes: 1 addition & 1 deletion src/Carbon/Traits/IntervalStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function convertDate(DateTimeInterface $dateTime, bool $negated = false):
if ($this->step) {
$carbonDate = Callback::parameter($this->step, $carbonDate->avoidMutation());

return $carbonDate->setDateTimeFrom(($this->step)($carbonDate, $negated));
return $carbonDate->modify(($this->step)($carbonDate, $negated)->format('Y-m-d H:i:s.u e O'));
}

if ($negated) {
Expand Down
4 changes: 2 additions & 2 deletions src/Carbon/Traits/Units.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public function add($unit, $value = 1, ?bool $overflow = null): static
$result = $this->resolveCarbon($unit($this, false));

if ($this !== $result && $this->isMutable()) {
return $this->setDateTimeFrom($result);
return $this->modify($result->rawFormat('Y-m-d H:i:s.u e O'));
}

return $result;
Expand Down Expand Up @@ -415,7 +415,7 @@ public function sub($unit, $value = 1, ?bool $overflow = null): static
$result = $this->resolveCarbon($unit($this, true));

if ($this !== $result && $this->isMutable()) {
return $this->setDateTimeFrom($result);
return $this->modify($result->rawFormat('Y-m-d H:i:s.u e O'));
}

return $result;
Expand Down
68 changes: 55 additions & 13 deletions tests/Carbon/SettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class SettersTest extends AbstractTestCase
{
public const SET_UNIT_NO_OVERFLOW_SAMPLE = 200;
public const SET_UNIT_NO_OVERFLOW_SAMPLE = 20_000;

public function testMonthEnum()
{
Expand Down Expand Up @@ -635,7 +635,7 @@ public function testSetUnitNoOverflow()
'current' => 0,
'start' => 0,
'end' => 0,
'failure' => 0,
'failure' => [],
];

for ($i = 0; $i < static::SET_UNIT_NO_OVERFLOW_SAMPLE; $i++) {
Expand Down Expand Up @@ -668,8 +668,22 @@ public function testSetUnitNoOverflow()
$start = $original->copy()->startOf($overflowUnit);
$end = $original->copy()->endOf($overflowUnit);

if ($date < $start || $date > $end) {
$results['failure']++;
if ($date->lessThan($start) || $date->greaterThan($end)) {
$results['failure'][] = [
'year' => $year,
'month' => $month,
'day' => $day,
'hour' => $hour,
'minute' => $minute,
'second' => $second,
'microsecond' => $microsecond,
'valueUnit' => $valueUnit,
'value' => $value,
'overflowUnit' => $overflowUnit,
'date' => $date->format('Y-m-d H:i:s'),
'start' => $start->format('Y-m-d H:i:s'),
'end' => $end->format('Y-m-d H:i:s'),
];

continue;
}
Expand Down Expand Up @@ -722,7 +736,7 @@ public function testSetUnitNoOverflow()
}

$minimum = static::SET_UNIT_NO_OVERFLOW_SAMPLE / 100;
$this->assertSame(0, $results['failure']);
$this->assertSame([], $results['failure']);
$this->assertGreaterThan($minimum, $results['start']);
$this->assertGreaterThan($minimum, $results['end']);
$this->assertGreaterThan($minimum, $results['current']);
Expand Down Expand Up @@ -766,7 +780,7 @@ public function testAddUnitNoOverflow()
'current' => 0,
'start' => 0,
'end' => 0,
'failure' => 0,
'failure' => [],
];

for ($i = 0; $i < static::SET_UNIT_NO_OVERFLOW_SAMPLE; $i++) {
Expand Down Expand Up @@ -799,8 +813,22 @@ public function testAddUnitNoOverflow()
$start = $original->copy()->startOf($overflowUnit);
$end = $original->copy()->endOf($overflowUnit);

if ($date < $start || $date > $end) {
$results['failure']++;
if ($date->lessThan($start) || $date->greaterThan($end)) {
$results['failure'][] = [
'year' => $year,
'month' => $month,
'day' => $day,
'hour' => $hour,
'minute' => $minute,
'second' => $second,
'microsecond' => $microsecond,
'valueUnit' => $valueUnit,
'value' => $value,
'overflowUnit' => $overflowUnit,
'date' => $date->format('Y-m-d H:i:s.u e'),
'start' => $start->format('Y-m-d H:i:s.u e'),
'end' => $end->format('Y-m-d H:i:s.u e'),
];

continue;
}
Expand Down Expand Up @@ -848,7 +876,7 @@ public function testAddUnitNoOverflow()
}

$minimum = static::SET_UNIT_NO_OVERFLOW_SAMPLE / 100;
$this->assertSame(0, $results['failure']);
$this->assertSame([], $results['failure']);
$this->assertGreaterThan($minimum, $results['start']);
$this->assertGreaterThan($minimum, $results['end']);
$this->assertGreaterThan($minimum, $results['current']);
Expand All @@ -861,7 +889,7 @@ public function testSubUnitNoOverflow()
'current' => 0,
'start' => 0,
'end' => 0,
'failure' => 0,
'failure' => [],
];

for ($i = 0; $i < static::SET_UNIT_NO_OVERFLOW_SAMPLE; $i++) {
Expand Down Expand Up @@ -894,8 +922,22 @@ public function testSubUnitNoOverflow()
$start = $original->copy()->startOf($overflowUnit);
$end = $original->copy()->endOf($overflowUnit);

if ($date < $start || $date > $end) {
$results['failure']++;
if ($date->lessThan($start) || $date->greaterThan($end)) {
$results['failure'][] = [
'year' => $year,
'month' => $month,
'day' => $day,
'hour' => $hour,
'minute' => $minute,
'second' => $second,
'microsecond' => $microsecond,
'valueUnit' => $valueUnit,
'value' => $value,
'overflowUnit' => $overflowUnit,
'date' => $date->format('Y-m-d H:i:s.u e'),
'start' => $start->format('Y-m-d H:i:s.u e'),
'end' => $end->format('Y-m-d H:i:s.u e'),
];

continue;
}
Expand Down Expand Up @@ -960,7 +1002,7 @@ public function testSubUnitNoOverflow()
}

$minimum = static::SET_UNIT_NO_OVERFLOW_SAMPLE / 100;
$this->assertSame(0, $results['failure']);
$this->assertSame([], $results['failure']);
$this->assertGreaterThan($minimum, $results['start']);
$this->assertGreaterThan($minimum, $results['end']);
$this->assertGreaterThan($minimum, $results['current']);
Expand Down

0 comments on commit 6178543

Please sign in to comment.