Skip to content

Commit 7d6f77b

Browse files
committed
fix: Time::difference() DST bug
Fixed to not convert to UTC when the two time zones are the same.
1 parent b2d0197 commit 7d6f77b

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

system/I18n/TimeTrait.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -1069,8 +1069,21 @@ public function humanize()
10691069
*/
10701070
public function difference($testTime, ?string $timezone = null)
10711071
{
1072-
$testTime = $this->getUTCObject($testTime, $timezone);
1073-
$ourTime = $this->getUTCObject($this);
1072+
if (is_string($testTime)) {
1073+
$timezone = ($timezone !== null) ? new DateTimeZone($timezone) : $this->timezone;
1074+
$testTime = new DateTime($testTime, $timezone);
1075+
} elseif ($testTime instanceof self) {
1076+
$testTime = $testTime->toDateTime();
1077+
}
1078+
1079+
assert($testTime instanceof DateTime);
1080+
1081+
if ($this->timezone->getOffset($this) !== $testTime->getTimezone()->getOffset($this)) {
1082+
$testTime = $this->getUTCObject($testTime, $timezone);
1083+
$ourTime = $this->getUTCObject($this);
1084+
} else {
1085+
$ourTime = $this->toDateTime();
1086+
}
10741087

10751088
return new TimeDifference($ourTime, $testTime);
10761089
}

tests/system/I18n/TimeDifferenceTest.php

+14-6
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function testHumanizeMonthsForward(): void
120120
$current = Time::parse('March 1, 2017', 'America/Chicago');
121121
$diff = $current->difference('May 1, 2017', 'America/Chicago');
122122

123-
$this->assertSame('in 1 month', $diff->humanize('en'));
123+
$this->assertSame('in 2 months', $diff->humanize('en'));
124124
}
125125

126126
public function testHumanizeDaysSingle(): void
@@ -211,12 +211,20 @@ public function testHumanizeWeeksPlural(): void
211211
$this->assertSame('2 weeks ago', $diff->humanize('en'));
212212
}
213213

214-
public function testHumanizeWeeksForward(): void
214+
public function testHumanizeWeeksForwardDST(): void
215215
{
216216
$current = Time::parse('March 10, 2017', 'America/Chicago');
217217
$diff = $current->difference('March 18, 2017', 'America/Chicago');
218218

219-
$this->assertSame('in 1 week', $diff->humanize('en'));
219+
$this->assertSame('in 2 weeks', $diff->humanize('en'));
220+
}
221+
222+
public function testHumanizeWeeksForwardUTC(): void
223+
{
224+
$current = Time::parse('2017-03-10');
225+
$diff = $current->difference('2017-03-18');
226+
227+
$this->assertSame('in 2 weeks', $diff->humanize('en'));
220228
}
221229

222230
public function testHumanizeNoDifference(): void
@@ -238,14 +246,14 @@ public function testGetterUTC(): void
238246
$this->assertNull($diff->nonsense);
239247
}
240248

241-
public function testGetterChicagoTime(): void
249+
public function testGetterDST(): void
242250
{
243251
$current = Time::parse('March 10, 2017', 'America/Chicago');
244252
$diff = $current->difference('March 18, 2017', 'America/Chicago');
245253

246254
// Daylight Saving Time had begun since Sun, 12 Mar, 02:00.
247-
$this->assertSame(7, $diff->getDays());
248-
$this->assertSame(7, $diff->days);
255+
$this->assertSame(8, $diff->getDays());
256+
$this->assertSame(8, $diff->days);
249257

250258
// The raw value does not take Daylight Saving Time into account.
251259
$this->assertSame(-8, (int) round($diff->getDays(true)));

0 commit comments

Comments
 (0)