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
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
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
Now that I see their implementation again, I'm wondering if
IntlDateTimePatternGenerator
is the right name, or we should also useIntlDatePatternGenerator
, which is more in line withIntlDateFormatter
.
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):