wfsp has asked for the wisdom of the Perl Monks concerning the following question:

I'm not having much luck with the POSIX docs. I expected the following to output a date in Spanish.
#!/usr/local/bin/perl use strict; use warnings; use POSIX qw(setlocale LC_ALL LC_CTYPE); #my $loc = setlocale(LC_ALL, q{es_ES}); my $loc = setlocale(LC_ALL, q{es}); print $loc?$loc:q{undef}, qq{\n}; my $str = POSIX::strftime("%A, %B %d, %Y", gmtime); print "$str\n";
outputs
undef Thursday, August 14, 2008
I used the snippet in the DateTime::Locale docs
my @ids = DateTime::Locale->ids();
and it returned a list that included the two I tried above.

I'm on Windows but I plan to use this on my webhoster (*nix).

Any ideas welcome.

Replies are listed 'Best First'.
Re: Spanish dates using POSIX (cases)
by tye (Sage) on Aug 14, 2008 at 19:25 UTC

    Your code also gives me English output on Unix. However:

    $ LC_ALL=es_ES perl -MPOSIX=strftime -le 'print strftime("%A, %B %d, +%Y", gmtime() )' jueves, agosto 14, 2008

    And using your code but with the commented version on Unix produces:

    es_ES jueves, agosto 14, 2008

    The equivalent to my command under cygwin on Win32 didn't work:

    > env LC_ALL=es_ES \apps\cygwin\bin\perl -MPOSIX=strftime -le "print +st rftime('%A, %B %d, %Y', gmtime() )" Thursday, August 14, 2008

    which didn't surprise me too much since I don't think I have a Win32 Spanish locale installed there.

    I hope those small leads help.

    - tye        

Re: Spanish dates using POSIX
by mr_mischief (Monsignor) on Aug 14, 2008 at 19:18 UTC
    On Mandriva with Perl 5.8.8 I get:
    es jueves, agosto 14, 2008
    I'm wondering if the locale handling in your perl is just broken or there's no good way to get the locale from your OS.

    Update: I get the same on perl 5.10.0 on Mandriva. I get this from cygwin 5.8.6:

    undef Thursday, August 14, 2008
    I get this from Strawberry and from ActivePerl, both 5.8.8/5.8.7 and 5.10:
    Estonian_Estonia.1257 neljapΣev, august 14, 2008
    How Estonian and Spanish get confused I'm not sure. I always thought 'es' was Spanish and Spain while 'et' was Estonian and Estonia (per sources like ISO 639, even though that particular list is marked obsolete).
Re: Spanish dates using POSIX
by bart (Canon) on Aug 19, 2008 at 11:39 UTC
    FWIW...

    It's funny that I see you, wfsp, use strftime from POSIX, where it was you who showed me on the Chatterbox a few weeks ago, that there is also a module Date::Format. And that module has multilingual support built in... but unfortunately not for Spanish. More on that later.

    A problem with this module is that the docs are wrong.

    Not only has the API changed, as suggested that could happen, in the docs, but there are also more languages supported than the 4 listed there: you can see the full list of supported languages in the files list in the distribution.

    For example, this snippet works, for Dutch (not one of the 4 basic languages):

    use Date::Format; use Date::Language; my $lang = Date::Language->new('Dutch'); local $\ = "\n"; print $lang->time2str('%A, %B %d, %Y', time, 'GMT');

    So, it looks to me like you can "patch" the module to support Spanish, by adding a module file Date::Language::Spanish, similar to the other Date::Language::* modules, but with Spanish words. No files from the distribution need to be changed.