Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:96767 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 25466 invoked from network); 8 Nov 2016 12:16:53 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Nov 2016 12:16:53 -0000 X-Host-Fingerprint: 77.165.170.254 ip4da5aafe.direct-adsl.nl Received: from [77.165.170.254] ([77.165.170.254:21779] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DB/40-23587-332C1285 for ; Tue, 08 Nov 2016 07:16:53 -0500 Message-ID: To: internals@lists.php.net References: <46.92.05967.9AB91285@pb1.pair.com> Date: Tue, 8 Nov 2016 13:16:46 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Posted-By: 77.165.170.254 Subject: Re: [PHP-DEV] DateTime microseconds discussion From: arjen@parse.nl (Arjen Schol) Hi Dan, I think you make some bad assumptions here. The examples provided by Sjon are scripts submitted to 3v4l.org They may have bad assumptions, but are real life examples of DateTime usage. And they will break. We have two issues in our codebase. We ARE testing RC release, I think feedback should be taken seriously. 1. We overloaded DateTime::setTime, this needed an update because of the extra parameter (point 3). BC break. 2. We test the following: A. $date = new DateTime; B. Store in database. C. Retrieve datetime. D. $fromDatabase = new DateTime(storedDateTime); E. $fromDatabase == $date (which fails now, because $date has microsecond precision while $fromDatabase doesn't). Where does this have a bad assumption? We tested an assumption (DateTime has seconds precision), and now it fails. Is that a bad assumption? I think it's just backward breaking with no good way to fix the code. We could set microseconds to 0, which is cumbersome because of a missing setMicroseconds method. One needs to call setTime with retrieved hours, minutes, seconds or setTimestamp($dt->format('U'))... "Rely only on reliable things."... Arjen On 11/08/2016 12:03 PM, Dan Ackroyd wrote: > On 8 November 2016 at 09:32, Arjen Schol wrote: >> Hi, >> >> Support for microseconds was added late in the 7.1 RC cycle, however is has >> some issues: > > > My understanding is that if this affects your code, then your code > already has a bad assumption in it. The code is mistakenly assuming > that two DateTime's generated will have the same the same time for > both of them.* > > I don't fully understand what problem you are want to solve through > this discussion. No-one is forcing you to update to PHP 7.1 > immediately; you should have plenty of time to fix the code that has > bad assumptions about the time in a generated DateTime before you > upgrade to PHP 7.1. > > SjonHortensius wrote: >> if generating a DateTime takes roughly 4 μs (which it does for me), this happens ~ 250.000 times >> more often > > Having bugs happen more frequently is a good thing. It stops you from > ignoring edge cases and programming by coincidence.** > > I realise that having a release make it more obvious that broken code > is broken is an annoying thing to have to fix....but that code is > already wrong. Delaying useful features, just to avoid having to fix > flaky code is not a good way for a project to proceed, imo. > > dshafik wrote: >> I think default microseconds to 0 when unspecific for relative strings is correct behavior > > I don't think that sounds like a good plan at all. When creating a > DateTime through `new DateTime('first day of next month');` only the > date values are set from the input string, the rest of the time is set > to now(). > > Wouldn't having microseconds behave differently to the rest of the > time values would be instantly an extra piece of inconsistency for > people to regret? > > cheers > Dan > > > * Example code that doesn't work correctly now. If the sleep period is > reduced, it becomes less obvious that the code is broken, but it is > still broken. > > for ($i=0; $i<10; $i++) { > $dt1 = new DateTime('first day of next month'); > usleep(rand(0, 500000)); > $dt2 = new DateTime('first day of next month'); > > if ($dt1 == $dt2) { > echo "Same\n"; > } > else { > echo "Different\n"; > } > } > > > ** Programming by Coincidence - > https://pragprog.com/the-pragmatic-programmer/extracts/coincidence >