Re: Surprising results from predefined CLDR formats
[email protected] (Dave Rolsky) Tue, 23 Feb 2021 23:34:05 -0600
| Newsgroups | perl.datetime |
|---|---|
| Message-ID | <CAHKw1MJ6ZLT2WtH7M01cZZQF5cOO2ge1cpB5BoXak_mgwqh0jg@mail.gmail.com> |
--000000000000c4815c05bc0e624c
Content-Type: text/plain; charset="UTF-8"
The formats returned by "$locale->available_formats" are keys that you use
to access the localized format from "$locale->format_for($key)".
So what you wanted to write was this:
```
use DateTime;
my $dt = DateTime->now( locale => 'en-US' );
for my $fmt ( $dt->locale->available_formats ) {
my $val = $dt->format_cldr( $dt->locale->format_for($fmt) );
say "$fmt : $val";
}
```
Each locale will implement some subset of the possible keys, but the keys
themselves are the same between locales. In other words, for all locales
that have a format for "abbreviated month with numeric day", the key will
be "MMMd", but the actual formats returned by `$local->format_for('MMMd')`
will differ.
Cheers,
Dave Rolsky
http://blog.urth.org
https://github.com/autarch
On Tue, Feb 23, 2021 at 11:27 PM BPJ <[email protected]> wrote:
> Please consider this code:
>
> ``````perl
> use DateTime;
>
> my $dt = DateTime->now( locale => 'sv' );
>
> for my $fmt ( $dt->locale->available_formats ) {
> my $val = $dt->format_cldr($fmt);
> say "$fmt : $val";
> }
> ``````
>
> Most of the formatted strings seem to be missing some whitespace and have
> things in a surprisising order.
> E.g. `MMMd` gives `feb.23` where I would have expected `23 feb.`.
>
> I don't know if/what I'm doing wrong, or whether this is a DateTime,
> DateTime::Locale or CLDR bug or indeed if it is a bug at all.
>
>
> --
> Better --help|less than helpless
>
--000000000000c4815c05bc0e624c
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>The formats=C2=A0returned by "$locale->availa=
ble_formats" are keys that you use to access the localized format from=
"$locale->format_for($key)".</div><div><br></div><div>So what=
you wanted to write was this:</div><div><br></div><div>```</div><div>use D=
ateTime;<br><br>my $dt =3D DateTime->now( locale =3D> 'en-US'=
);<br><br>for my $fmt ( $dt->locale->available_formats ) {<br>=C2=A0=
=C2=A0 my $val =3D $dt->format_cldr( $dt->locale->format_for($fmt=
) );<br>=C2=A0 =C2=A0 say "$fmt : $val";<br>}<br></div><div>```</=
div><div><br></div><div>Each locale will implement some subset of the possi=
ble keys, but the keys themselves are the same between locales. In other wo=
rds, for all=C2=A0locales that have a format for "abbreviated month wi=
th numeric day", the key will be "MMMd", but the actual form=
ats returned by `$local->format_for('MMMd')` will differ.<br></d=
iv><div><div><br clear=3D"all"><div><div dir=3D"ltr" class=3D"gmail_signatu=
re" data-smartmail=3D"gmail_signature"><div dir=3D"ltr"><div><div dir=3D"lt=
r"><div><br></div><div>Cheers,</div><div><br></div><div>Dave Rolsky</div><d=
iv><a href=3D"http://blog.urth.org" target=3D"_blank">http://blog.urth.org<=
/a></div><div><a href=3D"https://github.com/autarch" target=3D"_blank">http=
s://github.com/autarch</a></div></div></div></div></div></div><br></div></d=
iv></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_att=
r">On Tue, Feb 23, 2021 at 11:27 PM BPJ <<a href=3D"mailto:melroch@gmail=
.com">[email protected]</a>> wrote:<br></div><blockquote class=3D"gmail_=
quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,=
204);padding-left:1ex"><div dir=3D"auto"><div dir=3D"auto">Please consider =
this code:</div><div dir=3D"auto"><br></div><div dir=3D"auto">``````perl</d=
iv><div dir=3D"auto">use DateTime;</div><div dir=3D"auto"><br></div><div di=
r=3D"auto">my $dt =3D DateTime->now( locale =3D> 'sv' );</div=
><div dir=3D"auto"><br></div><div dir=3D"auto">for my $fmt ( $dt->locale=
->available_formats ) {</div><div dir=3D"auto">=C2=A0 my $val =3D $dt-&g=
t;format_cldr($fmt);</div><div dir=3D"auto">=C2=A0 say "$fmt : $val&qu=
ot;;</div><div dir=3D"auto">}</div><div dir=3D"auto">``````</div><div dir=
=3D"auto"><br></div><div dir=3D"auto">Most of the formatted strings seem to=
be missing some whitespace and have</div><div dir=3D"auto">things in a sur=
prisising order.</div><div dir=3D"auto">E.g. `MMMd` gives `feb.23` where I =
would have expected `23 feb.`.</div><div dir=3D"auto"><br></div><div dir=3D=
"auto">I don't know if/what I'm doing wrong, or whether this is a D=
ateTime,</div><div dir=3D"auto">DateTime::Locale or CLDR bug or indeed if i=
t is a bug at all.</div><div dir=3D"auto"><br></div><br><div>-- <br>Better =
--help|less than helpless</div></div>
</blockquote></div>
--000000000000c4815c05bc0e624c--