php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #48225
Patch bug48225 revision 2016-08-07 14:10 UTC by cmb@php.net

Patch bug48225 for Date/time related Bug #48225

Patch version 2016-08-07 14:10 UTC

Return to Bug #48225 | Download this patch
Patch Revisions:

Developer: cmb@php.net

From cb6334c8eff7100f3f809f005ded5b3bab409b70 Mon Sep 17 00:00:00 2001
From: "Christoph M. Becker" <cmbecker69@gmx.de>
Date: Sun, 7 Aug 2016 15:53:43 +0200
Subject: [PATCH] Bug #48225: DateTime parser doesn't set microseconds for
 "now"

time() delivers the Unix timestamp in seconds, so now->f is always 0, what
prevents timelib_fill_holes() from properly filling the ->f hole. We
therefore set now->f manually, if the ->have_time flag is not set.
---
 ext/date/php_date.c          |  5 +++++
 ext/date/tests/bug48225.phpt | 18 ++++++++++++++++++
 2 files changed, 23 insertions(+)
 create mode 100644 ext/date/tests/bug48225.phpt

diff --git a/ext/date/php_date.c b/ext/date/php_date.c
index 801208d..dd47677 100644
--- a/ext/date/php_date.c
+++ b/ext/date/php_date.c
@@ -2572,6 +2572,7 @@ PHPAPI int php_date_initialize(php_date_obj *dateobj, /*const*/ char *time_str,
 	int type = TIMELIB_ZONETYPE_ID, new_dst = 0;
 	char *new_abbr = NULL;
 	timelib_sll     new_offset;
+    struct timeval tv;
 
 	if (dateobj->time) {
 		timelib_time_dtor(dateobj->time);
@@ -2638,6 +2639,10 @@ PHPAPI int php_date_initialize(php_date_obj *dateobj, /*const*/ char *time_str,
 	}
 	timelib_unixtime2local(now, (timelib_sll) time(NULL));
 
+    if (!dateobj->time->have_time && !gettimeofday(&tv, NULL)) {
+        now->f = tv.tv_usec / 1000000.0;
+    }
+
 	timelib_fill_holes(dateobj->time, now, TIMELIB_NO_CLONE);
 	timelib_update_ts(dateobj->time, tzi);
 	timelib_update_from_sse(dateobj->time);
diff --git a/ext/date/tests/bug48225.phpt b/ext/date/tests/bug48225.phpt
new file mode 100644
index 0000000..b59dab4
--- /dev/null
+++ b/ext/date/tests/bug48225.phpt
@@ -0,0 +1,18 @@
+--TEST--
+Bug #48225 (DateTime parser doesn't set microseconds for "now")
+--INI--
+date.timezone=Europe/Amsterdam
+--FILE--
+<?php
+/* every second $ms will rightly be 0, so we try up to 3 times */
+$i = 3;
+do {
+    $dt = new DateTime();
+    $ms = (int) $dt->format('u');
+} while ($ms === 0 && --$i > 0);
+var_dump($ms !== 0);
+?>
+===DONE===
+--EXPECT--
+bool(true)
+===DONE===
-- 
2.8.1.windows.1

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Dec 08 07:01:23 2019 UTC