# Proposal: add IntlDateTimePatternGenerator

- Thread: https://externals.io/message/113831
- Original archive: https://news-web.php.net/php.internals/113831
- Started by Mel Dafert on 2021-03-28T14:54:19+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #113831 — Mel Dafert — 2021-03-28T14:54:19+00:00

URL: https://externals.io/message/113831#113831

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

---

## #113835 — kocsismate90@gmail.com — 2021-03-28T17:45:46+00:00

URL: https://externals.io/message/113831#113835 · In reply to: #113831

Hi Mel,

Thank you very much for working on this! I was bitten by lack of this
functionality a few years ago, and I couldn't find any (good) alternative.
Thus, I'm looking forward to having `IntlDateTimePatternGenerator` in
ext/intl.

I think it could be mentioned for those who don't open the bug report that
HHVM implemented this class long ago:
https://github.com/facebook/hhvm/commit/bc84daf7816e4cd268da59d535dcadfc6cf01085

Now that I see their implementation again, I'm wondering
if `IntlDateTimePatternGenerator` is the right name, or we should also use
`IntlDatePatternGenerator`, which is more in line with ``IntlDateFormatter`.

Regards:
Máté


Mel Dafert <mel@dafert.at> ezt írta (időpont: 2021. márc. 28., V, 16:54):

> 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
>
> --
> 
> To unsubscribe, visit: https://www.php.net/unsub.php

---

## #113836 — Mel Dafert — 2021-03-28T19:17:41+00:00

URL: https://externals.io/message/113831#113836 · In reply to: #113835

Now that I see their implementation again, I'm wondering if `IntlDateTimePatternGenerator` is the right name, or we should also use `IntlDatePatternGenerator`, which is more in line with `IntlDateFormatter`. 



I was also wondering about that. I ended up going for `IntlDateTimePatternGenerator`, because ICU has that same inconsistency (`icu::DateFormat` and `icu::DateTimePatternGenerator`). I think the big argument here is discoverability - if someone is wondering about whether PHP exposes the feature from ICU, it might be harder to find it if we name it differently (and vice versa). 

From: "Máté Kocsis" <kocsismate90@gmail.com> 
To: "Mel Dafert" <mel@dafert.at> 
Cc: "internals" <internals@lists.php.net> 
Sent: Sunday, March 28, 2021 7:45:46 PM 
Subject: Re: [PHP-DEV] Proposal: add IntlDateTimePatternGenerator 

Hi Mel, 
Thank you very much for working on this! I was bitten by lack of this functionality a few years ago, and I couldn't find any (good) alternative. Thus, I'm looking forward to having `IntlDateTimePatternGenerator` in ext/intl. 

I think it could be mentioned for those who don't open the bug report that HHVM implemented this class long ago: [ https://github.com/facebook/hhvm/commit/bc84daf7816e4cd268da59d535dcadfc6cf01085 | https://github.com/facebook/hhvm/commit/bc84daf7816e4cd268da59d535dcadfc6cf01085 ] 

Now that I see their implementation again, I'm wondering if `IntlDateTimePatternGenerator` is the right name, or we should also use `IntlDatePatternGenerator`, which is more in line with ``IntlDateFormatter`. 

Regards: 
Máté 


Mel Dafert < [ mailto:mel@dafert.at | mel@dafert.at ] > ezt írta (időpont: 2021. márc. 28., V, 16:54): 

BQ_BEGIN
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 | 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 | 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 | 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 

-- 
 
To unsubscribe, visit: [ https://www.php.net/unsub.php | https://www.php.net/unsub.php ] 


BQ_END

---

## #113837 — Mel Dafert — 2021-03-28T19:30:23+00:00

URL: https://externals.io/message/113831#113837 · In reply to: #113835

> Now that I see their implementation again, I'm wondering if `IntlDateTimePatternGenerator` is the right name, or we should also use `IntlDatePatternGenerator`, which is more in line with `IntlDateFormatter`. 

I was also wondering about that. I ended up going for `IntlDateTimePatternGenerator`, because ICU has that same inconsistency
(`icu::DateFormat` and `icu::DateTimePatternGenerator`).
I think the big argument here is discoverability - if someone is wondering about whether PHP exposes the feature from ICU, it might be harder
to find it if we name it differently (and vice versa).

PS: Sorry about the duplicate/badly formatted message - I am new to the concept of mailing lists.

----- Original Message -----
From: "Máté Kocsis" <kocsismate90@gmail.com>
To: "Mel Dafert" <mel@dafert.at>
Cc: "internals" <internals@lists.php.net>
Sent: Sunday, March 28, 2021 7:45:46 PM
Subject: Re: [PHP-DEV] Proposal: add IntlDateTimePatternGenerator

Hi Mel,

Thank you very much for working on this! I was bitten by lack of this
functionality a few years ago, and I couldn't find any (good) alternative.
Thus, I'm looking forward to having `IntlDateTimePatternGenerator` in
ext/intl.

I think it could be mentioned for those who don't open the bug report that
HHVM implemented this class long ago:
https://github.com/facebook/hhvm/commit/bc84daf7816e4cd268da59d535dcadfc6cf01085

Now that I see their implementation again, I'm wondering
if `IntlDateTimePatternGenerator` is the right name, or we should also use
`IntlDatePatternGenerator`, which is more in line with ``IntlDateFormatter`.

Regards:
Máté


Mel Dafert <mel@dafert.at> ezt írta (időpont: 2021. márc. 28., V, 16:54):
