Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113831 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 37570 invoked from network); 28 Mar 2021 14:57:39 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 Mar 2021 14:57:39 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AB5001804D8 for ; Sun, 28 Mar 2021 07:54:26 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=BAYES_20,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail02.x-net.at (mail02.x-net.at [83.65.141.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 28 Mar 2021 07:54:25 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by mail02.x-net.at (Postfix) with ESMTP id C2F21380082 for ; Sun, 28 Mar 2021 16:54:23 +0200 (CEST) Received: from mail02.x-net.at ([127.0.0.1]) by localhost (x-zimbra02.x [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id QFn825A3KcRe for ; Sun, 28 Mar 2021 16:54:20 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mail02.x-net.at (Postfix) with ESMTP id 77A56380702 for ; Sun, 28 Mar 2021 16:54:20 +0200 (CEST) X-Virus-Scanned: amavisd-new at x-t.at Received: from mail02.x-net.at ([127.0.0.1]) by localhost (x-zimbra02.x [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id W99fWpou5_z9 for ; Sun, 28 Mar 2021 16:54:20 +0200 (CEST) Received: from x-zimbra02.x (localhost [127.0.0.1]) by mail02.x-net.at (Postfix) with ESMTP id 62AA8380082 for ; Sun, 28 Mar 2021 16:54:20 +0200 (CEST) Date: Sun, 28 Mar 2021 16:54:19 +0200 (CEST) To: internals Message-ID: <1722754661.329421.1616943259775.JavaMail.zimbra@dafert.at> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Mailer: Zimbra 8.8.15_GA_3959 (ZimbraWebClient - FF87 (Linux)/8.8.15_GA_3953) Thread-Index: iuz211L2+9HJ8OhpoTg20kr6ybgy2Q== Thread-Topic: Proposal: add IntlDateTimePatternGenerator Subject: Proposal: add IntlDateTimePatternGenerator From: mel@dafert.at (Mel Dafert) Hello Internals, I would like to propose an addition to the intl extension, IntlDateTimePatternGenerator. ICU exposes the DateTimePatternGenerator class that allows generating a formatting pattern from a given "skeleton" for a given locale. (see https://unicode-org.github.io/icu-docs/apidoc/dev/icu4c/classicu_1_1DateTimePatternGenerator.html) This allows more flexibility than the current $format argument of IntlDateFormatter::format(), which takes either a few pre-defined constants or a hard-coded format. This functionality also has been requested in the past. (see https://bugs.php.net/bug.php?id=70377) For example, if an application requires a format that will always use the long version of a year, but the short version of a month (eg. "MM/dd/YYYY" for "en_US", or "dd.MM.YYYY" for "de_DE"), one is out of luck. IntlDateFormatter::SHORT will use the short year for "de_DE" ("dd.MM.YY"), and IntlDateFormatter::MEDIUM will use the long version of a month for "en_US" ("MMM dd, YYYY"). With IntlDateTimePatternGenerator::getBestPattern(), a skeleton can be provided to generate the appropriate pattern for a given locale. In the use-case given above, the skeleton "YYYYMMdd" will generate the desired patterns for each locale, which can then be passed to IntlDateFormatter::format(). Proposed Signature: ``` class IntlDateTimePatternGenerator { public function __construct(?string $locale = null) {} public static function create(?string $locale = null): ?IntlDateTimePatternGenerator {} public function getBestPattern(string $skeleton): string|false {} } ``` Example use-case: ``` $date = new \DateTime(); $skeleton = "YYYYMMdd"; $dtpg = new \IntlDateTimePatternGenerator(); // uses default locale if not specified $format = $dtpg->getBestPattern($skeleton); echo \IntlDateFormatter::formatObject($date, $format); // outputs "28.03.2020" for "de_DE" locale ``` The proposed implementation can be found here: https://github.com/php/php-src/pull/6771 I assume that this will require an RFC, in which case I request the karma to create one. My wiki account is deltragon. Regards, Mel Dafert