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

Dear fellow monks,

I need some functions to output numbers, dates, and times in a locale-dependent way with some predefined formats. I just want to specify the locale and one of these general formats: "number with 2 digits after decimal", "money with 2 digits after decimal", "short date", "medium date", "long date", "time without seconds", "time with seconds". The routines should handle/pick the formatting details that make sense for me.

For example time can perhaps be in 12-hour or 24-hour format, with different separator according to the locale: 11:22 PM or 23:22 or even 23h22. Short date output could perhaps be something like 2007.12.31 or 12/31/2007, numbers could be "23 000 000.00" or "23.000.000,00", etc. Basically I could care less as long as it is the common format for that locale.

Any module to do that?

  • Comment on simple locale-dependent output functions?

Replies are listed 'Best First'.
Re: simple locale-dependent output functions?
by TOD (Friar) on Nov 15, 2007 at 04:46 UTC
    take a look at POSIX::strftime
    --------------------------------
    masses are the opiate for religion.
      How about DateTime and DateTime::Locale ? Together, they should contain all the information you need. Here's a short (incomplete) example:
      #!/usr/bin/perl use DateTime; use DateTime::Locale; my $loc1 = DateTime::Locale->load('en_US'); my $loc2 = DateTime::Locale->load('de'); $date_en = DateTime->now(locale => 'en_GB',time_zone=>'Europe/London') +; $date_de = DateTime->now(locale => 'de_DE',time_zone=>'Europe/Berlin') +; $date_us = DateTime->now(locale => 'en_US', time_zone => 'America/Los_ +Angeles'); print "Date (UK) ",$date_en,"\n"; print "Date (Germany) ",$date_de,"\n"; print "Date (New York/US)",$date_us,"\n";
      Have a nice day!
      Sorry, forgot to mention that POSIX::strftime() as well as localeconv() is a bit too low-level for my need, I need something that wraps over POSIX routines and provides predefined formats for most locales.