Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:85800 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 23727 invoked from network); 14 Apr 2015 07:05:01 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Apr 2015 07:05:01 -0000 Authentication-Results: pb1.pair.com smtp.mail=andreas@heigl.org; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=andreas@heigl.org; sender-id=unknown Received-SPF: error (pb1.pair.com: domain heigl.org from 195.191.240.18 cause and error) X-PHP-List-Original-Sender: andreas@heigl.org X-Host-Fingerprint: 195.191.240.18 hos109.unaxus.net Received: from [195.191.240.18] ([195.191.240.18:38052] helo=hos109.unaxus.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FE/62-08334-A1CBC255 for ; Tue, 14 Apr 2015 03:04:59 -0400 Received: from [212.185.30.151] (port=59032 helo=wdv-hg-0C07-zX4090-HeiglAndreas.local) by hos109.unaxus.net with esmtpsa (TLSv1.2:DHE-RSA-AES128-SHA:128) (Exim 4.85) (envelope-from ) id 1Yhuty-00345t-JZ; Tue, 14 Apr 2015 09:04:54 +0200 Message-ID: <552CBC17.9040407@heigl.org> Date: Tue, 14 Apr 2015 09:04:55 +0200 MIME-Version: 1.0 To: Peter Lind CC: PHP internals References: In-Reply-To: Content-Type: multipart/signed; protocol="application/pkcs7-signature"; micalg=sha1; boundary="------------ms060508060400070702030907" X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - hos109.unaxus.net X-AntiAbuse: Original Domain - lists.php.net X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - heigl.org X-Get-Message-Sender-Via: hos109.unaxus.net: authenticated_id: a.heigl+heigl.org/only user confirmed/virtual account not confirmed Subject: Re: [PHP-DEV] DateInterval bug From: andreas@heigl.org (Andreas Heigl) --------------ms060508060400070702030907 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi All. Am 14.04.15 um 08:38 schrieb Peter Lind: > On 13 April 2015 at 22:20, Derick Rethans wrote: >=20 >> On Sun, 12 Apr 2015, Peter Lind wrote: >> >>> Hi, >>> >>> I wanted to get into PHP code development so I grabbed a random bug f= rom >>> bugs.php.net. Which turned out to be >> https://bugs.php.net/bug.php?id=3D69378 >>> >>> The problem the bug report describes is that creating a diff between = two >>> dates and then subtracting the diff from the later date does not give= you >>> the former date. Or, as the bug report state, this should hold: >>> >>> B - (B - A) =3D=3D A >>> >>> But it doesn't, because of the way DateInterval and DateTime interact= =2E A >>> DateInterval is broken up into years, months, days, hours, minutes an= d >>> seconds - which can be added or subtracted from a date. However, mont= hs >> are >>> not fit size, so the order in which things are added or subtracted >> matters. >>> In the bug report, the problem arises because months are subtracted >> before >>> days - and there's a huge difference between subtracting 17 days from= 2. >>> April and from 2. March. >>> >>> In itself, this isn't a big problem - but apparently this behaviour i= s >> how >>> the system is supposed to work. In the tests for the date extension, = I >>> found this test for DateTime::add >>> >>> echo "test_years_positive__6_shy_1_day: "; >>> examine_diff('2007-02-06', '2000-02-07', 'P+6Y11M30DT0H0M0S', 2556); >>> >>> echo "test_years_negative__6_shy_1_day: "; >>> examine_diff('2000-02-07', '2007-02-06', 'P-6Y11M28DT0H0M0S', 2556); >>> >>> The third argument in the examine_diff calls is used in the construct= or >>> call to DateInterval. The difference is whether the interval will be >>> positive or negative. Note the difference of two days - if you add a >>> positive interval, then 7 years minus 1 day is 6 years, 11 months and= 30 >>> days. If you add a negative interval, then 7 years minus 1 day is 6 >> years, >>> 11 months and 28 days. >>> >>> Is there a good explanation for this behaviour (which applies both to= >>> DateTime::add and DateTime::sub)? I've tried searching the internals = list >>> but couldn't see any discussion of it. It seems like a bug that never= got >>> fixed to the point where there are tests to make sure things are stil= l >>> calculated wrong. >> >> Why is it a bug? With DateTime math, reversing an operation isn't >> necessarily going to work... >> >> > Math that isn't consistent is problematic, in my book. Or, to put it > another way: if someone told me, that there is 6 years, 11 months and 3= 0 > days between 7th Feb 2000 and 6th Feb 2007, I would agree. If the same > person then told me that the interval *is also* 6 years, 11 months and = 28 > days *and that both intervals are correct* - I would question the sanit= y of > that person. I would go so far as to say: don't ever manage my calendar= =2E >=20 > The bug as I see it is that two classes/behaviours have been packed int= o > one - there should be DateIntervalRepresentation and DateInterval. The > first shows you how many years, months, days, etc there are between two= > dates. The second is the actual interval between two dates. The first i= s > context dependent - it needs anchoring in at least one date. The second= is > context independent. >=20 > Either that or do away with math you can't rely on. I think what we sho= uld > aim for in programming languages is the opposite of "isn't necessarily > going to work..." >=20 > Regards > Peter >=20 As far as I can see in http://3v4l.org/mUeRk in PHP 5.3.2 the behaviour was still as you'd expect it, in PHP5.3.3 it was as described in the ticket. So it seems that there has been a change introduced with PHP5.3.3 that "broke" the "expected" behaviour. Cheers Andreas --=20 ,,, (o o) +---------------------------------------------------------ooO-(_)-Ooo-+ | Andreas Heigl | | mailto:andreas@heigl.org N 50=C2=B022'59.5" E 08=C2=B0= 23'58" | | http://andreas.heigl.org http://hei.gl/wiFKy7 | +---------------------------------------------------------------------+ | http://hei.gl/root-ca | +---------------------------------------------------------------------+ --------------ms060508060400070702030907 Content-Type: application/pkcs7-signature; name="smime.p7s" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="smime.p7s" Content-Description: S/MIME Cryptographic Signature MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIIFrDCC BagwggOQoAMCAQICAw9P6TANBgkqhkiG9w0BAQ0FADB5MRAwDgYDVQQKEwdSb290IENBMR4w HAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNBIENlcnQgU2lnbmlu ZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRAY2FjZXJ0Lm9yZzAeFw0xNDA3 MjMwOTMyNDl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