com php-src: Fixed Bug #63435 Datetime::format('u') sometimes wrong by 1 microsecond: ext/date/php_date.c ext/date/tests/bug45 554.phpt
ext/date/tests/bug63435.phpt
| From: | Remi Collet | Date: | Sat, 01 Dec 2012 09:20:39 +0000 |
| Subject: | com php-src: Fixed Bug #63435 Datetime::format('u') sometimes wrong by 1 microsecond: ext/date/php_date.c ext/date/tests/bug45 554.phpt ext/date/tests/bug63435.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-70632@lists.php.net to get a copy of this message | ||
Commit: c2554b4bb43700ae16db34d9706db1db85ec78a4
Author: Remi Collet <remi@php.net> Sat, 1 Dec 2012 10:20:39 +0100
Parents: ff6c9e2726ab724707999ed651d1a414336665f2
Branches: PHP-5.3 PHP-5.4 PHP-5.5 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=c2554b4bb43700ae16db34d9706db1db85ec78a4
Log:
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).
Bugs:
https://bugs.php.net/63435
https://bugs.php.net/45554
Changed paths:
M ext/date/php_date.c
M ext/date/tests/bug45554.phpt
A ext/date/tests/bug63435.phpt
Diff:
diff --git a/ext/date/php_date.c b/ext/date/php_date.c
index e8a4570..47b79bc 100644
--- a/ext/date/php_date.c
+++ b/ext/date/php_date.c
@@ -1095,7 +1095,7 @@ static char *date_format(char *format, int format_len, timelib_time *t, int
loca
case 'H': length = slprintf(buffer, 32, "%02d", (int) t->h); break;
case 'i': length = slprintf(buffer, 32, "%02d", (int) t->i); break;
case 's': length = slprintf(buffer, 32, "%02d", (int) t->s); break;
- case 'u': length = slprintf(buffer, 32, "%06d", (int) floor(t->f *
1000000)); break;
+ case 'u': length = slprintf(buffer, 32, "%06d", (int) floor(t->f *
1000000 + 0.5)); break;
/* timezone */
case 'I': length = slprintf(buffer, 32, "%d", localtime ? offset->is_dst
: 0); break;
diff --git a/ext/date/tests/bug45554.phpt b/ext/date/tests/bug45554.phpt
index 0e9ebfd..a5042ff 100644
--- a/ext/date/tests/bug45554.phpt
+++ b/ext/date/tests/bug45554..phpt
@@ -9,12 +9,12 @@ $d = date_create_from_format($format, "03-15-2005 12:22:29.000000 PST");
echo $d->format($format), "\n";
$d = date_create_from_format($format, "03-15-2005 12:22:29.001001 PST");
-echo $d->format($format), " (precision isn't enough to show the 1 here)\n";
+echo $d->format($format), "\n";
$d = date_create_from_format($format, "03-15-2005 12:22:29.0010 PST");
echo $d->format($format), "\n";
?>
--EXPECT--
03-15-2005 12:22:29.000000 PST
-03-15-2005 12:22:29.001000 PST (precision isn't enough to show the 1 here)
+03-15-2005 12:22:29.001001 PST
03-15-2005 12:22:29.001000 PST
diff --git a/ext/date/tests/bug63435.phpt b/ext/date/tests/bug63435.phpt
new file mode 100644
index 0000000..dcec6e4
--- /dev/null
+++ b/ext/date/tests/bug63435.phpt
@@ -0,0 +1,16 @@
+--TEST--
+Bug #63435 Datetime::format('u') sometimes wrong by 1 microsecond
+--INI--
+date.timezone=UTC
+--FILE--
+<?php
+for ($i=1 ; $i<999 ; $i++) {
+ $datetime = Datetime::createFromFormat("u", sprintf("%06ld", $i));
+ $res = $datetime->format("u");
+ if ($res != $i) {
+ echo "$i != $res\n";
+ }
+}
+echo "Done";
+--EXPECT--
+Done