Skip to content

Commit c2554b4

Browse files
committed
Fixed Bug #63435 Datetime::format('u') sometimes wrong by 1 microsecond
When storing '015700' microseconds in a Datetime object, Datetime::format('u') returns '015699' Already known per bug45554 reproducer (also fixed).
1 parent ff6c9e2 commit c2554b4

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

ext/date/php_date.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@ static char *date_format(char *format, int format_len, timelib_time *t, int loca
10951095
case 'H': length = slprintf(buffer, 32, "%02d", (int) t->h); break;
10961096
case 'i': length = slprintf(buffer, 32, "%02d", (int) t->i); break;
10971097
case 's': length = slprintf(buffer, 32, "%02d", (int) t->s); break;
1098-
case 'u': length = slprintf(buffer, 32, "%06d", (int) floor(t->f * 1000000)); break;
1098+
case 'u': length = slprintf(buffer, 32, "%06d", (int) floor(t->f * 1000000 + 0.5)); break;
10991099

11001100
/* timezone */
11011101
case 'I': length = slprintf(buffer, 32, "%d", localtime ? offset->is_dst : 0); break;

ext/date/tests/bug45554.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ $d = date_create_from_format($format, "03-15-2005 12:22:29.000000 PST");
99
echo $d->format($format), "\n";
1010

1111
$d = date_create_from_format($format, "03-15-2005 12:22:29.001001 PST");
12-
echo $d->format($format), " (precision isn't enough to show the 1 here)\n";
12+
echo $d->format($format), "\n";
1313

1414
$d = date_create_from_format($format, "03-15-2005 12:22:29.0010 PST");
1515
echo $d->format($format), "\n";
1616
?>
1717
--EXPECT--
1818
03-15-2005 12:22:29.000000 PST
19-
03-15-2005 12:22:29.001000 PST (precision isn't enough to show the 1 here)
19+
03-15-2005 12:22:29.001001 PST
2020
03-15-2005 12:22:29.001000 PST

ext/date/tests/bug63435.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Bug #63435 Datetime::format('u') sometimes wrong by 1 microsecond
3+
--INI--
4+
date.timezone=UTC
5+
--FILE--
6+
<?php
7+
for ($i=1 ; $i<999 ; $i++) {
8+
$datetime = Datetime::createFromFormat("u", sprintf("%06ld", $i));
9+
$res = $datetime->format("u");
10+
if ($res != $i) {
11+
echo "$i != $res\n";
12+
}
13+
}
14+
echo "Done";
15+
--EXPECT--
16+
Done

0 commit comments

Comments
 (0)