Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113338 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 16608 invoked from network); 3 Mar 2021 11:16:20 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Mar 2021 11:16:20 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3D63E1804D3 for ; Wed, 3 Mar 2021 03:06:49 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail.hades.9dev.de (mail.hades.9dev.de [88.99.187.36]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 3 Mar 2021 03:06:47 -0800 (PST) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id B2E835E061; Wed, 3 Mar 2021 12:06:44 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=9dev.de; s=dkim; t=1614769605; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=ABjLuNakMPEfp2pHbrOKqF30QW5yJUdw9QqZBiM1EN4=; b=ZRNSryRtxrWBR1BJOFtsaFh8lTTTsMs2CvteqlcKqP+5o6h94ck+lMe6cFEraA+yGVTHJX HsmNjSQwqCiw5x1Zm03/kyNhGimf8qcjAykRq9RRsF8+zJH11u3aBRSthyQFeyKWjKeUma jjkyGApnzXK4t8z/yQTNMChmuINPLDHMijeXHnbfQwtaEoysviUDN7qq/pCGSdKhi5Ryz1 U0AUMGds0JX7nfjVNoWCnaWQSnHzc1GPmy1QUPxxpspsZzfrtLnXVHT6q9OX0+a0vJVjHm cRetYe87tkhjL1W9xS0/MXa0WUdv+qF0RYdURsnwa8RfPBVKV5wwxp1Dt7yZXQ== Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.60.0.2.21\)) In-Reply-To: <09ef54b2-a6b1-5bf5-8562-f2f436fd4d92@processus.org> Date: Wed, 3 Mar 2021 12:06:43 +0100 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <9BF1EB68-3F1F-4FBF-A87F-FADF98B09E4A@9dev.de> References: <09ef54b2-a6b1-5bf5-8562-f2f436fd4d92@processus.org> To: Pierre X-Last-TLS-Session-Version: TLSv1.2 Subject: Re: [PHP-DEV] Add __toString() to DateInterval From: m@9dev.de (Moritz Friedrich) Am 03.03.2021 um 11:53 schrieb Pierre : >=20 > Le 03/03/2021 =C3=A0 11:37, Moritz Friedrich a =C3=A9crit : >> Hi internals, >> I=E2=80=99ve been active in the PHP ecosystem as @Radiergummi for = quite a while now, but not on internals yet, so: nice to meet you all! >>=20 >> I would like to propose adding a `__toString()` method to the = `DateInterval` class that should return a valid ISO8601 interval = (https://en.wikipedia.org/wiki/ISO_8601#Time_intervals). As it stands, = the class supports creating instances from such interval strings passed = to its constructor, but the reverse isn=E2=80=99t true: The only way to = build a string representation of the interval is by querying the = `DateInterval::format` method for the individual durations multiple = times (see = https://stackoverflow.com/questions/33787039/format-dateinterval-as-iso860= 1#answers for examples). >>=20 >> Allowing to cast `DateInterval`s to strings would make the most sense = in my opinion, and probably doesn=E2=80=99t break BC. Use cases include = communicating with other APIs that work with ISO8601, or persistence of = periods in databases, for example. >>=20 >> So instead of the following: >>=20 >> function formatDateInterval(DateInterval $interval, string = $default =3D 'PT0S'): string { >> return rtrim(str_replace( >> ['M0S', 'H0M', 'DT0H', 'M0D', 'P0Y', 'Y0M', 'P0M'], >> ['M', 'H', 'DT', 'M', 'P', 'Y', 'P'], >> $interval->format('P%yY%mM%dDT%hH%iM%sS') >> ), 'PT') ?: $default; >> } >> formatDateInterval(new DateInterval('P1DT5H')) =3D=3D=3D = 'P1DT5H' >>=20 >> I=E2=80=99d like to be able to do this: >>=20 >> (string)new DateInterval('P1DT5H') =3D=3D=3D 'P1DT5H' >>=20 >> Following alternative approaches come to mind: >>=20 >> 1. Adding a new, specialized method to the class (eg. = `DateInterval::toIsoString(): string`): >> While being the most BC-safe option, this would be completely = different to the rest of the PHP date APIs. >>=20 >> 2. Adding a new interval format constant to be passed to = `DateInterval::format` (eg. `DATE_INTERVAL_ISO8601`): >> Most in line with the `DateTime` API, but probably requires = special case handling in `format` code. >>=20 >> =46rom those, having `__toString` seems most desirable to me. >> While I=E2=80=99m not proficient with C, I=E2=80=99d really like to = see this implemented and would gladly help out where I can. Might = someone be able to advise? >>=20 >>=20 >> Sincerely >> Moritz / Radiergummi >=20 > Hello, >=20 > I don't like implicit __toString() methods, they have to be very = carefully documented and it's "magic". Wouldn't it be best to have both = the `toIsoString()` and `__toString()` (as a fallback) methods ? >=20 > Having the constant is a good idea too, it would make it probably more = consistent with the DateTime class, I'm not opposed having three = variations to do skin the same cat. >=20 > Regards, >=20 > Pierre >=20 > --=20 > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php >=20 Hi Pierre, Having `__toString` proxy to `toIsoString` internally sounds like a good = idea, but I=E2=80=99m not too fond of the constant - all other built-in = date constants translate to a string of plain format characters, which = isn=E2=80=99t possible in this case, so it would be meaningless = everywhere else. Sincerely, Moritz