Skip to content

Commit 0ae9942

Browse files
committed
Merge remote-tracking branch 'derickr/timelib-2021-15-sync' into PHP-8.1
2 parents 5c10aa4 + 7831a1c commit 0ae9942

File tree

5 files changed

+83
-9
lines changed

5 files changed

+83
-9
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ PHP NEWS
88
- Date:
99
. Fixed bug GH-8730 (DateTime::diff miscalculation is same time zone of
1010
different type). (Derick)
11+
. Fixed bug GH-8964 (DateTime object comparison after applying delta less
12+
than 1 second). (Derick)
1113
. Fixed bug #81263 (Wrong result from DateTimeImmutable::diff). (Derick)
1214

1315
- DBA:

ext/date/lib/interval.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,21 @@ timelib_time *timelib_sub_wall(timelib_time *old_time, timelib_rel_time *interva
405405
timelib_update_ts(t, NULL);
406406
}
407407

408-
do_range_limit(0, 1000000, 1000000, &interval->us, &interval->s);
409-
t->sse -= bias * timelib_hms_to_seconds(interval->h, interval->i, interval->s);
410-
timelib_update_from_sse(t);
411-
t->us -= interval->us * bias;
412-
if (bias == -1 && interval->us > 0) {
413-
t->sse++;
408+
if (interval->us == 0) {
409+
t->sse -= bias * timelib_hms_to_seconds(interval->h, interval->i, interval->s);
410+
timelib_update_from_sse(t);
411+
} else {
412+
timelib_rel_time *temp_interval = timelib_rel_time_clone(interval);
413+
414+
do_range_limit(0, 1000000, 1000000, &temp_interval->us, &temp_interval->s);
415+
t->sse -= bias * timelib_hms_to_seconds(temp_interval->h, temp_interval->i, temp_interval->s);
416+
timelib_update_from_sse(t);
417+
t->us -= temp_interval->us * bias;
418+
419+
timelib_do_normalize(t);
420+
timelib_update_ts(t, NULL);
421+
422+
timelib_rel_time_dtor(temp_interval);
414423
}
415424
timelib_do_normalize(t);
416425
}

ext/date/lib/timelib.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
# include "timelib_config.h"
3131
#endif
3232

33-
#define TIMELIB_VERSION 202114
34-
#define TIMELIB_EXTENDED_VERSION 20211401
35-
#define TIMELIB_ASCII_VERSION "2021.14"
33+
#define TIMELIB_VERSION 202115
34+
#define TIMELIB_EXTENDED_VERSION 20211501
35+
#define TIMELIB_ASCII_VERSION "2021.15"
3636

3737
#include <stdlib.h>
3838
#include <stdbool.h>

ext/date/tests/bug-gh8964-001.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Test for bug GH-8964: DateTime object comparison after applying delta less than 1 second
3+
--INI--
4+
date.timezone=UTC
5+
--FILE--
6+
<?php
7+
$actual = new DateTimeImmutable("2022-07-21 15:00:10");
8+
$delta = new \DateInterval(sprintf('PT%dS', 0));
9+
$delta->f = 0.9;
10+
11+
$expectedLower = $actual->sub($delta);
12+
$expectedUpper = $actual->add($delta);
13+
14+
echo $expectedLower->format( 'H:i:s.u U' ), "\n";
15+
echo $actual ->format( 'H:i:s.u U' ), "\n";
16+
echo $expectedUpper->format( 'H:i:s.u U' ), "\n";
17+
18+
var_dump($actual < $expectedLower, $actual > $expectedUpper);
19+
?>
20+
--EXPECTF--
21+
15:00:09.100000 1658415609
22+
15:00:10.000000 1658415610
23+
15:00:10.900000 1658415610
24+
bool(false)
25+
bool(false)

ext/date/tests/bug-gh8964-002.phpt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
Test for bug GH-8964: DateTime object comparison after applying delta less than 1 second
3+
--INI--
4+
date.timezone=UTC
5+
--FILE--
6+
<?php
7+
for ($seconds = 0; $seconds < 3; $seconds++)
8+
{
9+
$actual = new DateTimeImmutable("2022-07-21 15:00:10");
10+
$delta = new \DateInterval(sprintf('PT%dS', $seconds));
11+
$delta->f = -0.9;
12+
13+
$expectedLower = $actual->sub($delta);
14+
$expectedUpper = $actual->add($delta);
15+
16+
echo $expectedLower->format( 'H:i:s.u U' ), "\n";
17+
echo $actual ->format( 'H:i:s.u U' ), "\n";
18+
echo $expectedUpper->format( 'H:i:s.u U' ), "\n";
19+
20+
var_dump($actual < $expectedLower, $actual > $expectedUpper);
21+
}
22+
?>
23+
--EXPECTF--
24+
15:00:10.900000 1658415610
25+
15:00:10.000000 1658415610
26+
15:00:09.100000 1658415609
27+
bool(true)
28+
bool(true)
29+
15:00:09.900000 1658415609
30+
15:00:10.000000 1658415610
31+
15:00:10.100000 1658415610
32+
bool(false)
33+
bool(false)
34+
15:00:08.900000 1658415608
35+
15:00:10.000000 1658415610
36+
15:00:11.100000 1658415611
37+
bool(false)
38+
bool(false)

0 commit comments

Comments
 (0)